"use client"; import { useEffect, useState } from "react"; import type { ChatGPTStatus } from "@/src/core"; export const CHATGPT_NOTICE = "LearnHarness stores your ChatGPT account details and encrypted credentials locally on this device. When this connection is used, relevant conversation context, learning material, and tool results are sent directly to OpenAI. ChatGPT plan limits apply; any additional credits are authorized and managed in ChatGPT, not LearnHarness."; const empty: ChatGPTStatus = { available: false, pending: false, activeId: null, accounts: [], error: null }; /** Status/control surface only; OAuth URLs, codes, and tokens never reach React. */ export function ChatGPTConnection() { const [status, setStatus] = useState(empty); const [selected, setSelected] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); useEffect(() => { const bridge = window.lh?.chatgpt; if (!bridge) return; let mounted = true; const refresh = async () => { try { const next = await bridge.status(); if (!mounted) return; setStatus(next); setSelected((old) => old || next.activeId || ""); } catch { if (mounted) setError("The desktop connection is unavailable. Retry when the app is ready."); } }; void refresh(); const off = bridge.onChange(() => void refresh()); return () => { mounted = false; off(); }; }, []); const account = status.accounts.find((a) => a.id === selected); async function control(action: () => Promise) { if (busy) return; setBusy(true); setError(null); try { const next = await action(); if (typeof next.available === "boolean") setStatus(next); setError(next.error ?? null); } catch { setError("The ChatGPT connection could not be changed. Try again."); } finally { setBusy(false); } } const bridge = typeof window !== "undefined" ? window.lh?.chatgpt : undefined; return (
ChatGPT {status.pending ? "Waiting for browser sign-in…" : status.accounts.some((a) => a.id === status.activeId && a.connected) ? "ChatGPT plan connection" : "Not connected"}

{CHATGPT_NOTICE}

OpenAI privacy Manage usage
{status.accounts.length > 0 && ( )}
{/* Official black/white format and unmodified OpenAI-provided mark. */} {status.pending && } {account?.connected && selected !== status.activeId && } {account && }
{!status.available &&

ChatGPT sign-in is available only in the desktop app with secure OS credential storage. API-key connections remain available here.

} {account && ( )} {(error || status.error || status.catalogError) &&

{error || status.error || status.catalogError}

}
); }