Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

64.5 KB
Raw Blame
// Agent-first setup (LIN-125): the first thing an instance admin sees while the install or their business is not set
// up. An AI agent (Claude Code, Claude, ChatGPT, Codex, Cursor, any MCP client) connects to the setup MCP with a
// setup key and does the setup with them; this page makes the key, says how to connect each agent, gives the prompt,
// and shows the agent's progress as it works. Setting up by hand stays one small link away.
// The page is part of Server admin, so it opens only after the admin's passkey check (App.tsx, SecondFactorPrompt):
// the passkey is no step here. Every step the admin does themselves is finished here, never sent into the app: the
// call forwarding and the test call (SetupRowActions.tsx). The agent ends its own access at the end; the page then says when.
// The server's steps are once per install; the workspace steps below them belong to the workspace picked in the
// dropdown that heads them (one per business or location, LIN-237). Picking one makes it the app's active workspace, so
// the forwarding and the test call always act on the workspace shown.
// Once everything is set up the page stays the way to change things: a new link for the agent, or the install
// walkthrough step by step (Server admin › Providers on Cloud, where the walkthrough is off).
// `scope="workspace"`: the same page for a workspace owner who is no server admin (a Linedesk Cloud customer). Their
// link is for the active workspace only (agentSetup.createWorkspaceKey, GET /v1/workspace/agent-setup): the page shows
// only that workspace's steps, no server part, no workspace dropdown and nothing of Server admin; setting up by hand
// leads into the step-by-step onboarding.
// `scope="partner"`: a white-label partner owner's page (#partner/agent, partner/PartnerAgentSetup.tsx): a link for the
// partner and its client workspaces only (agentSetup.createPartnerKey, GET /v1/partner/agent-setup); the partner's
// steps, then the client workspace picked in the dropdown. The owner's own rows: adding a client workspace (a seat on
// the partner bill, the partner area's New client dialog), a number, the call forwarding and the test call.
import {
  type AgentClient,
  type AgentSetupKeyCreated,
  type AgentSetupState,
  cloudClients,
  HOSTING_GUIDE_URL,
  installPlace,
  type MyPartner,
  SETUP_KEY_HOURS,
  type SetupChange,
  type SetupScope,
  type SetupStepGroup,
  type SetupWorkspaceSummary,
  startPlan,
  stepsByGroup,
  UNPAID_PARTNER_CLIENTS,
} from '@linedesk/actions'
import { T, useFormat, useT } from '@linedesk/i18n/react'
import { Fragment, useCallback, useEffect, useRef, useState } from 'react'
import { loadAgentSetup, loadInstallSetup, runAdmin, watchAdminSignIn } from '../admin/api.ts'
import { ADMIN_INSTALL_SETUP } from '../admin/Providers.tsx'
import { SecondFactorPrompt } from '../admin/SecondFactorPrompt.tsx'
import type { AdminSecondFactor, Me } from '../auth.ts'
import { brandName, installBrandName, useBrand } from '../brand.ts'
import { useQuery } from '../cachedQuery.ts'
import { microphoneAllowed } from '../call/microphone.ts'
import { i18n } from '../i18n/index.tsx'
import { isNativeApp } from '../native/mobile/platform.ts'
import { SETUP_LINK_KEY, signInAgain as signOutAndSignIn } from '../localData.ts'
import { NewClientDialog } from '../partner/PartnerArea.tsx'
import { loadPartner, PARTNER_QUERY } from '../partner/partner-api.ts'
import { callAction } from '../settings/settings-api.ts'
import { switchToWorkspace } from '../shell/invitations.ts'
import { NewWorkspaceDialog } from '../shell/NewWorkspace.tsx'
import { tabWorkspace } from '../tabWorkspace.ts'
import { dateTimeLabel, untilLabel } from '../time.ts'
import { AppLink } from '../ui/AppLink.tsx'
import { Select } from '../ui/controls.tsx'
import { FoldOut } from '../ui/FoldOut.tsx'
import { HelpTip } from '../ui/HelpTip.tsx'
import { Button, cx, Icon, Note, Spinner } from '../ui/kit.tsx'
import { LoadProblem, Skeleton, SkeletonPart, useLoading } from '../ui/LoadStates.tsx'
import { MarkGlyph, StatusMark } from '../ui/StatusMark.tsx'
import { AgentSteps } from './AgentForm.tsx'
import { AgentPicker, agentLabel, saveClient, savedClient } from './AgentPicker.tsx'
import { installPartsMissing } from './agent-setup-passkey.ts'
import { rememberDraftOpenedFrom } from './flow.ts'
import { openInstallSetup } from './InstallSetup.tsx'
import { clearResume, peekResume, type ResumeAction, saveResume } from './resume-after-switch.ts'
import { SetupFoot } from './SetupFoot.tsx'
import {
  SetupActivatePartner,
  SetupForwarding,
  SetupStartTrial,
  SetupTestCall,
  type SetupWorkspace,
} from './SetupRowActions.tsx'
import { SetupPage, SetupTopBar } from './SetupTopBar.tsx'
import { PARTNER_AGENT_HASH, rememberManual } from './setup-choice.ts'
import { loadPartnerAgentSetup, loadWorkspaceAgentSetup } from './workspace-agent-setup.ts'
import './agent-setup.css'
import { readStoredJson, writeStored, writeStoredJson } from '../safeStorage.ts'
import { StepHead } from './Frame.tsx'
import { useSetupKeys } from './setup-keys.ts'

/** The address of the setup page (#setup-agent): opened from Server admin › Overview, or after choosing the manual way. */
export { AGENT_SETUP_HASH, isAgentSetupHash } from '@linedesk/actions'
export { choseManualSetup, rememberManual } from './setup-choice.ts'

const POLL_MS = 3000

/**
 * The setup link made in this browser, kept until it ends: it expires, a new one is made, the admin ends agent access
 * or signs out (localData.ts). So a reload, another tab or picking another agent still shows the link and how to
 * start with it. The page shows it only while the server says this admin's working link is this one (AgentSetup).
 * In memory too, for private windows without storage. One slot per link: the install link, and each workspace
 * owner's link under its workspace's id (setupLinkSlot), so one workspace's page never holds another's link.
 */
const shownKeys = new Map<string, AgentSetupKeyCreated>()
const unexpired = (link: AgentSetupKeyCreated | null) => (link && Date.parse(link.expiresAt) > Date.now() ? link : null)
/** Where a link is kept: the install link at SETUP_LINK_KEY, a workspace owner's link under that workspace. */
export const setupLinkSlot = (scope: SetupScope, workspaceId: string | null, partnerId: string | null = null) =>
  scope === 'workspace'
    ? `${SETUP_LINK_KEY}.workspace.${workspaceId ?? 'none'}`
    : scope === 'partner'
      ? `${SETUP_LINK_KEY}.partner.${partnerId ?? 'none'}`
      : SETUP_LINK_KEY
function stillShown(slot: string): AgentSetupKeyCreated | null {
  const inMemory = unexpired(shownKeys.get(slot) ?? null)
  if (inMemory) return inMemory
  shownKeys.delete(slot)
  const saved = unexpired(readStoredJson<AgentSetupKeyCreated>('local', slot))
  if (!saved) writeStored('local', slot, null)
  return saved
}
function keepShown(slot: string, link: AgentSetupKeyCreated | null) {
  if (link) shownKeys.set(slot, link)
  else shownKeys.delete(slot)
  // Private windows: the link lasts while this page is open (in memory).
  writeStoredJson('local', slot, link)
}

