diff --git a/package.json b/package.json index 7c427ce..d4bc41e 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "dependencies": { "@emotion/react": "^11.14.0", "@emotion/styled": "^11.14.1", + "@hookform/resolvers": "^7.0.3", "@mui/icons-material": "^7.3.11", "@mui/material": "^7.3.11", "@mui/styled-engine-sc": "^7.3.10", @@ -36,9 +37,11 @@ "react": "^19.3.1", "react-dom": "^19.3.1", "react-email": "^6.11.6", + "react-hook-form": "8.0.0-beta.0", "react-minisearch": "^7.1.4", "react-router-dom": "^7.18.5", - "styled-components": "^6.6.0" + "styled-components": "^6.6.0", + "zod": "^4.6.7" }, "devDependencies": { "@biomejs/biome": "2.6.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6106758..680a732 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,6 +13,9 @@ importers: "@emotion/styled": specifier: ^11.14.1 version: 11.14.1(@emotion/react@11.14.0(@types/react@19.3.0)(react@19.3.1))(@types/react@19.3.0)(react@19.3.1) + "@hookform/resolvers": + specifier: ^7.0.3 + version: 7.0.3(ajv@8.20.0)(react-hook-form@8.0.0-beta.0(react@19.3.1))(zod@4.6.7) "@mui/icons-material": specifier: ^7.3.11 version: 7.3.11(@mui/material@7.3.11(@emotion/react@11.14.0(@types/react@19.3.0)(react@19.3.1))(@emotion/styled@11.14.1(@emotion/react@11.14.0(@types/react@19.3.0)(react@19.3.1))(@types/react@19.3.0)(react@19.3.1))(@types/react@19.3.0)(react-dom@19.3.1(react@19.3.1))(react@19.3.1))(@types/react@19.3.0)(react@19.3.1) @@ -52,6 +55,9 @@ importers: react-email: specifier: ^6.11.6 version: 6.11.6(react-dom@19.3.1(react@19.3.1))(react@19.3.1) + react-hook-form: + specifier: 8.0.0-beta.0 + version: 8.0.0-beta.0(react@19.3.1) react-minisearch: specifier: ^7.1.4 version: 7.1.4(minisearch@7.2.0)(react@19.3.1) @@ -61,6 +67,9 @@ importers: styled-components: specifier: ^6.6.0 version: 6.6.0(react-dom@19.3.1(react@19.3.1))(react@19.3.1) + zod: + specifier: ^4.6.7 + version: 4.6.7 devDependencies: "@biomejs/biome": specifier: 2.6.3 @@ -607,6 +616,84 @@ packages: cpu: [x64] os: [win32] + "@hookform/resolvers@7.0.3": + resolution: + { + integrity: sha512-beGY1JY6K5cyWlswD4BVf7UTb5MxSjKkjljrRyLPT3KIQzsTU0jOibBKqtdcKqIZYQUUyURed7roLWZtSdAtiQ==, + } + peerDependencies: + "@standard-schema/spec": ^1.0.0 + "@vinejs/vine": ^2.0.0 || ^3.0.0 || ^4.0.0 + ajv: ^8.12.0 + ajv-errors: ^3.0.0 + ajv-formats: ^2.1.1 + arktype: ^2.0.0 + ata-validator: ^1.23.0 + class-transformer: ">=0.4.0" + class-validator: ">=0.12.0" + computed-types: ^1.0.0 + effect: ^4.0.0 + fluentvalidation-ts: ^3.0.0 + fp-ts: ^2.7.0 + io-ts: ^2.0.0 + joi: ^17.0.0 || ^18.0.0 + nope-validator: ">=0.12.0" + react-hook-form: ^7.55.0 + superstruct: ">=0.12.0" + typanion: ^3.3.2 + typebox: ^1.0.0 + valibot: ">=0.31.0 || ^1.0.0-beta.4 || ^1.0.0-rc" + vest: ">=6.0.0" + yup: ^1.0.0 + zod: ^3.25.0 || ^4.0.0 + peerDependenciesMeta: + "@standard-schema/spec": + optional: true + "@vinejs/vine": + optional: true + ajv: + optional: true + ajv-errors: + optional: true + ajv-formats: + optional: true + arktype: + optional: true + ata-validator: + optional: true + class-transformer: + optional: true + class-validator: + optional: true + computed-types: + optional: true + effect: + optional: true + fluentvalidation-ts: + optional: true + fp-ts: + optional: true + io-ts: + optional: true + joi: + optional: true + nope-validator: + optional: true + superstruct: + optional: true + typanion: + optional: true + typebox: + optional: true + valibot: + optional: true + vest: + optional: true + yup: + optional: true + zod: + optional: true + "@jridgewell/gen-mapping@0.3.13": resolution: { @@ -979,6 +1066,12 @@ packages: integrity: sha512-9BCxFwvbGg/RsZK9tjXd8s4UcwR0MWeFQ1XEKIQVVvAGJyINdrqKMcTRyLoK8Rse1GjzLV9cwjWV1olXRWEXVA==, } + "@standard-schema/utils@0.3.0": + resolution: + { + integrity: sha512-e7Mew686owMaPJVNNLs55PUvgz371nKgwsc4vxE49zsODpJEnxgxRo2y/OKrqueavXgZNMDVj3DdHFlaSAeU8g==, + } + "@tanstack/react-virtual@3.14.24": resolution: { @@ -2089,6 +2182,15 @@ packages: react: ^18.0 || ^19.0 || ^19.0.0-rc react-dom: ^18.0 || ^19.0 || ^19.0.0-rc + react-hook-form@8.0.0-beta.0: + resolution: + { + integrity: sha512-tQINTMV8IsJ6/E8QfGC2Y2NNN7yAlf2M7PFDDn4VwDGYWkMgZGkgYyzwM5Qpm2EoajIJIgi0uIctiQKci6HLBQ==, + } + engines: { node: ">=18.0.0" } + peerDependencies: + react: ^16.8.0 || ^17 || ^18 || ^19 + react-is@16.13.1: resolution: { @@ -2473,6 +2575,12 @@ packages: } engines: { node: ">=18" } + zod@4.6.7: + resolution: + { + integrity: sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==, + } + snapshots: "@babel/code-frame@7.29.7": dependencies: @@ -2750,6 +2858,14 @@ snapshots: "@esbuild/win32-x64@0.28.2": optional: true + "@hookform/resolvers@7.0.3(ajv@8.20.0)(react-hook-form@8.0.0-beta.0(react@19.3.1))(zod@4.6.7)": + dependencies: + "@standard-schema/utils": 0.3.0 + react-hook-form: 8.0.0-beta.0(react@19.3.1) + optionalDependencies: + ajv: 8.20.0 + zod: 4.6.7 + "@jridgewell/gen-mapping@0.3.13": dependencies: "@jridgewell/sourcemap-codec": 1.6.0 @@ -2992,6 +3108,8 @@ snapshots: "@socket.io/component-emitter@3.1.2": {} + "@standard-schema/utils@0.3.0": {} + "@tanstack/react-virtual@3.14.24(react-dom@19.3.1(react@19.3.1))(react@19.3.1)": dependencies: "@tanstack/virtual-core": 3.19.9 @@ -3586,6 +3704,10 @@ snapshots: - supports-color - utf-8-validate + react-hook-form@8.0.0-beta.0(react@19.3.1): + dependencies: + react: 19.3.1 + react-is@16.13.1: {} react-is@19.3.1: {} @@ -3778,3 +3900,5 @@ snapshots: yaml@1.10.3: {} yoctocolors@2.2.0: {} + + zod@4.6.7: {} diff --git a/src/view/moles/AddAccountForm.tsx b/src/view/moles/AddAccountForm.tsx index d7ae6cb..6cabe4c 100644 --- a/src/view/moles/AddAccountForm.tsx +++ b/src/view/moles/AddAccountForm.tsx @@ -15,6 +15,9 @@ import type { RegistrationData, } from "../../nonview/core/accountStorage"; import ErrorMessage from "../atoms/ErrorMessage"; +import { z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm, useWatch } from "react-hook-form"; // Email service provider configurations // smtpSecure=true means implicit TLS (port 465); @@ -61,6 +64,33 @@ const EMAIL_PROVIDERS = { }, }; +type ProviderKey = keyof typeof EMAIL_PROVIDERS; + +const addAccountSchema = z.object({ + name: z.string().trim().min(1, "Name is required"), + email: z.email("Email is invalid").min(1, "Email is required"), + emailServiceProvider: z.enum(["gmail", "outlook", "yahoo", "custom"]), + emailAddress: z + .email("Email address is invalid") + .min(1, "Email address for service is required"), + emailPassword: z.string().min(1, "Email service password is required"), + imapHost: z.string().min(1, "IMAP host is required"), + imapPort: z.number().int().positive("Must be a valid port"), + imapSecure: z.boolean(), + smtpHost: z.string().min(1, "SMTP host is required"), + smtpPort: z.number().int().positive("Must be a valid port"), + smtpSecure: z.boolean(), +}); + +export type AddAccountFormData = z.infer; + +interface AddAccountFormProps { + onSubmit: (data: RegistrationData) => Promise; + loading?: boolean; + onSuccess: (account: LinkedAccount) => void; + successCtaLabel?: string; +} + type Step = 1 | 2 | "success"; interface AddAccountFormProps { @@ -78,137 +108,77 @@ const AddAccountForm = ({ successCtaLabel = "Continue", }: AddAccountFormProps) => { const [step, setStep] = useState(1); - const [formData, setFormData] = useState({ - name: "", - email: "", - emailServiceProvider: "gmail", - emailAddress: "", - emailPassword: "", - imapHost: EMAIL_PROVIDERS.gmail.imapHost, - imapPort: EMAIL_PROVIDERS.gmail.imapPort as number, - imapSecure: EMAIL_PROVIDERS.gmail.imapSecure, - smtpHost: EMAIL_PROVIDERS.gmail.smtpHost, - smtpPort: EMAIL_PROVIDERS.gmail.smtpPort as number, - smtpSecure: EMAIL_PROVIDERS.gmail.smtpSecure, - }); - const [errors, setErrors] = useState>({}); + const [formError, setFormError] = useState(""); - const [submitting, setSubmitting] = useState(false); const [createdAccount, setCreatedAccount] = useState( null, ); - const isBusy = loading || submitting; - - const handleChange = - (field: string) => (e: React.ChangeEvent) => { - const value = e.target.value; - let updates: Record = { [field]: value }; - - // Auto-populate server settings when provider changes. - if ( - field === "emailServiceProvider" && - EMAIL_PROVIDERS[value as keyof typeof EMAIL_PROVIDERS] - ) { - const provider = EMAIL_PROVIDERS[value as keyof typeof EMAIL_PROVIDERS]; - updates = { - ...updates, - imapHost: provider.imapHost, - imapPort: provider.imapPort, - imapSecure: provider.imapSecure, - smtpHost: provider.smtpHost, - smtpPort: provider.smtpPort, - smtpSecure: provider.smtpSecure, - }; - } - - setFormData((prev) => ({ ...prev, ...updates })); - if (errors[field]) { - setErrors((prev) => ({ ...prev, [field]: "" })); - } - }; + const { + register, + handleSubmit, + trigger, + setValue, + control, + formState: { errors, isSubmitting }, + } = useForm({ + resolver: zodResolver(addAccountSchema), - const validateStep1 = (): boolean => { - const newErrors: Record = {}; - if (!formData.name.trim()) { - newErrors.name = "Name is required"; - } - if (!formData.email) { - newErrors.email = "Email is required"; - } else if (!/\S+@\S+\.\S+/.test(formData.email)) { - newErrors.email = "Email is invalid"; - } - setErrors((prev) => ({ ...prev, ...newErrors })); - return Object.keys(newErrors).length === 0; - }; + shouldUnregister: false, + defaultValues: { + name: "", + email: "", + emailServiceProvider: "gmail", + emailAddress: "", + emailPassword: "", + imapHost: EMAIL_PROVIDERS.gmail.imapHost, + imapPort: EMAIL_PROVIDERS.gmail.imapPort, + imapSecure: EMAIL_PROVIDERS.gmail.imapSecure, + smtpHost: EMAIL_PROVIDERS.gmail.smtpHost, + smtpPort: EMAIL_PROVIDERS.gmail.smtpPort, + smtpSecure: EMAIL_PROVIDERS.gmail.smtpSecure, + }, + }); - const validateStep2 = (): boolean => { - const newErrors: Record = {}; - if (!formData.emailAddress) { - newErrors.emailAddress = "Email address for service is required"; - } else if (!/\S+@\S+\.\S+/.test(formData.emailAddress)) { - newErrors.emailAddress = "Email address is invalid"; - } - if (!formData.emailPassword) { - newErrors.emailPassword = "Email service password is required"; - } - if (!formData.imapHost) { - newErrors.imapHost = "IMAP host is required"; - } - if (!formData.smtpHost) { - newErrors.smtpHost = "SMTP host is required"; - } - setErrors((prev) => ({ ...prev, ...newErrors })); - return Object.keys(newErrors).length === 0; - }; + const selectedProvider = useWatch({ + control, + name: "emailServiceProvider", + }); - const handleContinue = () => { + const handleContinue = async () => { setFormError(""); - if (validateStep1()) { + const isValid = await trigger(["name", "email"]); + if (isValid) { setStep(2); } }; - const handleBack = () => { - setFormError(""); - setStep(1); - }; - - const handleComplete = async () => { + const onComplete = async (data: AddAccountFormData) => { setFormError(""); - if (!validateStep2()) return; - - // Prepare the payload for submission - const payload = { - name: formData.name, - email: formData.email, - emailServiceProvider: formData.emailServiceProvider, - emailAddress: formData.emailAddress, - emailPassword: formData.emailPassword, - imapHost: formData.imapHost, - imapPort: parseInt(String(formData.imapPort), 10), - imapSecure: formData.imapSecure, - smtpHost: formData.smtpHost, - smtpPort: parseInt(String(formData.smtpPort), 10), - smtpSecure: formData.smtpSecure, - }; - - setSubmitting(true); try { - const account = await onSubmit(payload as RegistrationData); + const account = await onSubmit(data as unknown as RegistrationData); setCreatedAccount(account); setStep("success"); } catch (error) { setFormError( (error as Error).message || "Registration failed. Please try again.", ); - } finally { - setSubmitting(false); } }; + const isBusy = loading || isSubmitting; + + const handleBack = () => { + setFormError(""); + setStep(1); + }; + return ( - + {step !== "success" && ( ) => { + const providerKey = e.target.value as ProviderKey; + const provider = EMAIL_PROVIDERS[providerKey]; + if (provider) { + setValue("imapHost", provider.imapHost); + setValue("imapPort", provider.imapPort); + setValue("imapSecure", provider.imapSecure); + setValue("smtpHost", provider.smtpHost); + setValue("smtpPort", provider.smtpPort); + setValue("smtpSecure", provider.smtpSecure); + } + }, + })} > {Object.entries(EMAIL_PROVIDERS).map(([key, provider]) => ( @@ -307,13 +287,12 @@ const AddAccountForm = ({ - {formData.emailServiceProvider === "custom" && ( + {selectedProvider === "custom" && ( <> IMAP Settings (Incoming Mail)