Skip to content

Commit de5c0ff

Browse files
committed
Merge pull request 'fix(web): a thread link to an unknown environment says so instead of a blank pane' (pingdotgg#26) from proposal/task-t3-thread-links-must-open-ev-1017-2120 into yu-v2
2 parents 8c55aa7 + eb90bd9 commit de5c0ff

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎apps/web/src/components/ThreadRouteView.tsx‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
22
import type { ScopedThreadRef } from "@t3tools/contracts";
3+
import { useAtomValue } from "@effect/atom-react";
34
import { useNavigate } from "@tanstack/react-router";
45
import { useEffect, useState } from "react";
56

67
import ChatView from "./ChatView";
78
import { resolveDraftPromotionNavigationTarget, threadHasStarted } from "./ChatView.logic";
89
import { waitForDraftHeroTransition } from "./chat/draftHeroTransition";
910
import { SidebarInset } from "./ui/sidebar";
11+
import { toastManager } from "./ui/toast";
1012
import {
1113
finalizePromotedDraftThreadByRef,
1214
markPromotedDraftThreadByRef,
1315
useBackgroundDraftSubmissionPending,
1416
useComposerDraftStore,
1517
} from "../composerDraftStore";
18+
import { environmentCatalog } from "../connection/catalog";
1619
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
1720
import { useEnvironmentThreadRefs, useThreadRefs, useThreadShell } from "../state/entities";
1821
import { 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

Comments
 (0)