type Props = {
  /**
   * Set up by hand. `installFirst`: the install is not set up yet, so the setup checklist (Server admin › Overview) comes
   * first; otherwise the business onboarding (or "Name your business" before there is a workspace).
   */
  onManual: (installFirst: boolean) => void
  /** The agent is done (or the admin wants to look): open the app as it is now. */
  onDone: () => void
  /** Called once the agent has created the admin's business (the page around this one needs a workspace to go on). */
  onWorkspaceReady?: () => void
  /**
   * The app's active workspace in this tab, once there is one: the page shows its steps (when the admin is owner or
   * admin there), and the test call and the forwarding check run in it. Picking another workspace switches it.
   */
  active?: SetupWorkspace
  /**
   * `workspace`: a workspace owner's page for the active workspace (`active` is required then): their own link and
   * that workspace's steps only, the title naming the software (brandName), never the business. Default `install`: an
   * instance admin's.
   */
  scope?: SetupScope
  /**
   * With `scope="workspace"`: change some settings of a workspace that is set up (Workspace or Assistant settings ›
   * "Change with an AI agent"): the title and the prompt name the workspace (and the assistant), and the way back is
   * "Back to settings" (`onManual`) in place of setting up by hand.
   */
  change?: SetupChange
  /**
   * The owner's onboarding: "Finish later" in the top bar (and Shift+Esc), as on the admin's install walkthrough and
   * the business onboarding: what is left waits in the go-live checklist.
   */
  onLater?: () => void
  /**
   * With `scope="workspace"` while the workspace waits for its trial: the owner starts it on its row (the onboarding's
   * activation step, in a dialog). `onTermsChanged`: the terms changed meanwhile, /v1/me is read again.
   */
  trial?: StartTrialProps
  /** With `scope="partner"` (required then): the partner this page sets up; its owner is the one here. */
  partner?: MyPartner
  /** With `scope="partner"` in no workspace yet: the first client workspace was made, the session loads again. */
  onFirstWorkspace?: () => void
  /**
   * With `scope="workspace"`: a partner's client workspace its owner opens; the partner may have set up the business
   * and the assistant already, and the page then says so.
   */
  prepared?: boolean
  /**
   * The business onboarding is still open: opening the app would only open its steps by hand, so the page offers no
   * "Open {brand} now" while the agent works ("Finish later" is the way out); "Open {brand}" once all is set up.
   */
  onboardingOpen?: boolean
}

type StartTrialProps = { me: Me; onTermsChanged: () => void }

