diff --git a/e2e/settingsFormLayout.spec.ts b/e2e/settingsFormLayout.spec.ts index af33510bfe..7a0c5c455d 100644 --- a/e2e/settingsFormLayout.spec.ts +++ b/e2e/settingsFormLayout.spec.ts @@ -123,7 +123,7 @@ test("Git Log settings group display and column options with aligned checkboxes" const columns = dialog.getByRole("region", { name: "Visible columns" }); const tagNames = view.getByRole("checkbox", { name: "Show tag names" }); const hash = columns.getByRole("checkbox", { name: "Hash" }); - await expect(view.getByRole("checkbox")).toHaveCount(5); + await expect(view.getByRole("checkbox")).toHaveCount(4); await expect(columns.getByRole("checkbox")).toHaveCount(4); for (const width of [1000, 520]) { diff --git a/src/features/vcs/LogScope.tsx b/src/features/vcs/LogScope.tsx index 8e8d3d794a..d4ec923de0 100644 --- a/src/features/vcs/LogScope.tsx +++ b/src/features/vcs/LogScope.tsx @@ -1,7 +1,7 @@ import { useEffect, useMemo, type ReactNode } from "react"; import type { EditorInput } from "@/workbench/contributions"; import { LogIdContext, LogOptionsContext } from "./logContext"; -import { rootLogOptions } from "./logOptions"; +import { applicationLogOptions, rootLogOptions } from "./logOptions"; import { ensureEditorLog, focusLogView, parkedLogOptions, useRootGitLog } from "./logStore"; import { LogView } from "./GitLog"; import styles from "./GitLog.module.css"; @@ -9,7 +9,11 @@ import styles from "./GitLog.module.css"; export function LogScope({ id, children }: { id: string; children: ReactNode }) { const active = useRootGitLog((s) => s.shownLog); const options = rootLogOptions(); - const value = useMemo(() => (active === id ? options : (parkedLogOptions(id) ?? options)), [active, id, options]); + const value = useMemo(() => { + if (active === id) return options; + const parked = parkedLogOptions(id); + return parked ? { ...parked, ...applicationLogOptions(options) } : options; + }, [active, id, options]); const focus = () => { if (useRootGitLog.getState().shownLog !== id) focusLogView(id); }; diff --git a/src/features/vcs/LogSettingsPage.tsx b/src/features/vcs/LogSettingsPage.tsx index 12eae65eff..f70ccad693 100644 --- a/src/features/vcs/LogSettingsPage.tsx +++ b/src/features/vcs/LogSettingsPage.tsx @@ -1,17 +1,18 @@ /** Settings › Version Control › Log: how the Git Log shows references, dates and columns. */ import { useId, type ReactNode } from "react"; import { Checkbox, T } from "@/ui"; -import { setLogOptions, useLogOptions, type LogOptions } from "./logOptions"; +import { setLogOptions, useLogOptions, type ApplicationLogOptions } from "./logOptions"; import styles from "./LogSettingsPage.module.css"; -type Flag = { [K in keyof LogOptions]: LogOptions[K] extends boolean ? K : never }[keyof LogOptions]; +type Flag = { + [K in keyof ApplicationLogOptions]: ApplicationLogOptions[K] extends boolean ? K : never; +}[keyof ApplicationLogOptions]; const OPTIONS: { key: Flag; label: string }[] = [ { key: "compactRefs", label: "Show only the first reference" }, { key: "tagNames", label: "Show tag names" }, { key: "commitTimestamp", label: "Show commit time rather than author time" }, { key: "refsOnLeft", label: "Show references on the left" }, - { key: "showLineStats", label: "Show lines added and deleted" }, ]; const COLUMNS: { key: Flag; label: string }[] = [ diff --git a/src/features/vcs/__tests__/logOptions.test.ts b/src/features/vcs/__tests__/logOptions.test.ts index a11f1fdc4d..317048acee 100644 --- a/src/features/vcs/__tests__/logOptions.test.ts +++ b/src/features/vcs/__tests__/logOptions.test.ts @@ -145,10 +145,19 @@ describe("log options", () => { expect(setLogOptions({ diffPreview: false })).toBe(false); }); - it("reloads only for options that change the query", () => { + it("reloads only for options that change the query and persists only application-level properties", () => { expect(setLogOptions({ compactRefs: false })).toBe(false); - expect(setLogOptions({ noMerges: true })).toBe(true); - expect(JSON.parse(localStorage.getItem("openidea.vcs.logOptions")!).noMerges).toBe(true); + expect(setLogOptions({ noMerges: true, showHash: true })).toBe(true); + const stored = JSON.parse(localStorage.getItem("openidea.vcs.logOptions")!); + expect(stored).toMatchObject({ compactRefs: false, showHash: true }); + expect(stored).not.toHaveProperty("noMerges"); + }); + + it("applies application settings from another window without replacing the active tab's options", () => { + setLogOptions({ noMerges: true }); + localStorage.setItem("openidea.vcs.logOptions", JSON.stringify({ compactRefs: false })); + window.dispatchEvent(new StorageEvent("storage", { key: "openidea.vcs.logOptions" })); + expect(useLogOptions.getState()).toMatchObject({ noMerges: true, compactRefs: false }); }); it("sends the graph options and the date range to the backend", async () => { diff --git a/src/features/vcs/__tests__/logPanes.test.tsx b/src/features/vcs/__tests__/logPanes.test.tsx index 2567d4cf85..269cea4589 100644 --- a/src/features/vcs/__tests__/logPanes.test.tsx +++ b/src/features/vcs/__tests__/logPanes.test.tsx @@ -530,7 +530,5 @@ describe("Settings › Version Control › Log", () => { expect(useLogOptions.getState().tagNames).toBe(!DEFAULT_LOG_OPTIONS.tagNames); fireEvent.click(screen.getByLabelText("Hash")); expect(useLogOptions.getState().showHash).toBe(!DEFAULT_LOG_OPTIONS.showHash); - fireEvent.click(screen.getByLabelText("Show lines added and deleted")); - expect(useLogOptions.getState().showLineStats).toBe(true); }); }); diff --git a/src/features/vcs/__tests__/logViews.test.tsx b/src/features/vcs/__tests__/logViews.test.tsx index 0b7d349d66..a2ca762273 100644 --- a/src/features/vcs/__tests__/logViews.test.tsx +++ b/src/features/vcs/__tests__/logViews.test.tsx @@ -58,8 +58,12 @@ afterEach(async () => { function Probe({ name }: { name: string }) { const text = useGitLog((s) => s.filters.text); - const hash = useLogOptions((s) => s.showHash); - return ; + const longEdges = useLogOptions((s) => s.longEdges); + return ( + + ); } describe("independent log views", () => { @@ -67,7 +71,7 @@ describe("independent log views", () => { setLogFilters({ text: "main" }); const other = openLogTab("log", "Other"); setLogFilters({ text: "other" }); - updateLogOptions({ showHash: true }); + updateLogOptions({ longEdges: true }); selectGitTab("log"); render( <> @@ -79,16 +83,16 @@ describe("independent log views", () => { , ); - expect(screen.getByText("main: main / hash false")).toBeInTheDocument(); - expect(screen.getByText("other: other / hash true")).toBeInTheDocument(); - const button = screen.getByText("other: other / hash true"); + expect(screen.getByText("main: main / long edges false")).toBeInTheDocument(); + expect(screen.getByText("other: other / long edges true")).toBeInTheDocument(); + const button = screen.getByText("other: other / long edges true"); await act(async () => { fireEvent.pointerDown(button); fireEvent.click(button); }); expect(useGitLog.getState().shownLog).toBe(other); - expect(screen.getByText("main: main / hash false")).toBeInTheDocument(); - expect(screen.getByText("other: other edited / hash true")).toBeInTheDocument(); + expect(screen.getByText("main: main / long edges false")).toBeInTheDocument(); + expect(screen.getByText("other: other edited / long edges true")).toBeInTheDocument(); }); it("restores each tab's filters and options after leaving and reopening the project", async () => { @@ -105,8 +109,9 @@ describe("independent log views", () => { selected: "/repo\0expanded", selection: ["/repo\0expanded"], }); - updateLogOptions({ showHash: true, diffLocation: "bottom" }); + updateLogOptions({ longEdges: true, diffLocation: "bottom" }); selectGitTab("log"); + expect(useLogOptions.getState()).toMatchObject({ longEdges: false, diffLocation: "bottom" }); await reloadLog(); useVcs.setState({ root: "/another", repo: "/another", roots: ["/another"] }); useVcs.setState({ root: "/repo", repo: "/repo", roots: ["/repo"] }); @@ -114,7 +119,7 @@ describe("independent log views", () => { expect(useGitLog.getState().fragments.expanded.size).toBe(0); selectGitTab(other); expect(useGitLog.getState().filters).toMatchObject({ paths: ["/repo/src"], branches: ["feature/.*"] }); - expect(useLogOptions.getState()).toMatchObject({ showHash: true, diffLocation: "bottom" }); + expect(useLogOptions.getState()).toMatchObject({ longEdges: true, diffLocation: "bottom" }); expect([...useGitLog.getState().fragments.expanded]).toEqual(["/repo\0expanded"]); expect([...useGitLog.getState().fragments.collapsed]).toEqual(["/repo\0collapsed"]); expect([...useGitLog.getState().fragments.merges]).toEqual(["/repo\0merge"]); diff --git a/src/features/vcs/logOptions.ts b/src/features/vcs/logOptions.ts index 5cf5f152bb..ecb2338368 100644 --- a/src/features/vcs/logOptions.ts +++ b/src/features/vcs/logOptions.ts @@ -86,6 +86,24 @@ export const DEFAULT_LOG_OPTIONS: LogOptions = { /** Options that change which commits the backend returns (a reload is needed). */ export const QUERY_OPTIONS: ReadonlyArray = ["sort", "firstParent", "noMerges"]; +const APPLICATION_LOG_OPTION_KEYS = [ + "compactRefs", + "tagNames", + "commitTimestamp", + "refsOnLeft", + "showAuthor", + "showDate", + "showHash", + "showSignature", + "showGitHubChecks", + "diffPreview", + "changesFromParents", + "diffLocation", + "columnOrder", +] as const satisfies readonly (keyof LogOptions)[]; + +export type ApplicationLogOptions = Pick; + const KEY = "openidea.vcs.logOptions"; export function parseLogOptions(raw: unknown): LogOptions { @@ -112,16 +130,20 @@ export function parseLogOptions(raw: unknown): LogOptions { return out; } -function load(): LogOptions { +function pickApplicationOptions(options: LogOptions): ApplicationLogOptions { + return Object.fromEntries(APPLICATION_LOG_OPTION_KEYS.map((key) => [key, options[key]])) as ApplicationLogOptions; +} + +function loadApplicationOptions(): ApplicationLogOptions { try { const raw = localStorage.getItem(KEY); - return parseLogOptions(raw ? JSON.parse(raw) : null); + return pickApplicationOptions(parseLogOptions(raw ? JSON.parse(raw) : null)); } catch { - return { ...DEFAULT_LOG_OPTIONS }; + return pickApplicationOptions(DEFAULT_LOG_OPTIONS); } } -export const rootLogOptions = create(() => load()); +export const rootLogOptions = create(() => ({ ...DEFAULT_LOG_OPTIONS, ...loadApplicationOptions() })); export const useLogOptions = Object.assign(function useScopedLogOptions( selector?: (state: LogOptions) => T, ): T { @@ -129,13 +151,25 @@ export const useLogOptions = Object.assign(function useScopedLogOptions (selector ? selector(scoped ?? state) : ((scoped ?? state) as T))); }, rootLogOptions); +export function applicationLogOptions(options: LogOptions = rootLogOptions.getState()): ApplicationLogOptions { + return pickApplicationOptions(options); +} + +export function withApplicationLogOptions(options: Partial): LogOptions { + return { ...DEFAULT_LOG_OPTIONS, ...options, ...applicationLogOptions() }; +} + +export function setActiveLogOptions(options: Partial): void { + rootLogOptions.setState(withApplicationLogOptions(options)); +} + // Settings › Version Control › Log edits them from the Settings window too. if (typeof window !== "undefined") window.addEventListener("storage", (e) => { - if (e.key === KEY) useLogOptions.setState(load()); + if (e.key === KEY) rootLogOptions.setState((state) => ({ ...state, ...loadApplicationOptions() })); }); -export const logOptions = () => useLogOptions.getState(); +export const logOptions = () => rootLogOptions.getState(); export type RefKind = "head" | "local" | "remote" | "tag"; @@ -184,13 +218,14 @@ export function refLabels( return out; } -/** Updates options and persists them; returns whether the log has to be reloaded. */ +/** Updates options and persists application-level properties; returns whether the log has to be reloaded. */ export function setLogOptions(patch: Partial): boolean { - const before = useLogOptions.getState(); - useLogOptions.setState(patch); - const after = useLogOptions.getState(); + const before = rootLogOptions.getState(); + const after = { ...before, ...patch }; + rootLogOptions.setState(after); try { - localStorage.setItem(KEY, JSON.stringify(after)); + // Per-tab properties are saved in the log session, not in the application store. + localStorage.setItem(KEY, JSON.stringify(applicationLogOptions(after))); } catch { // Storage unavailable: the options last for this session. } diff --git a/src/features/vcs/logStore.ts b/src/features/vcs/logStore.ts index ceeb2dba0a..ddb64f3668 100644 --- a/src/features/vcs/logStore.ts +++ b/src/features/vcs/logStore.ts @@ -9,7 +9,15 @@ import { tx } from "@/platform/i18n"; import type { Branch, BranchInfo, Commit, LogQuery } from "@/bindings"; import { call, commands, nextRequestId, type RequestId } from "@/platform/ipc"; import { workflowSettings } from "@/platform/workflowSettings"; -import { logOptions, parseLogOptions, setLogOptions, useLogOptions, type LogOptions } from "./logOptions"; +import { + logOptions, + parseLogOptions, + setActiveLogOptions, + setLogOptions, + useLogOptions, + withApplicationLogOptions, + type LogOptions, +} from "./logOptions"; import { clearConsole, errorMessage } from "./ops"; import { currentRepo, currentRepoRoot, repoName, repoOf, useVcs } from "./store"; import { chooseLogCommit } from "./CommitChooser"; @@ -382,6 +390,7 @@ function freshView(filters: LogFilters): LogView { /** Views of the log tabs not shown now. */ const parked = new Map(); +let switchingLogView = false; function currentView(): LogView { const { @@ -438,12 +447,19 @@ function setViewState(id: string, patch: Partial | ((state: LogState) function showLogView(id: string) { const s = useGitLog.getState(); if (s.shownLog === id) return; - parked.set(s.shownLog, currentView()); - const view = parked.get(id) ?? freshView(emptyFilters()); - parked.delete(id); - const { options, ...state } = view; - setLogOptions(options); - useGitLog.setState({ ...state, shownLog: id }); + switchingLogView = true; + try { + parked.set(s.shownLog, currentView()); + const view = parked.get(id) ?? freshView(emptyFilters()); + parked.delete(id); + const { options, ...state } = view; + useGitLog.setState({ ...state, shownLog: id }); + setActiveLogOptions(options); + } finally { + switchingLogView = false; + } + saveLogSession(useVcs.getState().root); + const view = logState(id); if (!view.commits.length && !view.done && !view.loading) void reloadLog(); } @@ -626,7 +642,7 @@ export function installGitLog(): () => void { const comparisons = new Set(logTabs.filter((tab) => tab.comparison).map((tab) => tab.id)); for (const [id, view] of parked) { if (comparisons.has(id)) void reloadLog({ preserve: true }, id); - else parked.set(id, { ...freshView(view.filters), options: view.options }); + else parked.set(id, { ...freshView(view.filters), options: withApplicationLogOptions(view.options) }); } void reloadLog({ preserve: comparisons.has(shownLog) }); } @@ -699,7 +715,7 @@ function parseFilters(value: unknown): LogFilters { } function saveLogSession(root: string | null) { - if (!root || restoring) return; + if (!root || restoring || switchingLogView) return; const state = useGitLog.getState(); const views = new Map(parked).set(state.shownLog, currentView()); try { @@ -757,7 +773,7 @@ function restoreLogSession(root: string | null) { tabs.find((tab) => tab.id === view.id), ), ), - options: parseLogOptions(view.options), + options: withApplicationLogOptions(parseLogOptions(view.options)), fragments: parseFragments(view.fragments), selected: typeof view.selected === "string" ? view.selected : null, selection: Array.isArray(view.selection) @@ -772,7 +788,7 @@ function restoreLogSession(root: string | null) { const view = parked.get(id) ?? freshView(emptyFilters()); parked.delete(id); const { options, ...state } = view; - setLogOptions(options); + setActiveLogOptions(options); const bookmarks = value.bookmarks && typeof value.bookmarks === "object" ? Object.fromEntries(