Skip to content

Commit 3faeb9e

Browse files
t3dotggclaude
andauthored
feat(web): Settings and Usage open as large modals over your thread (pingdotgg#24847)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 375da0f commit 3faeb9e

33 files changed

Lines changed: 593 additions & 465 deletions
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
2+
import { XIcon } from "lucide-react";
3+
import { useRef, useState, type ReactNode } from "react";
4+
5+
import { useCloseAppModal } from "./sidebar/mainAppLocation";
6+
import { Button } from "./ui/button";
7+
import { DialogBackdrop } from "./ui/dialog";
8+
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
9+
10+
/**
11+
* Large modal for the Settings and Usage routes. Esc, the close button, or a press on the
12+
* backdrop returns to the page that opened it. Keyboard guards that skip dialogs exempt
13+
* `[data-app-modal]`, because these pages own their shortcuts.
14+
*/
15+
export function AppRouteModal({ label, children }: { label: string; children: ReactNode }) {
16+
const closeAppModal = useCloseAppModal();
17+
const [open, setOpen] = useState(true);
18+
// Text selection that ends on the backdrop is not a dismissal.
19+
const backdropPressRef = useRef(false);
20+
// Focus the surface, not its first control: no stray focus ring, no phone keyboard.
21+
// On close, leave focus alone; returning it to the opener paints a focus ring.
22+
const popupRef = useRef<HTMLDivElement>(null);
23+
24+
return (
25+
<DialogPrimitive.Root
26+
open={open}
27+
// Portaled layers outside this tree (toasts, Clerk, confirm dialogs) are not backdrop presses.
28+
disablePointerDismissal
29+
onOpenChange={(nextOpen) => {
30+
if (!nextOpen) setOpen(false);
31+
}}
32+
onOpenChangeComplete={(nextOpen) => {
33+
if (!nextOpen) void closeAppModal();
34+
}}
35+
>
36+
<DialogPrimitive.Portal>
37+
<DialogBackdrop />
38+
<DialogPrimitive.Viewport
39+
className="fixed inset-0 z-50 flex items-center justify-center sm:p-6 lg:p-10"
40+
onPointerDown={(event) => {
41+
backdropPressRef.current = event.target === event.currentTarget;
42+
}}
43+
onClick={(event) => {
44+
if (backdropPressRef.current && event.target === event.currentTarget) setOpen(false);
45+
backdropPressRef.current = false;
46+
}}
47+
>
48+
<DialogPrimitive.Popup
49+
ref={popupRef}
50+
initialFocus={popupRef}
51+
finalFocus={false}
52+
aria-label={label}
53+
data-slot="dialog-popup"
54+
data-app-modal=""
55+
className="relative flex size-full min-h-0 min-w-0 flex-col overflow-hidden bg-background max-sm:pt-safe max-sm:pb-safe text-foreground outline-none transition-[opacity,scale] duration-150 ease-out [-webkit-app-region:no-drag] data-ending-style:scale-[0.985] data-ending-style:opacity-0 data-starting-style:scale-[0.985] data-starting-style:opacity-0 sm:max-h-[60rem] sm:max-w-[80rem] sm:rounded-2xl sm:border sm:shadow-2xl/40"
56+
>
57+
{children}
58+
</DialogPrimitive.Popup>
59+
</DialogPrimitive.Viewport>
60+
</DialogPrimitive.Portal>
61+
</DialogPrimitive.Root>
62+
);
63+
}
64+
65+
export function AppRouteModalClose() {
66+
return (
67+
<Tooltip>
68+
<TooltipTrigger
69+
render={
70+
<DialogPrimitive.Close
71+
aria-label="Close"
72+
render={<Button size="icon-sm" variant="ghost" />}
73+
/>
74+
}
75+
>
76+
<XIcon />
77+
</TooltipTrigger>
78+
<TooltipPopup side="bottom">Close (Esc)</TooltipPopup>
79+
</Tooltip>
80+
);
81+
}

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,10 @@ import { BackgroundThreadAnnouncer } from "./BackgroundThreadAnnouncer";
4141
import LegacyThreadSidebar from "./LegacySidebar";
4242
import { useThreadVisitedMigration } from "../hooks/useThreadVisitedMigration";
4343
import ThreadSidebar from "./Sidebar";
44-
import { SettingsSidebarNav } from "./settings/SettingsSidebarNav";
45-
import { SidebarBrandWidthProbe, SidebarChromeHeader } from "./sidebar/SidebarChrome";
46-
import { MainAppLocationTracker } from "./sidebar/mainAppLocation";
44+
import { SidebarBrandWidthProbe } from "./sidebar/SidebarChrome";
45+
import { isAppModalPage, MainAppLocationTracker } from "./sidebar/mainAppLocation";
4746
import type { SidebarRouteReveal } from "./sidebar/routeReveal";
4847
import { useSidebarStageBackdropVariant } from "./SidebarStageBackdrop";
49-
import { useProjects } from "../state/entities";
5048
import {
5149
clampThreadSidebarWidth,
5250
resolveInitialThreadSidebarWidth,
@@ -261,8 +259,8 @@ export function NavigationHistoryShortcuts() {
261259

262260
const MODIFIER_KEYS = new Set(["Alt", "Control", "Meta", "Shift"]);
263261

264-
// Undoes the thread actions in the sidebar's notice. Mounted wherever the
265-
// thread sidebar can show; Settings replaces it. Listens in the capture phase
262+
// Undoes the thread actions in the sidebar's notice. Settings and Usage own
263+
// mod+z while open. Listens in the capture phase
266264
// so mod+z reaches it before the composer's own undo handler consumes the key.
267265
function ThreadUndoShortcut() {
268266
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
@@ -320,14 +318,6 @@ function ThreadUndoShortcut() {
320318
return null;
321319
}
322320

323-
// Settings swaps the thread sidebar out of the tree. Keep the lightweight
324-
// project projection subscribed so returning to a draft never renders the
325-
// zero-project state while the environment snapshot reconnects.
326-
function ProjectProjectionRetention() {
327-
useProjects();
328-
return null;
329-
}
330-
331321
export function AppSidebarLayout({ children }: { children: ReactNode }) {
332322
const navigate = useNavigate();
333323
const legacySidebarEnabled = useLegacySidebarEnabled();
@@ -341,7 +331,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
341331
const pathname = useLocation({ select: (location) => location.pathname });
342332
const panelAnimationsSuppressed = usePanelNavigationSuppression(pathname);
343333
const routePanelAnimationsActive = panelAnimationsActive && !panelAnimationsSuppressed;
344-
const isOnSettings = pathname === "/settings" || pathname.startsWith("/settings/");
334+
const appModalOpen = isAppModalPage(pathname);
345335
const isMacosDesktop = isElectron && isMacPlatform(navigator.platform);
346336
const [sidebarWidth, setSidebarWidth] = useState(readInitialThreadSidebarWidth);
347337
// A clamped drag may not change width, so viewport changes need a separate subscription.
@@ -401,13 +391,12 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
401391
style={sidebarProviderStyle}
402392
>
403393
<SidebarBrandWidthProbe onWidthChange={setBrandWidth} />
404-
<ProjectProjectionRetention />
405394
<Sidebar
406395
side="left"
407396
collapsible="offcanvas"
408397
data-app-sidebar=""
409398
role="navigation"
410-
aria-label={isOnSettings ? "Settings" : "Threads"}
399+
aria-label="Threads"
411400
resizable={{
412401
maxWidth: sidebarMaximumWidth,
413402
minWidth: sidebarMinimumWidth,
@@ -419,12 +408,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
419408
}}
420409
style={windowControlsInsetStyle}
421410
>
422-
{isOnSettings ? (
423-
<>
424-
<SidebarChromeHeader isElectron={isElectron} />
425-
<SettingsSidebarNav pathname={pathname} />
426-
</>
427-
) : legacySidebarEnabled ? (
411+
{legacySidebarEnabled ? (
428412
<LegacyThreadSidebar routeReveal={sidebarRouteReveal} />
429413
) : (
430414
<ThreadSidebar routeReveal={sidebarRouteReveal} />
@@ -435,7 +419,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
435419
<BackgroundThreadAnnouncer />
436420
<SidebarControl />
437421
<NavigationHistoryShortcuts />
438-
{isOnSettings ? null : <ThreadUndoShortcut />}
422+
{appModalOpen ? null : <ThreadUndoShortcut />}
439423
<MainAppLocationTracker />
440424
</SidebarProvider>
441425
</PanelAnimationSuppressionProvider>

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
105105
import { resolveThreadReferenceCopyTarget } from "@t3tools/client-runtime/thread-reference";
106106
import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels";
107107
import { useAtomValue } from "@effect/atom-react";
108-
import { type SetStateAction, useEffect, useLayoutEffect, useRef, useState } from "react";
108+
import { type SetStateAction, use, useEffect, useLayoutEffect, useRef, useState } from "react";
109+
import { BackgroundThreadContext } from "./chat/backgroundThread";
109110
import { useLocation, useNavigate } from "@tanstack/react-router";
110111
import { assistantCitationFromLocation } from "../lib/assistantCitationNavigation";
111112
import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelection";
@@ -1590,6 +1591,7 @@ export default function ChatView(props: ChatViewProps) {
15901591
resourceKey: `git-status:${activeThreadKey ?? ""}:${gitStatusCwd ?? ""}`,
15911592
});
15921593
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
1594+
const backgroundThread = use(BackgroundThreadContext);
15931595
const scriptKeybindings =
15941596
useAtomValue(serverEnvironment.configValueAtom(environmentId))?.keybindings ??
15951597
DEFAULT_RESOLVED_KEYBINDINGS;
@@ -1602,7 +1604,7 @@ export default function ChatView(props: ChatViewProps) {
16021604
remoteOpenMode: remoteOpenState.mode,
16031605
});
16041606
useOpenFavoriteEditorShortcut({
1605-
enabled: showOpenInPicker,
1607+
enabled: showOpenInPicker && !backgroundThread,
16061608
environmentId: activeThread?.environmentId ?? environmentId,
16071609
keybindings,
16081610
availableEditors,
@@ -2683,7 +2685,7 @@ export default function ChatView(props: ChatViewProps) {
26832685
const closeThreadFind = () => threadFindControlsRef.current?.close();
26842686

26852687
useChatShortcuts({
2686-
enabled: activeThreadId !== null,
2688+
enabled: activeThreadId !== null && !backgroundThread,
26872689
keybindings,
26882690
scriptKeybindings,
26892691
getShortcutContext,

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

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useNavigate } from "@tanstack/react-router";
44
import { useEffect, useState } from "react";
55

66
import ChatView from "./ChatView";
7+
import { BackgroundThreadContext } from "./chat/backgroundThread";
78
import { resolveDraftPromotionNavigationTarget, threadHasStarted } from "./ChatView.logic";
89
import { waitForDraftHeroTransition } from "./chat/draftHeroTransition";
910
import { SidebarInset } from "./ui/sidebar";
@@ -24,8 +25,17 @@ import {
2425
type ThreadRouteTarget,
2526
} from "../threadRoutes";
2627

27-
/** Render in the shared layout to retain ChatView across route swaps; plain server threads remain unkeyed. */
28-
export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
28+
/**
29+
* Render in the shared layout to retain ChatView across route swaps; plain server threads remain unkeyed.
30+
* `background` keeps the thread mounted, inert and in place under Settings or Usage.
31+
*/
32+
export function ThreadRouteView({
33+
target,
34+
background = false,
35+
}: {
36+
target: ThreadRouteTarget;
37+
background?: boolean;
38+
}) {
2939
const navigate = useNavigate();
3040
const draftId = target.kind === "draft" ? target.draftId : null;
3141
const draftSession = useComposerDraftStore((store) =>
@@ -111,7 +121,7 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
111121
}, [draftSession?.promotedTo, inferredThreadRef]);
112122

113123
useEffect(() => {
114-
if (!canonicalThreadRef) {
124+
if (!canonicalThreadRef || background) {
115125
return;
116126
}
117127
let cancelled = false;
@@ -128,14 +138,14 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
128138
return () => {
129139
cancelled = true;
130140
};
131-
}, [canonicalThreadRef, navigate]);
141+
}, [background, canonicalThreadRef, navigate]);
132142

133143
useEffect(() => {
134-
if (target.kind !== "draft" || draftSession || canonicalThreadRef) {
144+
if (background || target.kind !== "draft" || draftSession || canonicalThreadRef) {
135145
return;
136146
}
137147
void navigate({ to: "/", replace: true });
138-
}, [canonicalThreadRef, draftSession, navigate, target.kind]);
148+
}, [background, canonicalThreadRef, draftSession, navigate, target.kind]);
139149

140150
useEffect(() => {
141151
if (target.kind !== "server" || !bootstrapComplete) {
@@ -144,11 +154,11 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
144154
if (renderState === "missing") {
145155
const { clearPendingFileDropsForThread } = useSidebarPendingFileDropStore.getState();
146156
clearPendingFileDropsForThread(target.threadRef);
147-
if (environmentHasAnyThreads) {
157+
if (environmentHasAnyThreads && !background) {
148158
void navigate({ to: "/", replace: true });
149159
}
150160
}
151-
}, [bootstrapComplete, environmentHasAnyThreads, navigate, renderState, target]);
161+
}, [background, bootstrapComplete, environmentHasAnyThreads, navigate, renderState, target]);
152162

153163
useEffect(() => {
154164
if (target.kind !== "server" || !serverThreadStarted || !draftThread) {
@@ -183,8 +193,11 @@ export function ThreadRouteView({ target }: { target: ThreadRouteTarget }) {
183193
}
184194

185195
return (
186-
<SidebarInset className="h-svh min-h-0 overflow-hidden overscroll-y-none md:h-dvh">
187-
{view}
196+
<SidebarInset
197+
className="h-svh min-h-0 overflow-hidden overscroll-y-none md:h-dvh"
198+
inert={background}
199+
>
200+
<BackgroundThreadContext value={background}>{view}</BackgroundThreadContext>
188201
</SidebarInset>
189202
);
190203
}

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { type RuntimeRequestId, type ScopedThreadRef } from "@t3tools/contracts";
2-
import { memo, useEffect, useEffectEvent, useRef, useState } from "react";
2+
import { memo, use, useEffect, useEffectEvent, useRef, useState } from "react";
3+
import { BackgroundThreadContext } from "./backgroundThread";
34
import { useStableCallback } from "~/hooks/useStableCallback";
45
import { type PendingUserInput } from "../../session-logic";
56
import {
@@ -142,9 +143,10 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
142143
};
143144

144145
const selectOptionFromShortcut = useEffectEvent(handleOptionSelection);
146+
const backgroundThread = use(BackgroundThreadContext);
145147

146148
useEffect(() => {
147-
if (!activeQuestion || responseDisabled || isCollapsed) return;
149+
if (!activeQuestion || responseDisabled || isCollapsed || backgroundThread) return;
148150
const handler = (event: globalThis.KeyboardEvent) => {
149151
if (event.metaKey || event.ctrlKey || event.altKey) return;
150152
const target = event.target;
@@ -168,7 +170,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
168170
};
169171
document.addEventListener("keydown", handler);
170172
return () => document.removeEventListener("keydown", handler);
171-
}, [activeQuestion, isCollapsed, responseDisabled]);
173+
}, [activeQuestion, backgroundThread, isCollapsed, responseDisabled]);
172174

173175
if (!activeQuestion) {
174176
return null;

‎apps/web/src/components/chat/OpenInPicker.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
type EnvironmentId,
77
type ResolvedKeybindingsConfig,
88
} from "@t3tools/contracts";
9-
import { memo, useEffect, useRef } from "react";
9+
import { memo, use, useEffect, useRef } from "react";
10+
import { BackgroundThreadContext } from "./backgroundThread";
1011
import { isOpenFavoriteEditorShortcut, shortcutLabelForCommand } from "../../keybindings";
1112
import { usePreferredEditor } from "../../editorPreferences";
1213
import { editorLabelForPlatform } from "../../editorLabels";
@@ -279,8 +280,9 @@ export const OpenInPicker = memo(function OpenInPicker({
279280
"editor.openFavorite",
280281
);
281282

283+
const backgroundThread = use(BackgroundThreadContext);
282284
useEffect(() => {
283-
if (!enableShortcut || !canOpenEditor) return;
285+
if (!enableShortcut || !canOpenEditor || backgroundThread) return;
284286
const handler = (e: globalThis.KeyboardEvent) => {
285287
if (e.defaultPrevented || !isOpenFavoriteEditorShortcut(e, keybindings)) return;
286288
if (!openInCwd) return;
@@ -300,6 +302,7 @@ export const OpenInPicker = memo(function OpenInPicker({
300302
window.addEventListener("keydown", handler, true);
301303
return () => window.removeEventListener("keydown", handler, true);
302304
}, [
305+
backgroundThread,
303306
canOpenEditor,
304307
enableShortcut,
305308
environmentId,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createContext } from "react";
2+
3+
/** True for a thread kept mounted under Settings or Usage; its global shortcuts stand down. */
4+
export const BackgroundThreadContext = createContext(false);

‎apps/web/src/components/chat/useComposerStash.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ import {
5454
type Dispatch,
5555
type RefObject,
5656
type SetStateAction,
57+
use,
5758
useEffect,
5859
useRef,
5960
useState,
6061
} from "react";
62+
import { BackgroundThreadContext } from "./backgroundThread";
6163
import { useStableCallback } from "../../hooks/useStableCallback";
6264

6365
/**
@@ -691,7 +693,9 @@ export function useComposerStash(input: {
691693
if (stashMenuClosesOn.prompt !== prompt || composerMenuOpen) setIsStashMenuOpen(false);
692694
}
693695

696+
const backgroundThread = use(BackgroundThreadContext);
694697
useEffect(() => {
698+
if (backgroundThread) return;
695699
const handler = (event: globalThis.KeyboardEvent) => {
696700
const command = resolveShortcutCommand(event, keybindings, {
697701
context: {
@@ -721,6 +725,7 @@ export function useComposerStash(input: {
721725
return () => window.removeEventListener("keydown", handler, true);
722726
}, [
723727
activePendingProgress,
728+
backgroundThread,
724729
isComposerModelPickerOpen,
725730
keybindings,
726731
pendingUserInputs.length,

0 commit comments

Comments
 (0)