Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

welcome.tsx

57 lines2.2 KB

1import { createFileRoute, redirect, useLocation, useNavigate } from "@tanstack/react-router";
2import { useState } from "react";
3import { EnvironmentId } from "@t3tools/contracts";
4import * as Schema from "effect/Schema";
5import * as Option from "effect/Option";
6​
7import { NoProjectsHero } from "../components/NoProjectsHero";
8import { WelcomeWizard } from "../components/onboarding/WelcomeWizard";
9import { useNewThreadHandler } from "../hooks/useHandleNewThread";
10​
11const decodeEnvironmentId = Schema.decodeOption(EnvironmentId);
12​
13export const Route = createFileRoute("/welcome")({
14 beforeLoad: ({ context }) => {
15 const { authGateState } = context;
16 if (authGateState.status !== "authenticated" && authGateState.status !== "hosted-static") {
17 throw redirect({ to: "/pair", replace: true });
18 }
19 },
20 component: WelcomeRouteView,
21});
22​
23function WelcomeRouteView() {
24 const { authGateState } = Route.useRouteContext();
25 const navigate = useNavigate();
26 const hash = useLocation({ select: (location) => location.hash });
27 const resumeEnvironmentId = hash.startsWith("agents:")
28 ? Option.getOrUndefined(decodeEnvironmentId(hash.slice("agents:".length)))
29 : undefined;
30 // The root shell can remount this pending outlet after the location changes.
31 // Never reopen setup while the destination route is still loading.
32 const isWelcomeRoute = useLocation({ select: (location) => location.pathname === "/welcome" });
33 const [dismissed, setDismissed] = useState(false);
34 const openNewThread = useNewThreadHandler();
35 const localAvailable = authGateState.status === "authenticated";
36 return (
37 <>
38 <NoProjectsHero />
39 {isWelcomeRoute && !dismissed ? (
40 <WelcomeWizard
41 localAvailable={localAvailable}
42 resumeEnvironmentId={resumeEnvironmentId}
43 onDone={async (projectRef) => {
44 setDismissed(true);
45 if (projectRef !== undefined) {
46 await openNewThread(projectRef, { replace: true }).catch(() =>
47 navigate({ to: "/", replace: true }),
48 );
49 return;
50 }
51 await navigate({ to: "/", replace: true });
52 }}
53 />
54 ) : null}
55 </>
56 );