The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { RefreshIcon } from "~/components/ui/refresh-icon"; |
| 2 | import { useAtomValue } from "@effect/atom-react"; |
| 3 | import { useLocation, useNavigate } from "@tanstack/react-router"; |
| 4 | import { Atom } from "effect/reactivity"; |
| 5 | import { useGT } from "gt-react"; |
| 6 | import { useEffect, useState } from "react"; |
| 7 | |
| 8 | import { |
| 9 | ensureClientSettingsHydrated, |
| 10 | useClientSettings, |
| 11 | useClientSettingsHydrationStatus, |
| 12 | } from "../../hooks/useSettings"; |
| 13 | import { completeOnboarding } from "../../onboarding/firstRun"; |
| 14 | import { |
| 15 | isFirstRunWorkspaceProvenanceAuthoritative, |
| 16 | isFreshFirstRunWorkspace, |
| 17 | resolveFirstRunDecision, |
| 18 | resolveHostedFirstRunDecision, |
| 19 | transitionFirstRunGateState, |
| 20 | type FirstRunGateState, |
| 21 | } from "../../onboarding/firstRun.logic"; |
| 22 | import { useAllEnvironmentShellsBootstrapped, useProjects } from "../../state/entities"; |
| 23 | import { useEnvironments } from "../../state/environments"; |
| 24 | import { environmentProjects } from "../../state/projects"; |
| 25 | import { primaryServerConfigAtom, primaryServerWelcomeAtom } from "../../state/server"; |
| 26 | import { environmentShell } from "../../state/shell"; |
| 27 | import { environmentThreadShells } from "../../state/threads"; |
| 28 | import { Button } from "../ui/button"; |
| 29 | |
| 30 | /** Pending blocks all rendering, including EventRouter welcome navigation. */ |
| 31 | |
| 32 | const FIRST_RUN_DECISION_TIMEOUT_MS = 4_000; |
| 33 | |
| 34 | const primaryShellLiveAtom = Atom.make((get) => { |
| 35 | const serverConfig = get(primaryServerConfigAtom); |
| 36 | return ( |
| 37 | serverConfig !== null && |
| 38 | get(environmentShell.stateValueAtom(serverConfig.environment.environmentId)).status === "live" |
| 39 | ); |
| 40 | }).pipe(Atom.withLabel("web-onboarding-primary-shell-live")); |
| 41 | |
| 42 | const workspaceEvidenceLiveAtom = Atom.make((get) => { |
| 43 | const environmentIds = new Set([ |
| 44 | ...get(environmentProjects.projectsAtom).map((project) => project.environmentId), |
| 45 | ...get(environmentThreadShells.threadShellsAtom).map((thread) => thread.environmentId), |
| 46 | ]); |
| 47 | |
| 48 | for (const environmentId of environmentIds) { |
| 49 | if (get(environmentShell.stateValueAtom(environmentId)).status !== "live") { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return true; |
| 55 | }).pipe(Atom.withLabel("web-onboarding-workspace-evidence-live")); |
| 56 | |
| 57 | // Subscribe only to thread count and the sole thread so populated-workspace shell updates cannot rerender the |
| 58 | // app root. |
| 59 | const threadCountAtom = Atom.make( |
| 60 | (get) => get(environmentThreadShells.threadShellsAtom).length, |
| 61 | ).pipe(Atom.withLabel("web-onboarding-thread-count")); |
| 62 | |
| 63 | const soleThreadAtom = Atom.make((get) => { |
| 64 | const threads = get(environmentThreadShells.threadShellsAtom); |
| 65 | return threads.length === 1 ? threads[0]! : null; |
| 66 | }).pipe(Atom.withLabel("web-onboarding-sole-thread")); |
| 67 | |
| 68 | export function FirstRunGate({ |
| 69 | enabled, |
| 70 | hostedStatic, |
| 71 | children, |
| 72 | }: { |
| 73 | readonly enabled: boolean; |
| 74 | readonly hostedStatic: boolean; |
| 75 | readonly children: React.ReactNode; |
| 76 | }) { |
| 77 | const navigate = useNavigate(); |
| 78 | const pathname = useLocation({ select: (location) => location.pathname }); |
| 79 | const hydrationStatus = useClientSettingsHydrationStatus(); |
| 80 | const hydrated = hydrationStatus === "ready"; |
| 81 | const onboardingCompletedAt = useClientSettings((settings) => settings.onboardingCompletedAt); |
| 82 | const bootstrapped = useAllEnvironmentShellsBootstrapped(); |
| 83 | const { isReady: environmentCatalogReady } = useEnvironments(); |
| 84 | const projects = useProjects(); |
| 85 | const threadCount = useAtomValue(threadCountAtom); |
| 86 | const soleThread = useAtomValue(soleThreadAtom); |
| 87 | const serverConfig = useAtomValue(primaryServerConfigAtom); |
| 88 | const serverWelcome = useAtomValue(primaryServerWelcomeAtom); |
| 89 | const primaryShellLive = useAtomValue(primaryShellLiveAtom); |
| 90 | const workspaceEvidenceLive = useAtomValue(workspaceEvidenceLiveAtom); |
| 91 | const [gateState, setGateState] = useState<FirstRunGateState>(() => ({ |
| 92 | decision: |
| 93 | (!enabled && !hostedStatic) || (hydrated && onboardingCompletedAt !== null) |
| 94 | ? "app" |
| 95 | : "pending", |
| 96 | stalled: false, |
| 97 | })); |
| 98 | const { decision, stalled } = gateState; |
| 99 | const settingsReadFailed = hydrationStatus === "failed" || hydrationStatus === "retrying"; |
| 100 | // The server's sole cwd-bootstrap project/thread still counts as fresh so npx t3 users see onboarding. |
| 101 | const serverCwd = serverConfig?.cwd ?? null; |
| 102 | const primaryEnvironmentId = serverConfig?.environment.environmentId ?? null; |
| 103 | const workspaceFresh = |
| 104 | threadCount <= 1 && |
| 105 | isFreshFirstRunWorkspace({ |
| 106 | primaryEnvironmentId, |
| 107 | serverCwd, |
| 108 | bootstrapProjectId: serverWelcome?.bootstrapProjectId, |
| 109 | bootstrapThreadId: serverWelcome?.bootstrapThreadId, |
| 110 | bootstrapProjectCreated: serverWelcome?.bootstrapProjectCreated, |
| 111 | bootstrapThreadCreated: serverWelcome?.bootstrapThreadCreated, |
| 112 | projects, |
| 113 | threads: soleThread === null ? [] : [soleThread], |
| 114 | }); |
| 115 | |
| 116 | const { decision: nextDecision, persistCompletion } = hostedStatic |
| 117 | ? resolveHostedFirstRunDecision(hydrated) |
| 118 | : resolveFirstRunDecision({ |
| 119 | enabled, |
| 120 | hydrated, |
| 121 | completed: onboardingCompletedAt !== null, |
| 122 | bootstrapped, |
| 123 | authoritative: primaryShellLive, |
| 124 | workspaceAuthoritative: workspaceEvidenceLive, |
| 125 | workspaceProvenanceAuthoritative: isFirstRunWorkspaceProvenanceAuthoritative({ |
| 126 | welcomeReceived: serverWelcome !== null, |
| 127 | bootstrapStatus: serverWelcome?.bootstrapStatus ?? null, |
| 128 | }), |
| 129 | catalogReady: environmentCatalogReady, |
| 130 | serverConfigAvailable: serverConfig !== null, |
| 131 | workspaceFresh, |
| 132 | projectCount: projects.length, |
| 133 | threadCount, |
| 134 | }); |
| 135 | |
| 136 | if (hydrated) { |
| 137 | const transitioned = transitionFirstRunGateState(gateState, { |
| 138 | type: "evidence", |
| 139 | decision: nextDecision, |
| 140 | }); |
| 141 | if (transitioned !== gateState) setGateState(transitioned); |
| 142 | } |
| 143 | |
| 144 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- persists onboarding completion to settings once the gate settles on skipping the wizard |
| 145 | useEffect(() => { |
| 146 | if (decision === "wizard" || !hydrated) return; |
| 147 | if (persistCompletion && onboardingCompletedAt === null) { |
| 148 | void completeOnboarding().catch(() => undefined); |
| 149 | } |
| 150 | }, [decision, hydrated, onboardingCompletedAt, persistCompletion]); |
| 151 | |
| 152 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- timer gives up on a pending first-run decision after the timeout |
| 153 | useEffect(() => { |
| 154 | if (!enabled || decision !== "pending" || !hydrated) return; |
| 155 | const timer = window.setTimeout( |
| 156 | () => setGateState((state) => transitionFirstRunGateState(state, { type: "timeout" })), |
| 157 | FIRST_RUN_DECISION_TIMEOUT_MS, |
| 158 | ); |
| 159 | return () => window.clearTimeout(timer); |
| 160 | }, [decision, enabled, hydrated]); |
| 161 | |
| 162 | // Setup opens by itself only from the home route. A direct link, such as |
| 163 | // Settings, opens as asked so setup can never lock a user out of the app. |
| 164 | const wizardOwnsRoute = pathname === "/"; |
| 165 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- navigates from the home route to /welcome when the gate decides on the wizard |
| 166 | useEffect(() => { |
| 167 | if (decision === "wizard" && wizardOwnsRoute) { |
| 168 | void navigate({ to: "/welcome", replace: true }); |
| 169 | } |
| 170 | }, [decision, navigate, wizardOwnsRoute]); |
| 171 | |
| 172 | if (settingsReadFailed) { |
| 173 | return <FirstRunRecovery reason="settings" retrying={hydrationStatus === "retrying"} />; |
| 174 | } |
| 175 | if (decision === "wizard" && !wizardOwnsRoute) { |
| 176 | return children; |
| 177 | } |
| 178 | if (decision !== "app") { |
| 179 | return stalled ? <FirstRunRecovery reason="connection" /> : null; |
| 180 | } |
| 181 | return children; |
| 182 | } |
| 183 | |
| 184 | function FirstRunRecovery({ |
| 185 | reason, |
| 186 | retrying = false, |
| 187 | }: { |
| 188 | readonly reason: "settings" | "connection"; |
| 189 | readonly retrying?: boolean; |
| 190 | }) { |
| 191 | const settingsReadFailed = reason === "settings"; |
| 192 | const gt = useGT(); |
| 193 | return ( |
| 194 | <main className="flex h-dvh min-h-0 items-center justify-center bg-background px-6 text-foreground"> |
| 195 | <div className="flex max-w-sm flex-col items-center text-center"> |
| 196 | <h1 className="text-lg font-semibold"> |
| 197 | {settingsReadFailed ? gt("Could not read settings") : gt("Still connecting")} |
| 198 | </h1> |
| 199 | <p className="mt-2 text-sm text-muted-foreground"> |
| 200 | {settingsReadFailed |
| 201 | ? gt("Your saved settings could not be loaded.") |
| 202 | : gt("T3 Code could not confirm this workspace.")} |
| 203 | </p> |
| 204 | <Button |
| 205 | className="mt-5" |
| 206 | size="sm" |
| 207 | variant="outline" |
| 208 | disabled={retrying} |
| 209 | onClick={() => { |
| 210 | if (settingsReadFailed) { |
| 211 | void ensureClientSettingsHydrated().catch(() => undefined); |
| 212 | } else { |
| 213 | window.location.reload(); |
| 214 | } |
| 215 | }} |
| 216 | > |
| 217 | <RefreshIcon refreshing={retrying} /> |
| 218 | {settingsReadFailed ? gt("Retry") : gt("Reload")} |
| 219 | </Button> |
| 220 | </div> |
| 221 | </main> |
| 222 | ); |
| 223 | } |