All files / src/components/website DomainSettings.tsx

97.67% Statements 42/43
92.59% Branches 25/27
100% Functions 6/6
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                                                        44x 44x 44x 44x 44x 44x   44x 3x   3x 3x 3x 3x 3x 2x 2x   1x   3x       44x 3x 3x 3x 3x 3x 2x   1x   3x       44x 4x 4x 4x 4x   4x   3x 1x 1x   2x     1x   4x       44x                                 5x   1x                                                                                                                                                                                                                                                
import { useState } from "react";
import {
	websiteApi,
	getErrorMessage,
} from "@/lib/api";
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
 
interface DomainSettingsProps {
	proId: string;
	proSlug: string;
	customDomain: string | null | undefined;
	customDomainVerified: boolean | undefined;
	customDomainUrl: string | null;
	cnameTarget: string | null;
	onDomainChanged: () => void;
}
 
export function DomainSettings({
	proId,
	proSlug,
	customDomain,
	customDomainVerified,
	customDomainUrl,
	cnameTarget,
	onDomainChanged,
}: DomainSettingsProps) {
	const [domainInput, setDomainInput] = useState("");
	const [settingDomain, setSettingDomain] = useState(false);
	const [verifyingDomain, setVerifyingDomain] = useState(false);
	const [removingDomain, setRemovingDomain] = useState(false);
	const [domainError, setDomainError] = useState<string | null>(null);
	const [domainVerifyResult, setDomainVerifyResult] = useState<string | null>(null);
 
	const handleSetCustomDomain = async () => {
		Iif (!domainInput.trim()) return;
 
		try {
			setSettingDomain(true);
			setDomainError(null);
			setDomainVerifyResult(null);
			await websiteApi.setCustomDomain(proId, domainInput.trim());
			setDomainInput("");
			onDomainChanged();
		} catch (err) {
			setDomainError(getErrorMessage(err));
		} finally {
			setSettingDomain(false);
		}
	};
 
	const handleRemoveCustomDomain = async () => {
		try {
			setRemovingDomain(true);
			setDomainError(null);
			setDomainVerifyResult(null);
			await websiteApi.removeCustomDomain(proId);
			onDomainChanged();
		} catch (err) {
			setDomainError(getErrorMessage(err));
		} finally {
			setRemovingDomain(false);
		}
	};
 
	const handleVerifyDomain = async () => {
		try {
			setVerifyingDomain(true);
			setDomainError(null);
			setDomainVerifyResult(null);
 
			const result = await websiteApi.verifyDomain(proId);
 
			if (result.verified) {
				setDomainVerifyResult("verified");
				onDomainChanged();
			} else {
				setDomainVerifyResult(result.error || "DNS not configured yet");
			}
		} catch (err) {
			setDomainError(getErrorMessage(err));
		} finally {
			setVerifyingDomain(false);
		}
	};
 
	return (
		<Card>
			<CardHeader>
				<CardTitle>Custom Domain</CardTitle>
			</CardHeader>
			<CardContent className="space-y-4">
				{!customDomain ? (
					/* State 1: No domain set */
					<div className="space-y-3">
						<p className="text-sm text-foreground-muted">
							Connect your own domain to your pro website. Your platform
							address ({proSlug}.interioring.com) will continue to work.
						</p>
						<div className="flex gap-2">
							<Input
								placeholder="www.example.com"
								value={domainInput}
								onChange={(e) => setDomainInput(e.target.value)}
								onKeyDown={(e) => {
									Eif (e.key === "Enter") handleSetCustomDomain();
								}}
							/>
							<Button
								onClick={handleSetCustomDomain}
								disabled={settingDomain || !domainInput.trim()}
							>
								{settingDomain ? "Connecting..." : "Connect Domain"}
							</Button>
						</div>
					</div>
				) : !customDomainVerified ? (
					/* State 2: Domain set, not verified */
					<div className="space-y-4">
						<div className="flex items-center justify-between">
							<div>
								<p className="font-medium text-foreground-default">
									{customDomain}
								</p>
								<span className="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-medium bg-amber-100 text-amber-700 rounded-full mt-1">
									Pending verification
								</span>
							</div>
							<button
								type="button"
								onClick={handleRemoveCustomDomain}
								disabled={removingDomain}
								className="text-sm text-foreground-muted hover:text-error transition-colors"
							>
								{removingDomain ? "Removing..." : "Remove"}
							</button>
						</div>
 
						{/* DNS Instructions */}
						<div className="p-4 bg-background-subtle border border-border-default rounded-lg space-y-3">
							<p className="text-sm font-medium text-foreground-default">
								Add this DNS record at your domain provider:
							</p>
							<div className="grid grid-cols-3 gap-2 text-sm">
								<div>
									<p className="text-foreground-muted text-xs uppercase tracking-wide">Type</p>
									<p className="font-mono font-medium text-foreground-default">CNAME</p>
								</div>
								<div>
									<p className="text-foreground-muted text-xs uppercase tracking-wide">Name</p>
									<p className="font-mono font-medium text-foreground-default break-all">
										{customDomain}
									</p>
								</div>
								<div>
									<p className="text-foreground-muted text-xs uppercase tracking-wide">Target</p>
									<p className="font-mono font-medium text-foreground-default break-all">
										{cnameTarget}
									</p>
								</div>
							</div>
							<p className="text-xs text-foreground-muted">
								DNS changes can take up to 48 hours to propagate. We recommend
								using a <strong>www</strong> subdomain for best compatibility.
							</p>
						</div>
 
						{/* Verify result message */}
						{domainVerifyResult && domainVerifyResult !== "verified" && (
							<div className="p-3 bg-amber-50 border border-amber-200 rounded-lg text-sm text-amber-700">
								{domainVerifyResult}
							</div>
						)}
 
						<Button
							variant="outline"
							onClick={handleVerifyDomain}
							disabled={verifyingDomain}
						>
							{verifyingDomain ? "Verifying..." : "Verify DNS"}
						</Button>
					</div>
				) : (
					/* State 3: Domain set + verified */
					<div className="space-y-3">
						<div className="flex items-center justify-between">
							<div>
								<div className="flex items-center gap-2">
									<p className="font-medium text-foreground-default">
										{customDomain}
									</p>
									<span className="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-medium bg-success-light text-success rounded-full">
										<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
											<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
										</svg>
										Verified
									</span>
								</div>
								<div className="mt-2 space-y-1 text-sm text-foreground-muted">
									<p>{customDomainUrl}</p>
									<p>https://{proSlug}.interioring.com</p>
								</div>
							</div>
							<button
								type="button"
								onClick={handleRemoveCustomDomain}
								disabled={removingDomain}
								className="text-sm text-foreground-muted hover:text-error transition-colors"
							>
								{removingDomain ? "Removing..." : "Remove"}
							</button>
						</div>
					</div>
				)}
 
				{/* Domain error */}
				{domainError && (
					<div className="p-3 bg-notification-error-bg border border-notification-error-border rounded-lg text-sm text-notification-error-text">
						{domainError}
					</div>
				)}
			</CardContent>
		</Card>
	);
}