Skip to content

Commit 1ae5ee6

Browse files
bj97301claude
andcommitted
feat(web): add an all-projects state to the terminal drawer pin
The pin button now cycles off → this project → every project → off. An environment-wide pin points every thread in the environment at one drawer and shadows project pins until it is removed. Pins are stored under a pin key that names a project or an environment, so cleanup on thread deletion covers both. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 4fe6337 commit 1ae5ee6

10 files changed

Lines changed: 297 additions & 176 deletions

File tree

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

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,18 @@ import {
266266
import { appendPreviewAnnotationPrompt } from "../lib/previewAnnotation";
267267
import { appendReviewCommentsToPrompt, type ReviewCommentContext } from "../reviewCommentContext";
268268
import { environmentCatalog } from "../connection/catalog";
269+
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
269270
import {
270-
selectPinnedTerminalThreadKey,
271-
selectThreadTerminalUiState,
272-
useTerminalUiStateStore,
273-
} from "../terminalUiStateStore";
274-
import { useTerminalDrawerRef, useThreadProjectKey } from "../hooks/useTerminalDrawerRef";
271+
useTerminalDrawerPin,
272+
useTerminalDrawerRef,
273+
useThreadProjectKey,
274+
} from "../hooks/useTerminalDrawerRef";
275+
import {
276+
environmentTerminalPinKey,
277+
nextTerminalDrawerPinState,
278+
projectTerminalPinKey,
279+
type TerminalDrawerPinState,
280+
} from "../lib/terminalDrawer";
275281
import { useKnownTerminalSessions, useThreadRunningTerminalIds } from "../state/terminalSessions";
276282
import { projectEnvironment } from "../state/projects";
277283
import { useEnvironmentQuery } from "../state/query";
@@ -808,8 +814,8 @@ interface PersistentThreadTerminalDrawerProps {
808814
/** Thread being viewed; links open in its preview even when this is another thread's pinned drawer. */
809815
previewThreadRef: ScopedThreadRef;
810816
active: boolean;
811-
pinned: boolean;
812-
onTogglePinned: () => void;
817+
pinState: TerminalDrawerPinState;
818+
onCyclePin: () => void;
813819
launchContext: PersistentTerminalLaunchContext | null;
814820
focusRequestId: number;
815821
splitShortcutLabel: string | undefined;
@@ -825,8 +831,8 @@ const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDra
825831
threadId,
826832
previewThreadRef,
827833
active,
828-
pinned,
829-
onTogglePinned,
834+
pinState,
835+
onCyclePin,
830836
launchContext,
831837
focusRequestId,
832838
splitShortcutLabel,
@@ -1155,8 +1161,8 @@ const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDra
11551161
previewThreadRef={previewThreadRef}
11561162
threadId={threadId}
11571163
cwd={cwd}
1158-
pinned={pinned}
1159-
onTogglePinned={onTogglePinned}
1164+
pinState={pinState}
1165+
onCyclePin={onCyclePin}
11601166
worktreePath={effectiveWorktreePath}
11611167
runtimeEnv={runtimeEnv}
11621168
visible={visible}
@@ -1817,37 +1823,40 @@ export default function ChatView(props: ChatViewProps) {
18171823
const activeThreadKey = activeThreadRef ? scopedThreadKey(activeThreadRef) : null;
18181824
// The drawer follows the project's pin: the pinned thread's drawer when one
18191825
// is pinned, otherwise the thread's own. Right-panel terminals ignore pins.
1820-
const activeTerminalDrawerRef = useTerminalDrawerRef(activeThreadRef);
1826+
const { drawerRef: activeTerminalDrawerRef, pinState: terminalDrawerPinState } =
1827+
useTerminalDrawerPin(activeThreadRef);
18211828
const activeTerminalDrawerKey = activeTerminalDrawerRef
18221829
? scopedThreadKey(activeTerminalDrawerRef)
18231830
: null;
18241831
const terminalDrawerThreadId = activeTerminalDrawerRef?.threadId ?? null;
18251832
const activeThreadProjectKey = useThreadProjectKey(activeThreadRef);
1826-
const pinnedTerminalThreadKey = useTerminalUiStateStore((state) =>
1827-
selectPinnedTerminalThreadKey(
1828-
state.pinnedTerminalThreadKeyByProjectKey,
1829-
activeThreadProjectKey,
1830-
),
1831-
);
1832-
// A pin only counts once it resolved; a stale pin to a deleted thread reads as unpinned.
1833-
const terminalDrawerPinned =
1834-
pinnedTerminalThreadKey !== null && pinnedTerminalThreadKey === activeTerminalDrawerKey;
18351833
const pinTerminalDrawer = useTerminalUiStateStore((state) => state.pinTerminalDrawer);
18361834
const unpinTerminalDrawer = useTerminalUiStateStore((state) => state.unpinTerminalDrawer);
1837-
// Pinning keeps the drawer in front for every thread in the project;
1838-
// unpinning hands each thread its own drawer back.
1839-
const toggleTerminalDrawerPinned = useCallback(() => {
1840-
if (activeThreadProjectKey === null || !activeThreadRef) return;
1841-
if (terminalDrawerPinned) {
1842-
unpinTerminalDrawer(activeThreadProjectKey);
1843-
} else {
1844-
pinTerminalDrawer(activeThreadProjectKey, activeThreadRef);
1835+
// The pin button cycles off → this project → every project → off. Moving
1836+
// from project to every project drops the project pin, so unpinning later
1837+
// lands on off instead of falling back to the project pin.
1838+
const cycleTerminalDrawerPin = useCallback(() => {
1839+
if (!activeThreadRef || !activeTerminalDrawerRef || activeThreadProjectKey === null) return;
1840+
const environmentPinKey = environmentTerminalPinKey(activeThreadRef.environmentId);
1841+
const projectPinKey = projectTerminalPinKey(activeThreadProjectKey);
1842+
switch (nextTerminalDrawerPinState(terminalDrawerPinState)) {
1843+
case "project":
1844+
pinTerminalDrawer(projectPinKey, activeThreadRef);
1845+
return;
1846+
case "environment":
1847+
pinTerminalDrawer(environmentPinKey, activeTerminalDrawerRef);
1848+
unpinTerminalDrawer(projectPinKey);
1849+
return;
1850+
case "none":
1851+
unpinTerminalDrawer(environmentPinKey);
1852+
return;
18451853
}
18461854
}, [
1855+
activeTerminalDrawerRef,
18471856
activeThreadProjectKey,
18481857
activeThreadRef,
18491858
pinTerminalDrawer,
1850-
terminalDrawerPinned,
1859+
terminalDrawerPinState,
18511860
unpinTerminalDrawer,
18521861
]);
18531862
const runningTerminalIds = useThreadRunningTerminalIds({
@@ -8247,8 +8256,8 @@ export default function ChatView(props: ChatViewProps) {
82478256
threadId={mountedThreadRef.threadId}
82488257
previewThreadRef={(isActiveDrawer ? activeThreadRef : null) ?? mountedThreadRef}
82498258
active={isActiveDrawer}
8250-
pinned={isActiveDrawer && terminalDrawerPinned}
8251-
onTogglePinned={toggleTerminalDrawerPinned}
8259+
pinState={isActiveDrawer ? terminalDrawerPinState : "none"}
8260+
onCyclePin={cycleTerminalDrawerPin}
82528261
launchContext={isActiveDrawer ? (activeTerminalLaunchContext ?? null) : null}
82538262
focusRequestId={isActiveDrawer ? terminalFocusRequestId : 0}
82548263
splitShortcutLabel={splitTerminalShortcutLabel ?? undefined}

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
type TerminalSessionState,
1212
} from "@t3tools/client-runtime/state/terminal";
1313
import {
14+
Globe,
1415
Pin,
15-
PinOff,
1616
Plus,
1717
Square,
1818
SquareSplitHorizontal,
@@ -46,6 +46,7 @@ import { PanelTabCloseButton } from "~/components/ui/panel-tab-close-button";
4646
import { readTextFromClipboard, writeTextToClipboard } from "~/hooks/useCopyToClipboard";
4747
import { cn } from "~/lib/utils";
4848
import { type TerminalContextSelection } from "~/lib/terminalContext";
49+
import { type TerminalDrawerPinState } from "~/lib/terminalDrawer";
4950
import {
5051
observeSelectionActions,
5152
resolveSelectionActionPosition,
@@ -955,9 +956,9 @@ interface ThreadTerminalDrawerProps {
955956
previewThreadRef?: ScopedThreadRef;
956957
threadId: ThreadId;
957958
cwd: string;
958-
/** Whether this drawer is pinned for its project. The pin button only renders with `onTogglePinned`. */
959-
pinned?: boolean;
960-
onTogglePinned?: (() => void) | undefined;
959+
/** Whether this drawer is pinned, and how far. The pin button only renders with `onCyclePin`. */
960+
pinState?: TerminalDrawerPinState;
961+
onCyclePin?: (() => void) | undefined;
961962
worktreePath?: string | null;
962963
runtimeEnv?: Record<string, string>;
963964
visible?: boolean;
@@ -1020,8 +1021,8 @@ export default function ThreadTerminalDrawer({
10201021
previewThreadRef = threadRef,
10211022
threadId,
10221023
cwd,
1023-
pinned = false,
1024-
onTogglePinned,
1024+
pinState = "none",
1025+
onCyclePin,
10251026
worktreePath,
10261027
runtimeEnv,
10271028
visible = true,
@@ -1236,14 +1237,19 @@ export default function ThreadTerminalDrawer({
12361237
const closeTerminalActionLabel = closeShortcutLabel
12371238
? `Close Terminal (${closeShortcutLabel})`
12381239
: "Close Terminal";
1239-
const pinTerminalActionLabel = pinned
1240-
? "Unpin Terminal (every thread in this project opens this terminal)"
1241-
: "Pin Terminal for Project (every thread in this project opens this terminal)";
1242-
const pinTerminalActionIcon = pinned ? (
1243-
<PinOff className="size-3.25" />
1244-
) : (
1245-
<Pin className="size-3.25" />
1246-
);
1240+
const pinTerminalActionLabel =
1241+
pinState === "environment"
1242+
? "Pinned for All Projects (click to unpin)"
1243+
: pinState === "project"
1244+
? "Pinned to Project (click to pin for all projects)"
1245+
: "Pin Terminal to Project (every thread in this project opens it)";
1246+
const pinTerminalActionIcon =
1247+
pinState === "environment" ? (
1248+
<Globe className="size-3.25" />
1249+
) : (
1250+
<Pin className="size-3.25" fill={pinState === "project" ? "currentColor" : "none"} />
1251+
);
1252+
const pinTerminalActionClassName = pinState === "none" ? "text-foreground/90" : "text-primary";
12471253
const onSplitTerminalAction = useCallback(() => {
12481254
if (hasReachedSplitLimit) return;
12491255
onSplitTerminal();
@@ -1459,14 +1465,12 @@ export default function ThreadTerminalDrawer({
14591465
>
14601466
<Trash2 className="size-3.25" />
14611467
</TerminalActionButton>
1462-
{onTogglePinned ? (
1468+
{onCyclePin ? (
14631469
<>
14641470
<div className="h-4 w-px bg-border/80" />
14651471
<TerminalActionButton
1466-
className={`p-1 transition-colors hover:bg-accent ${
1467-
pinned ? "text-primary" : "text-foreground/90"
1468-
}`}
1469-
onClick={onTogglePinned}
1472+
className={`p-1 transition-colors hover:bg-accent ${pinTerminalActionClassName}`}
1473+
onClick={onCyclePin}
14701474
label={pinTerminalActionLabel}
14711475
>
14721476
{pinTerminalActionIcon}
@@ -1617,12 +1621,10 @@ export default function ThreadTerminalDrawer({
16171621
>
16181622
<Trash2 className="size-3.25" />
16191623
</TerminalActionButton>
1620-
{onTogglePinned ? (
1624+
{onCyclePin ? (
16211625
<TerminalActionButton
1622-
className={`inline-flex h-full items-center border-l border-border/70 px-1 transition-colors hover:bg-accent/70 ${
1623-
pinned ? "text-primary" : "text-foreground/90"
1624-
}`}
1625-
onClick={onTogglePinned}
1626+
className={`inline-flex h-full items-center border-l border-border/70 px-1 transition-colors hover:bg-accent/70 ${pinTerminalActionClassName}`}
1627+
onClick={onCyclePin}
16261628
label={pinTerminalActionLabel}
16271629
>
16281630
{pinTerminalActionIcon}

‎apps/web/src/hooks/useTerminalDrawerRef.ts‎

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { type ScopedThreadRef } from "@t3tools/contracts";
77
import { useMemo } from "react";
88

99
import { useComposerDraftStore } from "../composerDraftStore";
10-
import { resolveTerminalDrawerRef } from "../lib/terminalDrawer";
10+
import {
11+
environmentTerminalPinKey,
12+
projectTerminalPinKey,
13+
resolveTerminalDrawer,
14+
type TerminalDrawerPinState,
15+
} from "../lib/terminalDrawer";
1116
import { useThreadShell } from "../state/entities";
1217
import { selectPinnedTerminalThreadKey, useTerminalUiStateStore } from "../terminalUiStateStore";
1318

@@ -23,29 +28,49 @@ export function useThreadProjectKey(threadRef: ScopedThreadRef | null): string |
2328
: null;
2429
}
2530

26-
/**
27-
* Drawer ref `terminal.toggle` targets for `threadRef`: the project's pinned
28-
* drawer when one is pinned and its thread still exists in that project,
29-
* otherwise the thread's own. Stable while the inputs are.
30-
*/
31-
export function useTerminalDrawerRef(threadRef: ScopedThreadRef | null): ScopedThreadRef | null {
32-
const projectKey = useThreadProjectKey(threadRef);
31+
function usePinnedThreadRef(pinKey: string | null): ScopedThreadRef | null {
3332
const pinnedThreadKey = useTerminalUiStateStore((state) =>
34-
selectPinnedTerminalThreadKey(state.pinnedTerminalThreadKeyByProjectKey, projectKey),
33+
selectPinnedTerminalThreadKey(state.pinnedTerminalThreadKeyByPinKey, pinKey),
3534
);
36-
const pinnedThreadRef = useMemo(
35+
return useMemo(
3736
() => (pinnedThreadKey ? parseScopedThreadKey(pinnedThreadKey) : null),
3837
[pinnedThreadKey],
3938
);
40-
// A deleted thread, or one moved to another project, no longer anchors the pin.
41-
const pinnedProjectKey = useThreadProjectKey(pinnedThreadRef);
42-
const pinnedThreadValid = pinnedProjectKey !== null && pinnedProjectKey === projectKey;
39+
}
40+
41+
/**
42+
* Drawer `terminal.toggle` targets for `threadRef`, and which pin put it
43+
* there. Pins to threads that no longer exist, or that moved to another
44+
* project, are ignored. Stable while the inputs are.
45+
*/
46+
export function useTerminalDrawerPin(threadRef: ScopedThreadRef | null): {
47+
drawerRef: ScopedThreadRef | null;
48+
pinState: TerminalDrawerPinState;
49+
} {
50+
const projectKey = useThreadProjectKey(threadRef);
51+
const projectPinnedRef = usePinnedThreadRef(
52+
projectKey === null ? null : projectTerminalPinKey(projectKey),
53+
);
54+
const environmentPinnedRef = usePinnedThreadRef(
55+
threadRef === null ? null : environmentTerminalPinKey(threadRef.environmentId),
56+
);
57+
// A null project key means the pinned thread is gone.
58+
const projectPinnedProjectKey = useThreadProjectKey(projectPinnedRef);
59+
const environmentPinnedProjectKey = useThreadProjectKey(environmentPinnedRef);
60+
const projectPinValid =
61+
projectPinnedProjectKey !== null && projectPinnedProjectKey === projectKey;
62+
const environmentPinValid = environmentPinnedProjectKey !== null;
4363
return useMemo(
4464
() =>
45-
resolveTerminalDrawerRef({
65+
resolveTerminalDrawer({
4666
threadRef,
47-
pinnedThreadRef: pinnedThreadValid ? pinnedThreadRef : null,
67+
projectPinnedThreadRef: projectPinValid ? projectPinnedRef : null,
68+
environmentPinnedThreadRef: environmentPinValid ? environmentPinnedRef : null,
4869
}),
49-
[pinnedThreadRef, pinnedThreadValid, threadRef],
70+
[environmentPinValid, environmentPinnedRef, projectPinValid, projectPinnedRef, threadRef],
5071
);
5172
}
73+
74+
export function useTerminalDrawerRef(threadRef: ScopedThreadRef | null): ScopedThreadRef | null {
75+
return useTerminalDrawerPin(threadRef).drawerRef;
76+
}

0 commit comments

Comments
 (0)