import { RefreshCwIcon } from "lucide-react"; import { useEffect, useState } from "react"; import { authNeededPresentation } from "../auth-needed-presentation"; import { cn } from "../lib/cn"; import { useOpenGeniText } from "../messages/runtime"; import { useEntranceAnimation } from "../timeline/entrance"; import type { AuthNeededItem } from "../timeline/types"; import { DecisionActions, decisionButtonClass, decisionCardClass, decisionCardPad, } from "./decision-card"; /** * The inline connection-recovery card: missing or lapsed access surfaces as a * calm, tappable affordance instead of a raw provider-domain error. The `reason` * only shapes human copy; no domain or enum code is shown as a label. * `onReconnect` (from the app, which owns the SDK client) starts the flow; * without it, a pre-minted authorization link is offered, or the card stays * informative. Recovery never claims to resume/replay the failed tool call. */ export function AuthNeededRow({ item, onReconnect, resolveProviderLogo, setupUnavailable = false, }: { item: AuthNeededItem; onReconnect?: ((item: AuthNeededItem) => void | Promise) | undefined; resolveProviderLogo?: ((providerDomain: string) => string | null | undefined) | undefined; /** This person cannot set the capability up from here; say so instead of offering a review. */ setupUnavailable?: boolean | undefined; }) { const t = useOpenGeniText(); const enter = useEntranceAnimation(); const [busy, setBusy] = useState(false); const [failed, setFailed] = useState(false); const recommendation = item.capability ?? null; const presentation = authNeededPresentation(t, item, { setupUnavailable }); const { provider, actionLabel, title, reasonLine, unavailable } = presentation; const hostAuthorizationUrl = item.authoritySource === "host" ? item.authorizationUrl : null; const start = async () => { if (!onReconnect || busy) { return; } setBusy(true); setFailed(false); try { // On success the app redirects to consent (or routes to credential entry), // so this row unmounts; a resolve without navigation just relaxes the button. // The callback starts authorization only. It never resumes this tool call. await onReconnect(item); setBusy(false); } catch { setFailed(true); setBusy(false); } }; if (recommendation?.id === "api:connected-machine") { return (

{t("thisComputer.card.name")}

{t("thisComputer.card.hostSetupUnavailable")}

); } const actionIcon = ( ); const action = hostAuthorizationUrl ? ( {actionIcon} {actionLabel} ) : item.authoritySource !== "host" && !unavailable && onReconnect ? ( ) : item.authoritySource !== "host" && !unavailable && item.authorizationUrl ? ( {actionIcon} {actionLabel} ) : null; return (

{title}

{presentation.providerLine ? (

{presentation.providerLine}

) : null}

{reasonLine}

{presentation.variablesLine ? (

{presentation.variablesLine}

) : null} {action ? ( {action} ) : null}
{presentation.footnote ? (

{presentation.footnote}

) : null} {failed ? (

{presentation.startFailed}

) : null}
); } /** * The provider's logo in a rounded tile, from a URL the HOST serves itself * (resolved via `resolveProviderLogo` → the app's catalog assets). A missing or * failed image falls back to a calm letter monogram — same as the rest of the * app — so the card never shows a broken-image glyph and never reaches off-origin * for a favicon (CSP + privacy). */ function AuthProviderLogo({ src, label }: { src: string | null; label: string }) { const [failed, setFailed] = useState(false); // A resolver that only returns the URL after a lazy catalog fetch means `src` // can arrive on a later render; reset the error latch so it gets its attempt. useEffect(() => setFailed(false), [src]); const showImage = src && !failed; return ( {showImage ? ( setFailed(true)} /> ) : ( {monogram(label)} )} ); } /** First one or two initials for the monogram fallback — mirrors the app's `capabilityMonogram` so the reconnect tile reads like every other logo tile. */ function monogram(label: string): string { const words = label.trim().split(/\s+/).filter(Boolean); if (!words.length) { return "?"; } if (words.length === 1) { return words[0]!.slice(0, 2).toUpperCase(); } return (words[0]![0]! + words[1]![0]!).toUpperCase(); }