The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createFileRoute, redirect, useLocation, useNavigate } from "@tanstack/react-router"; |
| 2 | import { useState } from "react"; |
| 3 | import { EnvironmentId } from "@t3tools/contracts"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | |
| 7 | import { NoProjectsHero } from "../components/NoProjectsHero"; |
| 8 | import { WelcomePage } from "../components/onboarding/WelcomePage"; |
| 9 | import { useNewThreadHandler } from "../hooks/useHandleNewThread"; |
| 10 | import { useEnvironments, usePrimaryEnvironmentId } from "../state/environments"; |
| 11 | |
| 12 | const decodeEnvironmentId = Schema.decodeOption(EnvironmentId); |
| 13 | |
| 14 | export const Route = createFileRoute("/welcome")({ |
| 15 | beforeLoad: ({ context }) => { |
| 16 | const { authGateState } = context; |
| 17 | if (authGateState.status !== "authenticated" && authGateState.status !== "hosted-static") { |
| 18 | throw redirect({ to: "/pair", replace: true }); |
| 19 | } |
| 20 | }, |
| 21 | component: WelcomeRouteView, |
| 22 | }); |
| 23 | |
| 24 | function WelcomeRouteView() { |
| 25 | const navigate = useNavigate(); |
| 26 | const hash = useLocation({ select: (location) => location.hash }); |
| 27 | // A ChatGPT sign-in returns here with the computer it set up. |
| 28 | const resumeEnvironmentId = hash.startsWith("agents:") |
| 29 | ? Option.getOrUndefined(decodeEnvironmentId(hash.slice("agents:".length))) |
| 30 | : undefined; |
| 31 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 32 | const { environments, isReady } = useEnvironments(); |
| 33 | // Trust the id only once the catalog has it; an unknown computer falls back to this one. |
| 34 | const waitingForCatalog = |
| 35 | !isReady && (resumeEnvironmentId !== undefined || primaryEnvironmentId === null); |
| 36 | const resumeKnown = |
| 37 | resumeEnvironmentId !== undefined && |
| 38 | environments.some((environment) => environment.environmentId === resumeEnvironmentId); |
| 39 | const environmentId = (resumeKnown ? resumeEnvironmentId : undefined) ?? primaryEnvironmentId; |
| 40 | // The root can remount this pending outlet after navigation; do not reopen setup. |
| 41 | const isWelcomeRoute = useLocation({ select: (location) => location.pathname === "/welcome" }); |
| 42 | const [dismissed, setDismissed] = useState(false); |
| 43 | const openNewThread = useNewThreadHandler(); |
| 44 | if (waitingForCatalog) return null; |
| 45 | if (environmentId === null || !isWelcomeRoute || dismissed) return <NoProjectsHero />; |
| 46 | return ( |
| 47 | <WelcomePage |
| 48 | key={environmentId} |
| 49 | environmentId={environmentId} |
| 50 | onDone={async (projectRef) => { |
| 51 | setDismissed(true); |
| 52 | if (projectRef !== undefined) { |
| 53 | await openNewThread(projectRef, { replace: true }).catch(() => |
| 54 | navigate({ to: "/", replace: true }), |
| 55 | ); |
| 56 | return; |
| 57 | } |
| 58 | await navigate({ to: "/", replace: true }); |
| 59 | }} |
| 60 | /> |
| 61 | ); |
| 62 | } |