diff --git a/FEATURES.md b/FEATURES.md index 6d04221c6..36b83c95f 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -92,6 +92,10 @@ Herdr 0.9.1+ resolves URLs that wrap across lines. See Open **Files**, **Changes**, **Commits**, or **Agent History** from the Inspector button, shortcuts, or workspace/agent menus. +- Showing or hiding the Inspector applies to the current workspace only, in + this browser. Switching workspaces shows each one's Inspector as it was left + there, in that checkout's view, and a reload brings it back. A workspace + where it was never shown keeps it hidden. On phones it is unchanged. - Dock right or bottom, resize, or expand. Narrow layouts use drill-down views. - Single-click opens a temporary preview tab; double-click keeps it. Open files are restored per connection and checkout. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 05a2de380..67dc5afbb 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -151,7 +151,9 @@ snapshots do not prove which agent made a change. Git resource keys combine `worktree.gui_settings_key` with the normalized checkout path, per connection. Workspaces on the same checkout may share caches. Actions remember their originating checkout and never fall back to a sibling. -Terminals stay mounted while the Inspector changes. +Terminals stay mounted while the Inspector changes. Whether the Inspector is +shown belongs to the workspace (`inspectorShown.v1`, per browser, connection and +workspace); its view, dock and expansion stay the checkout's. Browser preferences live in namespaced `localStorage` and sync across tabs on the same origin. Navigation, open files, live draft edits, and focus stay per tab. diff --git a/scripts/check-web-assets.mjs b/scripts/check-web-assets.mjs index b98853ed5..7d1b473eb 100644 --- a/scripts/check-web-assets.mjs +++ b/scripts/check-web-assets.mjs @@ -7,8 +7,8 @@ const publicRoot = fileURLToPath(new URL("../server/public/", import.meta.url)); const maxFileCount = 161; // PDF.js and its worker load only when the native viewer is unavailable. const maxTotalBytes = 16 * 1024 * 1024; -const maxInitialJsBytes = 833 * 1024; -const maxInitialJsGzipBytes = 257 * 1024; +const maxInitialJsBytes = 834 * 1024; +const maxInitialJsGzipBytes = 258 * 1024; const maxInitialCssBytes = 196 * 1024; /** Follow eager imports only, from app entries or explicitly selected features. */ diff --git a/web/src/App.tsx b/web/src/App.tsx index cf9f89c26..49e4ab76e 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -121,6 +121,7 @@ import { requestClosePane, requestCloseTab, TabBar } from "./components/TabBar"; import type { TerminalWorkspaceFileRequest } from "./components/TerminalView"; import { WorkspaceTree } from "./components/WorkspaceTree"; import { CreateWorkspaceDialog } from "./components/CreateWorkspaceDialog"; +import { inspectorWasShown, rememberInspectorShown } from "./inspectorShown"; import { AGENT_LIST_SCOPE_STORAGE_KEY } from "./workspaceAgentLayout"; import { isIosDevice } from "./downloadFile"; import { lazyWithReload } from "./lazyWithReload"; @@ -1752,9 +1753,29 @@ export default function App() { ); const commitInspectorState = useCallback( - (next: WorkspaceInspectorState | null) => { + ( + next: WorkspaceInspectorState | null, + // false when put away by a switch to a workspace that has it hidden: + // the one left keeps it shown. + remember = true, + ) => { + const previous = inspectorStateRef.current; inspectorStateRef.current = next; setInspectorState(next); + // Shown or hidden by the user: in that workspace only. + if ( + remember && + next && + (previous?.open !== next.open || + previous.scope.connectionId !== next.scope.connectionId || + previous.scope.workspaceId !== next.scope.workspaceId) + ) + rememberInspectorShown( + roamgateLocalStorage, + next.scope.connectionId, + next.scope.workspaceId, + next.open, + ); }, [], ); @@ -1763,12 +1784,8 @@ export default function App() { update: ( current: WorkspaceInspectorState | null, ) => WorkspaceInspectorState | null, - ) => { - const next = update(inspectorStateRef.current); - inspectorStateRef.current = next; - setInspectorState(next); - }, - [], + ) => commitInspectorState(update(inspectorStateRef.current)), + [commitInspectorState], ); const activateTerminalSurface = useCallback(() => { setMobileMoreOpen(false); @@ -2468,11 +2485,28 @@ export default function App() { const keepInspectorForWorkspace = useCallback( (workspaceId: string, originPane?: Pane) => { const current = inspectorStateRef.current; - if (!current?.open && !mobile) { + const snapshot = store.get(); + const target = snapshot.workspaces.find( + (candidate) => candidate.workspace_id === workspaceId, + ); + // Each workspace has its inspector shown or hidden as it was left + // there, in its own view. + if ( + !mobile && + (!target || + !inspectorWasShown( + roamgateLocalStorage, + connectionClient.connectionId, + workspaceId, + )) + ) { + if (current?.open) { + inspectorReturnFocusRef.current = null; + commitInspectorState({ ...current, open: false }, false); + } activateTerminalSurface(); return; } - const snapshot = store.get(); const explicitPane = originPane ? snapshot.panes.find( (candidate) => candidate.pane_id === originPane.pane_id, @@ -2490,12 +2524,18 @@ export default function App() { activateSessionSurface(routedPane ?? null); return; } - if (!current) return; const historyPane = paneHasAgentHistory(routedPane) ? routedPane : workspacePanes.find(paneHasAgentHistory); + const shownView = + !mobile && target + ? readInspectorPreferences( + roamgateLocalStorage, + resourceScopeForWorkspace(connectionClient.connectionId, target), + ).view + : (current?.view ?? "files"); const view = - current.view === "history" && !historyPane ? "files" : current.view; + shownView === "history" && !historyPane ? "files" : shownView; openInspector(view, workspaceId, { focusInspector: false, originPaneId: @@ -2503,7 +2543,14 @@ export default function App() { (view === "history" ? historyPane?.pane_id : undefined), }); }, - [activateSessionSurface, activateTerminalSurface, mobile, openInspector], + [ + activateSessionSurface, + activateTerminalSurface, + commitInspectorState, + connectionClient.connectionId, + mobile, + openInspector, + ], ); const toggleFileExplorer = useCallback(() => { const snapshot = store.get(); @@ -3076,20 +3123,38 @@ export default function App() { }), ); }, [annotationScope, annotationWorkspace, commitAnnotations, s.panes]); + // The workspace the inspector last followed a switch to: a switch shows + // or hides it as that workspace had it, a reload brings it back. + const inspectorFollowedRef = useRef(null); useLayoutEffect(() => { + if (!focusedWorkspace || s.pendingFocusWorkspaceId) return; + const followed = `${connectionClient.connectionId}\n${focusedWorkspace.workspace_id}`; + const switched = inspectorFollowedRef.current !== followed; + inspectorFollowedRef.current = followed; const current = inspectorStateRef.current; - if (!current?.open || !focusedWorkspace || s.pendingFocusWorkspaceId) { + if (current?.open) { + const routedWorkspace = resolveWorkspaceForScope( + current.scope, + s.workspaces, + ); + if (routedWorkspace?.workspace_id === focusedWorkspace.workspace_id) + return; + } else if ( + mobile || + !switched || + !inspectorWasShown( + roamgateLocalStorage, + connectionClient.connectionId, + focusedWorkspace.workspace_id, + ) + ) return; - } - const routedWorkspace = resolveWorkspaceForScope( - current.scope, - s.workspaces, - ); - if (routedWorkspace?.workspace_id === focusedWorkspace.workspace_id) return; keepInspectorForWorkspace(focusedWorkspace.workspace_id); }, [ + connectionClient.connectionId, focusedWorkspace, keepInspectorForWorkspace, + mobile, s.pendingFocusWorkspaceId, s.workspaces, ]); diff --git a/web/src/inspectorShown.test.ts b/web/src/inspectorShown.test.ts new file mode 100644 index 000000000..0b7b7e741 --- /dev/null +++ b/web/src/inspectorShown.test.ts @@ -0,0 +1,56 @@ +import { expect, test } from "bun:test"; +import { + INSPECTOR_SHOWN_STORAGE_KEY, + inspectorWasShown, + rememberInspectorShown, +} from "./inspectorShown"; + +function memoryStorage() { + const items = new Map(); + return { + items, + getItem: (key: string) => items.get(key) ?? null, + setItem: (key: string, value: string) => void items.set(key, value), + removeItem: (key: string) => void items.delete(key), + }; +} + +test("the inspector is shown per connection and workspace", () => { + const storage = memoryStorage(); + // Never shown: today's default, hidden. + expect(inspectorWasShown(storage, "alpha", "w1")).toBe(false); + + rememberInspectorShown(storage, "alpha", "w1", true); + expect(inspectorWasShown(storage, "alpha", "w1")).toBe(true); + // Not in the other workspaces, nor in the same id on another connection. + expect(inspectorWasShown(storage, "alpha", "w2")).toBe(false); + expect(inspectorWasShown(storage, "beta", "w1")).toBe(false); + + rememberInspectorShown(storage, "beta", "w1", true); + rememberInspectorShown(storage, "alpha", "w1", false); + expect(inspectorWasShown(storage, "alpha", "w1")).toBe(false); + expect(inspectorWasShown(storage, "beta", "w1")).toBe(true); + + // Nothing shown anywhere: nothing kept. + rememberInspectorShown(storage, "beta", "w1", false); + expect(storage.items.has(INSPECTOR_SHOWN_STORAGE_KEY)).toBe(false); +}); + +test("a malformed or unwritable value reads as hidden", () => { + const storage = memoryStorage(); + storage.setItem(INSPECTOR_SHOWN_STORAGE_KEY, "{not json"); + expect(inspectorWasShown(storage, "alpha", "w1")).toBe(false); + storage.setItem( + INSPECTOR_SHOWN_STORAGE_KEY, + JSON.stringify({ version: 2, connections: { alpha: ["w1"] } }), + ); + expect(inspectorWasShown(storage, "alpha", "w1")).toBe(false); + const full = { + ...memoryStorage(), + setItem: () => { + throw new Error("quota"); + }, + }; + expect(() => rememberInspectorShown(full, "alpha", "w1", true)).not.toThrow(); + expect(inspectorWasShown(full, "alpha", "w1")).toBe(false); +}); diff --git a/web/src/inspectorShown.ts b/web/src/inspectorShown.ts new file mode 100644 index 000000000..90705e812 --- /dev/null +++ b/web/src/inspectorShown.ts @@ -0,0 +1,104 @@ +/** + * Whether the workspace inspector (Files, Changes, Commits, History) was + * left shown, per browser, connection and workspace: showing it in one + * workspace shows it there only, and switching back brings it back as it + * was. Its view, dock and Zen are the checkout's (readInspectorPreferences). + * Kept in localStorage only: another browser showing its inspector must + * not open this one's. + */ +export const INSPECTOR_SHOWN_STORAGE_KEY = "inspectorShown.v1"; + +const MAX_CONNECTIONS = 64; +const MAX_WORKSPACES = 512; +const MAX_ID_LENGTH = 512; + +/** connection id -> ids of the workspaces whose inspector was shown */ +type ShownSpaces = Readonly>; + +function validId(value: unknown): value is string { + return ( + typeof value === "string" && + value.length > 0 && + value.length <= MAX_ID_LENGTH + ); +} + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} + +/** Validates the stored value; anything malformed reads as nothing shown. */ +function parseShown(value: unknown): ShownSpaces { + if (!isRecord(value) || value.version !== 1 || !isRecord(value.connections)) + return {}; + const shown: Record = {}; + for (const [connectionId, workspaces] of Object.entries( + value.connections, + ).slice(0, MAX_CONNECTIONS)) { + if (!validId(connectionId) || !Array.isArray(workspaces)) continue; + const ids = [...new Set(workspaces.filter(validId))].slice( + 0, + MAX_WORKSPACES, + ); + if (ids.length) shown[connectionId] = ids; + } + return shown; +} + +function withShown( + spaces: ShownSpaces, + connectionId: string, + workspaceId: string, + shown: boolean, +): ShownSpaces { + const others = (spaces[connectionId] ?? []).filter( + (id) => id !== workspaceId, + ); + const ids = shown ? [...others, workspaceId] : others; + const result: Record = { ...spaces }; + if (ids.length) result[connectionId] = ids; + else delete result[connectionId]; + return result; +} + +type ShownStorage = Pick; + +function readShown(storage: Pick) { + try { + const raw = storage.getItem(INSPECTOR_SHOWN_STORAGE_KEY); + return raw ? parseShown(JSON.parse(raw)) : {}; + } catch { + return {}; + } +} + +/** Whether this browser last left the workspace's inspector shown. */ +export function inspectorWasShown( + storage: Pick, + connectionId: string, + workspaceId: string, +): boolean { + return readShown(storage)[connectionId]?.includes(workspaceId) ?? false; +} + +export function rememberInspectorShown( + storage: ShownStorage, + connectionId: string, + workspaceId: string, + shown: boolean, +): void { + const spaces = readShown(storage); + if ((spaces[connectionId]?.includes(workspaceId) ?? false) === shown) return; + try { + const next = withShown(spaces, connectionId, workspaceId, shown); + if (Object.keys(next).length === 0) + storage.removeItem(INSPECTOR_SHOWN_STORAGE_KEY); + else + storage.setItem( + INSPECTOR_SHOWN_STORAGE_KEY, + JSON.stringify({ version: 1, connections: next }), + ); + } catch { + // The inspector still works; switching back just finds it hidden. + } +}