Skip to content
TypeScript

perf(web): Shift re-renders only each row's quick-action group (#22726)

Julius Marminge57d784404ed1
diff --git a/apps/web/src/components/pullRequest/PullRequestListGroups.tsx b/apps/web/src/components/pullRequest/PullRequestListGroups.tsx
index 2045896eaa8..7e8c4e3a26f 100644
--- a/apps/web/src/components/pullRequest/PullRequestListGroups.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestListGroups.tsx
@@ -3,7 +3,6 @@ import { EyeIcon, LayersIcon, PenLineIcon, UsersIcon, type LucideIcon } from "lu
import type { RefCallback } from "react";
import { cn } from "~/lib/utils";
-import { useShortcutModifierState } from "~/shortcutModifierState";
import { Separator } from "../ui/separator";
import {
pullRequestEntryKey,
@@ -12,7 +11,10 @@ import {
type PullRequestGroup,
} from "./pullRequestList.logic";
import { PullRequestRow, type PullRequestRowTarget } from "./PullRequestRow";
-import type { PullRequestSpeedActionResult } from "./PullRequestSpeedActions";
+import {
+ PullRequestSpeedModeProvider,
+ type PullRequestSpeedActionResult,
+} from "./PullRequestSpeedActions";
const GROUP_ICONS: Record<string, LucideIcon> = {
authored: PenLineIcon,
@@ -38,10 +40,6 @@ function PullRequestGroupHeader({
);
}
-/**
- * The grouped rows. Holding Shift turns rows into speed actions, so the modifier subscription lives
- * here: a key press re-renders the rows, not the page and its open pull request.
- */
export function PullRequestListGroups({
groups,
environmentLabels,
@@ -75,47 +73,46 @@ export function PullRequestListGroups({
sweepingKeys: ReadonlySet<string>;
onCloseSweepStart: (entry: EnvironmentPullRequestEntry, event: PointerEvent) => void;
}) {
- const modifiers = useShortcutModifierState(true);
- const speedMode =
- modifiers.shiftKey && !modifiers.metaKey && !modifiers.ctrlKey && !modifiers.altKey;
return (
- <div className={cn("space-y-3", sweepingKeys.size > 0 && "**:pointer-events-none")}>
- {groups.map((group) => (
- <div key={group.key} className="space-y-0.5">
- {group.label ? <PullRequestGroupHeader group={group} /> : null}
- {group.entries.map((entry) => {
- const entryKey = pullRequestEntryKey(entry);
- const environmentLabel = environmentLabels?.get(entry.environmentId);
- return (
- <PullRequestRow
- key={entryKey}
- statsKey={entryKey}
- statsRef={statsRef}
- entry={entry}
- showProjectTitle
- showProvider={showProvider}
- {...(environmentLabel !== undefined ? { environmentLabel } : {})}
- matchedElsewhere={
- query.length > 0 && scorePullRequestMatch(entry, query) <= MATCHED_ELSEWHERE_SCORE
- }
- selected={
- selected?.environmentId === entry.environmentId &&
- selected.repository === entry.repository &&
- selected.host?.toLowerCase() === entry.host.toLowerCase() &&
- selected.number === entry.number
- }
- onSelect={onSelect}
- speedMode={speedMode}
- hostActions={hostActionsOf(entry)}
- onActed={onActed}
- closing={closingKeys.has(entryKey)}
- sweeping={sweepingKeys.has(entryKey)}
- onCloseSweepStart={onCloseSweepStart}
- />
- );
- })}
- </div>
- ))}
- </div>
+ <PullRequestSpeedModeProvider>
+ <div className={cn("space-y-3", sweepingKeys.size > 0 && "**:pointer-events-none")}>
+ {groups.map((group) => (
+ <div key={group.key} className="space-y-0.5">
+ {group.label ? <PullRequestGroupHeader group={group} /> : null}
+ {group.entries.map((entry) => {
+ const entryKey = pullRequestEntryKey(entry);
+ const environmentLabel = environmentLabels?.get(entry.environmentId);
+ return (
+ <PullRequestRow
+ key={entryKey}
+ statsKey={entryKey}
+ statsRef={statsRef}
+ entry={entry}
+ showProjectTitle
+ showProvider={showProvider}
+ {...(environmentLabel !== undefined ? { environmentLabel } : {})}
+ matchedElsewhere={
+ query.length > 0 &&
+ scorePullRequestMatch(entry, query) <= MATCHED_ELSEWHERE_SCORE
+ }
+ selected={
+ selected?.environmentId === entry.environmentId &&
+ selected.repository === entry.repository &&
+ selected.host?.toLowerCase() === entry.host.toLowerCase() &&
+ selected.number === entry.number
+ }
+ onSelect={onSelect}
+ hostActions={hostActionsOf(entry)}
+ onActed={onActed}
+ closing={closingKeys.has(entryKey)}
+ sweeping={sweepingKeys.has(entryKey)}
+ onCloseSweepStart={onCloseSweepStart}
+ />
+ );
+ })}
+ </div>
+ ))}
+ </div>
+ </PullRequestSpeedModeProvider>
);
}
diff --git a/apps/web/src/components/pullRequest/PullRequestRow.tsx b/apps/web/src/components/pullRequest/PullRequestRow.tsx
index 853305d9d27..2800733917d 100644
--- a/apps/web/src/components/pullRequest/PullRequestRow.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestRow.tsx
@@ -78,7 +78,6 @@ function PullRequestRowImpl({
statsKey,
statsRef,
onSelect,
- speedMode,
hostActions,
onActed,
closing = false,
@@ -95,7 +94,6 @@ function PullRequestRowImpl({
statsKey?: string;
statsRef?: RefCallback<HTMLDivElement>;
onSelect: (entry: PullRequestRowTarget) => void;
- speedMode: boolean;
hostActions: ReadonlySet<PullRequestAction>;
onActed: (result: PullRequestSpeedActionResult) => void;
closing?: boolean;
@@ -243,7 +241,6 @@ function PullRequestRowImpl({
<PullRequestSpeedActions
entry={entry}
hostActions={hostActions}
- visible={speedMode}
onActed={onActed}
closing={closing}
sweeping={sweeping}
diff --git a/apps/web/src/components/pullRequest/PullRequestSpeedActions.tsx b/apps/web/src/components/pullRequest/PullRequestSpeedActions.tsx
index df0caeffeef..51d9911c13c 100644
--- a/apps/web/src/components/pullRequest/PullRequestSpeedActions.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestSpeedActions.tsx
@@ -1,5 +1,8 @@
import { useAtomValue } from "@effect/atom-react";
import type { PullRequestAction } from "@t3tools/contracts";
+import { createContext, use, type ReactNode } from "react";
+
+import { useShortcutModifierState } from "~/shortcutModifierState";
import { pullRequestEnvironment } from "~/state/pullRequests";
import { useUiStateStore } from "~/uiStateStore";
import { Button } from "../ui/button";
@@ -26,7 +29,6 @@ export interface PullRequestSpeedActionResult<Entry = EnvironmentPullRequestEntr
export function PullRequestSpeedActions<Entry extends PullRequestSpeedActionEntry>({
entry,
hostActions,
- visible,
onActed,
closing = false,
sweeping = false,
@@ -34,7 +36,6 @@ export function PullRequestSpeedActions<Entry extends PullRequestSpeedActionEntr
}: {
entry: Entry;
hostActions: ReadonlySet<PullRequestAction>;
- visible: boolean;
onActed?: (result: PullRequestSpeedActionResult<Entry>) => void;
closing?: boolean;
sweeping?: boolean;
@@ -75,12 +76,10 @@ export function PullRequestSpeedActions<Entry extends PullRequestSpeedActionEntr
const busy = actionPending || closing || sweeping;
if (actions.length === 0) return null;
return (
- <div
- className="shrink-0 items-center gap-1 pr-3"
- style={{ display: visible || busy ? "flex" : "none" }}
- role="group"
- aria-label={`Quick actions for pull request #${entry.number}`}
- data-pull-request-action-pending={actionPending || closing}
+ <SpeedModeGroup
+ busy={busy}
+ pending={actionPending || closing}
+ label={`Quick actions for pull request #${entry.number}`}
>
{actions.map((action) => {
const label = ACTIONS[action].label;
@@ -116,6 +115,45 @@ export function PullRequestSpeedActions<Entry extends PullRequestSpeedActionEntr
</Tooltip>
);
})}
+ </SpeedModeGroup>
+ );
+}
+
+/**
+ * Whether Shift is held, as context rather than a row prop: a Shift press then re-renders each
+ * row's action group, not the rows or their buttons, in one commit.
+ */
+export const PullRequestSpeedModeContext = createContext(false);
+
+/** Tracks Shift for the quick actions below it. */
+export function PullRequestSpeedModeProvider({ children }: { children: ReactNode }) {
+ const modifiers = useShortcutModifierState(true);
+ const speedMode =
+ modifiers.shiftKey && !modifiers.metaKey && !modifiers.ctrlKey && !modifiers.altKey;
+ return <PullRequestSpeedModeContext value={speedMode}>{children}</PullRequestSpeedModeContext>;
+}
+
+function SpeedModeGroup({
+ busy,
+ pending,
+ label,
+ children,
+}: {
+ busy: boolean;
+ pending: boolean;
+ label: string;
+ children: ReactNode;
+}) {
+ const speedMode = use(PullRequestSpeedModeContext);
+ return (
+ <div
+ className="shrink-0 items-center gap-1 pr-3"
+ style={{ display: speedMode || busy ? "flex" : "none" }}
+ role="group"
+ aria-label={label}
+ data-pull-request-action-pending={pending}
+ >
+ {children}
</div>
);
}
diff --git a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
index a650a71a977..f52e5b7c431 100644
--- a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
+++ b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
@@ -54,7 +54,7 @@ import {
} from "./pullRequestPresentation";
import { PullRequestGlyph } from "./pullRequestIcons";
import { pullRequestQuickActions } from "./pullRequestList.logic";
-import { PullRequestSpeedActions } from "./PullRequestSpeedActions";
+import { PullRequestSpeedActions, PullRequestSpeedModeContext } from "./PullRequestSpeedActions";
const SOURCE_LABELS: Record<ThreadPullRequestLink["source"], string> = {
manual: "Linked by you",
@@ -278,11 +278,7 @@ function LinkRowImpl({
/>
</a>
{actionEntry !== null ? (
- <PullRequestSpeedActions
- entry={actionEntry}
- hostActions={linkHostActions}
- visible={speedMode}
- />
+ <PullRequestSpeedActions entry={actionEntry} hostActions={linkHostActions} />
) : null}
<span
className={cn(
@@ -448,48 +444,50 @@ function EnabledThreadPullRequestsPanel({ threadRef }: { threadRef: ScopedThread
}
return (
- <div className="flex h-full min-h-0 flex-col">
- {/* Threads that run a stack or a sweep link hundreds of pull requests; only the rows in
+ <PullRequestSpeedModeContext value={speedMode}>
+ <div className="flex h-full min-h-0 flex-col">
+ {/* 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 · {linkCount} linked
- {lastSynced ? (
- <>
- {" · synced "}
- <RelativeTime timestamp={lastSynced} />
- </>
- ) : null}
- </span>
- <Button size="xs" variant="ghost" onClick={openLinkDialog}>
- <PlusIcon className="size-3.5" />
- Link
- </Button>
- </footer>
- </div>
+ <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 · {linkCount} linked
+ {lastSynced ? (
+ <>
+ {" · synced "}
+ <RelativeTime timestamp={lastSynced} />
+ </>
+ ) : null}
+ </span>
+ <Button size="xs" variant="ghost" onClick={openLinkDialog}>
+ <PlusIcon className="size-3.5" />
+ Link
+ </Button>
+ </footer>
+ </div>
+ </PullRequestSpeedModeContext>
);
}