TypeScript
fix(web): stored pull request details keep only the 20 most recent (#21245)
tiliakoos442d5ca6555e
diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.tsindex bb7f3b4620a..ee86b2ddc25 100644--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts@@ -1825,9 +1825,37 @@ describe("cached pull request detail", () => {return {getItem: (key: string) => held.get(key) ?? null,setItem: (key: string, value: string) => void held.set(key, value),+ removeItem: (key: string) => void held.delete(key),+ size: () => held.size,};};+ it("keeps only the 20 most recently saved pull requests", () => {+ const storage = makeStorage();+ const numbered = (number: number) => ({ ...reference, number });+ for (let number = 1; number <= 21; number += 1) {+ writePullRequestDetailSnapshot(storage, "env-1", numbered(number), detail({ number }));+ }+ expect(readPullRequestDetailSnapshot(storage, "env-1", numbered(1))).toBeNull();+ expect(readPullRequestDetailSnapshot(storage, "env-1", numbered(2))?.number).toBe(2);+ expect(readPullRequestDetailSnapshot(storage, "env-1", numbered(21))?.number).toBe(21);+ // Twenty snapshots plus the list of their keys.+ expect(storage.size()).toBe(21);+ });++ it("saving a pull request again keeps it as the newest without growing the list", () => {+ const storage = makeStorage();+ const numbered = (number: number) => ({ ...reference, number });+ for (let number = 1; number <= 20; number += 1) {+ writePullRequestDetailSnapshot(storage, "env-1", numbered(number), detail({ number }));+ }+ writePullRequestDetailSnapshot(storage, "env-1", numbered(1), detail({ number: 1 }));+ writePullRequestDetailSnapshot(storage, "env-1", numbered(21), detail({ number: 21 }));+ expect(readPullRequestDetailSnapshot(storage, "env-1", numbered(1))?.number).toBe(1);+ expect(readPullRequestDetailSnapshot(storage, "env-1", numbered(2))).toBeNull();+ expect(storage.size()).toBe(21);+ });+it("hydrates the last title, author, and counts so a reopen does not ghost the tab", () => {const storage = makeStorage();writePullRequestDetailSnapshot(storage, "env-1", reference, detail());diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.tsindex 6263cc9f51b..e27659d08f8 100644--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts@@ -1152,7 +1152,7 @@ export function canDeletePullRequestBranch();}-type SnapshotStorage = Pick<Storage, "getItem" | "setItem">;+type SnapshotStorage = Pick<Storage, "getItem" | "setItem" | "removeItem">;export function resolvePullRequestReferenceHost(reference: PullRequestRef,@@ -1203,17 +1203,38 @@ export function readPullRequestDetailSnapshot(}}+// Snapshot keys in the order they were last written, oldest first.+const PULL_REQUEST_DETAIL_SNAPSHOT_INDEX_KEY = "t3.pullRequests.recentDetails";+const PULL_REQUEST_DETAIL_SNAPSHOT_LIMIT = 20;++function readPullRequestDetailSnapshotIndex(storage: SnapshotStorage): string[] {+ try {+ const keys: unknown = JSON.parse(+ storage.getItem(PULL_REQUEST_DETAIL_SNAPSHOT_INDEX_KEY) ?? "[]",+ );+ return Array.isArray(keys) ? keys.filter((key): key is string => typeof key === "string") : [];+ } catch {+ return [];+ }+}++// Keeps the snapshots of the 20 most recently saved pull requests and removes older ones.export function writePullRequestDetailSnapshot(storage: SnapshotStorage | undefined,environmentId: string,reference: PullRequestDetailSnapshotRef,detail: PullRequestDetail,): void {+ if (!storage) return;try {- storage?.setItem(- pullRequestDetailSnapshotKey(environmentId, reference),- JSON.stringify(detail),- );+ const key = pullRequestDetailSnapshotKey(environmentId, reference);+ const recent = readPullRequestDetailSnapshotIndex(storage).filter((entry) => entry !== key);+ recent.push(key);+ for (const evicted of recent.splice(0, recent.length - PULL_REQUEST_DETAIL_SNAPSHOT_LIMIT)) {+ storage.removeItem(evicted);+ }+ storage.setItem(PULL_REQUEST_DETAIL_SNAPSHOT_INDEX_KEY, JSON.stringify(recent));+ storage.setItem(key, JSON.stringify(detail));} catch {}}