Skip to content
TypeScript

fix(web): leaving the home page while the app loads no longer bounces you to a new draft (#18672)

shivam346247bf1a46
diff --git a/apps/web/src/routes/_chat.index.tsx b/apps/web/src/routes/_chat.index.tsx
index 61e9e7a2e3a..b1f4343b385 100644
--- a/apps/web/src/routes/_chat.index.tsx
+++ b/apps/web/src/routes/_chat.index.tsx
@@ -1,8 +1,8 @@
import { RefreshIcon } from "~/components/ui/refresh-icon";
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
-import { createFileRoute, Link } from "@tanstack/react-router";
+import { createFileRoute, Link, useRouter } from "@tanstack/react-router";
import { LinkIcon, PlusIcon } from "lucide-react";
-import { useEffect, useMemo, useRef, useState } from "react";
+import { useEffect, useEffectEvent, useMemo, useRef, useState } from "react";
import { isLocalEnvironmentDisabled } from "../localEnvironment";
import { isElectron } from "../env";
@@ -44,6 +44,10 @@ function IndexDraftLanding() {
const threads = useThreadShells();
const bootstrapped = useAllEnvironmentShellsBootstrapped();
const handleNewThread = useNewThreadHandler();
+ const router = useRouter();
+ // Read at effect time: this page stays mounted while a navigation away from
+ // "/" is still loading, but the router already points at the new location.
+ const isStillOnIndex = useEffectEvent(() => router.state.location.pathname === "/");
const startingRef = useRef(false);
const [startState, setStartState] = useState({ failed: false, retryRequest: 0 });
@@ -56,7 +60,8 @@ function IndexDraftLanding() {
);
useEffect(() => {
- if (mostRecentProject === null || startingRef.current) {
+ // Starting a draft after the user has left would replace the page they picked.
+ if (mostRecentProject === null || startingRef.current || !isStillOnIndex()) {
return;
}
startingRef.current = true;