From bc9bd1fc927aa077b9400c827ae15c6f1ecc0e9c Mon Sep 17 00:00:00 2001 From: AKHIL Date: Mon, 9 Nov 2026 12:11:31 +0530 Subject: [PATCH] perf(api-client): stop sidebar and tab bar re-rendering per keystroke The API client root re-renders on every keystroke (it reads the active tab). CollectionsSidebar and TabBar re-rendered with it even though nothing they show had changed. - CollectionsSidebar is React.memo; its only prop is stable while typing. - TabBar is memoized on the chip fields it renders (id/kind/method/name), active tab and handlers. - reorderTabs takes (fromId, toId) and moves within the live state via moveTab, so a memoized TabBar holding a stale tabs array can no longer overwrite edits on drop. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/tabs-context.test.tsx | 20 ++++++++++++ .../collections/collections-sidebar.tsx | 6 +++- .../api-client/context/tabs-context.tsx | 18 +++++++++-- .../src/components/api-client/tab-bar.tsx | 31 +++++++++++++------ 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/apps/desktop-ui/src/components/api-client/__tests__/tabs-context.test.tsx b/apps/desktop-ui/src/components/api-client/__tests__/tabs-context.test.tsx index 1d3dfe80e..d6227412d 100644 --- a/apps/desktop-ui/src/components/api-client/__tests__/tabs-context.test.tsx +++ b/apps/desktop-ui/src/components/api-client/__tests__/tabs-context.test.tsx @@ -114,3 +114,23 @@ describe("slimTabsForStorage", () => { expect(s.body.formData[0].fileContentBase64).toBe("") }) }) + +describe("moveTab", () => { + const { moveTab } = require("../context/tabs-context") + const ids = (tabs: { id: string }[]) => tabs.map((t) => t.id).join("") + const abc = [{ id: "a", v: 1 }, { id: "b", v: 2 }, { id: "c", v: 3 }] + + it("moves forward and backward into the target's slot", () => { + expect(ids(moveTab(abc, "a", "c"))).toBe("bca") + expect(ids(moveTab(abc, "c", "a"))).toBe("cab") + }) + + it("keeps the live tab objects (no stale snapshot)", () => { + expect(moveTab(abc, "a", "b")[1]).toBe(abc[0]) + }) + + it("returns the same array for unknown or identical ids", () => { + expect(moveTab(abc, "x", "a")).toBe(abc) + expect(moveTab(abc, "a", "a")).toBe(abc) + }) +}) diff --git a/apps/desktop-ui/src/components/api-client/collections/collections-sidebar.tsx b/apps/desktop-ui/src/components/api-client/collections/collections-sidebar.tsx index f230e82e0..deb89b85b 100644 --- a/apps/desktop-ui/src/components/api-client/collections/collections-sidebar.tsx +++ b/apps/desktop-ui/src/components/api-client/collections/collections-sidebar.tsx @@ -55,7 +55,11 @@ interface CollectionsSidebarProps { onLoadRequest: (request: CollectionRequest) => void } -export function CollectionsSidebar({ onLoadRequest: loadRequest }: CollectionsSidebarProps) { +// Memoized: the API client root re-renders on every keystroke, and this +// sidebar's only prop (`onLoadRequest`) is stable across them. +export const CollectionsSidebar = React.memo(CollectionsSidebarImpl) + +function CollectionsSidebarImpl({ onLoadRequest: loadRequest }: CollectionsSidebarProps) { const panel = useToolSidebarPanel() // While the panel floats (mobile sheet, or the desktop flyout from the // collapsed rail), loading a request has to dismiss it or the loaded diff --git a/apps/desktop-ui/src/components/api-client/context/tabs-context.tsx b/apps/desktop-ui/src/components/api-client/context/tabs-context.tsx index 4ccba53a4..bba7b6fe9 100644 --- a/apps/desktop-ui/src/components/api-client/context/tabs-context.tsx +++ b/apps/desktop-ui/src/components/api-client/context/tabs-context.tsx @@ -40,6 +40,17 @@ function persistTabs(tabs: ApiRequestState[]) { } } +/** Move the tab `fromId` into `toId`'s slot; returns `tabs` unchanged when either id is missing. */ +export function moveTab(tabs: T[], fromId: string, toId: string): T[] { + const from = tabs.findIndex((t) => t.id === fromId) + const to = tabs.findIndex((t) => t.id === toId) + if (from === -1 || to === -1 || from === to) return tabs + const next = [...tabs] + const [moved] = next.splice(from, 1) + next.splice(to, 0, moved) + return next +} + export const createNewTab = (kind: "rest" | "websocket" | "grpc" = "rest"): ApiRequestState => ({ id: crypto.randomUUID(), name: kind === "websocket" ? "New WebSocket" : kind === "grpc" ? "New gRPC" : API_CLIENT_DEFAULT_TAB_NAME, @@ -77,7 +88,8 @@ type TabsActions = { closeTab(id: string): void duplicateTab(id: string): void renameTab(id: string, name: string): void - reorderTabs(next: ApiRequestState[]): void + /** Move `fromId` to `toId`'s slot. Id-based so a memoized caller holding a stale tabs array can't overwrite edits. */ + reorderTabs(fromId: string, toId: string): void setActiveTabId(id: string): void updateActiveTab(updates: Partial | ((tab: ApiRequestState) => Partial)): void } @@ -225,8 +237,8 @@ export function TabsProvider({ children }: { children: React.ReactNode }) { setTabs((prev) => prev.map((tab) => tab.id === id ? { ...tab, name } : tab)) }, - reorderTabs(next: ApiRequestState[]) { - setTabs(next) + reorderTabs(fromId: string, toId: string) { + setTabs((prev) => moveTab(prev, fromId, toId)) }, setActiveTabId(id: string) { diff --git a/apps/desktop-ui/src/components/api-client/tab-bar.tsx b/apps/desktop-ui/src/components/api-client/tab-bar.tsx index da89d5268..ff4d7a2c3 100644 --- a/apps/desktop-ui/src/components/api-client/tab-bar.tsx +++ b/apps/desktop-ui/src/components/api-client/tab-bar.tsx @@ -23,7 +23,7 @@ interface TabBarProps { onTabClose: (id: string) => void onTabAdd: (kind?: "rest" | "websocket" | "grpc") => void onTabRename?: (id: string, name: string) => void - onTabReorder?: (reordered: ApiRequestState[]) => void + onTabReorder?: (fromId: string, toId: string) => void onTabDuplicate?: (id: string) => void } @@ -38,7 +38,25 @@ const getMethodColor = (method: string) => { } } -export function TabBar({ +/** True when two tab lists render the same chips (the only tab fields the bar reads). */ +function sameChips(a: ApiRequestState[], b: ApiRequestState[]): boolean { + return a.length === b.length && a.every((t, i) => + t.id === b[i].id && t.kind === b[i].kind && t.method === b[i].method && t.name === b[i].name) +} + +/** + * `tabs` is a new array on every keystroke in any request field; skip the + * re-render unless a chip actually changed. Safe because the bar never hands + * tab objects back up (reorder is id-based). + */ +export const TabBar = React.memo(TabBarImpl, (prev, next) => { + for (const key of Object.keys(next) as (keyof TabBarProps)[]) { + if (key !== "tabs" && prev[key] !== next[key]) return false + } + return sameChips(prev.tabs, next.tabs) +}) + +function TabBarImpl({ tabs, activeTabId, onTabChange, @@ -112,14 +130,7 @@ export function TabBar({ e.preventDefault() if (!draggedId || draggedId === targetId || !onTabReorder) return - const from = tabs.findIndex((t) => t.id === draggedId) - const to = tabs.findIndex((t) => t.id === targetId) - if (from === -1 || to === -1) return - - const reordered = [...tabs] - const [moved] = reordered.splice(from, 1) - reordered.splice(to, 0, moved) - onTabReorder(reordered) + onTabReorder(draggedId, targetId) setDraggedId(null) setDragOverId(null) }