"use client"; import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { CheckCircle2, ClipboardCheck, FileUp, Plug, Watch, Wifi, type LucideIcon, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { pickStatus, useIntegrationStatuses, } from "@/components/settings/integrations/shared"; import { useAuth } from "@/hooks/use-auth"; import { useTranslations } from "@/lib/i18n/context"; import { cn } from "@/lib/utils"; import { apiPost } from "@/lib/api/api-fetch"; import { localizedApiError } from "@/lib/api/localized-error"; import { queryKeys } from "@/lib/query-keys"; /** * Onboarding step 2 (source). * * Presents the data sources HealthLog ships today. The headline cards * cover the two paths most people start on: * 1. Manual entry (enabled, recommended — works on any device) * 2. Withings — opens OAuth `/api/withings/connect` in a new tab once * the account has its own Withings developer app configured; the * callback flips the connection server-side. Before that, the card * is honest about the prerequisite (v1.29.x, UX audit H1/H2): the * CTA reads "Set up in Settings" and points at the credentials form * instead of launching an OAuth handshake that can only 400. * * Below them a calm "more sources" row lists the remaining shipped * integrations (Apple Health, WHOOP, Oura, Polar, Nightscout, Fitbit), * each linking to Settings → Integrations where it is actually * connected. We don't try to run six OAuth flows inside the wizard; * the row sets the expectation and hands off. A discreet import link * points at the Apple Health export.zip path — the cold-start escape * hatch for users with existing history. * * Garmin is omitted: not shipped, so a card would over-claim. * * Selection is non-binding — Manual is the implicit default and the * Next CTA always advances. Nothing here is auto-configured; the step * only signposts where each source is connected. */ interface SourceCard { slug: "manual" | "withings"; Icon: LucideIcon; titleKey: string; bodyKey: string; /** * One of: * - "enabled-select": tap selects the card (Manual) * - "enabled-oauth": tap opens OAuth in a new tab (Withings) */ state: "enabled-select" | "enabled-oauth"; /** External href for `enabled-oauth`. */ href?: string; /** Optional recommended badge. */ recommendedKey?: string; } const SOURCE_CARDS: ReadonlyArray = [ { slug: "manual", Icon: ClipboardCheck, titleKey: "onboarding.source.manual.title", bodyKey: "onboarding.source.manual.body", state: "enabled-select", recommendedKey: "onboarding.source.recommended", }, { slug: "withings", Icon: Watch, titleKey: "onboarding.source.withings.title", bodyKey: "onboarding.source.withings.body", state: "enabled-oauth", href: "/api/withings/connect", }, ]; /** * The remaining shipped integrations. Listed compactly so the step * stays calm — the full connect flow for each lives in Settings. */ const MORE_SOURCES: ReadonlyArray<{ slug: string; labelKey: string }> = [ { slug: "apple-health", labelKey: "onboarding.source.more.appleHealth" }, { slug: "whoop", labelKey: "onboarding.source.more.whoop" }, { slug: "oura", labelKey: "onboarding.source.more.oura" }, { slug: "polar", labelKey: "onboarding.source.more.polar" }, { slug: "nightscout", labelKey: "onboarding.source.more.nightscout" }, { slug: "fitbit", labelKey: "onboarding.source.more.fitbit" }, ]; export function SourceCardGrid() { const { t } = useTranslations(); const router = useRouter(); const queryClient = useQueryClient(); const { isAuthenticated } = useAuth(); const [selected, setSelected] = useState("manual"); const [advancing, setAdvancing] = useState(false); // v1.29.x — the wizard's "Connect Withings" card must not launch an OAuth // handshake that can only fail: Withings credentials are per-user BYO with // no env fallback, and a brand-new account never has them yet. Read the // same consolidated envelope Settings → Integrations uses so the card // knows whether the handshake can actually succeed. const { data: integrationStatus } = useIntegrationStatuses(isAuthenticated); const withingsConfigured = pickStatus(integrationStatus, "withings")?.configured ?? false; // 2026-07-17 UX-onboarding audit M6 — the connect CTA opens the OAuth // handshake in a new tab; the callback lands THAT tab on // Settings → Integrations while this wizard tab never learned the // connection succeeded. `useIntegrationStatuses` already refetches on // window focus, so returning to this tab after the OAuth round trip // picks up the fresh `connected` flag for free — the only missing piece // was rendering it as a badge instead of leaving the button looking // untouched. const withingsConnected = pickStatus(integrationStatus, "withings")?.connected ?? false; async function advance() { if (advancing) return; setAdvancing(true); try { await apiPost("/api/onboarding/step", { step: 3 }); await queryClient.invalidateQueries({ queryKey: queryKeys.auth() }); router.push("/onboarding/3"); } catch (err) { toast.error(localizedApiError(err, t, "onboarding.errorGeneric")); setAdvancing(false); } } return (
{/* Onboarding hero H1: intentionally semibold, not the app-wide bold PageHeader H1 (UI-STANDARDS §5 hero exception). Do not sweep to font-bold. */}

{t("onboarding.source.title")}

{t("onboarding.source.body")}

{SOURCE_CARDS.map((card) => ( setSelected(card.slug)} titleLabel={t(card.titleKey)} bodyLabel={t(card.bodyKey)} recommendedLabel={ card.recommendedKey ? t(card.recommendedKey) : null } withingsCtaLabel={t("onboarding.source.withings.cta")} withingsConfigured={withingsConfigured} withingsConnected={withingsConnected} withingsConnectedLabel={t("settings.integrationPill.connected")} withingsSetupCtaLabel={t("onboarding.source.withings.setupCta")} withingsSetupHint={t("onboarding.source.withings.setupHint")} /> ))}

{t("onboarding.source.more.title")}

{t("onboarding.source.more.body")}

    {MORE_SOURCES.map((item) => (
  • ))}
{/* #778 — Apple Health looked like one more web connector here, but there is nothing to connect on the web: the sync is set up in the iOS app after onboarding. Say so, so nobody hunts Settings for an OAuth flow that does not exist. The chip stays tappable (it lands on the Integrations page, whose Apple Health card walks through the iPhone-app setup) and Next / Skip advance as always. */}

{t("onboarding.source.more.appleHealthHint")}

{t("onboarding.source.import")}
); } interface SourceCardItemProps { card: SourceCard; selected: boolean; onSelect: () => void; titleLabel: string; bodyLabel: string; recommendedLabel: string | null; withingsCtaLabel: string; /** Whether the account already has its own Withings developer-app * credentials saved — i.e. whether `/api/withings/connect` can * actually succeed right now. */ withingsConfigured: boolean; /** Whether Withings is already connected (OAuth completed in the tab the * connect CTA opened). Replaces the connect button with a badge so * returning to this wizard tab shows the outcome instead of an * unchanged card. */ withingsConnected: boolean; /** Localised "Connected" label for the badge above. */ withingsConnectedLabel: string; /** CTA label shown instead of "Connect Withings" when unconfigured. */ withingsSetupCtaLabel: string; /** One-sentence explanation of the BYO credential prerequisite. */ withingsSetupHint: string; } function SourceCardItem({ card, selected, onSelect, titleLabel, bodyLabel, recommendedLabel, withingsCtaLabel, withingsConfigured, withingsConnected, withingsConnectedLabel, withingsSetupCtaLabel, withingsSetupHint, }: SourceCardItemProps) { const Icon = card.Icon; const baseClasses = cn( "relative flex flex-col gap-3 rounded-lg border p-4 text-left transition-colors duration-150 ease-out motion-reduce:transition-none", "bg-card border-border", ); const headerRow = (

{titleLabel}

{recommendedLabel ? ( {recommendedLabel} ) : null}
); const body = (

{bodyLabel}

); if (card.state === "enabled-oauth") { return (
{headerRow} {body} {withingsConnected ? (
) : withingsConfigured ? (
) : ( // No per-user Withings credentials yet — an OAuth handshake here // can only 400 (Withings has no env fallback). Point at the // credentials form instead of launching a doomed connect flow.

{withingsSetupHint}

)}
); } // enabled-select (Manual) return ( ); }