mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
BrowserRouter has no scroll handling and the browser's own restoration runs before the page has loaded its data, so it lands short. Track the window scroll per history entry in sessionStorage and restore it on POP, retrying as the page grows until it fits, the user scrolls or 5 s pass. Pushing a new pathname scrolls to the top; query-only pushes (table sort, filter, page) keep the position. Closes #1247 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
2.5 KiB
JavaScript
64 lines
2.5 KiB
JavaScript
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
loadScrollPositions, saveScrollPositions, scrollKey, setScrollPosition,
|
|
} from "../../packages/web_ui/src/util/useScrollRestoration.ts";
|
|
|
|
class MemoryStorage {
|
|
items = new Map();
|
|
getItem(key) { return this.items.get(key) ?? null; }
|
|
setItem(key, value) { this.items.set(key, String(value)); }
|
|
}
|
|
|
|
describe("web_ui/util/useScrollRestoration", function() {
|
|
describe("loadScrollPositions()", function() {
|
|
it("should round trip through saveScrollPositions", function() {
|
|
const storage = new MemoryStorage();
|
|
saveScrollPositions(storage, new Map([["a", 10], ["b", 20.5]]));
|
|
assert.deepEqual([...loadScrollPositions(storage)], [["a", 10], ["b", 20.5]]);
|
|
});
|
|
it("should return an empty map when nothing is stored", function() {
|
|
assert.equal(loadScrollPositions(new MemoryStorage()).size, 0);
|
|
});
|
|
it("should ignore corrupt data", function() {
|
|
const storage = new MemoryStorage();
|
|
storage.setItem("scroll_positions", "{not json");
|
|
assert.equal(loadScrollPositions(storage).size, 0);
|
|
storage.setItem("scroll_positions", JSON.stringify({ a: 1 }));
|
|
assert.equal(loadScrollPositions(storage).size, 0);
|
|
storage.setItem("scroll_positions", JSON.stringify([["a", 1], ["b", "x"], [2, 3]]));
|
|
assert.deepEqual([...loadScrollPositions(storage)], [["a", 1]]);
|
|
});
|
|
});
|
|
describe("saveScrollPositions()", function() {
|
|
it("should ignore storage errors", function() {
|
|
saveScrollPositions({ setItem() { throw new Error("QuotaExceededError"); } }, new Map([["a", 1]]));
|
|
});
|
|
});
|
|
describe("setScrollPosition()", function() {
|
|
it("should move an updated key to the newest position", function() {
|
|
const positions = new Map([["a", 1], ["b", 2]]);
|
|
setScrollPosition(positions, "a", 3);
|
|
assert.deepEqual([...positions], [["b", 2], ["a", 3]]);
|
|
});
|
|
it("should drop the oldest entries past 100", function() {
|
|
const positions = new Map();
|
|
for (let i = 0; i < 105; i++) {
|
|
setScrollPosition(positions, `k${i}`, i);
|
|
}
|
|
assert.equal(positions.size, 100);
|
|
assert.equal(positions.has("k4"), false);
|
|
assert.equal(positions.get("k5"), 5);
|
|
assert.equal(positions.get("k104"), 104);
|
|
});
|
|
});
|
|
describe("scrollKey()", function() {
|
|
it("should use the location key", function() {
|
|
assert.equal(scrollKey({ key: "abc123", pathname: "/users", search: "" }), "abc123");
|
|
});
|
|
it("should fall back to the path for entries created outside the router", function() {
|
|
assert.equal(scrollKey({ key: "default", pathname: "/users", search: "?q=a" }), "/users?q=a");
|
|
});
|
|
});
|
|
});
|