"use client"; /* ──────────────────────────────────────────────────────────────── * Use the server's shared AI access (per-user opt-in). * * Shown ONLY when the operator has connected a shared central Codex * (ChatGPT-subscription) account. OFF by default. Turning it ON reveals a * once-shown honesty confirm before the write, because the trade — a shared, * signed-in AI account bound by the operator's rate limits, on which the * provider may use content to improve its models with no data-processing * agreement — must be acknowledged, not buried. Turning it OFF is immediate. * * Opting in does not egress anything on its own: the shared connection is * server-managed external egress, so the existing AI-consent gate still applies * before any health data leaves for it. * ──────────────────────────────────────────────────────────────── */ import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { Share2, ShieldAlert } from "lucide-react"; import { Button } from "@/components/ui/button"; import { SettingsCard } from "@/components/settings/settings-card"; import { SettingsCardActions } from "@/components/settings/_card-actions"; import { SettingsCardHeader } from "@/components/settings/_card-header"; import { Switch } from "@/components/ui/switch"; import { apiPatch } from "@/lib/api/api-fetch"; import { useTranslations } from "@/lib/i18n/context"; import { aiInputDependentKeys, invalidateKeys } from "@/lib/query-keys"; import type { InsightsSettings } from "./shared"; interface UseCentralCodexPref { useCentralCodex: boolean; } export function CentralCodexSwitch({ settings, }: { settings: InsightsSettings | null | undefined; }) { const { t } = useTranslations(); const queryClient = useQueryClient(); // Off→on reveals the honesty confirm; the write only happens on confirm. const [pendingEnable, setPendingEnable] = useState(false); const enabled = settings?.useCentralCodex ?? false; const save = useMutation({ mutationFn: (next: boolean) => apiPatch("/api/auth/me/use-central-codex", { useCentralCodex: next, }), onSuccess: () => { // The opt-in changes which providers can serve this user — refresh the // settings summary (which carries the flag), the account's AI // capability answer, and every insight read. void invalidateKeys(queryClient, aiInputDependentKeys); setPendingEnable(false); }, }); // Only meaningful once the operator has connected the shared account. if (!settings?.centralCodexAvailable) return null; function onSwitch(next: boolean) { if (next) { if (!enabled) setPendingEnable(true); return; } setPendingEnable(false); if (enabled) save.mutate(false); } const busy = save.isPending; const checked = enabled || pendingEnable; return ( } /> {pendingEnable ? (

{t("settings.ai.centralCodex.honesty")}

) : null} {save.isError ? (

{t("settings.ai.errorGeneric")}

) : null}
); }