Files
PaperClipAI/ui/src/lib/onboarding-auto-open.ts
T
Devin FoleyandPaperclip e3d8f42e19 fix(ui): recover stalled onboarding connection checks (#19505)
Bound the complete onboarding connection check to two minutes, abort its
requests on timeout, and ignore late results. Keep the saved account
available for retry and resume its matching unfinished setup after refresh.

Verified 242 focused tests, workspace typecheck and build, and the complete
hosted CI check set. Greptile is 5/5 with no unresolved review comments.
Local full-UI timing failure and interrupted CI runner attempts remain
documented in the PR evidence.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-11-06 10:31:18 -08:00

53 lines
2.1 KiB
TypeScript

import type { Agent } from "@paperclipai/shared";
import { ONBOARDING_AGENT_STEP } from "./onboarding-route";
/** Resume only the draft this browser saved, while no completed agent exists. */
export function dashboardOnboardingOptions(
companyId: string,
agents: Pick<Agent, "id" | "companyId" | "pauseReason" | "status">[] | undefined,
rawDraft: string | null,
): { companyId: string; initialStep: 3 | 4 } | null {
if (!agents) return null;
if (agents.length === 0) return { companyId, initialStep: ONBOARDING_AGENT_STEP };
if (agents.some(agent => agent.pauseReason !== "setup" || agent.status === "terminated")) return null;
try {
const draft = JSON.parse(rawDraft ?? "null");
if (draft?.createdCompanyId !== companyId || draft.createdAgentId
|| !agents.some(agent => agent.id === draft.setupAgentId && agent.companyId === companyId)) return null;
return { companyId, initialStep: 4 };
} catch {
return null;
}
}
/**
* Which companies have already been offered onboarding automatically.
*
* The dashboard opens onboarding for a company that has no agent. That is an
* offer, and an offer that reappears after it is declined is not one, so each
* company gets at most one.
*
* Module scope rather than state inside the dashboard, on purpose. A ref dies
* with the component, so navigating away and back would offer again, and it
* holds one company, so visiting a second agentless company and returning to
* the first would too. A page reload does offer again, which is the right
* scope: that is a new visit rather than the same one continuing.
*/
const offeredCompanyIds = new Set<string>();
/**
* Records an offer, and reports whether it is the first one for this company.
* Returns false when onboarding has already been offered, in which case the
* caller must not open it again.
*/
export function claimOnboardingOffer(companyId: string): boolean {
if (offeredCompanyIds.has(companyId)) return false;
offeredCompanyIds.add(companyId);
return true;
}
/** Forget every offer. For tests, which must not inherit each other's state. */
export function resetOnboardingOffers(): void {
offeredCompanyIds.clear();
}