Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
31 changes: 26 additions & 5 deletions apps/web/src/components/pullRequest/pullRequestDetail.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Comment thread
tiliakoos marked this conversation as resolved.
storage.setItem(key, JSON.stringify(detail));
Comment thread
tiliakoos marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
} catch {}
}

Expand Down
Loading