export function AgentSetup({
  onManual,
  onDone,
  onWorkspaceReady,
  active,
  scope = 'install',
  change,
  onLater,
  trial,
  partner,
  onFirstWorkspace,
  prepared = false,
  onboardingOpen = false,
}: Props) {
  const t = useT()
  const brand = useBrand()
  const owner = scope === 'workspace'
  // A partner's owner: no Server admin (no passkey check, no server part, no hosting note), as a workspace owner.
  const partnerId = scope === 'partner' ? (partner?.id ?? null) : null
  const personal = owner || partnerId !== null
  // A partner's own setup shows the install's brand, never its white-label one (brand.ts), even with a client open.
  const product = scope === 'partner' ? installBrandName() : brandName()
  const [state, setState] = useState<AgentSetupState | null>(null)
  const activeId = active?.id ?? null
  const [loadError, setLoadError] = useState<string | null>(null)
  const slot = setupLinkSlot(scope, owner ? activeId : null, partnerId)
  const [created, setShown] = useState<AgentSetupKeyCreated | null>(() => stillShown(slot))
  // When the link on the page was set (0: kept from before), and when the shown state was asked for: only a state
  // asked for after the link was set can say it ended.
  const linkSetAt = useRef(0)
  const stateAskedAt = useRef(0)
  const setCreated = useCallback(
    (key: AgentSetupKeyCreated | null) => {
      keepShown(slot, key)
      linkSetAt.current = Date.now()
      setShown(key)
    },
    [slot],
  )
  // Another workspace in this tab: its own link (or none), never the one before.
  const shownSlot = useRef(slot)
  useEffect(() => {
    if (shownSlot.current === slot) return
    shownSlot.current = slot
    linkSetAt.current = 0
    setShown(stillShown(slot))
  }, [slot])

  const [client, setClient] = useState<AgentClient>(savedClient)
  const [busy, setBusy] = useState<'create' | 'end' | null>(null)
  const [error, setError] = useState<string | null>(null)
  // A workspace owner's link needs a sign-in from the last 10 minutes (the server's SIGN_IN_AGAIN).
  const [signInFirst, setSignInFirst] = useState(false)
  // The passkey check ran out (12 hours): the page turns into the passkey prompt, never an error line.
  const [signInAgain, setSignInAgain] = useState<AdminSecondFactor | null>(null)
  // Server admin's passkey check: not the owner's page, which is no part of Server admin.
  useEffect(() => (personal ? undefined : watchAdminSignIn(setSignInAgain)), [personal])
  const load = useCallback(async () => {
    try {
      const asked = Date.now()
      const next = partnerId
        ? await loadPartnerAgentSetup(partnerId, activeId)
        : owner
          ? await loadWorkspaceAgentSetup()
          : await loadAgentSetup(activeId)
      if (asked < stateAskedAt.current) return // an older answer that arrived late
      stateAskedAt.current = asked
      setState(next)
      setLoadError(null)
    } catch (e) {
      setLoadError(e instanceof Error ? e.message : t('onboarding.agentSetup.error.read'))
    }
  }, [activeId, owner, partnerId, t])
  // The agent works in another window: show its progress as it goes (only while this tab is visible).
  useEffect(() => {
    void load()
    const id = setInterval(() => {
      if (document.visibilityState === 'visible') void load()
    }, POLL_MS)
    return () => clearInterval(id)
  }, [load])

  // There is a workspace once the progress has one (the business row stays open until its profile is saved too).
  const workspaceReady = Boolean(state?.progress.workspace)
  const told = useRef(false)
  useEffect(() => {
    if (!workspaceReady || told.current) return
    told.current = true
    onWorkspaceReady?.()
  }, [workspaceReady, onWorkspaceReady])

  const pick = (next: AgentClient) => {
    setClient(next)
    saveClient(next)
  }
  const createKey = async () => {
    setBusy('create')
    setError(null)
    setSignInFirst(false)
    try {
      setCreated(
        partnerId
          ? await callAction<AgentSetupKeyCreated>('agentSetup.createPartnerKey', { partnerId, client })
          : owner
            ? await callAction<AgentSetupKeyCreated>('agentSetup.createWorkspaceKey', { client })
            : ((await runAdmin('agentSetup.createKey', { client })) as AgentSetupKeyCreated),
      )
      await load()
    } catch (e) {
      if ((e as { code?: unknown }).code === 'SIGN_IN_AGAIN') return setSignInFirst(true)
      setError(e instanceof Error ? e.message : t('onboarding.agentSetup.error.create'))
    } finally {
      setBusy(null)
    }
  }
  const endAccess = async () => {
    setBusy('end')
    setError(null)
    try {
      if (partnerId) await callAction('agentSetup.revokePartner', { partnerId })
      else if (owner) await callAction('agentSetup.revokeWorkspace')
      else await runAdmin('agentSetup.revoke', {})
      setCreated(null)
      await load()
    } catch (e) {
      setError(e instanceof Error ? e.message : t('onboarding.agentSetup.error.end'))
    } finally {
      setBusy(null)
    }
  }
  const manual = () => {
    // The owner's choice is kept by the page around this one (setup-choice.ts, per workspace or partner).
    if (personal) return onManual(false)
    rememberManual(true)
    // The install walkthrough only for parts it has not handled yet (skipped, set aside or finished: agent-setup-passkey.ts).
    onManual(state ? installPartsMissing(state.progress.steps, install?.progress) : false)
  }
  // Everything is set up: the page offers to change it, with the agent or the install walkthrough (opens at its
  // first part, nothing saved is reset; off on Cloud, where Server admin › Providers is the way by hand).
  const allDone = state?.progress.allDone ?? false
  // Finished on this visit: the page saw the setup open and then all done (the agent just finished). Then opening the
  // app is the main action. A page opened already done (Edit with AI, changing settings) is there to change something:
  // creating a link is its main action and opening the app the way out.
  const sawOpen = useRef(false)
  const [finishedHere, setFinishedHere] = useState(false)
  useEffect(() => {
    if (!state) return
    if (!state.progress.allDone) sawOpen.current = true
    else if (sawOpen.current) setFinishedHere(true)
  }, [state])
  // Server admin's page once everything is set up (Edit with AI): the agent changes providers or connects more, and
  // the optional providers show open.
  const editInstall = scope === 'install' && !change && allDone
  const install = useQuery(personal ? null : ADMIN_INSTALL_SETUP, () => loadInstallSetup()).data
  const stepByStep = () => {
    if (install?.enabled === false) window.location.hash = '#admin/providers'
    else openInstallSetup({ from: 'start' })
  }

  const connected = Boolean(state?.key?.lastUsedAt)
  // Once the agent is connected the page is about its progress: the title and the agent card fold to one line each
  // (the link and how to start stay one click away, `linkOpen`), the progress card is the page. Before that the
  // progress is not shown at all: the first step is only connecting the agent.
  const focus = connected
  const [linkOpen, setLinkOpen] = useState(false)
  // A new connection folds the card again, whatever it was before.
  useEffect(() => {
    if (connected) setLinkOpen(false)
  }, [connected])
  const stepDone = (id: string, group?: SetupStepGroup) =>
    state?.progress.steps.some(s => s.id === id && (!group || s.group === group) && s.state === 'done') ?? false
  // A partner after its Checkout: White-label Cloud runs; what is left for the owner is numbers and invitations.
  const partnerPaid = partnerId !== null && stepDone('plan', 'partner')
  // A client owner whose partner set up the business and the assistant: the page says who did and what is left.
  const preparedByPartner = owner && prepared && stepDone('business') && stepDone('assistant')
  // One setup link at a time: while one is active the page offers only to end it, otherwise only to create one.
  // The link ended elsewhere (expired, replaced in another browser, agent access ended): it goes from the page too.
  useEffect(() => {
    if (!state || !created || stateAskedAt.current < linkSetAt.current) return
    if (state.key?.prefix !== created.prefix || !unexpired(created)) setCreated(null)
  }, [state, created, setCreated])
  const hasLink = state ? Boolean(state.key) : Boolean(created)
  // Every agent flow goes the same way, setting up as much as changing settings (Server admin, a workspace, a partner):
  // 1. create a link, 2. connect the agent, 3. the checklist. Before the agent connects there is no checklist, also not
  // once everything is set up or when settings are changed; the link step is the only thing to do.
  // The title alone (no kicker, no lede, a size smaller) once the way is chosen: a link is made, or the agent works.
  // Changing settings keeps its title and lede (they name the workspace); only the kicker goes, as here.
  const compactHead = !change && (focus || (hasLink && !allDone))
  const kicker = !allDone && !compactHead && !(change && (hasLink || focus))
  const showProgress = Boolean(state && connected)
  const cloudBlocked = state !== null && !state.reachableFromCloud && cloudClients.includes(client)
  // Until the first read of the setup state, everything on the page that depends on it is a skeleton of its shape
  // (docs/design-principles.md rule 15): the title, the link step's button, the checklist and the way by hand. A
  // failed or endless first read turns the checklist into the load problem; the rest shows as for a new install.
  const view = useLoading(state === null && !loadError)
  const skeleton = state === null && (view.phase === 'reserve' || view.phase === 'skeleton')
  const reserved = view.phase === 'reserve'
  // The line under the top bar: how much of the checklist is done.
  const checklist = state?.progress.steps ?? []
  const checklistDone = checklist.length
    ? Math.round((checklist.filter(s => s.state === 'done').length / checklist.length) * 100)
    : 0
  // Open the app once there is a workspace to open (the agent connected, or all done); not while the workspace waits
  // for its trial (the app would only show the card step) and not on a partner's page (its way is the partner area).
  // Nor while the business onboarding is open and the agent still works: the app would only open its steps by hand.
  const openApp =
    (allDone || (connected && !onboardingOpen)) && Boolean(state?.progress.workspace) && !trial && partnerId === null
  // A link to create while none is active: the page's main action, also with Enter (⏎ on its button).
  const createIsMain = state !== null && !state.key && !created && !finishedHere
  useSetupKeys({ onEnter: createIsMain && busy === null ? () => void createKey() : null }, { fields: 'ignore' })
  // The footer's back slot: step by step once everything is set up. Changing settings has none: the top bar's Close
  // goes back to them. By hand is its own button under the agent card (byHand).
  const foot =
    change || (allDone && owner) || (allDone && partnerId)
      ? {}
      : allDone
        ? {
            onBack: stepByStep,
            backLabel: t('onboarding.agentSetup.stepByStep'),
            backTitle: t('onboarding.agentSetup.stepByStepTitle'),
            backIcon: false,
          }
        : {}
  // Setting up by hand, for people without an AI agent: a real button right under the agent card, never a small link
  // at the bottom only. Not while changing settings (Close goes back) or once all is set up.
  const byHand = !change && !allDone
  // After signing in, the page loads again at the same address (SecondFactorPrompt).
  if (signInAgain) return <SecondFactorPrompt state={signInAgain} onExit={onDone} />
  return (
    <SetupPage
      className={cx('as', scope === 'partner' && 'k-install-brand')}
      label={t('onboarding.agentSetup.ariaLabel', { brand: product })}
      busy={skeleton}
      mainClassName={cx('as-main', focus && 'as-main-focus')}
      bar={
        <SetupTopBar
          label={t('onboarding.agentSetup.topLabel')}
          progress={change ? 0 : checklistDone}
          exit={
            change
              ? { kind: 'close', onExit: () => onManual(false), title: t('onboarding.agentSetup.change.backTitle') }
              : onLater && !allDone
                ? {
                    kind: 'finish-later',
                    onExit: onLater,
                    // A partner's setup waits in the partner area, not in a go-live checklist.
                    title: partnerId ? t('team.partner.setup.finishLaterTitle') : undefined,
                  }
                : null
          }
          brand={scope === 'partner' ? 'install' : 'app'}
          workspaceId={active?.id}
          away={scope === 'partner'}
        />
      }
    >
      {skeleton && (
        <span className="sr-only" role="status">
          {t('onboarding.agentSetup.loading')}
        </span>
      )}
      <div className={cx('ob-body', compactHead && 'as-head-compact', view.fadeIn && 'ld-in')}>
        {/* "Recommended" is about choosing a way; once everything is set up there is nothing to choose. */}
        {kicker && (
          <span className="as-kicker">
            <Icon name="spark" size={14} /> {t('onboarding.agentSetup.kicker')}
          </span>
        )}
        {skeleton ? (
          <SkeletonPart reserved={reserved}>
            <Skeleton className="as-sk-h1" width="74%" />
            <span className="ob-lede as-sk-lede">
              <Skeleton shape="text" width="100%" />
              <Skeleton shape="text" width="98%" />
              <Skeleton shape="text" width="94%" />
              <Skeleton shape="text" width="18%" />
            </span>
          </SkeletonPart>
        ) : change ? (
          <StepHead
            title={
              change.assistant
                ? t('onboarding.agentSetup.change.assistantTitle', { name: change.assistant.name })
                : t('onboarding.agentSetup.change.workspaceTitle')
            }
            lede={t('onboarding.agentSetup.change.lede', { workspace: change.workspaceName })}
          />
        ) : partnerId ? (
          <StepHead
            title={
              allDone
                ? t('onboarding.agentSetup.partner.changeTitle', { partner: partner?.name ?? '' })
                : t('onboarding.agentSetup.partner.title', { partner: partner?.name ?? '' })
            }
            lede={
              partnerPaid ? (
                t('onboarding.agentSetup.partner.ledePaid')
              ) : (
                <T k="onboarding.agentSetup.partner.lede" values={{ b: chunks => <b>{chunks}</b> }} />
              )
            }
          />
        ) : allDone ? (
          <StepHead
            title={t('onboarding.agentSetup.changeTitle', { brand: brandName() })}
            lede={t('onboarding.agentSetup.changeLede')}
          />
        ) : (
          <StepHead
            title={
              focus
                ? t('onboarding.agentSetup.connected.title', { brand: brandName() })
                : t('onboarding.agentSetup.title', { brand: brandName() })
            }
            lede={
              preparedByPartner ? (
                t('onboarding.agentSetup.owner.ledePrepared', {
                  partner: brand.partner ? brand.name : t('onboarding.agentSetup.owner.yourPartner'),
                })
              ) : (
                <T
                  k={owner ? 'onboarding.agentSetup.owner.lede' : 'onboarding.agentSetup.lede'}
                  values={{ b: chunks => <b>{chunks}</b> }}
                />
              )
            }
          />
        )}
      </div>

      {/* Before the state is read, this page's own address stands for the server's: the note shows at once. Where
            the server runs is its admin's business, not a workspace owner's. */}
      {!personal && !focus && (
        <HostingNote address={state?.mcpUrl ?? `${globalThis.location?.origin ?? ''}/mcp/setup`} />
      )}

      {focus && !linkOpen ? (
        <AgentConnectedBar
          until={timeOf(created?.expiresAt ?? state?.key?.expiresAt ?? '')}
          onOpen={() => setLinkOpen(true)}
        />
      ) : (
        <section className="as-card" aria-label={t('onboarding.agentSetup.yourAgent')}>
          <div className="as-link-step">
            <b className="as-step-title">
              {t('onboarding.agentSetup.link.title')}
              <HelpTip
                label={t('onboarding.agentSetup.link.helpLabel')}
                text={t(
                  partnerId
                    ? 'onboarding.agentSetup.partner.linkHelp'
                    : owner
                      ? 'onboarding.agentSetup.owner.linkHelp'
                      : 'onboarding.agentSetup.link.help',
                  { hours: SETUP_KEY_HOURS },
                )}
              />
              {focus && (
                <Button
                  size="sm"
                  variant="ghost"
                  className="as-fold"
                  title={t('onboarding.agentSetup.connected.hideTitle')}
                  aria-expanded
                  onClick={() => setLinkOpen(false)}
                >
                  {t('onboarding.agentSetup.connected.hide')} <Icon name="up" size={14} />
                </Button>
              )}
            </b>
            <AgentPicker client={client} onPick={pick} />
            {cloudBlocked && (
              <Note
                tone="warning"
                title={t('onboarding.agentSetup.cloudBlocked.title', { agent: agentLabel(client, t) })}
              >
                {t('onboarding.agentSetup.cloudBlocked.body', {
                  agent: agentLabel(client, t),
                  address: state?.mcpUrl.replace(/\/mcp\/setup$/, '') ?? '',
                  brand: product,
                })}
              </Note>
            )}
            {hasLink ? (
              <>
                <div className="as-link">
                  <span className="as-small">
                    {t('onboarding.agentSetup.link.ready', {
                      until: timeOf(created?.expiresAt ?? state?.key?.expiresAt ?? ''),
                    })}
                  </span>
                  <Button
                    size="sm"
                    busy={busy === 'end'}
                    onClick={() => void endAccess()}
                    title={t('onboarding.agentSetup.link.endTitle')}
                  >
                    {t('onboarding.agentSetup.link.end')}
                  </Button>
                </div>
                {created ? (
                  // The setup link is a key: never in a Help report's screenshot (feedback/report.ts).
                  <div data-no-shot style={{ display: 'contents' }}>
                    {cloudBlocked ? (
                      // A cloud agent cannot reach this install: its steps would not work here, so they wait behind a
                      // quiet "Show the steps anyway" (for whoever puts a public address in front of it first).
                      <FoldOut summary={t('onboarding.agentSetup.cloudBlocked.showSteps')}>
                        <StartAgent
                          client={client}
                          url={created.url}
                          scope={scope}
                          change={change}
                          edit={editInstall}
                        />
                      </FoldOut>
                    ) : (
                      <StartAgent client={client} url={created.url} scope={scope} change={change} edit={editInstall} />
                    )}
                  </div>
                ) : (
                  <span className="as-small">{t('onboarding.agentSetup.link.otherBrowser')}</span>
                )}
                {/* The checklist waits until the agent connects; meanwhile this says the page is watching for it. */}
                {!connected && state?.key && (
                  <span className="as-small as-waiting-line" role="status">
                    <Spinner /> {t('onboarding.agentSetup.progress.waiting')}
                  </span>
                )}
              </>
            ) : skeleton ? (
              <div className="as-step">
                <SkeletonPart reserved={reserved}>
                  <Skeleton width={148} height={32} />
                </SkeletonPart>
              </div>
            ) : (
              <div className="as-step">
                {state?.endedByAgentAt && <AgentEndedNote at={state.endedByAgentAt} />}
                <div>
                  <Button
                    variant={createIsMain ? 'primary' : 'secondary'}
                    busy={busy === 'create'}
                    shortcut={createIsMain ? ['⏎'] : undefined}
                    className="as-enter"
                    onClick={() => void createKey()}
                  >
                    {t('onboarding.agentSetup.link.create')}
                  </Button>
                </div>
              </div>
            )}
          </div>
          {signInFirst && (
            <Note
              tone="warning"
              title={t('onboarding.agentSetup.owner.signInFirst.title')}
              action={
                <Button size="sm" title={t('account.signInAgain.title')} onClick={() => void signOutAndSignIn()}>
                  {t('account.signInAgain.button')}
                </Button>
              }
            >
              {partnerId
                ? t('onboarding.agentSetup.partner.signInFirst', { hours: SETUP_KEY_HOURS })
                : t('onboarding.agentSetup.owner.signInFirst.text', { hours: SETUP_KEY_HOURS })}
            </Note>
          )}
          {error && <Note tone="danger">{error}</Note>}
        </section>
      )}

      {byHand && !skeleton && !focus && (
        <SetUpYourself
          text={t(partnerId ? 'onboarding.agentSetup.partner.noAgent' : 'onboarding.agentSetup.noAgent')}
          label={t(partnerId ? 'onboarding.agentSetup.partner.byHand' : 'onboarding.agentSetup.manual')}
          title={t(partnerId ? 'onboarding.agentSetup.partner.byHandTitle' : 'onboarding.agentSetup.manualTitle')}
          onClick={manual}
        />
      )}

      {state ? (
        showProgress && (
          <Progress
            state={state}
            change={Boolean(change)}
            active={active ?? null}
            reload={load}
            fadeIn={view.fadeIn}
            single={owner}
            trial={trial}
            partner={partner ?? null}
            partnerScope={partnerId !== null}
            onFirstWorkspace={onFirstWorkspace}
            editing={editInstall}
          />
        )
      ) : skeleton ? null : (
        <LoadProblem
          what={t('onboarding.agentSetup.progressWhat')}
          message={loadError ?? undefined}
          onRetry={() => void load()}
        />
      )}

      {skeleton ? (
        <div className="ob-foot">
          <SkeletonPart reserved={reserved}>
            <Skeleton width={148} height={36} />
          </SkeletonPart>
        </div>
      ) : (
        <SetupFoot
          {...foot}
          primary={
            openApp ? (
              <Button size="lg" variant={finishedHere && !hasLink ? 'primary' : 'secondary'} onClick={onDone}>
                {allDone
                  ? t('onboarding.agentSetup.open', { brand: brandName() })
                  : t('onboarding.agentSetup.openNow', { brand: brandName() })}
              </Button>
            ) : partnerId && allDone ? (
              <Button
                size="lg"
                variant={finishedHere && !hasLink ? 'primary' : 'secondary'}
                onClick={onDone}
                title={t('onboarding.agentSetup.partner.areaTitle')}
              >
                {t('onboarding.agentSetup.partner.area')}
              </Button>
            ) : null
          }
        />
      )}
    </SetupPage>
  )
}

