Skip to content

Commit b3d0564

Browse files
refactor(web): the thread pull requests panel keeps its caching without "use memo" hooks (pingdotgg#20740)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 8653c8f commit b3d0564

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,6 @@ function LinkRowImpl({
334334
/** Memoized so a modifier key or a sync of one link re-renders only the rows that changed. */
335335
const LinkRow = memo(LinkRowImpl);
336336

337-
// LegendList re-renders its rows when `data` changes identity. Inline, the compiler
338-
// recomputes these lines whenever a modifier key or row input changes too.
339-
function usePullRequestLines(
340-
pullRequests: Parameters<typeof visibleThreadPullRequests>[0] | undefined,
341-
) {
342-
"use memo";
343-
const links = visibleThreadPullRequests(pullRequests ?? []);
344-
const lines = pullRequestListLines(resolveThreadPullRequestChains(links));
345-
return { links, lines };
346-
}
347-
348337
function latestSyncedAt(links: ReadonlyArray<ThreadPullRequestLink>): string | null {
349338
let latest: string | null = null;
350339
for (const link of links) {
@@ -378,6 +367,17 @@ function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
378367
.toSorted((left, right) =>
379368
left.id === thread?.projectId ? -1 : right.id === thread?.projectId ? 1 : 0,
380369
);
370+
// LegendList re-renders its rows when `data` changes identity. `links` and values derived
371+
// from it go through plain functions only here, before the next hook, so the compiler keeps
372+
// `lines` in its own cache scope.
373+
const links = visibleThreadPullRequests(thread?.pullRequests ?? []);
374+
const lines = pullRequestListLines(resolveThreadPullRequestChains(links));
375+
const openCount = links.filter(
376+
(link) => link.snapshot === null || link.snapshot.state === "open",
377+
).length;
378+
const lastSynced = latestSyncedAt(links);
379+
const syncedLabel = lastSynced ? ` · synced ${formatRelativeTimeLabel(lastSynced)}` : "";
380+
const linkCount = links.length;
381381
const modifiers = useShortcutModifierState(true);
382382
const speedMode =
383383
modifiers.shiftKey && !modifiers.metaKey && !modifiers.ctrlKey && !modifiers.altKey;
@@ -386,7 +386,6 @@ function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
386386
const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: true });
387387
const capabilities = useServerConfigs().get(threadRef.environmentId)?.environment.capabilities;
388388
const supportsWatch = capabilities?.threadPullRequestWatch === true;
389-
const { links, lines } = usePullRequestLines(thread?.pullRequests);
390389
const handleUnlink = (link: ThreadPullRequestLink) => {
391390
void unlink({
392391
environmentId: threadRef.environmentId,
@@ -410,16 +409,12 @@ function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
410409
},
411410
});
412411
};
413-
const openCount = links.filter(
414-
(link) => link.snapshot === null || link.snapshot.state === "open",
415-
).length;
416-
const lastSynced = latestSyncedAt(links);
417412

418413
// Legend re-renders visible rows only when data or extraData changes, so everything the rows
419414
// read from outside their line goes in here.
420415
const rowInputs = { speedMode, environmentProjects, capabilities, supportsWatch, thread };
421416

422-
if (links.length === 0) {
417+
if (linkCount === 0) {
423418
return (
424419
<div className="flex h-full flex-col items-center justify-center gap-2 p-6 text-center">
425420
<PullRequestGlyph.link aria-hidden className="size-6 text-muted-foreground/60" />
@@ -466,8 +461,8 @@ function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
466461
/>
467462
<footer className="flex items-center justify-between border-t border-border/60 px-2 py-1.5 text-2xs text-muted-foreground">
468463
<span>
469-
{openCount} open · {links.length} linked
470-
{lastSynced ? ` · synced ${formatRelativeTimeLabel(lastSynced)}` : ""}
464+
{openCount} open · {linkCount} linked
465+
{syncedLabel}
471466
</span>
472467
<Button size="xs" variant="ghost" onClick={openLinkDialog}>
473468
<PlusIcon className="size-3.5" />

0 commit comments

Comments
 (0)