- {isSplitView ? (
-
- {visibleTerminalIds.map((terminalId) => {
- const terminalLaunchLocation = resolveTerminalLaunchLocation(terminalId);
- return (
-
{
- if (terminalId !== resolvedActiveTerminalId) {
- onActiveTerminalChange(terminalId);
- }
- }}
- >
-
- onCloseTerminal(terminalId)}
- onAddTerminalContext={onAddTerminalContext}
- focusRequestId={focusRequestId}
- autoFocus={terminalId === resolvedActiveTerminalId}
- resizeEpoch={resizeEpoch}
- drawerHeight={drawerHeight}
- keybindings={keybindings}
- />
-
+
+
+ {isSplitView ? (
+
+ {visibleTerminalIds.map((terminalId) => {
+ const terminalLaunchLocation = resolveTerminalLaunchLocation(terminalId);
+ const isActive = terminalId === resolvedActiveTerminalId;
+ return (
+
{
+ if (!isActive) onActiveTerminalChange(terminalId);
+ }}
+ >
+
+ closeTerminalSafely(terminalId)}
+ onAddTerminalContext={onAddTerminalContext}
+ focusRequestId={focusRequestId}
+ autoFocus={isActive}
+ resizeEpoch={resizeEpoch}
+ drawerHeight={drawerHeight}
+ keybindings={keybindings}
+ />
- );
- })}
-
- ) : (
-
- onCloseTerminal(resolvedActiveTerminalId)}
- onAddTerminalContext={onAddTerminalContext}
- focusRequestId={focusRequestId}
- autoFocus
- resizeEpoch={resizeEpoch}
- drawerHeight={drawerHeight}
- keybindings={keybindings}
- />
-
- )}
-
-
- {hasTerminalSidebar && (
-
+
+ );
+ })}
+
+ ) : (
+
+ closeTerminalSafely(resolvedActiveTerminalId)}
+ onAddTerminalContext={onAddTerminalContext}
+ focusRequestId={focusRequestId}
+ autoFocus
+ resizeEpoch={resizeEpoch}
+ drawerHeight={drawerHeight}
+ keybindings={keybindings}
+ />
+
)}
+
+
);
diff --git a/apps/web/src/hooks/useResizableWidth.ts b/apps/web/src/hooks/useResizableWidth.ts
index 08c067471f..6a9a364d91 100644
--- a/apps/web/src/hooks/useResizableWidth.ts
+++ b/apps/web/src/hooks/useResizableWidth.ts
@@ -1,7 +1,7 @@
import * as Schema from "effect/Schema";
import { type PointerEvent as ReactPointerEvent, useCallback, useRef, useState } from "react";
-import { getLocalStorageItem, setLocalStorageItem } from "./useLocalStorage";
+import { useLocalStorage } from "./useLocalStorage";
const WidthSchema = Schema.Finite;
@@ -28,12 +28,10 @@ export interface ResizableWidthHandlers {
/**
* Width state for a side-anchored panel resized via a drag handle on the
- * specified edge. Width is read from localStorage on mount and persisted on
- * drag-end (not on every rAF tick — would otherwise be ~60 writes/sec).
+ * specified edge. Mounted consumers sharing a storage key stay synchronized.
*
- * The hook updates an internal `width` state during drag (so the panel
- * follows the cursor live) and only commits to localStorage when the user
- * lifts the pointer.
+ * The hook keeps drag width local so the panel follows the cursor without
+ * writing at rAF frequency, then commits once when the pointer lifts.
*/
export function useResizableWidth(options: UseResizableWidthOptions): {
readonly width: number;
@@ -49,19 +47,9 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
[defaultWidth, maxWidth, minWidth],
);
- // No cross-tab subscription: panel width is per-window state.
- const [width, setWidth] = useState
(() => {
- if (typeof window === "undefined") return defaultWidth;
- try {
- const stored = getLocalStorageItem(storageKey, WidthSchema);
- return clamp(stored ?? defaultWidth);
- } catch (error) {
- console.error("Could not read persisted panel width.", error);
- return defaultWidth;
- }
- });
-
- const clampedWidth = clamp(width);
+ const [storedWidth, setStoredWidth] = useLocalStorage(storageKey, defaultWidth, WidthSchema);
+ const [dragWidth, setDragWidth] = useState(null);
+ const clampedWidth = clamp(dragWidth ?? storedWidth);
const dragStateRef = useRef<{
pointerId: number;
@@ -127,7 +115,7 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
const active = dragStateRef.current;
if (!active) return;
active.rafId = null;
- setWidth(active.pending);
+ setDragWidth(active.pending);
});
},
[clamp, edge],
@@ -139,24 +127,19 @@ export function useResizableWidth(options: UseResizableWidthOptions): {
if (!state || state.pointerId !== event.pointerId) return;
const finalWidth = clamp(state.pending);
releasePointer(event.pointerId);
- // Commit once at drag-end to avoid 60Hz localStorage writes.
- try {
- setLocalStorageItem(storageKey, finalWidth, WidthSchema);
- } catch (error) {
- console.error("Could not persist panel width.", error);
- }
- setWidth(finalWidth);
+ setStoredWidth(finalWidth);
+ setDragWidth(null);
},
- [clamp, releasePointer, storageKey],
+ [clamp, releasePointer, setStoredWidth],
);
const onPointerCancel = useCallback(
(event: ReactPointerEvent) => {
const state = dragStateRef.current;
if (!state || state.pointerId !== event.pointerId) return;
- // Don't persist a cancelled drag; revert to the start width.
+ // Don't persist a cancelled drag; use the latest shared stored width.
releasePointer(event.pointerId);
- setWidth(state.startWidth);
+ setDragWidth(null);
},
[releasePointer],
);
diff --git a/apps/web/src/terminalUiStateStore.test.ts b/apps/web/src/terminalUiStateStore.test.ts
index b0b1df96e1..f62c735dd1 100644
--- a/apps/web/src/terminalUiStateStore.test.ts
+++ b/apps/web/src/terminalUiStateStore.test.ts
@@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it } from "vite-plus/test";
import {
migratePersistedTerminalUiStateStoreState,
+ selectThreadTerminalCustomLabels,
selectThreadTerminalUiState,
useTerminalUiStateStore,
} from "./terminalUiStateStore";
@@ -18,6 +19,7 @@ describe("terminalUiStateStore actions", () => {
useTerminalUiStateStore.persist.clearStorage();
useTerminalUiStateStore.setState({
terminalUiStateByThreadKey: {},
+ terminalCustomLabelsByThreadKey: {},
suppressedTerminalIdsByThreadKey: {},
});
});
@@ -212,9 +214,53 @@ describe("terminalUiStateStore actions", () => {
});
});
+ it("normalizes persisted terminal labels", () => {
+ const migrated = migratePersistedTerminalUiStateStoreState(
+ {
+ terminalCustomLabelsByThreadKey: {
+ [scopedThreadKey(THREAD_REF)]: {
+ " term-1 ": " API server ",
+ empty: " ",
+ invalid: 42,
+ },
+ "legacy-thread-id": { "term-2": "Ignored" },
+ },
+ },
+ 4,
+ );
+
+ expect(migrated).toEqual({
+ terminalUiStateByThreadKey: {},
+ terminalCustomLabelsByThreadKey: {
+ [scopedThreadKey(THREAD_REF)]: { "term-1": "API server" },
+ },
+ });
+ });
+
+ it("sets and clears a terminal label", () => {
+ const store = useTerminalUiStateStore.getState();
+ store.setTerminalCustomLabel(THREAD_REF, "term-1", " API server ");
+
+ expect(
+ selectThreadTerminalCustomLabels(
+ useTerminalUiStateStore.getState().terminalCustomLabelsByThreadKey,
+ THREAD_REF,
+ ),
+ ).toEqual({ "term-1": "API server" });
+
+ store.setTerminalCustomLabel(THREAD_REF, "term-1", null);
+ expect(
+ selectThreadTerminalCustomLabels(
+ useTerminalUiStateStore.getState().terminalCustomLabelsByThreadKey,
+ THREAD_REF,
+ ),
+ ).toEqual({});
+ });
+
it("resets to default and clears persisted entry when closing the last terminal", () => {
const store = useTerminalUiStateStore.getState();
store.newTerminal(THREAD_REF, "terminal-only");
+ store.setTerminalCustomLabel(THREAD_REF, "terminal-only", "Build");
store.closeTerminal(THREAD_REF, "terminal-only");
expect(
@@ -226,6 +272,12 @@ describe("terminalUiStateStore actions", () => {
THREAD_REF,
).terminalIds,
).toEqual([]);
+ expect(
+ selectThreadTerminalCustomLabels(
+ useTerminalUiStateStore.getState().terminalCustomLabelsByThreadKey,
+ THREAD_REF,
+ ),
+ ).toEqual({});
});
it("keeps a valid active terminal after closing an active split terminal", () => {
@@ -248,6 +300,8 @@ describe("terminalUiStateStore actions", () => {
it("reconciles terminal ids from an external ordered list", () => {
const store = useTerminalUiStateStore.getState();
store.setTerminalOpen(THREAD_REF, true);
+ store.setTerminalCustomLabel(THREAD_REF, "term-a", "API server");
+ store.setTerminalCustomLabel(THREAD_REF, "stale-term", "Old task");
store.reconcileTerminalIds(THREAD_REF, ["term-a", "term-b"]);
const terminalUiState = selectThreadTerminalUiState(
@@ -260,6 +314,11 @@ describe("terminalUiStateStore actions", () => {
{ id: "group-term-a", terminalIds: ["term-a"] },
{ id: "group-term-b", terminalIds: ["term-b"] },
]);
+ expect(
+ useTerminalUiStateStore.getState().terminalCustomLabelsByThreadKey[
+ scopedThreadKey(THREAD_REF)
+ ],
+ ).toEqual({ "term-a": "API server" });
});
it("does not import a closed panel terminal from stale metadata", () => {
diff --git a/apps/web/src/terminalUiStateStore.ts b/apps/web/src/terminalUiStateStore.ts
index 290ca8e595..545e195a12 100644
--- a/apps/web/src/terminalUiStateStore.ts
+++ b/apps/web/src/terminalUiStateStore.ts
@@ -32,8 +32,11 @@ const TERMINAL_UI_STATE_STORAGE_KEY = "t3code:terminal-state:v1";
interface PersistedTerminalUiStateStoreState {
terminalUiStateByThreadKey?: Record;
terminalStateByThreadKey?: Record;
+ terminalCustomLabelsByThreadKey?: Record>;
}
+const EMPTY_TERMINAL_CUSTOM_LABELS: Readonly> = Object.freeze({});
+
export function migratePersistedTerminalUiStateStoreState(
persistedState: unknown,
_version: number,
@@ -50,8 +53,32 @@ export function migratePersistedTerminalUiStateStoreState(
parseScopedThreadKey(threadKey),
),
);
+ const terminalCustomLabelsByThreadKey = Object.fromEntries(
+ Object.entries(candidate.terminalCustomLabelsByThreadKey ?? {}).flatMap(
+ ([threadKey, labels]) => {
+ if (!parseScopedThreadKey(threadKey) || !labels || typeof labels !== "object") return [];
+ const normalizedLabels = Object.fromEntries(
+ Object.entries(labels).flatMap(([terminalId, label]) => {
+ const normalizedTerminalId = terminalId.trim();
+ const normalizedLabel = typeof label === "string" ? label.trim().slice(0, 80) : "";
+ return normalizedTerminalId && normalizedLabel
+ ? [[normalizedTerminalId, normalizedLabel] as const]
+ : [];
+ }),
+ );
+ return Object.keys(normalizedLabels).length > 0
+ ? [[threadKey, normalizedLabels] as const]
+ : [];
+ },
+ ),
+ );
- return { terminalUiStateByThreadKey };
+ return {
+ terminalUiStateByThreadKey,
+ ...(Object.keys(terminalCustomLabelsByThreadKey).length > 0
+ ? { terminalCustomLabelsByThreadKey }
+ : {}),
+ };
}
function createTerminalUiStateStorage() {
@@ -489,6 +516,18 @@ export function selectThreadTerminalUiState(
);
}
+export function selectThreadTerminalCustomLabels(
+ terminalCustomLabelsByThreadKey: Record>,
+ threadRef: ScopedThreadRef | null | undefined,
+): Readonly> {
+ if (!threadRef || threadRef.threadId.length === 0) {
+ return EMPTY_TERMINAL_CUSTOM_LABELS;
+ }
+ return (
+ terminalCustomLabelsByThreadKey[terminalThreadKey(threadRef)] ?? EMPTY_TERMINAL_CUSTOM_LABELS
+ );
+}
+
function updateTerminalUiStateByThreadKey(
terminalUiStateByThreadKey: Record,
threadRef: ScopedThreadRef,
@@ -562,6 +601,7 @@ function removeRecordEntry(record: Record, key: string): Record;
+ terminalCustomLabelsByThreadKey: Record>;
/** Closed ids hidden from stale server metadata until that id is explicitly opened again. */
suppressedTerminalIdsByThreadKey: Record;
setTerminalOpen: (threadRef: ScopedThreadRef, open: boolean) => void;
@@ -575,6 +615,11 @@ interface TerminalUiStateStoreState {
options?: { open?: boolean; active?: boolean },
) => void;
setActiveTerminal: (threadRef: ScopedThreadRef, terminalId: string) => void;
+ setTerminalCustomLabel: (
+ threadRef: ScopedThreadRef,
+ terminalId: string,
+ label: string | null,
+ ) => void;
closeTerminal: (threadRef: ScopedThreadRef, terminalId: string) => void;
reconcileTerminalIds: (threadRef: ScopedThreadRef, nextIds: string[]) => void;
clearTerminalUiState: (threadRef: ScopedThreadRef) => void;
@@ -591,7 +636,12 @@ export const useTerminalUiStateStore = create()(
state: ThreadTerminalUiState,
suppressedTerminalIds: readonly string[],
) => ThreadTerminalUiState,
- suppression?: { terminalId: string; suppressed: boolean },
+ suppression?: {
+ terminalId: string;
+ suppressed: boolean;
+ clearCustomLabel?: boolean;
+ },
+ pruneCustomLabels = false,
) => {
set((state) => {
const threadKey = terminalThreadKey(threadRef);
@@ -609,21 +659,57 @@ export const useTerminalUiStateStore = create()(
suppression.suppressed,
)
: state.suppressedTerminalIdsByThreadKey;
+ const terminalIdToClear = suppression?.clearCustomLabel
+ ? suppression.terminalId.trim()
+ : "";
+ const currentLabels = state.terminalCustomLabelsByThreadKey[threadKey] ?? {};
+ let nextTerminalCustomLabelsByThreadKey =
+ terminalIdToClear.length > 0 && currentLabels[terminalIdToClear] !== undefined
+ ? Object.keys(currentLabels).length === 1
+ ? removeRecordEntry(state.terminalCustomLabelsByThreadKey, threadKey)
+ : {
+ ...state.terminalCustomLabelsByThreadKey,
+ [threadKey]: removeRecordEntry(currentLabels, terminalIdToClear),
+ }
+ : state.terminalCustomLabelsByThreadKey;
+ if (pruneCustomLabels) {
+ const survivingIds = new Set(
+ selectThreadTerminalUiState(nextTerminalUiStateByThreadKey, threadRef).terminalIds,
+ );
+ const labelsForThread = nextTerminalCustomLabelsByThreadKey[threadKey] ?? {};
+ const survivingLabels = Object.fromEntries(
+ Object.entries(labelsForThread).filter(([terminalId]) =>
+ survivingIds.has(terminalId),
+ ),
+ );
+ if (Object.keys(survivingLabels).length !== Object.keys(labelsForThread).length) {
+ nextTerminalCustomLabelsByThreadKey =
+ Object.keys(survivingLabels).length > 0
+ ? {
+ ...nextTerminalCustomLabelsByThreadKey,
+ [threadKey]: survivingLabels,
+ }
+ : removeRecordEntry(nextTerminalCustomLabelsByThreadKey, threadKey);
+ }
+ }
if (
nextTerminalUiStateByThreadKey === state.terminalUiStateByThreadKey &&
- nextSuppressedTerminalIdsByThreadKey === state.suppressedTerminalIdsByThreadKey
+ nextSuppressedTerminalIdsByThreadKey === state.suppressedTerminalIdsByThreadKey &&
+ nextTerminalCustomLabelsByThreadKey === state.terminalCustomLabelsByThreadKey
) {
return state;
}
return {
terminalUiStateByThreadKey: nextTerminalUiStateByThreadKey,
suppressedTerminalIdsByThreadKey: nextSuppressedTerminalIdsByThreadKey,
+ terminalCustomLabelsByThreadKey: nextTerminalCustomLabelsByThreadKey,
};
});
};
return {
terminalUiStateByThreadKey: {},
+ terminalCustomLabelsByThreadKey: {},
suppressedTerminalIdsByThreadKey: {},
setTerminalOpen: (threadRef, open) => {
const terminalState = selectThreadTerminalUiState(
@@ -682,22 +768,56 @@ export const useTerminalUiStateStore = create()(
),
setActiveTerminal: (threadRef, terminalId) =>
updateTerminal(threadRef, (state) => setThreadActiveTerminal(state, terminalId)),
+ setTerminalCustomLabel: (threadRef, terminalId, label) =>
+ set((state) => {
+ const normalizedTerminalId = terminalId.trim();
+ if (normalizedTerminalId.length === 0) return state;
+ const threadKey = terminalThreadKey(threadRef);
+ const currentLabels = state.terminalCustomLabelsByThreadKey[threadKey] ?? {};
+ const normalizedLabel = label?.trim().slice(0, 80) ?? "";
+ if (normalizedLabel.length > 0) {
+ if (currentLabels[normalizedTerminalId] === normalizedLabel) return state;
+ return {
+ terminalCustomLabelsByThreadKey: {
+ ...state.terminalCustomLabelsByThreadKey,
+ [threadKey]: { ...currentLabels, [normalizedTerminalId]: normalizedLabel },
+ },
+ };
+ }
+ if (currentLabels[normalizedTerminalId] === undefined) return state;
+ const { [normalizedTerminalId]: _removed, ...remainingLabels } = currentLabels;
+ return {
+ terminalCustomLabelsByThreadKey:
+ Object.keys(remainingLabels).length > 0
+ ? {
+ ...state.terminalCustomLabelsByThreadKey,
+ [threadKey]: remainingLabels,
+ }
+ : removeRecordEntry(state.terminalCustomLabelsByThreadKey, threadKey),
+ };
+ }),
closeTerminal: (threadRef, terminalId) =>
updateTerminal(threadRef, (state) => closeThreadTerminal(state, terminalId), {
terminalId,
suppressed: true,
+ clearCustomLabel: true,
}),
reconcileTerminalIds: (threadRef, nextIds) =>
- updateTerminal(threadRef, (state, suppressedTerminalIds) => {
- if (suppressedTerminalIds.length === 0) {
- return reconcileThreadTerminalSessionIds(state, nextIds);
- }
- const suppressedIds = new Set(suppressedTerminalIds);
- return reconcileThreadTerminalSessionIds(
- state,
- nextIds.filter((terminalId) => !suppressedIds.has(terminalId)),
- );
- }),
+ updateTerminal(
+ threadRef,
+ (state, suppressedTerminalIds) => {
+ if (suppressedTerminalIds.length === 0) {
+ return reconcileThreadTerminalSessionIds(state, nextIds);
+ }
+ const suppressedIds = new Set(suppressedTerminalIds);
+ return reconcileThreadTerminalSessionIds(
+ state,
+ nextIds.filter((terminalId) => !suppressedIds.has(terminalId)),
+ );
+ },
+ undefined,
+ true,
+ ),
clearTerminalUiState: (threadRef) =>
set((state) => {
const threadKey = terminalThreadKey(threadRef);
@@ -708,14 +828,20 @@ export const useTerminalUiStateStore = create()(
);
const hadSuppressedTerminalIds =
state.suppressedTerminalIdsByThreadKey[threadKey] !== undefined;
+ const hadCustomLabels = state.terminalCustomLabelsByThreadKey[threadKey] !== undefined;
if (
nextTerminalUiStateByThreadKey === state.terminalUiStateByThreadKey &&
- !hadSuppressedTerminalIds
+ !hadSuppressedTerminalIds &&
+ !hadCustomLabels
) {
return state;
}
return {
terminalUiStateByThreadKey: nextTerminalUiStateByThreadKey,
+ terminalCustomLabelsByThreadKey: removeRecordEntry(
+ state.terminalCustomLabelsByThreadKey,
+ threadKey,
+ ),
suppressedTerminalIdsByThreadKey: removeRecordEntry(
state.suppressedTerminalIdsByThreadKey,
threadKey,
@@ -728,7 +854,8 @@ export const useTerminalUiStateStore = create()(
const hadTerminalUiState = state.terminalUiStateByThreadKey[threadKey] !== undefined;
const hadSuppressedTerminalIds =
state.suppressedTerminalIdsByThreadKey[threadKey] !== undefined;
- if (!hadTerminalUiState && !hadSuppressedTerminalIds) {
+ const hadCustomLabels = state.terminalCustomLabelsByThreadKey[threadKey] !== undefined;
+ if (!hadTerminalUiState && !hadSuppressedTerminalIds && !hadCustomLabels) {
return state;
}
return {
@@ -736,6 +863,10 @@ export const useTerminalUiStateStore = create()(
state.terminalUiStateByThreadKey,
threadKey,
),
+ terminalCustomLabelsByThreadKey: removeRecordEntry(
+ state.terminalCustomLabelsByThreadKey,
+ threadKey,
+ ),
suppressedTerminalIdsByThreadKey: removeRecordEntry(
state.suppressedTerminalIdsByThreadKey,
threadKey,
@@ -747,6 +878,7 @@ export const useTerminalUiStateStore = create()(
const orphanedIds = new Set(
[
...Object.keys(state.terminalUiStateByThreadKey),
+ ...Object.keys(state.terminalCustomLabelsByThreadKey),
...Object.keys(state.suppressedTerminalIdsByThreadKey),
].filter((key) => !activeThreadKeys.has(key)),
);
@@ -757,12 +889,17 @@ export const useTerminalUiStateStore = create()(
const nextSuppressedTerminalIdsByThreadKey = {
...state.suppressedTerminalIdsByThreadKey,
};
+ const nextTerminalCustomLabelsByThreadKey = {
+ ...state.terminalCustomLabelsByThreadKey,
+ };
for (const id of orphanedIds) {
delete nextTerminalUiStateByThreadKey[id];
+ delete nextTerminalCustomLabelsByThreadKey[id];
delete nextSuppressedTerminalIdsByThreadKey[id];
}
return {
terminalUiStateByThreadKey: nextTerminalUiStateByThreadKey,
+ terminalCustomLabelsByThreadKey: nextTerminalCustomLabelsByThreadKey,
suppressedTerminalIdsByThreadKey: nextSuppressedTerminalIdsByThreadKey,
};
}),
@@ -770,11 +907,12 @@ export const useTerminalUiStateStore = create()(
},
{
name: TERMINAL_UI_STATE_STORAGE_KEY,
- version: 4,
+ version: 5,
storage: createJSONStorage(createTerminalUiStateStorage),
migrate: migratePersistedTerminalUiStateStoreState,
partialize: (state) => ({
terminalUiStateByThreadKey: state.terminalUiStateByThreadKey,
+ terminalCustomLabelsByThreadKey: state.terminalCustomLabelsByThreadKey,
}),
},
),