Skip to content

Commit 07bb8b2

Browse files
perf(web): virtualize a thread's linked pull requests
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bb4e4e7 commit 07bb8b2

1 file changed

Lines changed: 42 additions & 24 deletions

File tree

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

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
MoreHorizontalIcon,
1616
PlusIcon,
1717
} from "lucide-react";
18-
import { useCallback, useMemo, useState } from "react";
18+
import { LegendList } from "@legendapp/list/react";
19+
import { memo, useCallback, useMemo, useState } from "react";
1920

2021
import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
2122
import { findProjectForChangeRequest, useOpenPrLink } from "~/lib/openPullRequestLink";
@@ -29,7 +30,6 @@ import { formatRelativeTimeLabel } from "~/timestampFormat";
2930
import { Button } from "../ui/button";
3031
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
3132
import { MiddleTruncate } from "../ui/middle-truncate";
32-
import { ScrollArea } from "../ui/scroll-area";
3333
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
3434
import { openLinkPullRequestDialog } from "./LinkPullRequestDialog";
3535
import { pullRequestListLines, type PullRequestListLine } from "./pullRequestListLines";
@@ -78,7 +78,7 @@ function ChecksGlyph({
7878
);
7979
}
8080

81-
function LinkRow({
81+
function LinkRowImpl({
8282
line,
8383
threadRef,
8484
projectId,
@@ -326,6 +326,12 @@ function LinkRow({
326326
);
327327
}
328328

329+
/** Memoized so a modifier key or a sync of one link re-renders only the rows that changed. */
330+
const LinkRow = memo(LinkRowImpl);
331+
332+
const lineKey = (line: PullRequestListLine) =>
333+
`${line.link.host}/${line.link.repository}#${line.link.number}`;
334+
329335
export function ThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThreadRef }) {
330336
const configs = useServerConfigs();
331337
if (configs.get(threadRef.environmentId)?.environment.capabilities.threadPullRequests !== true) {
@@ -403,6 +409,13 @@ function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
403409
return latest;
404410
}, [links]);
405411

412+
// Legend re-renders visible rows only when data or extraData changes, so everything the rows
413+
// read from outside their line goes in here.
414+
const rowInputs = useMemo(
415+
() => ({ speedMode, environmentProjects, capabilities, supportsWatch, thread }),
416+
[speedMode, environmentProjects, capabilities, supportsWatch, thread],
417+
);
418+
406419
if (links.length === 0) {
407420
return (
408421
<div className="flex h-full flex-col items-center justify-center gap-2 p-6 text-center">
@@ -422,27 +435,32 @@ function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
422435

423436
return (
424437
<div className="flex h-full min-h-0 flex-col">
425-
<ScrollArea className="min-h-0 flex-1">
426-
<div className="flex flex-col p-1.5">
427-
{lines.map((line) => (
428-
<LinkRow
429-
key={`${line.link.host}/${line.link.repository}#${line.link.number}`}
430-
line={line}
431-
threadRef={threadRef}
432-
projectId={
433-
capabilities?.pullRequests === true
434-
? (findProjectForChangeRequest(environmentProjects, line.link)?.id ??
435-
thread?.projectId ??
436-
null)
437-
: null
438-
}
439-
speedMode={speedMode}
440-
onUnlink={handleUnlink}
441-
onSetWatching={supportsWatch ? handleSetWatching : null}
442-
/>
443-
))}
444-
</div>
445-
</ScrollArea>
438+
{/* Threads that run a stack or a sweep link hundreds of pull requests; only the rows in
439+
view are mounted. */}
440+
<LegendList<PullRequestListLine>
441+
data={lines}
442+
keyExtractor={lineKey}
443+
extraData={rowInputs}
444+
renderItem={({ item: line }) => (
445+
<LinkRow
446+
line={line}
447+
threadRef={threadRef}
448+
projectId={
449+
capabilities?.pullRequests === true
450+
? (findProjectForChangeRequest(environmentProjects, line.link)?.id ??
451+
thread?.projectId ??
452+
null)
453+
: null
454+
}
455+
speedMode={speedMode}
456+
onUnlink={handleUnlink}
457+
onSetWatching={supportsWatch ? handleSetWatching : null}
458+
/>
459+
)}
460+
estimatedItemSize={44}
461+
drawDistance={440}
462+
className="min-h-0 flex-1 overscroll-contain p-1.5"
463+
/>
446464
<footer className="flex items-center justify-between border-t border-border/60 px-2 py-1.5 text-2xs text-muted-foreground">
447465
<span>
448466
{openCount} open · {links.length} linked

0 commit comments

Comments
 (0)