Open History to see the last commit
1import { useForm } from "@tanstack/react-form";
2import type { ReactNode } from "react";
3
4import { Button } from "@/components/ui/button";
5import { Input } from "@/components/ui/input";
6import {
7 Select,
8 SelectContent,
9 SelectItem,
10 SelectTrigger,
11 SelectValue,
12} from "@/components/ui/select";
13import { Switch } from "@/components/ui/switch";
14import { updateAccountSettings, useAccount, useFolders } from "@/data/notes-data";
15import type { AccountSettings } from "@/data/notes-data";
16import { accountSchema, newNoteStyleSchema, sortNotesBySchema } from "@/fixtures";
17import type { NewNoteStyle, SortNotesBy } from "@/fixtures";
18import { cn } from "@/lib/utils";
19
20import { AccountAvatar } from "./icons";
21
22const accountSettingsSchema = accountSchema.pick({
23 name: true,
24 email: true,
25 defaultFolderId: true,
26 sortNotesBy: true,
27 newNotesStartWith: true,
28 groupNotesByDate: true,
29});
30
31const SORT_LABELS: Record<SortNotesBy, string> = {
32 dateEdited: "Date Edited",
33 dateCreated: "Date Created",
34 title: "Title",
35};
36
37const NEW_NOTE_LABELS: Record<NewNoteStyle, string> = {
38 title: "Title",
39 heading: "Heading",
40 body: "Body",
41};
42
43interface SettingsFormProps {
44 /** Cards sit on the macOS content background or the iOS grouped background. */
45 variant: "mac" | "ios";
46}
47
48export function SettingsForm({ variant }: SettingsFormProps) {
49 const account = useAccount();
50 const folders = useFolders();
51
52 const defaultValues: AccountSettings = {
53 name: account.name,
54 email: account.email,
55 defaultFolderId: account.defaultFolderId,
56 sortNotesBy: account.sortNotesBy,
57 newNotesStartWith: account.newNotesStartWith,
58 groupNotesByDate: account.groupNotesByDate,
59 };
60
61 const form = useForm({
62 defaultValues,
63 validators: { onChange: accountSettingsSchema },
64 onSubmit: async ({ value, formApi }) => {
65 await updateAccountSettings(value);
66 formApi.reset(value);
67 },
68 });
69
70 const card =
71 variant === "ios"
72 ? "rounded-[26px] bg-ios-card"
73 : "rounded-xl border border-mac-separator bg-black/[0.025] dark:bg-white/[0.04]";
74 const usedPercent = Math.round((account.storageUsedGb / account.storageTotalGb) * 100);
75
76 return (
77 <form
78 className={cn("space-y-6", variant === "ios" ? "text-[17px]" : "text-[13px]")}
79 onSubmit={(event) => {
80 event.preventDefault();
81 void form.handleSubmit();
82 }}
83 >
84 <div className={cn(card, "flex items-center gap-4 p-4")}>
85 <AccountAvatar name={account.name} className="size-14 text-lg" />
86 <div className="min-w-0 flex-1">
87 <p className="truncate text-[1.15em] font-semibold">{account.name}</p>
88 <p className="truncate text-[0.95em] opacity-60">{account.email}</p>
89 <div className="mt-2 h-1.5 overflow-hidden rounded-full bg-black/10 dark:bg-white/10">
90 <div
91 className="h-full rounded-full bg-notes-accent"
92 style={{ width: `${usedPercent}%` }}
93 />
94 </div>
95 <p className="mt-1 text-[0.85em] opacity-60">
96 {account.plan} · {account.storageUsedGb} GB of {account.storageTotalGb} GB used
97 </p>
98 </div>
99 </div>
100
101 <SettingsGroup title="Account" className={card}>
102 <form.Field name="name">
103 {(field) => (
104 <SettingsRow
105 label="Name"
106 htmlFor={field.name}
107 error={firstError(field.state.meta.errors)}
108 >
109 <Input
110 id={field.name}
111 value={field.state.value}
112 onChange={(event) => field.handleChange(event.target.value)}
113 onBlur={field.handleBlur}
114 aria-invalid={!field.state.meta.isValid}
115 className="w-full max-w-64 text-right"
116 />
117 </SettingsRow>
118 )}
119 </form.Field>
120 <form.Field name="email">
121 {(field) => (
122 <SettingsRow
123 label="Email"
124 htmlFor={field.name}
125 error={firstError(field.state.meta.errors)}
126 >
127 <Input
128 id={field.name}
129 type="email"
130 value={field.state.value}
131 onChange={(event) => field.handleChange(event.target.value)}
132 onBlur={field.handleBlur}
133 aria-invalid={!field.state.meta.isValid}
134 className="w-full max-w-64 text-right"
135 />
136 </SettingsRow>
137 )}
138 </form.Field>
139 </SettingsGroup>
140
141 <SettingsGroup title="Notes" className={card}>
142 <form.Field name="defaultFolderId">
143 {(field) => (
144 <SettingsRow label="Default folder" htmlFor={field.name}>
145 <Select value={field.state.value} onValueChange={field.handleChange}>
146 <SelectTrigger id={field.name} size="sm" className="min-w-36">
147 <SelectValue />
148 </SelectTrigger>
149 <SelectContent align="end">
150 {folders.map((folder) => (
151 <SelectItem key={folder.id} value={folder.id}>
152 {folder.name}
153 </SelectItem>
154 ))}
155 </SelectContent>
156 </Select>
157 </SettingsRow>
158 )}
159 </form.Field>
160 <form.Field name="sortNotesBy">
161 {(field) => (
162 <SettingsRow label="Sort notes by" htmlFor={field.name}>
163 <Select
164 value={field.state.value}
165 onValueChange={(value) => field.handleChange(sortNotesBySchema.parse(value))}
166 >
167 <SelectTrigger id={field.name} size="sm" className="min-w-36">
168 <SelectValue />
169 </SelectTrigger>
170 <SelectContent align="end">
171 {sortNotesBySchema.options.map((option) => (
172 <SelectItem key={option} value={option}>
173 {SORT_LABELS[option]}
174 </SelectItem>
175 ))}
176 </SelectContent>
177 </Select>
178 </SettingsRow>
179 )}
180 </form.Field>
181 <form.Field name="newNotesStartWith">
182 {(field) => (
183 <SettingsRow label="New notes start with" htmlFor={field.name}>
184 <Select
185 value={field.state.value}
186 onValueChange={(value) => field.handleChange(newNoteStyleSchema.parse(value))}
187 >
188 <SelectTrigger id={field.name} size="sm" className="min-w-36">
189 <SelectValue />
190 </SelectTrigger>
191 <SelectContent align="end">
192 {newNoteStyleSchema.options.map((option) => (
193 <SelectItem key={option} value={option}>
194 {NEW_NOTE_LABELS[option]}
195 </SelectItem>
196 ))}
197 </SelectContent>
198 </Select>
199 </SettingsRow>
200 )}
201 </form.Field>
202 <form.Field name="groupNotesByDate">
203 {(field) => (
204 <SettingsRow label="Group notes by date" htmlFor={field.name}>
205 <Switch
206 id={field.name}
207 checked={field.state.value}
208 onCheckedChange={field.handleChange}
209 className="data-checked:bg-[#34c759] dark:[&>span]:bg-white"
210 />
211 </SettingsRow>
212 )}
213 </form.Field>
214 </SettingsGroup>
215
216 <form.Subscribe
217 selector={(state) => [state.isDirty, state.canSubmit, state.isSubmitting] as const}
218 >
219 {([isDirty, canSubmit, isSubmitting]) => (
220 <div className="flex justify-end gap-2">
221 <Button type="button" variant="ghost" disabled={!isDirty} onClick={() => form.reset()}>
222 Revert
223 </Button>
224 <Button type="submit" disabled={!isDirty || !canSubmit || isSubmitting}>
225 {isSubmitting ? "Saving…" : "Save"}
226 </Button>
227 </div>
228 )}
229 </form.Subscribe>
230 </form>
231 );
232}
233
234function firstError(errors: unknown[]): string | undefined {
235 const [error] = errors;
236 if (!error) return undefined;
237 if (typeof error === "string") return error;
238 return (error as { message?: string }).message;
239}
240
241function SettingsGroup({
242 title,
243 className,
244 children,
245}: {
246 title: string;
247 className: string;
248 children: ReactNode;
249}) {
250 return (
251 <section>
252 <h2 className="mb-1.5 px-4 text-[0.85em] font-semibold tracking-wide uppercase opacity-55">
253 {title}
254 </h2>
255 <div className={cn(className, "divide-y divide-mac-separator dark:divide-white/10")}>
256 {children}
257 </div>
258 </section>
259 );
260}
261
262function SettingsRow({
263 label,
264 htmlFor,
265 error,
266 children,
267}: {
268 label: string;
269 htmlFor: string;
270 error?: string;
271 children: ReactNode;
272}) {
273 return (
274 <div className="px-4 py-2.5">
275 <div className="flex min-h-8 items-center justify-between gap-4">
276 <label htmlFor={htmlFor} className="shrink-0">
277 {label}
278 </label>
279 <div className="flex min-w-0 flex-1 justify-end">{children}</div>
280 </div>
281 {error && <p className="mt-1 text-right text-[0.85em] text-destructive">{error}</p>}
282 </div>
283 );
284}