Skip to content

Commit d9a97e7

Browse files
Mnigosmaria-rcksjuliusmarminge
authored
fix(web): sidebar keeps the open thread active while Settings or Usage is open (pingdotgg#28703)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 4f555c7 commit d9a97e7

7 files changed

Lines changed: 157 additions & 26 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ import {
257257
useComposerDraftStore,
258258
usePromotingDraftThreadKeys,
259259
} from "../composerDraftStore";
260-
import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
260+
import { useShownThreadRouteTarget } from "../hooks/useShownThreadRouteTarget";
261261
import {
262262
SidebarSearchResultRow,
263263
SortableThreadRow,
@@ -1006,10 +1006,14 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
10061006
const toggleThreadSelection = useThreadSelectionStore((s) => s.toggleThread);
10071007
const rangeSelectTo = useThreadSelectionStore((s) => s.rangeSelectTo);
10081008
const acknowledgeWoke = useAcknowledgeThreadWoke();
1009-
const routeTarget = useThreadRouteTarget();
1009+
// A Settings or Usage modal keeps the thread behind it current.
1010+
const routeTarget = useShownThreadRouteTarget().shownTarget;
10101011
const routeDraftThread = useComposerDraftStore((store) =>
10111012
routeTarget?.kind === "draft" ? store.getDraftSession(routeTarget.draftId) : null,
10121013
);
1014+
const routeDraftRedirect = useComposerDraftStore((store) =>
1015+
routeTarget?.kind === "draft" ? store.getPromotedDraftRedirect(routeTarget.draftId) : null,
1016+
);
10131017
const environmentMachineById = useEnvironmentMachines();
10141018
const projectGroupsRef = useRef(projectGroups);
10151019
const persistedProjectScopeKeys = useUiStateStore((store) => store.sidebarProjectScopeKeys);
@@ -1070,7 +1074,11 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
10701074
const snoozingThreadKeysRef = useRef(new Set<string>());
10711075
const removeFromSelection = useThreadSelectionStore((s) => s.removeFromSelection);
10721076
const openAddProjectCommandPalette = () => openCommandPalette({ open: "add-project" });
1073-
const routeThreadRef = resolveActiveThreadRouteRef(routeTarget, routeDraftThread);
1077+
const routeThreadRef = resolveActiveThreadRouteRef(
1078+
routeTarget,
1079+
routeDraftThread,
1080+
routeDraftRedirect,
1081+
);
10741082
const routeThreadKey = routeThreadRef ? scopedThreadKey(routeThreadRef) : null;
10751083

10761084
// A selection made under one scope can hold threads the next scope hides.

‎apps/web/src/components/sidebar/mainAppLocation.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useNavigate, type AnyRouter } from "@tanstack/react-router";
22

33
import { GUEST_PAGE_PATH } from "../../guestPage";
44
import { withoutAssistantCitationHash } from "../../lib/assistantCitationNavigation";
5+
import { resolveThreadRouteTarget, type ThreadRouteTarget } from "../../threadRoutes";
56

67
/** Settings and Usage open as modals over the last main-app view. */
78
export function isAppModalPage(pathname: string) {
@@ -19,6 +20,7 @@ const SHELL_LESS_PATHS = new Set(["/pair", "/connect", "/connect-agent", GUEST_P
1920

2021
let mainAppHref: string | null = null;
2122
let appModalReturnHref: string | null = null;
23+
let appModalReturnThreadTarget: ThreadRouteTarget | null = null;
2224

2325
/** Remembers where Settings, Usage, and the utility pages lead back to. */
2426
export function startMainAppLocationTracking(router: AnyRouter): () => void {
@@ -27,12 +29,20 @@ export function startMainAppLocationTracking(router: AnyRouter): () => void {
2729
if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
2830
const href = withoutAssistantCitationHash(router.state.location.href);
2931
appModalReturnHref = href;
32+
appModalReturnThreadTarget = resolveThreadRouteTarget(
33+
router.state.matches.at(-1)?.params ?? {},
34+
);
3035
if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
3136
};
3237
record();
3338
return router.subscribe("onResolved", record);
3439
}
3540

41+
/** The thread Settings or Usage opened over, for views mounted while the modal is open. */
42+
export function getAppModalReturnThreadTarget() {
43+
return appModalReturnThreadTarget;
44+
}
45+
3646
export function useNavigateToMainApp() {
3747
const navigate = useNavigate();
3848
return () => navigate({ href: mainAppHref ?? "/" });
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import {
2+
createMemoryHistory,
3+
createRootRoute,
4+
createRoute,
5+
createRouter,
6+
Outlet,
7+
RouterProvider,
8+
} from "@tanstack/react-router";
9+
import { describe, expect, it } from "vite-plus/test";
10+
11+
import { act, render } from "../../test/render";
12+
import { startMainAppLocationTracking } from "../components/sidebar/mainAppLocation";
13+
import { useShownThreadRouteTarget } from "./useShownThreadRouteTarget";
14+
15+
function ShownThread() {
16+
const { shownTarget } = useShownThreadRouteTarget();
17+
const label =
18+
shownTarget === null
19+
? "none"
20+
: shownTarget.kind === "server"
21+
? shownTarget.threadRef.threadId
22+
: shownTarget.draftId;
23+
return (
24+
<>
25+
<output>{label}</output>
26+
<Outlet />
27+
</>
28+
);
29+
}
30+
31+
function createAppRouter(initialPath: string) {
32+
const rootRoute = createRootRoute({ component: ShownThread });
33+
const threadRoute = createRoute({
34+
getParentRoute: () => rootRoute,
35+
path: "$environmentId/$threadId",
36+
});
37+
const settingsRoute = createRoute({ getParentRoute: () => rootRoute, path: "settings" });
38+
const settingsGeneralRoute = createRoute({
39+
getParentRoute: () => settingsRoute,
40+
path: "general",
41+
});
42+
const usageRoute = createRoute({ getParentRoute: () => rootRoute, path: "usage" });
43+
const newRoute = createRoute({ getParentRoute: () => rootRoute, path: "new" });
44+
return createRouter({
45+
routeTree: rootRoute.addChildren([
46+
threadRoute,
47+
settingsRoute.addChildren([settingsGeneralRoute]),
48+
usageRoute,
49+
newRoute,
50+
]),
51+
history: createMemoryHistory({ initialEntries: [initialPath] }),
52+
});
53+
}
54+
55+
describe("useShownThreadRouteTarget", () => {
56+
it("keeps the thread that opened Settings or Usage shown until the user leaves for another page", async () => {
57+
const router = createAppRouter("/env-1/thread-a");
58+
await router.load();
59+
// The hook reads thread params and modal paths from real routes, which the default router lacks.
60+
await using screen = await render(<RouterProvider router={router} />, { router: false });
61+
const shown = screen.getByRole("status");
62+
await expect.element(shown).toHaveTextContent("thread-a");
63+
64+
await act(() => router.navigate({ to: "/settings/general" }));
65+
await expect.element(shown).toHaveTextContent("thread-a");
66+
67+
await act(() => router.navigate({ to: "/usage" }));
68+
await expect.element(shown).toHaveTextContent("thread-a");
69+
70+
await act(() => router.navigate({ href: "/env-1/thread-b" }));
71+
await expect.element(shown).toHaveTextContent("thread-b");
72+
73+
await act(() => router.navigate({ to: "/new" }));
74+
await expect.element(shown).toHaveTextContent("none");
75+
76+
await act(() => router.navigate({ to: "/settings/general" }));
77+
await expect.element(shown).toHaveTextContent("none");
78+
});
79+
80+
it("shows the thread behind Settings when it mounts while Settings is already open", async () => {
81+
const router = createAppRouter("/env-1/thread-a");
82+
await router.load();
83+
using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
84+
await router.navigate({ to: "/settings/general" });
85+
86+
// The hook reads thread params and modal paths from real routes, which the default router lacks.
87+
await using screen = await render(<RouterProvider router={router} />, { router: false });
88+
89+
await expect.element(screen.getByRole("status")).toHaveTextContent("thread-a");
90+
});
91+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2+
import { useMatches } from "@tanstack/react-router";
3+
import { useState } from "react";
4+
5+
import {
6+
getAppModalReturnThreadTarget,
7+
isAppModalPage,
8+
} from "../components/sidebar/mainAppLocation";
9+
import type { ThreadRouteTarget } from "../threadRoutes";
10+
import { useThreadRouteTarget } from "./useThreadRouteTarget";
11+
12+
function threadRouteTargetKey(target: ThreadRouteTarget | null) {
13+
if (!target) return null;
14+
return target.kind === "server" ? scopedThreadKey(target.threadRef) : target.draftId;
15+
}
16+
17+
/** Settings and Usage render over the thread that opened them; that thread stays the shown one. */
18+
export function useShownThreadRouteTarget() {
19+
const routeTarget = useThreadRouteTarget();
20+
// Read matches, not location, so this agrees with the params useThreadRouteTarget reads.
21+
const modalOpen = useMatches({
22+
select: (matches) => matches.some((match) => isAppModalPage(match.pathname)),
23+
});
24+
const [backgroundTarget, setBackgroundTarget] = useState(() =>
25+
modalOpen ? getAppModalReturnThreadTarget() : routeTarget,
26+
);
27+
if (!modalOpen && threadRouteTargetKey(backgroundTarget) !== threadRouteTargetKey(routeTarget)) {
28+
setBackgroundTarget(routeTarget);
29+
}
30+
return { shownTarget: modalOpen ? backgroundTarget : routeTarget, modalOpen };
31+
}

‎apps/web/src/routes/_chat.tsx‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2-
import { Outlet, createFileRoute, redirect, useMatches } from "@tanstack/react-router";
3-
import { useState } from "react";
1+
import { Outlet, createFileRoute, redirect } from "@tanstack/react-router";
42

53
import { ThreadRouteView } from "../components/ThreadRouteView";
64
import { isAppModalPage } from "../components/sidebar/mainAppLocation";
75
import { SidebarInset } from "../components/ui/sidebar";
8-
import { resolveThreadRouteTarget, type ThreadRouteTarget } from "../threadRoutes";
6+
import { resolveThreadRouteTarget } from "../threadRoutes";
97
import { retainThreadFindForRoute } from "../components/chat/threadFindStore";
108
import { ThreadLinkPrompt } from "../components/promptLinks/ThreadLinkPrompt";
11-
import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
12-
13-
function threadRouteTargetKey(target: ThreadRouteTarget | null) {
14-
if (!target) return null;
15-
return target.kind === "server" ? scopedThreadKey(target.threadRef) : target.draftId;
16-
}
9+
import { useShownThreadRouteTarget } from "../hooks/useShownThreadRouteTarget";
1710

1811
function ChatRouteLayout() {
1912
// Render both routes here so draft promotion keeps the same ChatView mounted.
20-
const threadTarget = useThreadRouteTarget();
21-
// Read matches, not location, so this agrees with the params useThreadRouteTarget reads.
22-
const modalOpen = useMatches({
23-
select: (matches) => matches.some((match) => isAppModalPage(match.pathname)),
24-
});
25-
// Settings and Usage render over the thread that opened them, so ChatView never remounts.
26-
const [backgroundTarget, setBackgroundTarget] = useState(threadTarget);
27-
if (!modalOpen && threadRouteTargetKey(backgroundTarget) !== threadRouteTargetKey(threadTarget)) {
28-
setBackgroundTarget(threadTarget);
29-
}
30-
const shownTarget = modalOpen ? backgroundTarget : threadTarget;
13+
const { shownTarget, modalOpen } = useShownThreadRouteTarget();
3114
return (
3215
<>
3316
{shownTarget?.kind === "server" && !modalOpen ? (

‎apps/web/src/threadRoutes.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ describe("threadRoutes", () => {
8383
environmentId: "env-2",
8484
threadId: "server-thread",
8585
});
86+
expect(
87+
resolveActiveThreadRouteRef(
88+
target,
89+
null,
90+
scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
91+
),
92+
).toEqual({ environmentId: "env-2", threadId: "server-thread" });
8693
});
8794

8895
it("does not treat a draft's reserved thread ref as an active sidebar thread", () => {

‎apps/web/src/threadRoutes.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,13 @@ export function resolveThreadRouteTarget(
118118
export function resolveActiveThreadRouteRef(
119119
target: ThreadRouteTarget | null,
120120
draftThread: DraftThreadRouteState | null,
121+
promotedDraftRedirect: ScopedThreadRef | null = null,
121122
): ScopedThreadRef | null {
122123
if (target?.kind === "server") {
123124
return target.threadRef;
124125
}
125-
if (target?.kind !== "draft" || !draftThread?.promotedTo) {
126+
if (target?.kind !== "draft") {
126127
return null;
127128
}
128-
return draftThread.promotedTo;
129+
return draftThread?.promotedTo ?? promotedDraftRedirect;
129130
}

0 commit comments

Comments
 (0)