All files / src/components Welcome.tsx

87.5% Statements 42/48
86.48% Branches 32/37
76.19% Functions 16/21
86.95% Lines 40/46

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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380                                                          469x 469x   469x               469x             469x 469x     469x 190x 190x 12x 12x           178x       469x 5x 5x 5x   5x     5x 5x                                           5x     4x 4x   1x 1x     1x     3x   1x   5x       469x                                                                     31x                                                               2x                                                                     1x                                                             306x                             37x               2x             34x               3x                       2x                                                                                                                               1232x           263x                  
import { useState } from "react";
import { api, type CreateInboundConnectorRequest } from "../api";
import { WELL_KNOWN_PROVIDERS } from "../utils";
import { MoonIcon, SunIcon } from "./Icons";
 
interface WelcomeProps {
	onSetupComplete: () => void;
	dark: boolean;
	onToggleDark: () => void;
}
 
type ConnectorType = "imap" | "cloudflare-r2";
 
interface ImapFormData {
	imap_host: string;
	imap_port: number;
	imap_tls: number;
	imap_user: string;
	imap_pass: string;
}
 
interface R2FormData {
	cf_r2_account_id: string;
	cf_r2_bucket_name: string;
	cf_r2_access_key_id: string;
	cf_r2_secret_access_key: string;
}
 
