TypeScript
feat: add Personal details page
Vipul Kumar035e85a63c00
diff --git a/src/app/personal-details/page.tsx b/src/app/personal-details/page.tsxnew file mode 100644index 0000000..06faa8e--- /dev/null+++ b/src/app/personal-details/page.tsx@@ -0,0 +1,8 @@+import type { Metadata } from "next";+import { PersonalDetailsPage } from "@/components/pages/personal-details/personal-details-page";++export const metadata: Metadata = { title: "Personal details" };++export default function Page() {+ return <PersonalDetailsPage />;+}diff --git a/src/components/pages/personal-details/personal-details-page.tsx b/src/components/pages/personal-details/personal-details-page.tsxnew file mode 100644index 0000000..2f74810--- /dev/null+++ b/src/components/pages/personal-details/personal-details-page.tsx@@ -0,0 +1,304 @@+"use client";++import { useEffect, useMemo, useState } from "react";+import { BriefcaseIcon, CakeIcon, MailIcon, MapPinIcon, PhoneIcon, RotateCcwIcon } from "lucide-react";+import { toast } from "sonner";+import { Field } from "@/components/dialogs/field";+import { Button } from "@/components/ui/button";+import { Input } from "@/components/ui/input";+import { Textarea } from "@/components/ui/textarea";+import { fromKey, MONTHS } from "@/lib/dates";+import type { Profile } from "@/lib/types";+import { cn } from "@/lib/utils";+import { useToday } from "@/hooks/use-today";+import { useAppStore } from "@/stores/app-store";+import { Card, CardHeader, PageHeader, PageRoot } from "@/components/pages/shared/page-chrome";++const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;+const BIO_MAX = 280;++function initialsFrom(name: string) {+ const parts = name.trim().split(/\s+/).filter(Boolean);+ if (!parts.length) return "";+ const first = parts[0][0] ?? "";+ const last = parts.length > 1 ? parts[parts.length - 1][0] : "";+ return (first + last).toUpperCase();+}++function tenure(start: string, today: string) {+ if (!start || start > today) return null;+ const a = fromKey(start);+ const b = fromKey(today);+ let months = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth());+ if (b.getDate() < a.getDate()) months -= 1;+ const y = Math.floor(months / 12);+ const m = months % 12;+ if (!y && !m) return "Less than a month";+ return [y ? `${y} ${y === 1 ? "year" : "years"}` : "", m ? `${m} ${m === 1 ? "month" : "months"}` : ""]+ .filter(Boolean)+ .join(" ");+}++function validate(p: Profile): Partial<Record<keyof Profile, string>> {+ const errors: Partial<Record<keyof Profile, string>> = {};+ if (!p.name.trim()) errors.name = "Your name is required.";+ if (!p.email.trim()) errors.email = "An email address is required.";+ else if (!EMAIL_RE.test(p.email.trim())) errors.email = "Enter a valid email address.";+ if (!p.initials.trim()) errors.initials = "Add 1–3 initials.";+ if (p.bio.length > BIO_MAX) errors.bio = `Keep it under ${BIO_MAX} characters.`;+ if (p.startDate && p.birthday && p.birthday >= p.startDate) errors.birthday = "Birthday must be before your start date.";+ return errors;+}++export function PersonalDetailsPage() {+ const today = useToday();+ const profile = useAppStore((s) => s.profile);+ const updateProfile = useAppStore((s) => s.updateProfile);++ // Only the fields the user touched are kept locally; everything else reads+ // through to the store, so a late store hydration never clobbers the form.+ const [edits, setEdits] = useState<Partial<Profile>>({});+ const [showErrors, setShowErrors] = useState(false);++ const draft = useMemo(() => ({ ...profile, ...edits }), [profile, edits]);+ const changed = useMemo(+ () => (Object.keys(edits) as (keyof Profile)[]).filter((k) => edits[k] !== profile[k]),+ [edits, profile],+ );+ const dirty = changed.length > 0;+ const errors = useMemo(() => validate(draft), [draft]);+ const hasErrors = Object.keys(errors).length > 0;++ const set = <K extends keyof Profile>(key: K, value: Profile[K]) =>+ setEdits((e) => {+ const next = { ...e, [key]: value };+ if (value === profile[key]) delete next[key];+ return next;+ });++ const reset = () => {+ setEdits({});+ setShowErrors(false);+ };++ const save = () => {+ if (!dirty) return;+ if (hasErrors) {+ setShowErrors(true);+ toast.error("Check the highlighted fields", { description: "Some details need fixing before saving." });+ return;+ }+ const patch: Partial<Profile> = {};+ for (const k of changed) (patch as Record<string, string>)[k] = (draft[k] as string).trim();+ if (patch.initials) patch.initials = patch.initials.toUpperCase();+ updateProfile(patch);+ setEdits({});+ setShowErrors(false);+ toast.success("Profile saved", {+ description: `${changed.length} ${changed.length === 1 ? "field" : "fields"} updated`,+ });+ };++ // ⌘S / Ctrl+S saves; warn before leaving with unsaved edits.+ useEffect(() => {+ const onKey = (e: KeyboardEvent) => {+ if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "s") {+ e.preventDefault();+ document.getElementById("profile-save")?.click();+ }+ };+ const onUnload = (e: BeforeUnloadEvent) => {+ if (dirty) e.preventDefault();+ };+ window.addEventListener("keydown", onKey);+ window.addEventListener("beforeunload", onUnload);+ return () => {+ window.removeEventListener("keydown", onKey);+ window.removeEventListener("beforeunload", onUnload);+ };+ }, [dirty]);++ // Errors show once a field has been edited, or for every field after a failed save.+ const err = (k: keyof Profile) => ((showErrors || k in edits) && errors[k]) || null;+ const input = (k: keyof Profile, props: React.ComponentProps<typeof Input> = {}) => (+ <Input+ id={`pd-${k}`}+ value={draft[k]}+ onChange={(e) => set(k, e.target.value)}+ aria-invalid={err(k) ? true : undefined}+ {...props}+ />+ );++ const t = tenure(draft.startDate, today);+ const birthday = draft.birthday ? fromKey(draft.birthday) : null;++ return (+ <PageRoot>+ <PageHeader+ title="Your profile"+ description={+ dirty+ ? `${changed.length} unsaved ${changed.length === 1 ? "change" : "changes"} · ⌘S to save`+ : "Shared with your manager and People Ops"+ }+ actions={+ <>+ <Button onClick={reset} disabled={!dirty}>+ <RotateCcwIcon className="text-icon" /> Reset+ </Button>+ <Button id="profile-save" type="submit" form="profile-form" variant="brand" disabled={!dirty}>+ Save changes+ </Button>+ </>+ }+ />+ <form+ id="profile-form"+ className="mx-auto grid w-full max-w-[1040px] grid-cols-1 items-start gap-4 p-4 lg:grid-cols-[280px_minmax(0,1fr)]"+ onSubmit={(e) => {+ e.preventDefault();+ save();+ }}+ noValidate+ >+ {/* Summary */}+ <Card className="lg:sticky lg:top-[84px]">+ <div className="flex flex-col items-center gap-3 px-4 pt-6 pb-5 text-center">+ <span className="flex size-16 items-center justify-center rounded-[18px] bg-brand-tint">+ <span className="font-inter text-[22px] leading-none font-semibold tracking-[-0.33px] text-brand">+ {draft.initials.toUpperCase() || "?"}+ </span>+ </span>+ <div className="grid gap-2">+ <p className="t-16 text-fg">{draft.name || "Your name"}</p>+ <p className="t-14 text-fg-2">+ {[draft.jobTitle, draft.department].filter(Boolean).join(" · ") || "Add a job title"}+ </p>+ </div>+ </div>+ <ul className="grid gap-3 border-t border-subtle px-4 py-4">+ <SummaryRow icon={<MailIcon />}>{draft.email || "—"}</SummaryRow>+ <SummaryRow icon={<PhoneIcon />}>{draft.phone || "—"}</SummaryRow>+ <SummaryRow icon={<MapPinIcon />}>{draft.location || "—"}</SummaryRow>+ <SummaryRow icon={<BriefcaseIcon />}>+ {draft.startDate ? `Joined ${MONTHS[fromKey(draft.startDate).getMonth()].slice(0, 3)} ${draft.startDate.slice(0, 4)}` : "—"}+ {t ? <span className="text-placeholder"> · {t}</span> : null}+ </SummaryRow>+ <SummaryRow icon={<CakeIcon />}>+ {birthday ? `${MONTHS[birthday.getMonth()]} ${birthday.getDate()}` : "—"}+ </SummaryRow>+ </ul>+ </Card>++ <div className="grid min-w-0 gap-4">+ <Section title="Basic information" description="How you appear across the workspace">+ <div className="grid grid-cols-[minmax(0,1fr)_120px] items-start gap-4">+ <Field label="Full name" htmlFor="pd-name" error={err("name")}>+ {input("name", { autoComplete: "name", required: true })}+ </Field>+ <Field label="Initials" htmlFor="pd-initials" error={err("initials")}>+ {input("initials", {+ maxLength: 3,+ className: "uppercase",+ onChange: (e) => set("initials", e.target.value.toUpperCase().replace(/[^A-Z]/g, "")),+ })}+ </Field>+ </div>+ {initialsFrom(draft.name) && initialsFrom(draft.name) !== draft.initials ? (+ <button+ type="button"+ onClick={() => set("initials", initialsFrom(draft.name))}+ className="t-12 -mt-2 w-fit text-brand outline-none hover-fine:text-brand-strong focus-visible:underline"+ >+ Use “{initialsFrom(draft.name)}” from your name+ </button>+ ) : null}+ <div className="grid grid-cols-2 items-start gap-4">+ <Field label="Email" htmlFor="pd-email" error={err("email")}>+ {input("email", { type: "email", autoComplete: "email", required: true })}+ </Field>+ <Field label="Phone" htmlFor="pd-phone">+ {input("phone", { type: "tel", autoComplete: "tel", placeholder: "+44 20 0000 0000" })}+ </Field>+ </div>+ </Section>++ <Section title="Work" description="Your role and reporting line">+ <div className="grid grid-cols-2 items-start gap-4">+ <Field label="Job title" htmlFor="pd-jobTitle">+ {input("jobTitle", { autoComplete: "organization-title" })}+ </Field>+ <Field label="Department" htmlFor="pd-department">+ {input("department")}+ </Field>+ <Field label="Manager" htmlFor="pd-manager">+ {input("manager")}+ </Field>+ <Field label="Location" htmlFor="pd-location">+ {input("location", { placeholder: "City, Country" })}+ </Field>+ <Field label="Start date" htmlFor="pd-startDate" hint={t ? `${t} at the company` : undefined}>+ {input("startDate", { type: "date", max: today })}+ </Field>+ </div>+ </Section>++ <Section title="Personal" description="Only visible to People Ops">+ <div className="grid grid-cols-2 items-start gap-4">+ <Field label="Birthday" htmlFor="pd-birthday" error={err("birthday")}>+ {input("birthday", { type: "date", max: today })}+ </Field>+ <Field label="Emergency contact" htmlFor="pd-emergencyContact" hint="Name and phone number">+ {input("emergencyContact", { placeholder: "Name · phone" })}+ </Field>+ <Field label="Home address" htmlFor="pd-address" className="col-span-2">+ {input("address", { autoComplete: "street-address" })}+ </Field>+ </div>+ </Section>++ <Section title="About" description="A short intro for your teammates">+ <Field+ label="Bio"+ htmlFor="pd-bio"+ error={err("bio")}+ hint={+ <span className={cn("tabular-nums", draft.bio.length > BIO_MAX * 0.9 && "text-warning")}>+ {draft.bio.length}/{BIO_MAX}+ </span>+ }+ >+ <Textarea+ id="pd-bio"+ rows={3}+ value={draft.bio}+ onChange={(e) => set("bio", e.target.value)}+ placeholder="What you work on, what you love…"+ aria-invalid={err("bio") ? true : undefined}+ />+ </Field>+ </Section>+ </div>+ </form>+ </PageRoot>+ );+}++function Section({ title, description, children }: { title: string; description: string; children: React.ReactNode }) {+ return (+ <Card>+ <CardHeader title={title} description={description} />+ <div className="grid gap-4 p-4">{children}</div>+ </Card>+ );+}++function SummaryRow({ icon, children }: { icon: React.ReactNode; children: React.ReactNode }) {+ return (+ <li className="flex items-center gap-2.5 [&>svg]:size-3.5 [&>svg]:shrink-0 [&>svg]:text-icon">+ {icon}+ <span className="t-14 min-w-0 flex-1 truncate text-fg-2">{children}</span>+ </li>+ );+}