TypeScript
refactor(web): the legacy sidebar keeps its caching without "use memo" (#20793)
Julius Marminge2c722ba396b0
diff --git a/apps/web/src/components/LegacySidebar.tsx b/apps/web/src/components/LegacySidebar.tsxindex bf04efdf4e0..89ed68e027f 100644--- a/apps/web/src/components/LegacySidebar.tsx+++ b/apps/web/src/components/LegacySidebar.tsx@@ -291,13 +291,6 @@ function projectGroupingModeDescription(mode: SidebarProjectGroupingMode): strin}}-// Every project row keys on this map, so it gets its own cache: inline, it-// shared one with the modifier-key state and rebuilt on every key press.-function useThreadJumpLabelMap(input: Parameters<typeof buildThreadJumpLabelMap>[0]) {- "use memo";- return buildThreadJumpLabelMap(input);-}-function buildThreadJumpLabelMap(input: {keybindings: ResolvedKeybindingsConfig;platform: string;@@ -383,13 +376,6 @@ function checkTaskPermission(environmentId: EnvironmentId): boolean {return false;}-// The row's handlers key on this ref; built inline, the hooks after it kept the-// compiler from caching it, so every render rebuilt all of them.-function useScopedThreadRef(environmentId: EnvironmentId, threadId: ThreadId) {- "use memo";- return scopeThreadRef(environmentId, threadId);-}-const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowProps) {const {orderedProjectThreadKeys,@@ -420,16 +406,8 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP} = props;const canOperateThread = useEnvironmentScope(thread.environmentId, AuthOrchestrationOperateScope);const canOperatePreview = useEnvironmentScope(thread.environmentId, AuthPreviewOperateScope);- const threadRef = useScopedThreadRef(thread.environmentId, thread.id);- const threadKey = scopedThreadKey(threadRef);+ const threadKey = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));const [isFileDragOver, setIsFileDragOver] = useState(false);- const fileDropHandlers = makeWorkspaceFileDropHandlers({- setDragActive: setIsFileDragOver,- addFiles: (files) => {- onFileDropThreads(threadRef, files);- },- addFolders: () => {},- });useEffect(() => {if (!isFileDragOver) return;const clearFileDrag = () => setIsFileDragOver(false);@@ -462,6 +440,32 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowPconst threadEnvironmentLabel = isRemoteThread? (remoteEnvLabel ?? (isDesktopLocalThread ? "Local" : "Remote")): null;+ const isThreadRunning = !threadRuntimeCanArchive(thread.runtime);+ const threadStatus = resolveThreadStatusPill({+ thread: {+ ...thread,+ lastVisitedAt,+ },+ });+ const linkedPullRequestStatus = useLinkedThreadPullRequest(+ thread.environmentId,+ thread.linkedPullRequest,+ leaseLiveStatus,+ thread.pullRequests,+ thread.branchPullRequest,+ );+ const pr = linkedPullRequestStatus?.pr ?? null;+ const supportsMultiplePullRequests = useSupportsMultiplePullRequests(thread.environmentId);+ // Every hook runs above this line: the handlers below key on this ref, and the+ // compiler can only cache it once no hook call follows its plain uses.+ const threadRef = scopeThreadRef(thread.environmentId, thread.id);+ const fileDropHandlers = makeWorkspaceFileDropHandlers({+ setDragActive: setIsFileDragOver,+ addFiles: (files) => {+ onFileDropThreads(threadRef, files);+ },+ addFolders: () => {},+ });const isHighlighted = isActive || isSelected;const handleOpenDiscoveredPort = (event: React.MouseEvent<HTMLButtonElement>) => {const port = discoveredPorts[0];@@ -484,22 +488,6 @@ const SidebarThreadRow = memo(function SidebarThreadRow(props: SidebarThreadRowP);})();};- const isThreadRunning = !threadRuntimeCanArchive(thread.runtime);- const threadStatus = resolveThreadStatusPill({- thread: {- ...thread,- lastVisitedAt,- },- });- const linkedPullRequestStatus = useLinkedThreadPullRequest(- thread.environmentId,- thread.linkedPullRequest,- leaseLiveStatus,- thread.pullRequests,- thread.branchPullRequest,- );- const pr = linkedPullRequestStatus?.pr ?? null;- const supportsMultiplePullRequests = useSupportsMultiplePullRequests(thread.environmentId);const currentLinkedPr = supportsMultiplePullRequests? resolveThreadCurrentPullRequestLink(thread.pullRequests): null;@@ -1124,7 +1112,6 @@ function resolveProjectThreadStatus(// SidebarProjectItem frozen: inline, its later calls made the compiler fuse the// ordered keys with the route-dependent panel, handing every row a new array.function useProjectThreads(project: SidebarProjectSnapshot) {- "use memo";const threadSortOrder = useClientSettings<SidebarThreadSortOrder>((settings) => settings.sidebarThreadSortOrder,);@@ -1203,7 +1190,6 @@ function useProjectThreadPanel(input: {projectExpanded: boolean;isThreadListExpanded: boolean;}) {- "use memo";const {visibleProjectThreads,lastVisitedAtByThreadKey,@@ -3046,7 +3032,6 @@ function openAddProjectCommandPalette() {// LegacySidebar frozen and cached on their real inputs; inline, its later hook// calls made the compiler recompute them, and the props of every row with them.function useLegacySidebarProjects() {- "use memo";const projects = useProjects();const projectOrder = useUiStateStore((store) => store.projectOrder);const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);@@ -3110,7 +3095,6 @@ function useSortedLegacySidebarProjects(input: {projectPhysicalKeyByScopedRef: ReadonlyMap<string, string>;sidebarProjectSortOrder: SidebarProjectSortOrder;}) {- "use memo";const {sidebarProjects,physicalToLogicalKey,@@ -3160,7 +3144,6 @@ function useLegacySidebarThreads(input: {sidebarThreadPreviewCount: number;expandedThreadListsByProject: ReadonlySet<string>;}) {- "use memo";const {sortedProjects,physicalToLogicalKey,@@ -3406,17 +3389,19 @@ export default function LegacySidebar() {};const isManualProjectSorting = sidebarProjectSortOrder === "manual";- const sidebarShortcutContext = {- terminalFocus: terminalFocused,- terminalOpen: routeTerminalOpen,- modelPickerOpen: isModelPickerOpen(),- };- const threadJumpLabelByKey = useThreadJumpLabelMap({+ // Built before the modifier-dependent shortcut context so the compiler gives it+ // its own cache; every project row keys on this map.+ const threadJumpLabelByKey = buildThreadJumpLabelMap({keybindings,platform,terminalOpen: routeTerminalOpen,threadJumpCommandByKey,});+ const sidebarShortcutContext = {+ terminalFocus: terminalFocused,+ terminalOpen: routeTerminalOpen,+ modelPickerOpen: isModelPickerOpen(),+ };const shouldShowThreadJumpHintsNow = shouldShowThreadJumpHintsForModifiers(shortcutModifiers,keybindings,