const timeOf = (iso: string) => untilLabel(new Date(iso))

/** The dropdown's item that opens "New workspace" instead of picking one. */
export const NEW_WORKSPACE = '__new'
/** Its value while the admin has no workspace yet. */
const NO_WORKSPACE = ''

/** What the agent has set up so far, live. */
function Progress({
  state,
  change = false,
  active,
  reload,
  fadeIn,
  single = false,
  trial,
  partner = null,
  partnerScope = false,
  onFirstWorkspace,
  editing = false,
}: {
  state: AgentSetupState
  /** Changing settings (AgentSetup's `change`): what the agent can change, with no "set up" words and no count. */
  change?: boolean
  active: SetupWorkspace | null
  reload: () => Promise<void>
  /** The state just arrived after the skeleton: the card fades in. */
  fadeIn: boolean
  /** A workspace owner's page: one workspace, named, with no dropdown (no switching, no new workspace). */
  single?: boolean
  /** Waiting for the trial: the owner starts it on the Start your free trial row. */
  trial?: StartTrialProps
  /** A partner owner's page: its client workspaces in the dropdown; "New client" in place of "New workspace". */
  partner?: MyPartner | null
  partnerScope?: boolean
  onFirstWorkspace?: () => void
  /** Changing an install that is set up (Edit with AI): the optional providers show open, connecting more is the point. */
  editing?: boolean
}) {
  const t = useT()
  // The assistant answered a test call made here: the row shows done at once; the next read of the progress agrees.
  const [calledHere, setCalledHere] = useState(false)
  const onCalled = useCallback(() => {
    setCalledHere(true)
    void reload()
  }, [reload])
  const [newWorkspace, setNewWorkspace] = useState(false)
  const [switching, setSwitching] = useState<string | null>(null)
  // The row button whose click is switching the tab: busy until the page reloads into the workspace.
  const [switchingFor, setSwitchingFor] = useState<ResumeAction | null>(null)
  const [switchError, setSwitchError] = useState<string | null>(null)
  // A row button clicked before the reload into this workspace: it carries on here, once.
  const [resume] = useState(() => peekResume(active?.id ?? null))
  useEffect(() => {
    if (resume) clearResume()
  }, [resume])
  // Picking a workspace makes it the app's active one (the page reloads into it), so its rows' buttons act on it.
  const switchTo = useCallback(
    (id: string) => {
      setSwitching(id)
      setSwitchError(null)
      openWorkspaceFromHere(id).catch(e => {
        setSwitching(null)
        setSwitchError(e instanceof Error ? e.message : t('onboarding.agentSetup.error.switch'))
      })
    },
    [t],
  )
  // One click is enough: a row button for another workspace switches to it and carries on there after the reload.
  // The test call asks for the microphone inside this click, before the switch: the call after the reload then
  // finds it allowed, and a refusal is said here and switches nothing.
  const switchAndDo = useCallback(
    async (id: string, action: ResumeAction) => {
      setSwitchingFor(action)
      setSwitchError(null)
      if (action === 'test_call') {
        const blocked = await microphoneAllowed()
        if (blocked) {
          setSwitchingFor(null)
          setSwitchError(blocked)
          return
        }
      }
      saveResume(id, action)
      try {
        await openWorkspaceFromHere(id)
      } catch (e) {
        clearResume()
        setSwitchingFor(null)
        setSwitchError(e instanceof Error ? e.message : t('onboarding.agentSetup.error.switch'))
      }
    },
    [t],
  )
  const { allDone } = state.progress
  const steps = partnerOrder(withTestCallMade(state.progress.steps, calledHere))
  // The workspace the rows are about; its buttons work right here once it is the app's active workspace.
  const selected = state.progress.workspace ?? null
  const here = selected && active?.id === selected.id ? active : null
  const done = steps.filter(s => s.state === 'done').length
  // A partner before its Checkout: drafts up to UNPAID_PARTNER_CLIENTS, a limit for the setup only (said on the row).
  const draftsOnly =
    partnerScope && steps.some(s => s.id === 'plan' && s.group === 'partner' && s.state === 'admin_only')
  const draftsFull = draftsOnly && (state.progress.workspaces?.length ?? 0) >= UNPAID_PARTNER_CLIENTS
  return (
    <section
      className={cx('as-card', 'as-progress', fadeIn && 'ld-in')}
      aria-label={t('onboarding.agentSetup.progress.label')}
      aria-live="polite"
    >
      {/* Shown only once the agent is connected (AgentSetup's showProgress); changing settings has no count. */}
      {!change && <ProgressHead done={done} total={steps.length} allDone={allDone} />}
      {/* By part of the setup: the admin's own, the server's (once per install), then the picked workspace's. */}
      {groupsInOrder(steps).map(group => (
        <div
          key={group.id}
          className={cx('as-group', (group.id === 'business' || group.id === 'activate') && 'as-group-ws')}
        >
          {group.id === 'business' && single ? (
            <WorkspaceHeading name={selected?.name ?? ''} unnamed={selected?.unnamed ?? false} />
          ) : group.id === 'business' ? (
            <WorkspacePicker
              workspaces={state.progress.workspaces ?? []}
              selectedId={selected?.id ?? null}
              busy={switching ?? (switchingFor && selected ? selected.id : null)}
              onPick={id => (id === NEW_WORKSPACE ? setNewWorkspace(true) : switchTo(id))}
              clients={partnerScope}
              drafts={draftsOnly}
            />
          ) : group.id === 'activate' ? null : (
            <h3 className="as-group-label" id={`as-group-${group.id}`}>
              {groupLabel(group, t)}
            </h3>
          )}
          <ul
            className="as-list"
            aria-labelledby={group.id === 'activate' ? undefined : `as-group-${group.id}`}
            aria-label={group.id === 'activate' ? t('team.partner.setup.activate.title') : undefined}
          >
            {group.steps.map(step => (
              <Fragment key={step.id}>
                <li className={cx('as-item', step.state)}>
                  <span className="as-mark" aria-hidden>
                    {step.state === 'done' ? (
                      <MarkGlyph kind="works" size={12} />
                    ) : step.state === 'failing' ? (
                      <MarkGlyph kind="partly" size={12} />
                    ) : null}
                  </span>
                  <span className="as-item-label">
                    {planRowInApp(step)
                      ? t('onboarding.steps.activateApp')
                      : step.id === 'activate' && trial
                        ? t(
                            trial.me.workspace?.activation?.trialDays === null
                              ? 'onboarding.steps.activatePlan'
                              : 'onboarding.steps.activate',
                          )
                        : step.label}
                    {step.hint && !priceHintInApp(step) && <span className="as-hint"> · {step.hint}</span>}
                    {/* The trial row before the free steps are done: no button yet, so it says when it opens. */}
                    {step.id === 'activate' && step.state === 'todo' && !isNativeApp() && (
                      <span className="as-hint"> · {t('onboarding.agentSetup.step.trialWaits')}</span>
                    )}
                    {draftsOnly && step.id === 'clients' && (
                      <span className="as-hint as-hint-line">
                        {t('team.partner.clients.setupLimit', { limit: UNPAID_PARTNER_CLIENTS })}
                      </span>
                    )}
                  </span>
                  {step.state === 'failing' && (
                    <span className="as-small">{t('onboarding.agentSetup.progress.failing')}</span>
                  )}
                  <StepAction
                    step={step}
                    workspace={here}
                    onSwitch={selected && !here ? action => void switchAndDo(selected.id, action) : null}
                    switching={switchingFor ?? (switching ? 'workspace' : null)}
                    resume={here ? resume : null}
                    assistantName={selected?.assistantName ?? t('onboarding.agentSetup.yourAssistant')}
                    onCalled={onCalled}
                    reload={reload}
                    trial={trial}
                    onAddClient={partnerScope ? () => setNewWorkspace(true) : undefined}
                    clientsFull={draftsFull}
                    clientsDraft={draftsOnly}
                    activatePartnerId={partnerScope ? (partner?.id ?? null) : null}
                    onAddNumber={
                      partnerScope && selected
                        ? () => {
                            // The client workspace's Phone number page (Workspace settings), in that workspace.
                            if (here) window.location.hash = '#workspace/phone'
                            else void switchToWorkspace(selected.id, '#workspace/phone')
                          }
                        : undefined
                    }
                  />
                </li>
              </Fragment>
            ))}
          </ul>
        </div>
      ))}
      {switchError && <Note tone="danger">{switchError}</Note>}
      {newWorkspace && partnerScope && partner ? (
        <NewClient
          partner={partner}
          onClose={() => setNewWorkspace(false)}
          onFirstWorkspace={onFirstWorkspace}
          onMade={id => {
            setNewWorkspace(false)
            // The page shows the new client workspace's steps next: it becomes the tab's workspace.
            switchTo(id)
          }}
        />
      ) : newWorkspace ? (
        <NewWorkspaceDialog onClose={() => setNewWorkspace(false)} ownerChoice={false} />
      ) : null}
      {state.progress.optional?.length ? (
        <OptionalProviders items={state.progress.optional} openFirst={editing} />
      ) : null}
    </section>
  )
}

