"use client"; import Link from "next/link"; import { useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import { useQuery } from "@tanstack/react-query"; import { CheckCircle2, Compass, FileUp, PlusCircle, Plug, Settings2, Sparkles, UserPlus, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { MedicalDisclaimer } from "@/components/common/medical-disclaimer"; import { SampleBriefingCard } from "@/components/onboarding/sample-briefing-card"; import { StepHeading } from "@/components/onboarding/step-heading"; import { useOnboardingAnswer } from "@/components/onboarding/use-onboarding-flow"; import { useAuth } from "@/hooks/use-auth"; import { useAccountSwitch } from "@/hooks/use-account-switch"; import { setTourReferrer } from "@/components/onboarding/tour-launcher"; import { queryKeys } from "@/lib/query-keys"; import { apiGet } from "@/lib/api/api-fetch"; import { useTranslations } from "@/lib/i18n/context"; import { markChecklistExpanded } from "@/lib/onboarding/checklist-storage"; import type { OnboardingStateDto } from "@/lib/onboarding/needs"; import { accountLabel } from "@/lib/sharing/account-access-view"; interface AiProviderStatus { /** Origin of the provider that would serve this user, if any. */ managedBy?: "user" | "local" | "server" | null; } /** * v1.39 (C2) — the setup flow's exit. * * The dashboard is one click away, already ordered, and the checklist opens * on arrival (design spec §The screens, 5). The tour is an OFFER here rather * than an automatic launch: pressing "Take a short tour" sets the referrer * the shell-level `` reads, and nothing else does, so somebody * who wants the dashboard gets the dashboard. * * Two record-shaped exits ride the answer to Q1: "both" is offered the * profile for the person they look after, and "someone I look after" — whose * profile was created on the confirm screen — is offered that record, through * the same switch the account menu uses, so their first entry lands in the * right record. * * The step is marked done on arrival, once. Everything else on this screen * is presentational: the module map was derived on confirm, the cookie * cleared there too. * * The AI panel leads with the payoff (a static, clearly-labelled SAMPLE * briefing — no model call, no egress, no consent), then the honest * local-first ladder and the "useful without AI" release valve. Value-first, * never a gate: every exit below stays, and setup is a single optional * deep-link. */ export function DoneScreen({ state }: { state: OnboardingStateDto }) { const { t } = useTranslations(); const router = useRouter(); const { user } = useAuth(); const [sampleOpen, setSampleOpen] = useState(false); const answer = useOnboardingAnswer(); const accountSwitch = useAccountSwitch(); // Mark the step once. The write is idempotent, but a strict-mode double // mount would still send it twice; the ref keeps it to one. const marked = useRef(false); const donePending = state.steps.find((step) => step.id === "done")?.status === "pending"; useEffect(() => { if (!donePending || marked.current) return; marked.current = true; answer.mutate({ step: "done" }); // `answer` is a fresh object every render; the mutation it wraps is not. // eslint-disable-next-line react-hooks/exhaustive-deps }, [donePending]); const recordTarget = state.needs.recordTarget; const managedRecord = recordTarget === "someone-else" ? (user?.accountAccess?.accounts.find( (entry) => entry.recordKind === "managed", ) ?? null) : null; function openDashboard() { markChecklistExpanded(); router.push("/"); } function takeTour() { if (user?.id) setTourReferrer(user.id); markChecklistExpanded(); router.push("/"); } // The shared-key note is the ONLY honest divergence in the panel: on a // deployment that ships an operator key, insights already work for the // user, so we say so plainly instead of pushing a BYOK/local setup they // don't need. Presence-only read; no key material is exposed. const { data: aiProvider } = useQuery({ queryKey: queryKeys.userAiProvider(), queryFn: async () => { return apiGet("/api/user/ai-provider"); }, enabled: !!user, }); const sharedKeyServes = aiProvider?.managedBy === "server"; return (

{t("onboarding.done.learning")}

{/* v1.28 — the flagship AI value, made reachable at the one screen every fresh user passes through. The daily briefing / Coach need a provider the rest of onboarding never provisions, so this leads with the payoff (a static, clearly-labelled SAMPLE briefing — no model call, no egress, no consent), then the honest local-first ladder and the "useful without AI" release valve. Value-first, never a gate: the three exits below stay, and setup is a single optional deep-link. */}

{t("onboarding.ai.panelTitle")}

{t("onboarding.ai.panelIntro")}

{sampleOpen ? ( ) : ( )} {sharedKeyServes ? (

{t("onboarding.ai.sharedKeyNote")}

) : null}

{t("onboarding.ai.ladderTitle")}

    {/* Local first — the calm, private default — then BYOK, then the subscription/OAuth path with its training caveat. Same ordering + vendor-blind framing as the shipped document- provider governance. */}
  • {t("onboarding.ai.ladderLocalTitle")}

    {t("onboarding.ai.ladderLocalBody")}

  • {t("onboarding.ai.ladderByokTitle")}

    {t("onboarding.ai.ladderByokBody")}

  • {t("onboarding.ai.ladderOauthTitle")}

    {t("onboarding.ai.ladderOauthBody")}

{t("onboarding.ai.keylessLine")}

{t("onboarding.ai.setupCta")}
{managedRecord ? ( ) : null} {recordTarget === "both" ? ( ) : null}
{/* 2026-07-17 UX-onboarding audit M5 — the module system (default-on set + opt-ins like nutrients / mental health / documents) is otherwise undiscoverable during first-run: the wizard never mentions it, and the only pointer used to be one tour stop. A quiet link here, beside the existing import link, gives every new account one honest pointer to Settings → Modules before they leave the wizard. */}
{t("onboarding.done.importCta")} {t("onboarding.done.modulesCta")}
); }