Keep the saved order of recent repositories; it was reversed on every start
This commit is contained in:
+2
-5
@@ -10,7 +10,7 @@ import { isRefVisible, lineOwners, reachable, refKind, shortName } from "./lib/f
|
|||||||
import { layout } from "./lib/layout";
|
import { layout } from "./lib/layout";
|
||||||
import { textWidth } from "./lib/measure";
|
import { textWidth } from "./lib/measure";
|
||||||
import { colorOf } from "./lib/palette";
|
import { colorOf } from "./lib/palette";
|
||||||
import { addRecent } from "./lib/recents";
|
import { addRecent, mergeRecents } from "./lib/recents";
|
||||||
import type { FileDiff, Layout } from "./lib/diff";
|
import type { FileDiff, Layout } from "./lib/diff";
|
||||||
import type { ChangedFile, CommitDetails, FilterState, Ref, RefKind, RepoData } from "./lib/types";
|
import type { ChangedFile, CommitDetails, FilterState, Ref, RefKind, RepoData } from "./lib/types";
|
||||||
import DiffView from "./components/DiffView.vue";
|
import DiffView from "./components/DiffView.vue";
|
||||||
@@ -59,10 +59,7 @@ async function loadSettings() {
|
|||||||
if (typeof saved?.previewHeight === "number") settings.previewHeight = Math.max(PREVIEW_MIN, saved.previewHeight);
|
if (typeof saved?.previewHeight === "number") settings.previewHeight = Math.max(PREVIEW_MIN, saved.previewHeight);
|
||||||
if (typeof saved?.previewShown === "boolean") settings.previewShown = saved.previewShown;
|
if (typeof saved?.previewShown === "boolean") settings.previewShown = saved.previewShown;
|
||||||
if (saved?.diffLayout && LAYOUTS.includes(saved.diffLayout)) settings.diffLayout = saved.diffLayout;
|
if (saved?.diffLayout && LAYOUTS.includes(saved.diffLayout)) settings.diffLayout = saved.diffLayout;
|
||||||
if (Array.isArray(saved?.recents)) {
|
if (Array.isArray(saved?.recents)) settings.recents = mergeRecents(settings.recents, saved.recents);
|
||||||
// A repository opened before the settings arrived is already at the front.
|
|
||||||
settings.recents = saved.recents.reduceRight((list, p) => (list.includes(p) ? list : [...list, p]), settings.recents);
|
|
||||||
}
|
|
||||||
settingsLoaded = true;
|
settingsLoaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-1
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { addRecent, MAX_RECENTS } from "./recents";
|
import { addRecent, MAX_RECENTS, mergeRecents } from "./recents";
|
||||||
|
|
||||||
describe("addRecent", () => {
|
describe("addRecent", () => {
|
||||||
it("puts the path first", () => {
|
it("puts the path first", () => {
|
||||||
@@ -16,3 +16,12 @@ describe("addRecent", () => {
|
|||||||
expect(out).not.toContain(`/r${MAX_RECENTS - 1}`);
|
expect(out).not.toContain(`/r${MAX_RECENTS - 1}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("mergeRecents", () => {
|
||||||
|
it("keeps the saved order", () => {
|
||||||
|
expect(mergeRecents([], ["/a", "/b", "/c"])).toEqual(["/a", "/b", "/c"]);
|
||||||
|
});
|
||||||
|
it("puts a path opened before the settings arrived first, once", () => {
|
||||||
|
expect(mergeRecents(["/b"], ["/a", "/b", "/c"])).toEqual(["/b", "/a", "/c"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -4,3 +4,11 @@ export const MAX_RECENTS = 12;
|
|||||||
export function addRecent(list: string[], path: string): string[] {
|
export function addRecent(list: string[], path: string): string[] {
|
||||||
return [path, ...list.filter((p) => p !== path)].slice(0, MAX_RECENTS);
|
return [path, ...list.filter((p) => p !== path)].slice(0, MAX_RECENTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins the saved list with paths opened before the saved settings arrived: those come
|
||||||
|
* first, then the saved ones in their saved order, each path once.
|
||||||
|
*/
|
||||||
|
export function mergeRecents(openedFirst: string[], saved: string[]): string[] {
|
||||||
|
return [...openedFirst, ...saved.filter((p) => !openedFirst.includes(p))].slice(0, MAX_RECENTS);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user