"use client"; import { useAiProviderState } from "@/hooks/use-ai-capability"; import Link from "next/link"; import { useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import { useQuery } from "@tanstack/react-query"; import { showErrorToast } from "@/lib/errors/error-toast"; 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 { useGrantAiConsent } from "@/hooks/use-grant-ai-consent"; 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.38.19 — the instance-wide tri-state. Fail closed for the * offer: neither `unhealthy` nor `unknown` earns one. Only `unhealthy` is * a verdict this screen may state out loud. */ serverProviderHealth?: "healthy" | "unhealthy" | "unknown"; /** Whether the shared provider may honestly be offered in one tap. */ serverProviderOffer?: boolean; /** Whether this user already holds an active `ai_full` / `ai_coach` receipt. */ serverProviderConsent?: boolean; } /** * 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("/"); } // ── v1.38.19 — the shared provider, offered only when it works ── // // This block used to be one line: `managedBy === "server"` painted a note // reading "insights work for you right now — no setup needed". Both halves // were wrong. `managedBy` is a PRESENCE read — it knows the operator // configured a key, never that the key answers — and "no setup needed" // skipped the consent receipt `consent-guard.ts` demands before any health // data reaches `admin-openai` / `admin-codex`, so the person who believed // the note walked into a `consent.ai.required` refusal on their first // briefing. On 2026-09-11 the operator's own instance was answering HTTP // 500 from its OAuth proxy while this note kept promising otherwise. // // The server decides now, and the four branches below only READ it. The // rest of the panel — sample, ladder, the "fully useful without AI" line, // the setup link — is identical in every branch; nothing here is a gate. // The panel is one optional offer, shown only while this person could set // AI up here: `provider.canConfigure` is false while the operator has AI // switched off and inside a record somebody else owns. HealthLog is // complete without it, so with nothing to offer the screen simply goes // from "done" to the exits. const providerState = useAiProviderState(); const showAiPanel = providerState.canConfigure; const { data: aiProvider } = useQuery({ queryKey: queryKeys.userAiProvider(), queryFn: async () => { return apiGet("/api/user/ai-provider"); }, enabled: !!user && showAiPanel, }); // The tap's own outcome, held locally so the line changes the moment the // receipt is minted rather than a refetch later. const [justGranted, setJustGranted] = useState(false); // The tap on the offer is the consent act; the hook sends the explicit // confirmation the server requires before it records one. const grantConsent = useGrantAiConsent(); function agreeToSharedProvider() { grantConsent.mutate(undefined, { onSuccess: () => setJustGranted(true), onError: (failure) => { // The button stays. A failed grant is a failed grant; the screen // must not read as though consent were on file. showErrorToast(t("onboarding.ai.offer.error"), { cause: failure }); }, }); } const operatorProvides = aiProvider?.managedBy === "server"; const sharedHealth = aiProvider?.serverProviderHealth; const consentOnFile = justGranted || aiProvider?.serverProviderConsent === true; const offerShared = operatorProvides && !consentOnFile && aiProvider?.serverProviderOffer === true; // A receipt on file is a fact about the ACCOUNT; it says nothing about // whether the provider works. This line used to promise "insights work for // you right now" on the strength of the receipt alone, so somebody who had // just consented was told a dead provider was serving them — the same // unmeasured claim as the offer's, made at the other end of the panel. The // positive sentence is now earned by health as well, and a consented // account on a broken provider reads the same honest note as anybody else. const sharedKeyNote = operatorProvides && consentOnFile && sharedHealth === "healthy"; // Only a health verdict somebody EARNED paints the "not answering" card — // `unhealthy`, which means the provider was tried and the last thing that // happened was a failure. `unknown` is not that verdict: it is an empty // ledger, which is the state of every instance until its first AI call and // therefore the most common way through this screen. It falls to the // neutral panel, as does every other non-health reason the offer is // withheld (the assistant surfaces switched off, a managed profile whose // provider is the operator's by definition). The neutral panel claims // nothing either way, which is the only honest thing to say about a state // nobody measured. const sharedUnavailable = operatorProvides && !sharedKeyNote && !offerShared && sharedHealth === "unhealthy"; 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. */} {showAiPanel ? (

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

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

{sampleOpen ? ( ) : ( )} {sharedKeyNote ? (

{justGranted ? t("onboarding.ai.offer.granted") : t("onboarding.ai.sharedKeyNote")}

) : null} {offerShared ? (

{t("onboarding.ai.offer.title")}

{t("onboarding.ai.offer.body")}

) : null} {sharedUnavailable ? (

{t("onboarding.ai.unavailable.title")}

{t("onboarding.ai.unavailable.body")}

) : 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")}
) : null}
{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")}
); }