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

welcome.tsx

56 lines2.1 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 can remount this pending outlet after navigation; do not reopen setup.
31 const isWelcomeRoute = useLocation({ select: (location) => location.pathname === "/welcome" });
32 const [dismissed, setDismissed] = useState(false);
33 const openNewThread = useNewThreadHandler();
34 const localAvailable = authGateState.status === "authenticated";
35 return (
36 <>
37 <NoProjectsHero />
38 {isWelcomeRoute && !dismissed ? (
39 <WelcomeWizard
40 localAvailable={localAvailable}
41 resumeEnvironmentId={resumeEnvironmentId}
42 onDone={async (projectRef) => {
43 setDismissed(true);
44 if (projectRef !== undefined) {
45 await openNewThread(projectRef, { replace: true }).catch(() =>
46 navigate({ to: "/", replace: true }),
47 );
48 return;
49 }
50 await navigate({ to: "/", replace: true });
51 }}
52 />
53 ) : null}
54 </>
55 );