type OptionalProvider = NonNullable<AgentSetupState['progress']['optional']>[number]

/**
 * An install's providers calls do not need (sign-in, calendar apps, apps, recordings, …), the last part of the card
 * and never in its count: folded to one line while setting up, so the setup keeps to what calls need; open when an
 * install that is set up is changed (Edit with AI), where connecting more is the point. The agent offers them too
 * (setup_status.optional); each row's Open goes to its group in Server admin › Providers, the way by hand.
 */
function OptionalProviders({ items, openFirst }: { items: readonly OptionalProvider[]; openFirst: boolean }) {
  const t = useT()
  const [open, setOpen] = useState(openFirst)
  const connected = items.filter(i => i.state === 'done').length
  return (
    <div className="as-group as-group-ws as-optional">
      <div className="as-ws-head">
        {/* Title and count one text block: on a phone they wrap as one line of words, beside the button. */}
        <p className="as-optional-name">
          <span className="as-group-label" id="as-group-optional" role="heading" aria-level={3}>
            {t('onboarding.agentSetup.optional.title')}
          </span>{' '}
          <span className="as-hint">
            {t('onboarding.agentSetup.optional.count', { done: connected, total: items.length })}
          </span>
        </p>
        <Button
          size="sm"
          variant="ghost"
          className="as-optional-toggle"
          aria-expanded={open}
          aria-controls="as-optional-list"
          onClick={() => setOpen(o => !o)}
          title={t(open ? 'onboarding.agentSetup.optional.hideTitle' : 'onboarding.agentSetup.optional.showTitle')}
        >
          {t(open ? 'onboarding.agentSetup.optional.hide' : 'onboarding.agentSetup.optional.show')}
        </Button>
      </div>
      {open && (
        <>
          <p className="as-hint as-optional-lede">{t('onboarding.agentSetup.optional.lede')}</p>
          <ul className="as-list" id="as-optional-list" aria-labelledby="as-group-optional">
            {items.map(item => (
              <li key={item.id} className={cx('as-item', item.state === 'off' ? 'todo' : item.state)}>
                <span className="as-mark" aria-hidden>
                  {item.state === 'done' ? (
                    <MarkGlyph kind="works" size={12} />
                  ) : item.state === 'failing' ? (
                    <MarkGlyph kind="partly" size={12} />
                  ) : null}
                </span>
                <span className="as-item-label">
                  {item.label}
                  {item.state === 'off' && (
                    <span className="as-hint"> · {t('onboarding.agentSetup.optional.off')}</span>
                  )}
                </span>
                <span className="as-item-action">
                  <AppLink to={`#admin/providers/${item.id}`} title={t('onboarding.agentSetup.optional.openTitle')}>
                    {t('onboarding.agentSetup.optional.open')}
                  </AppLink>
                </span>
              </li>
            ))}
          </ul>
        </>
      )}
    </div>
  )
}

