mirror of
https://github.com/docmost/docmost.git
synced 2026-08-24 17:22:15 +10:00
feat(client): add resolveComparePair helper for history compare
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { resolveComparePair } from "./resolve-compare-pair";
|
||||||
|
|
||||||
|
// list is newest-first, matching usePageHistoryListQuery order
|
||||||
|
const items = [{ id: "v3" }, { id: "v2" }, { id: "v1" }];
|
||||||
|
|
||||||
|
describe("resolveComparePair", () => {
|
||||||
|
it("orders newer before older regardless of selection order", () => {
|
||||||
|
expect(resolveComparePair(items, ["v1", "v3"])).toEqual({
|
||||||
|
newerId: "v3",
|
||||||
|
olderId: "v1",
|
||||||
|
});
|
||||||
|
expect(resolveComparePair(items, ["v3", "v1"])).toEqual({
|
||||||
|
newerId: "v3",
|
||||||
|
olderId: "v1",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null unless exactly two versions are selected", () => {
|
||||||
|
expect(resolveComparePair(items, [])).toBeNull();
|
||||||
|
expect(resolveComparePair(items, ["v1"])).toBeNull();
|
||||||
|
expect(resolveComparePair(items, ["v1", "v2", "v3"])).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null when a selected id is not in the list", () => {
|
||||||
|
expect(resolveComparePair(items, ["v1", "missing"])).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null when the same id is selected twice", () => {
|
||||||
|
expect(resolveComparePair(items, ["v2", "v2"])).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { ComparePair } from "@/features/page-history/atoms/history-atoms";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves which of the two selected versions is newer using their position
|
||||||
|
* in the history list (list is newest-first: lower index = newer).
|
||||||
|
*/
|
||||||
|
export function resolveComparePair(
|
||||||
|
historyItems: { id: string }[],
|
||||||
|
selection: string[],
|
||||||
|
): ComparePair | null {
|
||||||
|
if (selection.length !== 2) return null;
|
||||||
|
const indexA = historyItems.findIndex((item) => item.id === selection[0]);
|
||||||
|
const indexB = historyItems.findIndex((item) => item.id === selection[1]);
|
||||||
|
if (indexA === -1 || indexB === -1 || indexA === indexB) return null;
|
||||||
|
return indexA < indexB
|
||||||
|
? { newerId: selection[0], olderId: selection[1] }
|
||||||
|
: { newerId: selection[1], olderId: selection[0] };
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user