import { useState, type FormEvent } from "react"; import type { ConnectAttempt, ConnectController } from "@opengeni/connect"; import { copyTextToClipboard } from "./lib/clipboard"; import { useOpenGeniText } from "./messages/runtime"; import { useConnectSetupFlow } from "./connect-setup-model"; export { adminApprovalLink, curatedAutoAdvance, isCuratedDefinitionAttempt, isOwnerApprovalPending, } from "./connect-setup-model"; function CopyLinkButton({ url }: { url: string }) { const t = useOpenGeniText(); const [copied, setCopied] = useState(false); return ( ); } export type ConnectSetupProps = { controller: ConnectController; /** Called synchronously in the click handler so a host can open a popup. */ onAuthorize: (attempt: ConnectAttempt) => void | Promise; /** Called synchronously on Continue; hosts can reserve a popup before advancing. */ onSelectAccount?: (accountId: string) => void | Promise; /** Host pagination must retain selections across pages and submit the final set. */ onBrowseResources?: (attempt: ConnectAttempt) => void | Promise; className?: string; /** Provider-specific label supplied by the embedding host. */ authorizeLabel?: string | undefined; /** * In a chat card the person signs in and moves on: offer "Check status" only * when the provider asks them to finish elsewhere and come back. */ inChat?: boolean; /** Begin a fresh attempt after declined or administrator-gated authorization. */ onRetry?: () => void | Promise; }; /** Controller-backed setup surface, styled by opt-in connect.css. No provider secrets enter React state. Host owns * chooser, controller lifetime, navigation and recovery after full redirect. */ export function ConnectSetup(props: ConnectSetupProps) { // Attempt IDs/revisions are not a substitute for actor scope. A host can // recover the same attempt through a replacement controller; never preserve // a previous actor's unsent credential DOM or local errors across that swap. const [scope, setScope] = useState(props.controller); const [generation, setGeneration] = useState(0); if (scope !== props.controller) { setScope(props.controller); setGeneration(generation + 1); } return ; } function ScopedSetup({ controller, onAuthorize, onBrowseResources, onSelectAccount, className, authorizeLabel: authorizeLabelProp, inChat = false, onRetry, }: ConnectSetupProps) { const t = useOpenGeniText(); const authorizeLabel = authorizeLabelProp === undefined ? t("connectSetup.authorizeConnection") : authorizeLabelProp; const flow = useConnectSetupFlow(controller, { onAuthorize, onSelectAccount, inChat }); const { view, attempt, navigating, invoke } = flow; if (!attempt) return (

{flow.status}

); const action = attempt.nextAction; const { verificationUrl, awaitingOwner, adminApproval, offersGitHubRequest } = flow; const submit = (event: FormEvent) => { event.preventDefault(); const form = event.currentTarget; const data = new FormData(form); if (flow.busy) return; if (action.type === "credentials") { const values = Object.fromEntries( action.fields.map((field, i) => [field.name, String(data.get(`field-${i}`) ?? "")]), ); // Clear the DOM before awaiting remote work; controller never retains values. form.reset(); flow.submitCredentials(values); } else if (action.type === "select_account") { flow.selectAccount(String(data.get("account") ?? "")); } else if (action.type === "select_resources") { flow.selectResources(data.getAll("resource").map(String)); } else if (action.type === "preview") { flow.install(data.getAll("operation").map(String)); } }; return (
{/* A chat card already names the service and whose account it is; a sign-in step there needs only its button, and any other step only its status. */} {adminApproval ? null : inChat ? ( action.type === "authorize" ? null : (

{flow.status}

) ) : (

{flow.scopeLabel}

{flow.status}

{flow.scopeDescription}

)} {attempt.account &&

{t("connectSetup.accountLabel", { label: attempt.account.label })}

} {awaitingOwner && (

{t("connectSetup.yourRequestWasSentGithub")}

  1. {t("connectSetup.anOrganizationOwnerApprovesThe")}
  2. {t("connectSetup.anOwnerThenConnectsThe")}

{t("connectSetup.nothingIsConnectedUntilThen")}

)} {adminApproval && (

{adminApproval.required ? t("connectSetup.microsoftAdminApprovalRequired") : t("connectSetup.microsoftAdminApprovalMaybe")}

{inChat && ( {t("connectSetup.adminApprovalLink")} )}
{onRetry && ( )}
)} {flow.errorText &&

{flow.errorText}

} {flow.notice &&

{flow.notice}

} {flow.interactive && !adminApproval && (
{flow.formStep ? ( {t("connectSetup.connectionDetails")} ) : null} {attempt.state === "connected_but_incomplete" && action.type === "none" && !awaitingOwner && ( )} {action.type === "credentials" && action.fields.map((field, i) => ( ))} {action.type === "select_account" && ( )} {offersGitHubRequest && (

{t("connectSetup.donTSeeYourOrganization")}

)} {action.type === "select_resources" && action.resources.map((resource) => ( ))} {action.type === "select_resources" && action.cursor && ( <>

{t("connectSetup.moreResourcesAreAvailableReview")}

{onBrowseResources && ( )} )} {action.type === "preview" && ( <>

{t("connectSetup.chooseTheToolsAgentsCan")}

{action.operations.map((operation) => ( ))} )} {flow.formStep && ( )} {action.type === "authorize" && ( )} {action.type === "wait" && ( <> {/* In reading order: the code to enter, the page to enter it on, then what to do there. */} {action.userCode && (

{t("connectSetup.verificationCode")} {action.userCode}

)} {verificationUrl && ( {t("connectSetup.openProviderVerificationPageNew")} {t("connectSetup.opensInNewTab")} )}

{t("connectSetup.completeProviderAuthorizationThenCheck")}

)}
{!inChat || action.type === "wait" ? ( ) : null}
)}
); }