/**
 * A partner owner's page before White-label Cloud runs (one flow, agent-first): agency and brand,
 * the clients (the agent sets each up), then "Activate White-label Cloud" as the very last row, below the picked
 * client's steps. Team invitations go out only after the Checkout, so the Team row waits until then.
 */
export function partnerOrder<S extends { id: string; group?: string; state: string }>(steps: readonly S[]): S[] {
  const plan = steps.find(s => s.id === 'plan' && s.group === 'partner')
  if (plan?.state !== 'admin_only') return [...steps]
  return [...steps.filter(s => s !== plan && !(s.id === 'team' && s.group === 'partner')), plan]
}

/**
 * The row that starts a plan (a sign-up's trial, `activate`; White-label Cloud's, the partner's `plan`) inside the iOS
 * and Android app, where nothing is bought (LIN-874): a neutral name and line instead of its button.
 */
export const planRowInApp = (step: { id: string; group?: string }) =>
  isNativeApp() && (step.id === 'activate' || (step.id === 'plan' && step.group === 'partner'))

/** In the app, the rows whose server hint is a price (the plan's; the partner's client workspaces, a seat each). */
const priceHintInApp = (step: { id: string; group?: string }) =>
  planRowInApp(step) || (isNativeApp() && step.id === 'clients' && step.group === 'partner')

/** The steps by group, in the order they come; Activate (last, see partnerOrder) as a group of its own at the end. */
function groupsInOrder<S extends { id: string; group: SetupStepGroup; state: string }>(steps: readonly S[]) {
  const last = steps.at(-1)
  if (last?.id !== 'plan' || last.state !== 'admin_only') return stepsByGroup(steps)
  const lastGroup: { id: SetupStepGroup | 'activate'; label: string; steps: S[] } = {
    id: 'activate',
    label: '',
    steps: [last],
  }
  return [...stepsByGroup<S>(steps.slice(0, -1)), lastGroup]
}

/** A group's heading: the server's from the steps' groups; the partner's in this page's language. */
/** A part's heading in the reader's language; the API's `label` is English (the setup MCP reads it). */
function groupLabel(group: { id: string; label: string }, t: ReturnType<typeof useT>) {
  if (group.id === 'partner') return t('onboarding.agentSetup.partner.group')
  if (group.id === 'server') return t('onboarding.agentSetup.server.group')
  if (group.id === 'business') return t('onboarding.agentSetup.workspace.label')
  return group.label
}

/**
 * "Add client" on a partner owner's page: the partner area's New client dialog (a client workspace is a seat on the
 * partner bill, so only the owner adds one, never the agent). Waits for the partner to load first.
 */
