Files
buzz/desktop/src/features/projects/ui/RepositoryCards.tsx
Max Lampert 040b203f73 perf(desktop): make the Projects surface render-cheap (#6460)
Diagnostic profiling on a large community (101 issues, 258 PRs) showed
Projects tab switches taking 2.5–3.6s, dominated by single React commits
of 0.5–1.5s and per-render recomputation — fetch work was already off
the main thread; the cost was building the UI.

### Measured: tab click → painted, per tab

| tab | before | after |
|---|---|---:|
| projects | 3,608ms | 320–580ms |
| repositories | 3,126–3,534ms | 310–410ms |
| tasks | 395–1,101ms | ~115ms |
| reviews | 322–2,603ms | ~96ms |
| activity | 597–741ms | ~148ms |

Single-commit ceiling dropped from 1,541ms to ≤200ms (growth steps
25–40ms). Fixes in profiled-cost order:

- **Profile popover body mounts only while open.** `UserProfilePopover`
carried seven query subscriptions plus interaction hooks per instance
even when closed; grids mount hundreds (five per card in people stacks,
one per row author) — measured **~40ms per card**, the dominant share of
the 1.2s card-tab commits. The always-mounted shell is now just the
Radix root + trigger; trigger markup, hover timing, and keyboard
handling are unchanged, and hover/tooltip event continuity is preserved
because the trigger never remounts.
- **Incremental row mounting.** The first 12 cards / 30 rows render in
the first commit; the rest stream in 36–60-per-frame low-priority
transitions. Grouped lists trim across group boundaries via a pure,
tested slicer; the mounted count survives in-place refetches.
- **Activity feed**: was rebuilt unmemoized on every render,
markdown-flattening every issue/PR/comment body in the community just to
sort and keep 30 items (~360+ flattens per render on the measured
community). Now memoized, and bodies stay raw until after the sort+slice
— 30 flattens, once per data change.
- **Contribution graph** (always-visible rail, so every tab paid for
it): ~180 day cells each wrapped in a Radix tooltip with per-cell Intl
date formatting per render. Now memoized, cells precomputed once per
data change, native `title` tooltips. (The activity-bar segments keep
their styled Radix tooltips — pinned by an existing spec.)
- **Rows/cards memoized with identity-stable props**: per-row selection
arrays were rebuilt per row per render (O(n²) — 258 PRs × 258-item
arrays each render) and are now hoisted and shared; people arrays derive
inside the memoized cards; the rail's stat walk over every issue/PR is
memoized.
- **`content-visibility: auto`** on cards and rows so offscreen entries
skip layout and paint; **tab switches run in a React transition** so the
click stays responsive while the new tree mounts.

Remaining known cost (out of scope): cold-entry data readiness — the
work-item and activity queries ship thousands of events to compute
counts (2–4s on a large community; see the fan-lifecycle PR). The
structural fix is a relay-side aggregate; tracked as follow-up.

---------

Signed-off-by: Max Lampert <maxwell@squareup.com>
2026-08-22 08:42:55 -06:00

318 lines
9.3 KiB
TypeScript

import * as React from "react";
import { FolderGit2, Globe, SquareTerminal } from "lucide-react";
import type {
Project,
ProjectActivitySummary,
Repository,
} from "@/features/projects/hooks";
import {
resolveUserLabel,
type UserProfileLookup,
} from "@/features/profile/lib/identity";
import {
projectRepoHostForRepository,
repositoryDisplayPath,
} from "@/features/projects/lib/projectRepoHost";
import { repositoryShareLink } from "@/features/projects/lib/projectShareLinks";
import {
selectionItemFromRepository,
type ProjectSelectionItem,
} from "@/features/projects/lib/projectSelection";
import { cn } from "@/shared/lib/cn";
import { normalizePubkey } from "@/shared/lib/pubkey";
import { useRelayOrigin } from "@/shared/lib/useRelayOrigin";
import { BuzzMark } from "@/shared/ui/buzz-logo/BuzzMark";
import { Card } from "@/shared/ui/card";
import { DropdownMenuItem } from "@/shared/ui/dropdown-menu";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/shared/ui/tooltip";
import {
PROJECT_LIST_ROW_PREVIEW_CLASS,
PROJECT_LIST_ROW_TITLE_CLASS,
} from "./projectListRowStyles";
import { ProjectEntityListRow } from "./ProjectEntityListRow";
import {
ProjectActivityBar,
ProjectPeopleStack,
ProjectStatsRow,
} from "./ProjectCards";
import { CopyShareLinkMenuItem } from "./CopyShareLinkMenuItem";
import { GitHubMark } from "./GitHubMark";
import { ProjectListRowMenu } from "./ProjectListRowMenu";
import { PROJECT_GRID_CARD_BODY_CLASS } from "./projectGridCardStyles";
import { projectTerminalLabel } from "./useOpenProjectTerminal";
export type RepositoryListItem = {
project: Project;
repository: Repository;
};
type RepositoryItemProps = RepositoryListItem & {
hasLocal: boolean;
onOpen: (project: Project, repository: Repository) => void;
onOpenTerminal: (repository: Repository) => void;
profiles?: UserProfileLookup;
selectionRangeItems?: ProjectSelectionItem[];
summary?: ProjectActivitySummary;
};
function RepositoryHostIcon({
compact = false,
repository,
}: {
compact?: boolean;
repository: Repository;
}) {
const host = projectRepoHostForRepository(repository, useRelayOrigin());
const label =
host.kind === "buzz"
? "Buzz-hosted repository"
: host.kind === "external"
? `Git data hosted on ${host.host}`
: "Repository host";
const mark =
host.kind === "buzz" ? (
<BuzzMark className={compact ? "h-3.5 w-4" : "h-4.5 w-5"} />
) : host.kind === "external" && host.host === "github.com" ? (
<GitHubMark className={compact ? "h-3.5 w-3.5" : "h-4.5 w-4.5"} />
) : host.kind === "external" ? (
<Globe className={compact ? "h-3.5 w-3.5" : "h-4.5 w-4.5"} />
) : (
<FolderGit2 className={compact ? "h-3.5 w-3.5" : "h-4.5 w-4.5"} />
);
return (
<Tooltip>
<TooltipTrigger asChild>
<span
aria-label={label}
className={
compact
? "pointer-events-auto flex h-4 w-4 shrink-0 items-center justify-center text-muted-foreground/70"
: "pointer-events-auto flex h-9 w-9 shrink-0 items-center justify-center rounded-lg border border-border/60 bg-muted/40 text-muted-foreground"
}
data-testid="repository-host-icon"
role="img"
>
{mark}
</span>
</TooltipTrigger>
<TooltipContent>{label}</TooltipContent>
</Tooltip>
);
}
function RepositoryOpenButton({
onOpen,
project,
repository,
}: Pick<RepositoryItemProps, "onOpen" | "project" | "repository">) {
return (
<button
className="absolute inset-0 z-0 cursor-pointer"
onClick={() => onOpen(project, repository)}
type="button"
>
<span className="sr-only">View {repository.name}</span>
</button>
);
}
function RepositoryIdentity({
inlineBranch = false,
profiles,
project,
repository,
}: Pick<RepositoryItemProps, "profiles" | "project" | "repository"> & {
inlineBranch?: boolean;
}) {
// Where the git data lives beats repeating the (often identical) project
// name — "github.com/block/buzz" for external repos, "owner/repo" for
// Buzz-hosted ones.
const displayPath = repositoryDisplayPath(
repository,
useRelayOrigin(),
resolveUserLabel({ pubkey: repository.owner, profiles }),
);
return (
<>
<RepositoryHostIcon repository={repository} />
<div className="min-w-0">
<div className="flex min-w-0 items-center gap-2">
<span className={PROJECT_LIST_ROW_TITLE_CLASS}>
{repository.name}
</span>
</div>
<p className={PROJECT_LIST_ROW_PREVIEW_CLASS}>
{displayPath ?? project.name}
{inlineBranch ? ` · ${repository.defaultBranch}` : ""}
</p>
</div>
</>
);
}
function repositoryPeople(
repository: Repository,
summary: ProjectActivitySummary | undefined,
) {
return [
...new Set(
[
repository.owner,
...repository.contributors,
...(summary?.participantPubkeys ?? []),
].map(normalizePubkey),
),
];
}
function RepositoryActionsMenu({
hasLocal,
onOpenTerminal,
repository,
}: Pick<RepositoryItemProps, "hasLocal" | "onOpenTerminal" | "repository">) {
return (
<ProjectListRowMenu label={`More options for ${repository.name}`}>
<CopyShareLinkMenuItem
link={repositoryShareLink(repository)}
testId={`repository-copy-link-${repository.dtag}`}
/>
<DropdownMenuItem
onSelect={(event) => {
event.preventDefault();
event.stopPropagation();
onOpenTerminal(repository);
}}
>
<SquareTerminal className="h-4 w-4" />
{projectTerminalLabel(hasLocal)}
</DropdownMenuItem>
</ProjectListRowMenu>
);
}
// Memoized: unbounded grids/lists; identity-stable caller props keep
// re-renders scoped to changed cards.
export const RepositoryGridCard = React.memo(function RepositoryGridCard(
props: RepositoryItemProps,
) {
const {
hasLocal,
onOpen,
onOpenTerminal,
profiles,
project,
repository,
summary,
} = props;
return (
<Card
className="group relative flex h-full min-h-40 flex-col overflow-hidden border-border/60 bg-transparent shadow-none transition-colors duration-150 hover:bg-muted/20"
data-testid={`repository-card-${repository.dtag}`}
>
<RepositoryOpenButton
onOpen={onOpen}
project={project}
repository={repository}
/>
<div className="pointer-events-none relative z-10 flex min-h-0 flex-1 flex-col p-4">
<div className="flex min-w-0 items-start gap-2.5">
<RepositoryIdentity
inlineBranch
profiles={profiles}
project={project}
repository={repository}
/>
<div className="pointer-events-auto ml-auto">
<RepositoryActionsMenu
hasLocal={hasLocal}
onOpenTerminal={onOpenTerminal}
repository={repository}
/>
</div>
</div>
<p
className={cn(
PROJECT_GRID_CARD_BODY_CLASS,
"my-2 text-muted-foreground",
)}
data-testid="projects-grid-card-body"
>
{repository.description || "A repository in this project."}
</p>
<div className="pointer-events-auto mt-auto">
<ProjectPeopleStack
profiles={profiles}
pubkeys={repositoryPeople(repository, summary)}
workOwnerPubkey={repository.owner}
/>
</div>
<div className="mt-2">
<ProjectStatsRow summary={summary} />
<div className="mt-2">
<ProjectActivityBar summary={summary} />
</div>
</div>
</div>
</Card>
);
});
export const RepositoryListRow = React.memo(function RepositoryListRow(
props: RepositoryItemProps,
) {
const {
hasLocal,
onOpen,
onOpenTerminal,
profiles,
project,
repository,
selectionRangeItems,
summary,
} = props;
const updatedAt = summary?.updatedAt || repository.createdAt;
const selectionItem = selectionItemFromRepository({
channelId: repository.channelId ?? project.projectChannelId,
id: repository.id,
owner: repository.owner,
shareLink: repositoryShareLink(repository),
title: repository.name,
});
return (
<ProjectEntityListRow
beforeDate={
<div className="w-44" data-testid="repositories-row-activity-bar">
<ProjectActivityBar summary={summary} />
</div>
}
dateSeconds={updatedAt}
dateTestId="repositories-row-date"
icon={<RepositoryHostIcon compact repository={repository} />}
onClick={() => onOpen(project, repository)}
people={repositoryPeople(repository, summary)}
peopleTestId="repositories-row-people"
profiles={profiles}
selection={
selectionRangeItems
? { item: selectionItem, rangeItems: selectionRangeItems }
: undefined
}
testId={`repository-row-${repository.dtag}`}
title={repository.name}
titleAttr={repository.name}
titleSecondary={repository.description || undefined}
titleSecondaryTestId="repositories-row-description"
trailing={
<RepositoryActionsMenu
hasLocal={hasLocal}
onOpenTerminal={onOpenTerminal}
repository={repository}
/>
}
/>
);
});