11import { scopedThreadKey } from "@t3tools/client-runtime/environment" ;
22import type { ScopedThreadRef } from "@t3tools/contracts" ;
3+ import { useAtomValue } from "@effect/atom-react" ;
34import { useNavigate } from "@tanstack/react-router" ;
45import { useEffect , useState } from "react" ;
56
67import ChatView from "./ChatView" ;
78import { resolveDraftPromotionNavigationTarget , threadHasStarted } from "./ChatView.logic" ;
89import { waitForDraftHeroTransition } from "./chat/draftHeroTransition" ;
910import { SidebarInset } from "./ui/sidebar" ;
11+ import { toastManager } from "./ui/toast" ;
1012import {
1113 finalizePromotedDraftThreadByRef ,
1214 markPromotedDraftThreadByRef ,
1315 useBackgroundDraftSubmissionPending ,
1416 useComposerDraftStore ,
1517} from "../composerDraftStore" ;
18+ import { environmentCatalog } from "../connection/catalog" ;
1619import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore" ;
1720import { useEnvironmentThreadRefs , useThreadRefs , useThreadShell } from "../state/entities" ;
1821import { useEnvironmentQuery } from "../state/query" ;
@@ -23,6 +26,8 @@ import {
2326 type ThreadRouteTarget ,
2427} from "../threadRoutes" ;
2528
29+ const UNKNOWN_ENVIRONMENT_GRACE_MS = 15_000 ;
30+
2631/**
2732 * The single chat surface behind both `/draft/$draftId` and
2833 * `/$environmentId/$threadId`. Each draft gets its own ChatView instance (so
@@ -71,6 +76,7 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
7176 serverThreadRef === null ? null : environmentShell . stateAtom ( serverThreadRef . environmentId ) ,
7277 ) ;
7378 const serverThreadShell = serverThread ;
79+ const catalog = useAtomValue ( environmentCatalog . catalogValueAtom ) ;
7480 const environmentThreadRefs = useEnvironmentThreadRefs ( serverThreadRef ?. environmentId ?? null ) ;
7581 const bootstrapComplete = shell . data ?. snapshot . _tag === "Some" ;
7682 const draftThread = useComposerDraftStore ( ( store ) =>
@@ -141,6 +147,29 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
141147 void navigate ( { to : "/" , replace : true } ) ;
142148 } , [ canonicalThreadRef , draftSession , navigate , target . kind ] ) ;
143149
150+ // A thread link (`t3code://threads/...`) can name an environment this client does not know,
151+ // which would otherwise sit on a blank pane forever. Say so and go home. Host-managed and
152+ // relay environments register after the saved ones, so a cold start gets a grace period
153+ // before an unknown environment counts as missing (the primary one is discovered by a 3 s poll). Drafts never reach this.
154+ const unknownEnvironmentId =
155+ target . kind === "server" &&
156+ catalog . isReady &&
157+ ! catalog . entries . has ( target . threadRef . environmentId )
158+ ? target . threadRef . environmentId
159+ : null ;
160+ useEffect ( ( ) => {
161+ if ( unknownEnvironmentId === null ) return ;
162+ const timer = setTimeout ( ( ) => {
163+ toastManager . add ( {
164+ type : "warning" ,
165+ title : "Thread not found" ,
166+ description : "That environment is not connected to this app." ,
167+ } ) ;
168+ void navigate ( { to : "/" , replace : true } ) ;
169+ } , UNKNOWN_ENVIRONMENT_GRACE_MS ) ;
170+ return ( ) => clearTimeout ( timer ) ;
171+ } , [ navigate , unknownEnvironmentId ] ) ;
172+
144173 useEffect ( ( ) => {
145174 if ( target . kind !== "server" || ! bootstrapComplete ) {
146175 return ;
0 commit comments