Skip to content

Commit 371b52d

Browse files
maria-rcksclaude
andauthored
feat(web): answer pull request actions on the row at once (pingdotgg#12843)
Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 1de563c commit 371b52d

4 files changed

Lines changed: 367 additions & 15 deletions

File tree

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,12 @@ export function PullRequestDetailPanel({
458458
* An action changed this pull request on the host, so a list showing it is now out of date.
459459
* Told rather than assumed: only the page knows whether it is showing one.
460460
*/
461-
onActed?: () => void;
461+
/**
462+
* Each host action as it goes: "sent" the moment it leaves, so a list can answer before the
463+
* host does; "done" or "failed" when the host has spoken. Undefined for one the caller cannot
464+
* name, which is only ever "done".
465+
*/
466+
onActed?: (action?: PullRequestAction, phase?: "sent" | "done" | "failed") => void;
462467
/** Page-owned detail columns use this to clear the selected pull request. */
463468
onClose?: () => void;
464469
/**
@@ -961,6 +966,7 @@ export function PullRequestDetailPanel({
961966
method?: PullRequestMergeMethod,
962967
updateMethod?: PullRequestUpdateMethod,
963968
) => {
969+
onActed?.(action, "sent");
964970
const result = await runAction({
965971
environmentId,
966972
input: {
@@ -988,6 +994,7 @@ export function PullRequestDetailPanel({
988994
title: ACTION_FAILURE_LABELS[action],
989995
description: readableFailure(failure, hint),
990996
});
997+
onActed?.(action, "failed");
991998
return false;
992999
}
9931000
toastManager.add({ type: "success", title: ACTION_SUCCESS_LABELS[action] });
@@ -1001,7 +1008,7 @@ export function PullRequestDetailPanel({
10011008
} else {
10021009
refreshDetail();
10031010
}
1004-
onActed?.();
1011+
onActed?.(action, "done");
10051012
return true;
10061013
};
10071014

‎apps/web/src/components/pullRequest/pullRequestList.logic.test.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import {
3232
resolveQueryEnvironmentIds,
3333
resolveSelectedEnvironmentId,
3434
type EnvironmentPullRequestEntry,
35+
applyPullRequestOverrides,
36+
pullRequestOverrideAfterAction,
37+
reusePullRequestEntries,
38+
settlePullRequestOverrides,
3539
} from "./pullRequestList.logic";
3640
import {
3741
pullRequestListPreferences,
@@ -1600,3 +1604,93 @@ describe("the priority groups against a paginated feed", () => {
16001604
).toEqual([6123]);
16011605
});
16021606
});
1607+
1608+
describe("pull request list overrides", () => {
1609+
const entry = (number: number, state: "open" | "closed" | "merged") =>
1610+
({
1611+
host: "github.com",
1612+
repository: "pingdotgg/t3code",
1613+
number,
1614+
state,
1615+
isDraft: false,
1616+
updatedAt: "2026-07-01T00:00:00Z",
1617+
labels: [],
1618+
}) as unknown as PullRequestListEntry;
1619+
const key = (row: { number: number }) => `#${row.number}`;
1620+
1621+
it("maps the actions that change a row's state and nothing else", () => {
1622+
const now = new Date("2026-07-02T00:00:00Z");
1623+
expect(pullRequestOverrideAfterAction(entry(1, "open"), "close", now, 7)).toEqual({
1624+
state: "closed",
1625+
updatedAt: "2026-07-02T00:00:00.000Z",
1626+
token: 7,
1627+
at: now.getTime(),
1628+
});
1629+
expect(pullRequestOverrideAfterAction(entry(1, "closed"), "reopen", now, 1)?.state).toBe(
1630+
"open",
1631+
);
1632+
expect(pullRequestOverrideAfterAction(entry(1, "open"), "merge", now, 1)?.state).toBe("merged");
1633+
expect(pullRequestOverrideAfterAction(entry(1, "open"), "draft", now, 1)?.isDraft).toBe(true);
1634+
expect(pullRequestOverrideAfterAction(entry(1, "open"), "update-branch", now, 1)).toBeNull();
1635+
});
1636+
1637+
it("writes the override over the row and drops it from a list whose state it left", () => {
1638+
const rows = [entry(1, "open"), entry(2, "open")];
1639+
const overrides = new Map([
1640+
["#1", { state: "closed" as const, updatedAt: "2026-07-03T00:00:00Z", token: 1, at: 0 }],
1641+
]);
1642+
expect(
1643+
applyPullRequestOverrides(rows, overrides, key, "open").map((row) => row.number),
1644+
).toEqual([2]);
1645+
const all = applyPullRequestOverrides(rows, overrides, key, "all");
1646+
expect(all.map((row) => [row.number, row.state])).toEqual([
1647+
[1, "closed"],
1648+
[2, "open"],
1649+
]);
1650+
expect(applyPullRequestOverrides(rows, new Map(), key, "open")).toBe(rows);
1651+
});
1652+
1653+
it("hands back the held object for a row a refresh did not change", () => {
1654+
const previous = [entry(1, "open"), entry(2, "open")];
1655+
const next = [{ ...entry(1, "open") }, { ...entry(2, "open"), state: "merged" as const }];
1656+
const reused = reusePullRequestEntries(previous, next, key);
1657+
expect(reused[0]).toBe(previous[0]);
1658+
expect(reused[1]).toBe(next[1]);
1659+
expect(
1660+
reusePullRequestEntries(previous, [{ ...entry(1, "open") }, { ...entry(2, "open") }], key),
1661+
).toBe(previous);
1662+
});
1663+
});
1664+
1665+
describe("pull request list override settlement", () => {
1666+
const entry = (number: number, state: "open" | "closed" | "merged") =>
1667+
({ number, state, isDraft: false, labels: [] }) as unknown as PullRequestListEntry;
1668+
const key = (row: { number: number }) => `#${row.number}`;
1669+
1670+
it("keeps an override until an answer agrees with it", () => {
1671+
const at = 1_000_000;
1672+
const closed = { state: "closed" as const, updatedAt: "2026-07-03T00:00:00Z", token: 1, at };
1673+
const overrides = new Map([["#1", closed]]);
1674+
// A read from before the action still says open: the override stands.
1675+
expect(settlePullRequestOverrides(overrides, [entry(1, "open")], key, at + 5_000)).toBe(
1676+
overrides,
1677+
);
1678+
// Absent from the answer says nothing: the row may live in another group or page.
1679+
expect(settlePullRequestOverrides(overrides, [entry(2, "open")], key, at + 5_000).size).toBe(1);
1680+
// Present as closed: confirmed.
1681+
expect(settlePullRequestOverrides(overrides, [entry(1, "closed")], key, at + 5_000).size).toBe(
1682+
0,
1683+
);
1684+
// Present as open a good while later: the host's news, which outranks the note.
1685+
expect(settlePullRequestOverrides(overrides, [entry(1, "open")], key, at + 90_000).size).toBe(
1686+
0,
1687+
);
1688+
});
1689+
1690+
it("does not hand back the old order when only the order changed", () => {
1691+
const previous = [entry(1, "open"), entry(2, "open")];
1692+
const swapped = reusePullRequestEntries(previous, [entry(2, "open"), entry(1, "open")], key);
1693+
expect(swapped).not.toBe(previous);
1694+
expect(swapped.map((row) => row.number)).toEqual([2, 1]);
1695+
});
1696+
});

‎apps/web/src/components/pullRequest/pullRequestList.logic.ts‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import {
99
} from "@t3tools/contracts";
1010
import type {
1111
ProjectId,
12+
PullRequestAction,
1213
PullRequestActor,
1314
PullRequestDiffStat,
1415
PullRequestInvolvement,
1516
PullRequestLabel,
1617
PullRequestListCursors,
1718
PullRequestListFilters,
1819
PullRequestListState,
20+
PullRequestState,
1921
} from "@t3tools/contracts";
2022

2123
import { toSortableTimestamp } from "../../lib/threadSort";
@@ -1140,3 +1142,127 @@ export function withDiffStat<
11401142
const stat = statsByRow.get(pullRequestDiffStatKey(entry));
11411143
return stat === undefined ? entry : { ...entry, ...stat };
11421144
}
1145+
1146+
/**
1147+
* What a row should say the moment an action is sent, before any host has answered. The host
1148+
* is the record and a later read replaces this, but the reader pressed the button and should
1149+
* see the row answer at once: a closed pull request leaves an "open" list on the click, not
1150+
* after the reads that follow.
1151+
*/
1152+
export interface PullRequestListOverride {
1153+
readonly state: PullRequestState;
1154+
readonly isDraft?: boolean;
1155+
readonly updatedAt: string;
1156+
/** Which action wrote it, so a failure takes back its own note and not a later one's. */
1157+
readonly token: number;
1158+
/** When it was written, in the reader's clock. */
1159+
readonly at: number;
1160+
}
1161+
1162+
export function pullRequestOverrideAfterAction(
1163+
entry: Pick<PullRequestListEntry, "state" | "isDraft">,
1164+
action: PullRequestAction,
1165+
now: Date,
1166+
token: number,
1167+
): PullRequestListOverride | null {
1168+
const stamp = { updatedAt: now.toISOString(), token, at: now.getTime() };
1169+
switch (action) {
1170+
case "close":
1171+
return { state: "closed", ...stamp };
1172+
case "reopen":
1173+
return { state: "open", ...stamp };
1174+
case "merge":
1175+
return { state: "merged", ...stamp };
1176+
case "draft":
1177+
return { state: entry.state, isDraft: true, ...stamp };
1178+
case "ready":
1179+
return { state: entry.state, isDraft: false, ...stamp };
1180+
default:
1181+
return null;
1182+
}
1183+
}
1184+
1185+
/** The rows with their pending answers written over them, and the ones the list's state filter no longer holds dropped. */
1186+
export function applyPullRequestOverrides<Entry extends PullRequestListEntry>(
1187+
entries: ReadonlyArray<Entry>,
1188+
overrides: ReadonlyMap<string, PullRequestListOverride>,
1189+
keyOf: (entry: Entry) => string,
1190+
state: PullRequestListState,
1191+
): ReadonlyArray<Entry> {
1192+
if (overrides.size === 0) return entries;
1193+
const out: Entry[] = [];
1194+
for (const entry of entries) {
1195+
const override = overrides.get(keyOf(entry));
1196+
if (override === undefined) {
1197+
out.push(entry);
1198+
continue;
1199+
}
1200+
if (state !== "all" && override.state !== state) continue;
1201+
out.push({ ...entry, ...override });
1202+
}
1203+
return out;
1204+
}
1205+
1206+
/**
1207+
* A fresh answer with the rows it did not change handed back as the objects already held, so a
1208+
* memoized row whose data is the same does not render again. Every refresh otherwise rebuilds
1209+
* every entry, and a hundred rows repaint for the one that moved.
1210+
*/
1211+
export function reusePullRequestEntries<Entry extends PullRequestListEntry>(
1212+
previous: ReadonlyArray<Entry>,
1213+
next: ReadonlyArray<Entry>,
1214+
keyOf: (entry: Entry) => string,
1215+
): ReadonlyArray<Entry> {
1216+
if (previous.length === 0) return next;
1217+
const held = new Map(previous.map((entry) => [keyOf(entry), entry]));
1218+
let reused = 0;
1219+
const out = next.map((entry) => {
1220+
const before = held.get(keyOf(entry));
1221+
if (before !== undefined && JSON.stringify(before) === JSON.stringify(entry)) {
1222+
reused += 1;
1223+
return before;
1224+
}
1225+
return entry;
1226+
});
1227+
return reused === next.length &&
1228+
previous.length === next.length &&
1229+
previous.every((entry, index) => keyOf(entry) === keyOf(next[index]!))
1230+
? previous
1231+
: out;
1232+
}
1233+
1234+
/** How long a read that disagrees is taken for a stale one rather than for news. */
1235+
const PULL_REQUEST_OVERRIDE_TRUST_MS = 60_000;
1236+
1237+
/**
1238+
* The overrides an answer has confirmed, dropped; the rest kept. A read that started before
1239+
* the action can land after it and still say the old thing, so an override is not cleared
1240+
* because an answer arrived but because the answer agrees: the row is there in the state the
1241+
* override said. A row that is absent says nothing — the authored and reviewing groups are read
1242+
* apart from the feed, and a page is only a page — so absence never confirms. A row present in
1243+
* another state is taken for a stale read for a minute, and for the host's news after that,
1244+
* which is how a pull request reopened elsewhere comes back.
1245+
*/
1246+
export function settlePullRequestOverrides<Entry extends PullRequestListEntry>(
1247+
overrides: ReadonlyMap<string, PullRequestListOverride>,
1248+
answered: ReadonlyArray<Entry>,
1249+
keyOf: (entry: Entry) => string,
1250+
now: number,
1251+
): ReadonlyMap<string, PullRequestListOverride> {
1252+
if (overrides.size === 0) return overrides;
1253+
const byKey = new Map(answered.map((entry) => [keyOf(entry), entry]));
1254+
const kept = new Map<string, PullRequestListOverride>();
1255+
for (const [key, override] of overrides) {
1256+
const row = byKey.get(key);
1257+
if (row === undefined) {
1258+
kept.set(key, override);
1259+
continue;
1260+
}
1261+
const agrees =
1262+
row.state === override.state &&
1263+
(override.isDraft === undefined || row.isDraft === override.isDraft);
1264+
const outranked = now - override.at > PULL_REQUEST_OVERRIDE_TRUST_MS;
1265+
if (!agrees && !outranked) kept.set(key, override);
1266+
}
1267+
return kept.size === overrides.size ? overrides : kept;
1268+
}

0 commit comments

Comments
 (0)