function NewClient({
  partner,
  onClose,
  onFirstWorkspace,
  onMade,
}: {
  partner: MyPartner
  onClose: () => void
  onFirstWorkspace?: () => void
  onMade: (id: string) => void
}) {
  const data = useQuery(PARTNER_QUERY(partner.id), () => loadPartner(partner.id)).data
  if (!data) return null
  return (
    <NewClientDialog
      data={data}
      returnHash={PARTNER_AGENT_HASH}
      onFirstWorkspace={tabWorkspace() ? undefined : onFirstWorkspace}
      onOpen={id => onMade(id)}
      onInvite={onClose}
      onClose={onClose}
      onMade={workspace => onMade(workspace.id)}
    />
  )
}

/**
 * The workspace dropdown's items: each workspace the admin can set up with how far it got (or that there is none yet),
 * then "New workspace…".
 */
export function workspaceOptions(workspaces: readonly SetupWorkspaceSummary[], clients = false, drafts = false) {
  const { t } = i18n()
  return [
    ...(workspaces.length
      ? workspaces.map(w => ({
          value: w.id,
          label: w.name,
          description:
            w.done >= w.total
              ? t('onboarding.agentSetup.workspace.setUp')
              : t('onboarding.agentSetup.progress.count', { done: w.done, total: w.total }),
        }))
      : [
          {
            value: NO_WORKSPACE,
            label: t('onboarding.agentSetup.workspace.none'),
            description: t('onboarding.agentSetup.workspace.noneDescription'),
          },
        ]),
    clients
      ? {
          value: NEW_WORKSPACE,
          label: t('onboarding.agentSetup.partner.newClient'),
          description: t(
            drafts
              ? 'onboarding.agentSetup.partner.newClientDescriptionDraft'
              : 'onboarding.agentSetup.partner.newClientDescription',
          ),
        }
      : {
          value: NEW_WORKSPACE,
          label: t('onboarding.agentSetup.workspace.new'),
          description: t('onboarding.agentSetup.workspace.newDescription'),
        },
  ]
}

/**
 * The heading of the workspace steps: which workspace they show. Lists every workspace the admin can set up (owner or
 * admin there) with how far it got, and "New workspace…". The server above is one; workspaces can be many.
 */
export function WorkspacePicker({
  workspaces,
  selectedId,
  busy,
  onPick,
  clients = false,
  drafts = false,
}: {
  workspaces: readonly SetupWorkspaceSummary[]
  selectedId: string | null
  /** The workspace being switched to: the dropdown waits for the page to reload into it. */
  busy: string | null
  onPick: (id: string) => void
  /** A partner owner's page: its client workspaces, and "Add client…". */
  clients?: boolean
  /** A partner before its Checkout: a new client is a free draft. */
  drafts?: boolean
}) {
  const t = useT()
  const options = workspaceOptions(workspaces, clients, drafts)
  return (
    <div className="as-ws-head" id="as-group-business">
      <span className="as-group-label">
        {t(clients ? 'onboarding.agentSetup.partner.client' : 'onboarding.agentSetup.workspace.label')}
      </span>
      <Select
        label={t('onboarding.agentSetup.workspace.select')}
        value={busy ?? selectedId ?? NO_WORKSPACE}
        options={options}
        disabled={Boolean(busy)}
        size="sm"
        onChange={onPick}
      />
      <HelpTip
        text={t(clients ? 'onboarding.agentSetup.partner.clientHelp' : 'onboarding.agentSetup.workspace.help')}
      />
    </div>
  )
}

/**
 * The heading of the workspace steps on a workspace owner's page: their one business, by name (`unnamed`: made by
 * sign-up and not named yet, so no placeholder name).
 */
export function WorkspaceHeading({ name, unnamed = false }: { name: string; unnamed?: boolean }) {
  const t = useT()
  return (
    <div className="as-ws-head" id="as-group-business">
      <span className="as-group-label">{t('onboarding.agentSetup.owner.business')}</span>
      <b className="as-ws-name">{unnamed ? t('onboarding.agentSetup.owner.unnamed') : name}</b>
    </div>
  )
}

/** The agent ended its own access at the end of the setup (setup_endAccess): when, in this browser's time. */
/**
 * The agent card once the agent is connected: one line that says so and how long the link works, and a button that
 * unfolds the card (the agent picker, the link, how to start it, End access).
 */
export function AgentConnectedBar({ until, onOpen }: { until: string; onOpen: () => void }) {
  const t = useT()
  return (
    <section className="as-card as-connected" aria-label={t('onboarding.agentSetup.yourAgent')}>
      <b className="as-connected-what">
        <StatusMark state="works" label={t('onboarding.agentSetup.progress.connected')} />
        {t('onboarding.agentSetup.progress.connected')}
      </b>
      <span className="as-small as-connected-until">{t('onboarding.agentSetup.connected.until', { until })}</span>
      <Button
        size="sm"
        variant="ghost"
        className="as-fold"
        title={t('onboarding.agentSetup.connected.showTitle')}
        aria-expanded={false}
        onClick={onOpen}
      >
        {t('onboarding.agentSetup.connected.show')} <Icon name="down" size={14} />
      </Button>
    </section>
  )
}

/** The progress card's head while the agent works: how far it got, in words and as a bar. */
function ProgressHead({ done, total, allDone }: { done: number; total: number; allDone: boolean }) {
  const t = useT()
  return (
    <div className="as-progress-head as-progress-focus">
      <b>{allDone ? t('onboarding.agentSetup.progress.allDone') : t('onboarding.agentSetup.progress.label')}</b>
      <span className="as-small">{t('onboarding.agentSetup.progress.count', { done, total })}</span>
      <span
        className="as-bar"
        role="progressbar"
        aria-label={t('onboarding.agentSetup.progress.label')}
        aria-valuemin={0}
        aria-valuemax={total}
        aria-valuenow={done}
      >
        <span style={{ width: `${total ? Math.round((done / total) * 100) : 0}%` }} />
      </span>
    </div>
  )
}

export function AgentEndedNote({ at }: { at: string }) {
  const t = useT()
  return (
    <span className="as-small" role="status">
      {t('onboarding.agentSetup.agentEnded', { when: dateTimeLabel(new Date(at)) })}
    </span>
  )
}

/** A test call answered here a moment ago: the step is done before the next read of the progress says so. */
export function withTestCallMade<S extends { id: string; state: string }>(
  steps: readonly S[],
  calledHere: boolean,
): S[] {
  return steps.map(s => (calledHere && s.id === 'test_call' ? { ...s, state: 'done' } : s))
}

/**
 * How the admin finishes a step on this page (never "do this in the app"): the call forwarding (the
 * agent can also run it) and the test call, always for the workspace shown. Steps the agent does have none.
 */
