Skip to content
TypeScript

perf(web): virtualize a thread's linked pull requests (#20107)

Julius Marminged5139979d427
diff --git a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
index 23e877ee058..fc565b5327d 100644
--- a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
+++ b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
@@ -15,7 +15,8 @@ import {
MoreHorizontalIcon,
PlusIcon,
} from "lucide-react";
-import { useCallback, useMemo, useState } from "react";
+import { LegendList } from "@legendapp/list/react";
+import { memo, useCallback, useMemo, useState } from "react";
import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
import { findProjectForChangeRequest, useOpenPrLink } from "~/lib/openPullRequestLink";
@@ -29,7 +30,6 @@ import { formatRelativeTimeLabel } from "~/timestampFormat";
import { Button } from "../ui/button";
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu";
import { MiddleTruncate } from "../ui/middle-truncate";
-import { ScrollArea } from "../ui/scroll-area";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { openLinkPullRequestDialog } from "./LinkPullRequestDialog";
import { pullRequestListLines, type PullRequestListLine } from "./pullRequestListLines";
@@ -78,7 +78,7 @@ function ChecksGlyph({
);
}
-function LinkRow({
+function LinkRowImpl({
line,
threadRef,
projectId,
@@ -326,6 +326,12 @@ function LinkRow({
);
}
+/** Memoized so a modifier key or a sync of one link re-renders only the rows that changed. */
+const LinkRow = memo(LinkRowImpl);
+
+const lineKey = (line: PullRequestListLine) =>
+ `${line.link.host}/${line.link.repository}#${line.link.number}`;
+
export function ThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThreadRef }) {
const configs = useServerConfigs();
if (configs.get(threadRef.environmentId)?.environment.capabilities.threadPullRequests !== true) {
@@ -403,6 +409,13 @@ function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
return latest;
}, [links]);
+ // Legend re-renders visible rows only when data or extraData changes, so everything the rows
+ // read from outside their line goes in here.
+ const rowInputs = useMemo(
+ () => ({ speedMode, environmentProjects, capabilities, supportsWatch, thread }),
+ [speedMode, environmentProjects, capabilities, supportsWatch, thread],
+ );
+
if (links.length === 0) {
return (
<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
return (
<div className="flex h-full min-h-0 flex-col">
- <ScrollArea className="min-h-0 flex-1">
- <div className="flex flex-col p-1.5">
- {lines.map((line) => (
- <LinkRow
- key={`${line.link.host}/${line.link.repository}#${line.link.number}`}
- line={line}
- threadRef={threadRef}
- projectId={
- capabilities?.pullRequests === true
- ? (findProjectForChangeRequest(environmentProjects, line.link)?.id ??
- thread?.projectId ??
- null)
- : null
- }
- speedMode={speedMode}
- onUnlink={handleUnlink}
- onSetWatching={supportsWatch ? handleSetWatching : null}
- />
- ))}
- </div>
- </ScrollArea>
+ {/* Threads that run a stack or a sweep link hundreds of pull requests; only the rows in
+ view are mounted. */}
+ <LegendList<PullRequestListLine>
+ data={lines}
+ keyExtractor={lineKey}
+ extraData={rowInputs}
+ renderItem={({ item: line }) => (
+ <LinkRow
+ line={line}
+ threadRef={threadRef}
+ projectId={
+ capabilities?.pullRequests === true
+ ? (findProjectForChangeRequest(environmentProjects, line.link)?.id ??
+ thread?.projectId ??
+ null)
+ : null
+ }
+ speedMode={speedMode}
+ onUnlink={handleUnlink}
+ onSetWatching={supportsWatch ? handleSetWatching : null}
+ />
+ )}
+ estimatedItemSize={44}
+ drawDistance={440}
+ className="min-h-0 flex-1 overscroll-contain p-1.5"
+ />
<footer className="flex items-center justify-between border-t border-border/60 px-2 py-1.5 text-2xs text-muted-foreground">
<span>
{openCount} open · {links.length} linked