export function Welcome({ onSetupComplete, dark, onToggleDark }: WelcomeProps) {
	const [step, setStep] = useState<"intro" | "form">("intro");
	const [connectorType, setConnectorType] = useState<ConnectorType>("imap");
 
	const [imap, setImap] = useState<ImapFormData>({
		imap_host: "",
		imap_port: 993,
		imap_tls: 1,
		imap_user: "",
		imap_pass: "",
	});
 
	const [r2, setR2] = useState<R2FormData>({
		cf_r2_account_id: "",
		cf_r2_bucket_name: "",
		cf_r2_access_key_id: "",
		cf_r2_secret_access_key: "",
	});
 
	const [loading, setLoading] = useState(false);
	const [error, setError] = useState<string | null>(null);
 
	// Auto-fill IMAP server settings when username looks like an email address.
	const handleImapUserChange = (username: string) => {
		const domain = username.split("@")[1]?.toLowerCase();
		if (domain && WELL_KNOWN_PROVIDERS[domain]) {
			const provider = WELL_KNOWN_PROVIDERS[domain];
			setImap((f) => ({
				...f,
				imap_user: username,
				imap_host: f.imap_host || provider.imap_host,
			}));
		} else {
			setImap((f) => ({ ...f, imap_user: username }));
		}
	};
 
	const handleSubmit = async (e: React.FormEvent) => {
		e.preventDefault();
		setLoading(true);
		setError(null);
 
		try {
			// Build connector request
			let connectorReq: CreateInboundConnectorRequest;
			if (connectorType === "imap") {
				connectorReq = {
					name: imap.imap_user || imap.imap_host,
					type: "imap",
					imap_host: imap.imap_host,
					imap_port: imap.imap_port,
					imap_tls: imap.imap_tls,
					imap_user: imap.imap_user,
					imap_pass: imap.imap_pass,
					sync_delete_from_server: 0,
				};
			} else E{
				connectorReq = {
					name: r2.cf_r2_bucket_name || "Cloudflare R2",
					type: "cloudflare-r2",
					cf_r2_account_id: r2.cf_r2_account_id,
					cf_r2_bucket_name: r2.cf_r2_bucket_name,
					cf_r2_access_key_id: r2.cf_r2_access_key_id,
					cf_r2_secret_access_key: r2.cf_r2_secret_access_key,
					cf_r2_prefix: "pending/",
				};
			}
 
			const connector = await api.connectors.inbound.create(connectorReq);
 
			// Test the connection before closing the wizard
			const result = await api.connectors.inbound.test(connector.id);
			if (!result.ok) {
				// Connection failed — delete the connector and show the error
				await api.connectors.inbound.delete(connector.id);
				setError(
					result.error || "Could not connect — please check your credentials and try again.",
				);
				return;
			}
 
			onSetupComplete();
		} catch (err) {
			setError((err as Error).message);
		} finally {
			setLoading(false);
		}
	};
 
	return (
		<div className="h-screen flex flex-col items-center justify-center bg-gray-50 dark:bg-gray-950 px-4">
			{/* Dark mode toggle */}
			<button
				type="button"
				onClick={onToggleDark}
				className="absolute top-4 right-4 text-sm text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors flex items-center gap-1.5"
				title="Toggle dark mode"
			>
				{dark ? (
					<>
						<SunIcon className="w-3.5 h-3.5" /> Light
					</>
				) : (
					<>
						<MoonIcon className="w-3.5 h-3.5" /> Dark
					</>
				)}
			</button>
 
			{step === "intro" ? (
				<div className="text-center max-w-md animate-fadeIn">
					{/* Stork icon */}
					<div className="mb-6">
						<img src="/stork.svg" alt="Stork" className="w-20 h-20 mx-auto rounded-2xl shadow-lg" />
					</div>
					<h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100 mb-2">
						Welcome to Stork
					</h1>
					<p className="text-gray-500 dark:text-gray-400 mb-8 leading-relaxed">
						Your inbox, on your terms. Stork syncs email from any source into an encrypted local
						database — private by default, searchable in seconds.
					</p>
					<button
						type="button"
						onClick={() => setStep("form")}
						className="px-6 py-2.5 bg-stork-600 hover:bg-stork-700 text-white rounded-lg font-medium transition-colors shadow-sm"
					>
						Get Started
					</button>
				</div>
			) : (
				<div className="w-full max-w-lg animate-fadeIn">
					<div className="mb-6 text-center">
						<h2 className="text-xl font-semibold text-gray-900 dark:text-gray-100">
							Connect Your Email
						</h2>
						<p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
							Where should Stork pull your email from?
						</p>
					</div>
 
					{/* Connector type selector */}
					<div className="flex gap-2 mb-4 justify-center">
						<button
							type="button"
							onClick={() => setConnectorType("imap")}
							className={`px-4 py-2 rounded-lg text-sm font-medium border transition-colors ${
								connectorType === "imap"
									? "bg-stork-600 border-stork-600 text-white"
									: "bg-white dark:bg-gray-900 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:border-stork-400"
							}`}
						>
							IMAP
						</button>
						<button
							type="button"
							onClick={() => setConnectorType("cloudflare-r2")}
							className={`px-4 py-2 rounded-lg text-sm font-medium border transition-colors ${
								connectorType === "cloudflare-r2"
									? "bg-stork-600 border-stork-600 text-white"
									: "bg-white dark:bg-gray-900 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:border-stork-400"
							}`}
						>
							Cloudflare R2
						</button>
					</div>
 
					<form
						onSubmit={handleSubmit}
						className="bg-white dark:bg-gray-900 rounded-xl shadow-lg border border-gray-200 dark:border-gray-800 p-6 space-y-5 min-h-[28rem]"
					>
						<p className="text-xs text-gray-500 dark:text-gray-400 text-center">
							Credentials never leave your machine. Stork connects directly from your device.
						</p>
 
						{error && (
							<div className="text-sm text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-900/20 px-3 py-2 rounded-md">
								{error}
							</div>
						)}
 
						{connectorType === "imap" ? (
							<ImapForm data={imap} onChange={setImap} onUserChange={handleImapUserChange} />
						) : (
							<R2Form data={r2} onChange={setR2} />
						)}
 
						{/* Actions */}
						<div className="flex items-center justify-between pt-2">
							<button
								type="button"
								onClick={() => setStep("intro")}
								className="text-sm text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 transition-colors"
							>
								Back
							</button>
							<button
								type="submit"
								disabled={loading}
								className="px-5 py-2 bg-stork-600 hover:bg-stork-700 disabled:opacity-50 text-white rounded-lg font-medium text-sm transition-colors"
							>
								{loading ? "Testing connection..." : "Connect"}
							</button>
						</div>
					</form>
				</div>
			)}
		</div>
	);
}
 
