Skip to content
TypeScript

fix(mobile): the split-view sidebar no longer crashes in an update loop (#21867)

Alex325a7e2ae4bf
diff --git a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx
index 82bd286cbdb..40b0e6e6969 100644
--- a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx
+++ b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx
@@ -17,6 +17,7 @@ import {
use,
useEffect,
useEffectEvent,
+ useMemo,
useRef,
useState,
type ReactNode,
@@ -247,7 +248,14 @@ function AdaptiveWorkspaceLayoutContent(
const [primarySidebarSearchQuery, setPrimarySidebarSearchQuery] = useState("");
const [focusedAuxiliaryPaneRole, setFocusedAuxiliaryPaneRole] =
useState<WorkspaceAuxiliaryPaneRole | null>(null);
- const baseLayout = deriveLayout({ width: viewportWidth, height, nativeMetrics });
+ // The compiler can't memoize these derivations: they stay live across later hook calls. Fresh
+ // objects here change the workspace context on every render, so every workspace screen
+ // re-renders on any layout state change, and the inspector registration then loops: each
+ // re-register re-renders its screen, which hands over a new inspector renderer.
+ const baseLayout = useMemo(
+ () => deriveLayout({ width: viewportWidth, height, nativeMetrics }),
+ [height, viewportWidth, nativeMetrics],
+ );
const primarySidebarPinned = isPrimarySidebarPinned({
pathname,
nativeWorkspace,
@@ -260,17 +268,30 @@ function AdaptiveWorkspaceLayoutContent(
setHinge(next);
setWindowHinge(nextWindow);
};
- const layout = alignListPaneToHinge({ layout: baseLayout, hinge, viewportWidth });
+ const layout = useMemo(
+ () => alignListPaneToHinge({ layout: baseLayout, hinge, viewportWidth }),
+ [baseLayout, hinge, viewportWidth],
+ );
const shouldRenderPrimarySidebar = layout.usesSplitView;
- const fileInspector = deriveFileInspectorPaneLayout({
- layout,
- viewportWidth,
- preferredWidth: fileInspectorPreferredWidth ?? undefined,
- reservedLeadingWidth:
- shouldRenderPrimarySidebar && showPrimarySidebar
- ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0)
- : 0,
- });
+ const fileInspector = useMemo(
+ () =>
+ deriveFileInspectorPaneLayout({
+ layout,
+ viewportWidth,
+ preferredWidth: fileInspectorPreferredWidth ?? undefined,
+ reservedLeadingWidth:
+ shouldRenderPrimarySidebar && showPrimarySidebar
+ ? (layout.listPaneWidth ?? 0) + (layout.listPaneGap ?? 0)
+ : 0,
+ }),
+ [
+ fileInspectorPreferredWidth,
+ layout,
+ showPrimarySidebar,
+ shouldRenderPrimarySidebar,
+ viewportWidth,
+ ],
+ );
const auxiliaryPaneRole: WorkspaceAuxiliaryPaneRole =
focusedAuxiliaryPaneRole ?? (/\/files(?:\/|$)/.test(pathname) ? "inspector" : "supplementary");
const auxiliaryPanePreferredVisible =
@@ -281,14 +302,25 @@ function AdaptiveWorkspaceLayoutContent(
auxiliaryPaneRole === "inspector"
? fileInspectorPreferredWidth
: supplementaryPanePreferredWidth;
- const panes = deriveWorkspacePaneLayout({
- layout,
- viewportWidth,
- primarySidebarPreferredVisible: showPrimarySidebar,
- auxiliaryPanePreferredVisible,
- auxiliaryPaneRole,
- auxiliaryPanePreferredWidth: auxiliaryPanePreferredWidth ?? undefined,
- });
+ const panes = useMemo(
+ () =>
+ deriveWorkspacePaneLayout({
+ layout,
+ viewportWidth,
+ primarySidebarPreferredVisible: showPrimarySidebar,
+ auxiliaryPanePreferredVisible,
+ auxiliaryPaneRole,
+ auxiliaryPanePreferredWidth: auxiliaryPanePreferredWidth ?? undefined,
+ }),
+ [
+ auxiliaryPanePreferredVisible,
+ auxiliaryPaneRole,
+ auxiliaryPanePreferredWidth,
+ layout,
+ showPrimarySidebar,
+ viewportWidth,
+ ],
+ );
const activeThread = parseActiveThreadPath(pathname);
const environmentId = activeThread?.environmentId ?? null;
const threadId = activeThread?.threadId ?? null;
diff --git a/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx b/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx
index d9cf141d2f9..a588120b0dc 100644
--- a/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx
+++ b/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx
@@ -13,7 +13,7 @@ import {
import { LegendList } from "@legendapp/list/react-native";
import type { MenuAction } from "@react-native-menu/menu";
import { useAtomValue } from "@effect/atom-react";
-import { useEffect, useRef, useState } from "react";
+import { useEffect, useMemo, useRef, useState } from "react";
import type { LayoutChangeEvent, TextInputInstance } from "react-native";
import { Platform, StyleSheet, TextInput, View } from "react-native";
import { GestureDetector, useNativeGesture } from "react-native-gesture-handler";
@@ -180,7 +180,9 @@ function ThreadNavigationSidebarPane(
? [options.selectedEnvironmentId]
: [];
const threadSearch = useThreadSearch(searchEnvironmentIds, props.searchQuery);
- const threadSearchMatchByKey = (() => {
+ // These maps reach LegendList's extraData, which re-renders every row whenever it changes
+ // identity. The compiler leaves them unmemoized because they stay live across later hook calls.
+ const threadSearchMatchByKey = useMemo(() => {
const matches = new Map<string, EnvironmentThreadSearchMatch>();
for (const match of threadSearch.matches) {
if (match.source === "user" || match.source === "assistant") {
@@ -188,7 +190,7 @@ function ThreadNavigationSidebarPane(
}
}
return matches;
- })();
+ }, [threadSearch.matches]);
const matchedThreadKeys = new Set(threadSearch.matches.map(threadSearchMatchKey));
const projectScopeKey = options.selectedProjectKey;
const resolvedProjectKey = resolveScratchProjectScopeKey(projectScopeKey, projects, {
@@ -200,22 +202,33 @@ function ThreadNavigationSidebarPane(
setSelectedProjectKey(resolvedProjectKey);
}
}, [projectScopeKey, resolvedProjectKey, setSelectedProjectKey]);
- const projectScopes = buildHomeProjectScopes({
- projects,
- environmentId: options.selectedEnvironmentId,
- projectGroupingMode: options.projectGroupingMode,
- });
+ const projectScopes = useMemo(
+ () =>
+ buildHomeProjectScopes({
+ projects,
+ environmentId: options.selectedEnvironmentId,
+ projectGroupingMode: options.projectGroupingMode,
+ }),
+ [projects, options.selectedEnvironmentId, options.projectGroupingMode],
+ );
const projectFilterOptions = projectScopes.map((scope) => ({
key: scope.key,
label: scope.title,
}));
- const projectTitleByProjectKey = new Map(
- projectScopes.flatMap((scope) =>
- scope.projectRefs.map(
- (projectRef) =>
- [scopedProjectKey(projectRef.environmentId, projectRef.projectId), scope.title] as const,
+ const projectTitleByProjectKey = useMemo(
+ () =>
+ new Map(
+ projectScopes.flatMap((scope) =>
+ scope.projectRefs.map(
+ (projectRef) =>
+ [
+ scopedProjectKey(projectRef.environmentId, projectRef.projectId),
+ scope.title,
+ ] as const,
+ ),
+ ),
),
- ),
+ [projectScopes],
);
const selectedProjectScope =
projectScopes.find((scope) => scope.key === resolvedProjectKey) ?? null;
@@ -228,13 +241,13 @@ function ThreadNavigationSidebarPane(
scopedProjectKey(projectRef.environmentId, projectRef.projectId),
),
);
- const projectByKey = (() => {
+ const projectByKey = useMemo(() => {
const map = new Map<string, EnvironmentProject>();
for (const project of projects) {
map.set(scopedProjectKey(project.environmentId, project.id), project);
}
return map;
- })();
+ }, [projects]);
const [settledVisibleCount, setSettledVisibleCount] = useState(
THREAD_LIST_V2_SETTLED_INITIAL_COUNT,