export function StepAction({
  step,
  workspace,
  onSwitch,
  switching = null,
  resume = null,
  assistantName,
  onCalled,
  reload,
  trial,
  onAddClient,
  clientsFull = false,
  clientsDraft = false,
  onAddNumber,
  activatePartnerId = null,
}: {
  step: AgentSetupState['progress']['steps'][number]
  /** The workspace shown, when it is the app's active workspace in this tab: the buttons act in it right here. */
  workspace: SetupWorkspace | null
  /**
   * The workspace shown is not the active one (or this tab has none): its buttons make it active first, then carry on
   * with what was clicked (one click, resume-after-switch.ts).
   */
  onSwitch: ((action: ResumeAction) => void) | null
  /** A switch is under way: the button that started it is busy, the other one waits. */
  switching?: ResumeAction | 'workspace' | null
  /** Clicked before the reload into this workspace: start the call or open the dialog at once. */
  resume?: ResumeAction | null
  assistantName: string
  onCalled: () => void
  reload: () => Promise<void>
  /** Waiting for the trial: "Start free trial" on its row, once the agent's free steps are done (admin_only). */
  trial?: StartTrialProps
  /** A partner owner's page: "Add client" on the Client workspaces row (a seat on the bill: the owner's click). */
  onAddClient?: () => void
  /** A partner owner's page before its Checkout, at the draft cap: Add client greyed, the row says why. */
  clientsFull?: boolean
  /** A partner owner's page before its Checkout: a new client is a free draft (Add client's tooltip says so). */
  clientsDraft?: boolean
  /** A partner owner's page: "Add number" on a client workspace's Phone number row (it costs money). */
  onAddNumber?: () => void
  /** A partner owner's page: the partner whose "Activate White-label Cloud" row opens the setup's Activate step. */
  activatePartnerId?: string | null
}) {
  const t = useT()
  // The iOS and Android app starts no plan (LIN-874): the row only says it is not available in the app, with no button, link or word about where.
  if (planRowInApp(step)) {
    if (step.state !== 'admin_only') return null
    return <span className="as-small">{t('common.plan.managedOnWeb')}</span>
  }
  if (step.id === 'plan' && activatePartnerId) {
    if (step.state !== 'admin_only') return null
    return (
      <span className="as-item-action">
        <SetupActivatePartner />
      </span>
    )
  }
  // A partner after its Checkout: its team can be invited now, by the agent or by the owner in the partner area.
  if (step.id === 'team' && step.group === 'partner' && activatePartnerId && step.state !== 'done')
    return (
      <span className="as-item-action">
        <Button
          size="sm"
          variant="secondary"
          title={t('onboarding.agentSetup.partner.inviteTitle')}
          onClick={() => {
            window.location.hash = '#partner/team'
          }}
        >
          {t('onboarding.agentSetup.partner.invite')}
        </Button>
      </span>
    )
  if (step.id === 'clients' && onAddClient)
    return (
      <span className="as-item-action">
        <Button
          size="sm"
          variant="secondary"
          icon="plus"
          disabled={clientsFull}
          title={
            clientsFull
              ? t('team.partner.clients.setupLimit', { limit: UNPAID_PARTNER_CLIENTS })
              : t(
                  clientsDraft
                    ? 'onboarding.agentSetup.partner.addClientTitleDraft'
                    : 'onboarding.agentSetup.partner.addClientTitle',
                )
          }
          onClick={onAddClient}
        >
          {t('onboarding.agentSetup.partner.addClient')}
        </Button>
      </span>
    )
  if (step.id === 'phone' && onAddNumber && step.state === 'admin_only')
    return (
      <span className="as-item-action">
        <Button
          size="sm"
          variant="secondary"
          title={t('onboarding.agentSetup.partner.addNumberTitle')}
          onClick={onAddNumber}
        >
          {t('onboarding.agentSetup.partner.addNumber')}
        </Button>
      </span>
    )
  if (step.id === 'activate') {
    if (step.state !== 'admin_only' || !trial) return null
    return (
      <span className="as-item-action">
        <SetupStartTrial me={trial.me} onTermsChanged={trial.onTermsChanged} />
      </span>
    )
  }
  if (step.id === 'test_call') {
    // Also once done ("Call again"), so a call in progress keeps its panel when the row turns done.
    if (workspace)
      return (
        <span className="as-item-action">
          <SetupTestCall
            workspace={workspace}
            assistantName={assistantName}
            done={step.state === 'done'}
            onCalled={onCalled}
            autoStart={resume === 'test_call'}
          />
        </span>
      )
    if (step.state === 'done') return null
    if (onSwitch)
      return (
        <span className="as-item-action">
          <Button
            size="sm"
            variant="secondary"
            icon="phone-in"
            busy={switching === 'test_call'}
            disabled={Boolean(switching)}
            title={t('onboarding.agentSetup.step.callNowTitle')}
            onClick={() => onSwitch('test_call')}
          >
            {t('onboarding.agentSetup.step.callNow', { name: assistantName })}
          </Button>
        </span>
      )
    return <span className="as-small">{t('onboarding.agentSetup.step.onceWorkspaceExists')}</span>
  }
  if (step.id === 'forwarding' && step.state !== 'done') {
    if (workspace)
      return (
        <span className="as-item-action">
          <SetupForwarding workspace={workspace} onClosed={() => void reload()} autoOpen={resume === 'forwarding'} />
        </span>
      )
    if (onSwitch)
      return (
        <span className="as-item-action">
          <Button
            size="sm"
            variant="secondary"
            busy={switching === 'forwarding'}
            disabled={Boolean(switching)}
            title={t('onboarding.agentSetup.step.forwardingTitle')}
            onClick={() => onSwitch('forwarding')}
          >
            {t('onboarding.rowActions.forwarding')}
          </Button>
        </span>
      )
  }
  return null
}

/**
 * The way without an AI agent, under the agent card: one line and a secondary button that opens the setup by hand
 * (AgentSetup's `manual`). The agent stays the primary way; this is there for everyone who has none.
 */
export function SetUpYourself({
  text,
  label,
  title,
  onClick,
}: {
  text: string
  label: string
  title: string
  onClick: () => void
}) {
  return (
    <div className="as-by-hand">
      <span>{text}</span>
      <Button variant="secondary" title={title} onClick={onClick}>
        {label}
      </Button>
    </div>
  )
}

/**
 * How to start the chosen agent with the link. Where one link opens the agent with the prompt filled in (`open`), that
 * button comes first and the command or prompt to copy waits behind a quiet "Or …" (opened on request). Elsewhere the
 * steps are the way: one command or link where the agent connects itself, the prompt to copy where pasting it is.
 */
export function StartAgent({
  client,
  url,
  scope = 'install',
  change,
  edit = false,
}: {
  client: AgentClient
  url: string
  scope?: SetupScope
  /** Changing settings (AgentSetup's `change`): the prompt names what to change and reads it first. */
  change?: SetupChange
  /** An install that is set up (Edit with AI): the prompt changes providers or connects more, no setup again. */
  edit?: boolean
}) {
  const t = useT()
  const format = useFormat()
  // The page's language: the steps and the prompt the agent gets, so it talks to the person in it.
  return <AgentSteps plan={startPlan(client, url, scope, change, { t, format }, edit)} />
}

/**
 * On an install that is not on a public https address (installPlace): what does not work there, and the guide to
 * putting it on a server (docs/hosting.md). Nothing on a public install.
 */
export function HostingNote({ address }: { address: string }) {
  const t = useT()
  const place = installPlace(address)
  if (place === 'public') return null
  const local = place === 'this_computer'
  return (
    <Note
      tone="warning"
      title={t(local ? 'onboarding.agentSetup.hosting.localTitle' : 'onboarding.agentSetup.hosting.noHttpsTitle')}
    >
      {local
        ? t('onboarding.agentSetup.hosting.local', { brand: brandName() })
        : t('onboarding.agentSetup.hosting.noHttps')}{' '}
      <a
        href={local ? HOSTING_GUIDE_URL : `${HOSTING_GUIDE_URL}#your-own-domain`}
        target="_blank"
        rel="noreferrer"
        title={t('onboarding.agentSetup.hosting.guideTitle')}
      >
        {local ? t('onboarding.agentSetup.hosting.putOnServer') : t('onboarding.agentSetup.hosting.addHttps')}
      </a>
    </Note>
  )
}

/**
 * Opens workspace `id` in this tab (the page reloads into it). A partner's client draft opened from the partner's agent
 * page remembers it: the draft's last step comes back here, not to the partner area (onboarding/flow.ts).
 */
export function openWorkspaceFromHere(
  id: string,
  hash: string = window.location.hash,
  open: (id: string, hash: string | null) => Promise<unknown> = switchToWorkspace,
) {
  rememberDraftOpenedFrom(id, hash)
  return open(id, null)
}