// ── IMAP form ─────────────────────────────────────────────────────────────────
 
function ImapForm({
	data,
	onChange,
	onUserChange,
}: {
	data: ImapFormData;
	onChange: (d: ImapFormData) => void;
	onUserChange: (username: string) => void;
}) {
	return (
		<div className="space-y-3">
			<Field
				label="Username"
				value={data.imap_user}
				onChange={onUserChange}
				placeholder="you@example.com"
				required
				autoFocus
			/>
			<div className="grid grid-cols-3 gap-3">
				<div className="col-span-2">
					<Field
						label="Server"
						value={data.imap_host}
						onChange={(v) => onChange({ ...data, imap_host: v })}
						placeholder="imap.example.com"
						required
					/>
				</div>
				<Field
					label="Port"
					value={String(data.imap_port)}
					onChange={(v) => onChange({ ...data, imap_port: Number(v) || 993 })}
					type="number"
				/>
			</div>
			<Field
				label="Password"
				value={data.imap_pass}
				onChange={(v) => onChange({ ...data, imap_pass: v })}
				type="password"
				required
			/>
			<label className="flex items-center gap-2 text-sm text-gray-600 dark:text-gray-400">
				<input
					type="checkbox"
					checked={data.imap_tls === 1}
					onChange={(e) => onChange({ ...data, imap_tls: e.target.checked ? 1 : 0 })}
					className="rounded border-gray-300 dark:border-gray-600"
				/>
				Use TLS (recommended)
			</label>
		</div>
	);
}
 
// ── Cloudflare R2 form ────────────────────────────────────────────────────────
 
function R2Form({ data, onChange }: { data: R2FormData; onChange: (d: R2FormData) => void }) {
	return (
		<div className="space-y-3">
			<div className="text-xs text-blue-700 dark:text-blue-400 bg-blue-50 dark:bg-blue-900/20 rounded px-3 py-2 space-y-1">
				<p className="font-medium">Cloudflare R2 queue/poll model</p>
				<p>
					A Cloudflare Email Worker writes each inbound email as an object to an R2 bucket. Stork
					polls the bucket on a regular interval — no public webhook required.
				</p>
			</div>
			<Field
				label="Account ID"
				value={data.cf_r2_account_id}
				onChange={(v) => onChange({ ...data, cf_r2_account_id: v })}
				placeholder="Cloudflare Account ID"
				required
				autoFocus
			/>
			<Field
				label="Bucket Name"
				value={data.cf_r2_bucket_name}
				onChange={(v) => onChange({ ...data, cf_r2_bucket_name: v })}
				placeholder="my-email-bucket"
				required
			/>
			<div className="grid grid-cols-2 gap-3">
				<Field
					label="Access Key ID"
					value={data.cf_r2_access_key_id}
					onChange={(v) => onChange({ ...data, cf_r2_access_key_id: v })}
					placeholder="Access Key ID"
					required
				/>
				<Field
					label="Secret Access Key"
					value={data.cf_r2_secret_access_key}
					onChange={(v) => onChange({ ...data, cf_r2_secret_access_key: v })}
					type="password"
					placeholder="Secret Key"
					required
				/>
			</div>
		</div>
	);
}
 
// ── Generic field ─────────────────────────────────────────────────────────────
 
function Field({
	label,
	value,
	onChange,
	type = "text",
	placeholder,
	required = false,
	autoFocus = false,
}: {
	label: string;
	value: string;
	onChange: (v: string) => void;
	type?: string;
	placeholder?: string;
	required?: boolean;
	autoFocus?: boolean;
}) {
	return (
		<label className="block">
			<span className="block text-xs text-gray-500 dark:text-gray-400 mb-1">{label}</span>
			<input
				type={type}
				value={value}
				onChange={(e) => onChange(e.target.value)}
				placeholder={placeholder}
				required={required}
				autoFocus={autoFocus}
				className="w-full text-sm bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-md px-3 py-1.5 placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:ring-1 focus:ring-stork-500 focus:border-stork-500"
			/>
		</label>
	);
}