TypeScript
perf(web): typing in the ACP registry search no longer re-renders every result row (#20817)
Julius Marminge1994d6396fda
diff --git a/apps/web/src/components/settings/AcpRegistrySearchStep.test.tsx b/apps/web/src/components/settings/AcpRegistrySearchStep.test.tsxindex 03da36f8f89..3524d784177 100644--- a/apps/web/src/components/settings/AcpRegistrySearchStep.test.tsx+++ b/apps/web/src/components/settings/AcpRegistrySearchStep.test.tsx@@ -1,4 +1,4 @@-import type { ReactElement } from "react";+import { isValidElement, type ReactElement } from "react";import { EnvironmentId, ProviderDriverKind, type AcpRegistrySearchAgent } from "@t3tools/contracts";import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";@@ -118,11 +118,25 @@ function findByAriaLabel(tree: ReactElement<Record<string, unknown>>,label: string,): ReactElement<Record<string, unknown>> {- const found = visitElements(tree, (element) => element.props["aria-label"] === label);+ const found = visitElements(expandRows(tree), (element) => element.props["aria-label"] === label);expect(found).not.toBeNull();return found!;}+// The harness calls components as plain functions, so result rows stay+// unexpanded elements until rendered here.+function expandRows(node: unknown): unknown {+ if (Array.isArray(node)) return node.map(expandRows);+ if (!isValidElement<Record<string, unknown>>(node)) return node;+ if (typeof node.type === "function" && node.type.name === "AcpRegistryAgentRow") {+ return expandRows((node.type as (props: unknown) => unknown)(node.props));+ }+ const props = Object.fromEntries(+ Object.entries(node.props).map(([key, value]) => [key, expandRows(value)]),+ );+ return { ...node, props };+}+describe("AcpRegistrySearchStep", () => {beforeEach(() => {state.canManage = true;diff --git a/apps/web/src/components/settings/AcpRegistrySearchStep.tsx b/apps/web/src/components/settings/AcpRegistrySearchStep.tsxindex 9730fcc28f9..594f9ac3387 100644--- a/apps/web/src/components/settings/AcpRegistrySearchStep.tsx+++ b/apps/web/src/components/settings/AcpRegistrySearchStep.tsx@@ -217,72 +217,16 @@ export function AcpRegistrySearchStep({) : (<ScrollArea scrollFade className="max-h-64"><div className="pr-2.5">- {results.map((agent) => {- const alreadyAdded = isConfiguredAcpRegistryAgent(providerInstances, agent.id);- const isPreparing = preparingId === agent.id;- const progressLabel = agent.distribution === "binary" ? "Downloading" : "Preparing";- return (- <article className="min-w-0 py-2.5" key={agent.id}>- <div className="flex min-w-0 items-center justify-between gap-3">- <div className="flex min-w-0 flex-1 items-center gap-3">- <ProviderInstanceIcon- driverKind={ProviderDriverKind.make("acpRegistry")}- displayName={agent.name}- acpRegistryAgentId={agent.id}- acpRegistryIconUrl={agent.icon ?? undefined}- iconClassName="size-6 rounded-md text-muted-foreground"- />- <div className="min-w-0 flex-1">- <div className="flex min-w-0 items-baseline gap-2">- <h4 className="min-w-0 truncate text-sm font-medium text-foreground">- {agent.name}- </h4>- </div>- {agent.description ? (- <p className="mt-0.5 truncate text-xs text-muted-foreground">- {agent.description}- </p>- ) : null}- </div>- </div>- <div className="flex shrink-0 items-center gap-1.5">- {agent.website || agent.repository ? (- <Tooltip>- <TooltipTrigger- render={- <Button- size="icon-xs"- variant="ghost-muted"- aria-label={`About ${agent.name}`}- render={- <a- href={agent.website || agent.repository || undefined}- target="_blank"- rel="noreferrer"- />- }- >- <ExternalLinkIcon />- </Button>- }- />- <TooltipPopup>About {agent.name}</TooltipPopup>- </Tooltip>- ) : null}- <Button- aria-label={`${alreadyAdded ? "Already added" : isPreparing ? progressLabel : "Add"} ${agent.name}`}- disabled={!canManageProviders || alreadyAdded || preparingId !== null}- onClick={() => void handlePrepare(agent)}- size="xs"- variant={isPreparing ? "secondary" : "outline"}- >- {alreadyAdded ? "Added" : isPreparing ? progressLabel : "Add"}- </Button>- </div>- </div>- </article>- );- })}+ {results.map((agent) => (+ <AcpRegistryAgentRow+ agent={agent}+ alreadyAdded={isConfiguredAcpRegistryAgent(providerInstances, agent.id)}+ canAdd={canManageProviders && preparingId === null}+ isPreparing={preparingId === agent.id}+ key={agent.id}+ onAdd={handlePrepare}+ />+ ))}</div></ScrollArea>)@@ -290,3 +234,71 @@ export function AcpRegistrySearchStep({</section>);}++function AcpRegistryAgentRow({+ agent,+ alreadyAdded,+ canAdd,+ isPreparing,+ onAdd,+}: {+ readonly agent: AcpRegistrySearchAgent;+ readonly alreadyAdded: boolean;+ readonly canAdd: boolean;+ readonly isPreparing: boolean;+ readonly onAdd: (agent: AcpRegistrySearchAgent) => void;+}) {+ const progressLabel = agent.distribution === "binary" ? "Downloading" : "Preparing";+ const aboutUrl = agent.website || agent.repository;+ return (+ <article className="min-w-0 py-2.5">+ <div className="flex min-w-0 items-center justify-between gap-3">+ <div className="flex min-w-0 flex-1 items-center gap-3">+ <ProviderInstanceIcon+ driverKind={ProviderDriverKind.make("acpRegistry")}+ displayName={agent.name}+ acpRegistryAgentId={agent.id}+ acpRegistryIconUrl={agent.icon ?? undefined}+ iconClassName="size-6 rounded-md text-muted-foreground"+ />+ <div className="min-w-0 flex-1">+ <div className="flex min-w-0 items-baseline gap-2">+ <h4 className="min-w-0 truncate text-sm font-medium text-foreground">{agent.name}</h4>+ </div>+ {agent.description ? (+ <p className="mt-0.5 truncate text-xs text-muted-foreground">{agent.description}</p>+ ) : null}+ </div>+ </div>+ <div className="flex shrink-0 items-center gap-1.5">+ {aboutUrl ? (+ <Tooltip>+ <TooltipTrigger+ render={+ <Button+ size="icon-xs"+ variant="ghost-muted"+ aria-label={`About ${agent.name}`}+ render={<a href={aboutUrl} target="_blank" rel="noreferrer" />}+ >+ <ExternalLinkIcon />+ </Button>+ }+ />+ <TooltipPopup>About {agent.name}</TooltipPopup>+ </Tooltip>+ ) : null}+ <Button+ aria-label={`${alreadyAdded ? "Already added" : isPreparing ? progressLabel : "Add"} ${agent.name}`}+ disabled={!canAdd || alreadyAdded}+ onClick={() => onAdd(agent)}+ size="xs"+ variant={isPreparing ? "secondary" : "outline"}+ >+ {alreadyAdded ? "Added" : isPreparing ? progressLabel : "Add"}+ </Button>+ </div>+ </div>+ </article>+ );+}