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

welcome.tsx

52 lines2.0 KB

1import { createFileRoute, redirect, useLocation, useNavigate } from "@tanstack/react-router";
2import { useState } from "react";
3​
4import { NoProjectsHero } from "../components/NoProjectsHero";
5import { WelcomeWizard } from "../components/onboarding/WelcomeWizard";
6import { useNewThreadHandler } from "../hooks/useHandleNewThread";
7​
8/** Onboarding overlays the workspace. Visiting /welcome reopens setup. */
9export const Route = createFileRoute("/welcome")({
10 beforeLoad: ({ context }) => {
11 const { authGateState } = context;
12 if (authGateState.status !== "authenticated" && authGateState.status !== "hosted-static") {
13 throw redirect({ to: "/pair", replace: true });
14 }
15 },
16 component: WelcomeRouteView,
17});
18​
19function WelcomeRouteView() {
20 const { authGateState } = Route.useRouteContext();
21 const navigate = useNavigate();
22 // The root shell can remount this pending outlet after the location changes.
23 // Never reopen setup while the destination route is still loading.
24 const isWelcomeRoute = useLocation({ select: (location) => location.pathname === "/welcome" });
25 const [dismissed, setDismissed] = useState(false);
26 const openNewThread = useNewThreadHandler();
27 // An authenticated gate means a primary server is serving this app —
28 // desktop, `npx t3`, or a dev server — and that server is "this machine"
29 // no matter what hostname the browser used. Only hosted-static has no
30 // local server to offer.
31 const localAvailable = authGateState.status === "authenticated";
32 return (
33 <>
34 <NoProjectsHero />
35 {isWelcomeRoute && !dismissed ? (
36 <WelcomeWizard
37 localAvailable={localAvailable}
38 onDone={(projectRef) => {
39 setDismissed(true);
40 if (projectRef !== undefined) {
41 void openNewThread(projectRef, { replace: true }).catch(() => {
42 void navigate({ to: "/", replace: true });
43 });
44 return;
45 }
46 void navigate({ to: "/", replace: true });
47 }}
48 />
49 ) : null}
50 </>
51 );