From d3534764174bb32b8d0ee86998aaf22996ba5a69 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 24 Jun 2026 09:25:28 -0700 Subject: [PATCH] Memoize thread navigation callbacks - Stabilize thread selection routing in the adaptive workspace layout - Extract and memoize thread sidebar rows to reduce unnecessary rerenders Co-authored-by: codex --- .../layout/AdaptiveWorkspaceLayout.tsx | 47 +++--- .../threads/ThreadNavigationSidebar.tsx | 145 ++++++++++++------ 2 files changed, 125 insertions(+), 67 deletions(-) diff --git a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx index 825481fb85..3b97521783 100644 --- a/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx +++ b/apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx @@ -189,29 +189,32 @@ export function AdaptiveWorkspaceLayout(props: { readonly children: ReactNode }) transform: [{ translateX: (sidebarProgress.value - 1) * 24 }], })); - const handleSelectThread = (thread: EnvironmentThreadShell) => { - const destination = buildThreadRoutePath(thread); - const navigationAction = resolveThreadSelectionNavigationAction({ - usesSplitView: layout.usesSplitView, - pathname, - }); - if (navigationAction === "set-params") { - // Auxiliary content belongs to the current thread. Close it before - // reusing the current native detail screen for a peer thread selection. - setFileInspectorPreferredVisible(false); - router.setParams({ - environmentId: String(thread.environmentId), - threadId: String(thread.id), + const handleSelectThread = useCallback( + (thread: EnvironmentThreadShell) => { + const destination = buildThreadRoutePath(thread); + const navigationAction = resolveThreadSelectionNavigationAction({ + usesSplitView: layout.usesSplitView, + pathname, }); - return; - } - if (navigationAction === "replace") { - setFileInspectorPreferredVisible(false); - router.replace(destination); - return; - } - router.push(destination); - }; + if (navigationAction === "set-params") { + // Auxiliary content belongs to the current thread. Close it before + // reusing the current native detail screen for a peer thread selection. + setFileInspectorPreferredVisible(false); + router.setParams({ + environmentId: String(thread.environmentId), + threadId: String(thread.id), + }); + return; + } + if (navigationAction === "replace") { + setFileInspectorPreferredVisible(false); + router.replace(destination); + return; + } + router.push(destination); + }, + [layout.usesSplitView, pathname, router], + ); return ( diff --git a/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx b/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx index 959c99b5c7..db84429f87 100644 --- a/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx +++ b/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx @@ -1,6 +1,7 @@ import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; import { SymbolView } from "expo-symbols"; -import { useCallback, useMemo, useRef, useState } from "react"; +import { memo, useCallback, useMemo, useRef, useState } from "react"; +import type { ColorValue } from "react-native"; import { Pressable, ScrollView, StyleSheet, TextInput, View } from "react-native"; import type { SwipeableMethods } from "react-native-gesture-handler/ReanimatedSwipeable"; import { useSafeAreaInsets } from "react-native-safe-area-context"; @@ -17,6 +18,95 @@ import { buildThreadNavigationGroups } from "./thread-navigation-groups"; import { SidebarHeaderActions } from "./sidebar-header-actions"; import { threadStatusTone } from "./threadPresentation"; +const ThreadNavigationRow = memo(function ThreadNavigationRow(props: { + readonly backgroundColor: ColorValue; + readonly fullSwipeWidth: number; + readonly onArchiveThread: (thread: EnvironmentThreadShell) => void; + readonly onDeleteThread: (thread: EnvironmentThreadShell) => void; + readonly onSelectThread: (thread: EnvironmentThreadShell) => void; + readonly onSwipeableClose: (methods: SwipeableMethods) => void; + readonly onSwipeableWillOpen: (methods: SwipeableMethods) => void; + readonly pressedBackgroundColor: ColorValue; + readonly selected: boolean; + readonly selectedBackgroundColor: ColorValue; + readonly thread: EnvironmentThreadShell; +}) { + const { + backgroundColor, + fullSwipeWidth, + onArchiveThread, + onDeleteThread, + onSelectThread, + onSwipeableClose, + onSwipeableWillOpen, + pressedBackgroundColor, + selected, + selectedBackgroundColor, + thread, + } = props; + const handleArchive = useCallback(() => { + onArchiveThread(thread); + }, [onArchiveThread, thread]); + const handleDelete = useCallback(() => { + onDeleteThread(thread); + }, [onDeleteThread, thread]); + const primaryAction = useMemo( + () => ({ + accessibilityLabel: `Archive ${thread.title}`, + icon: "archivebox" as const, + label: "Archive", + onPress: handleArchive, + }), + [handleArchive, thread.title], + ); + + return ( + + {(close) => ( + { + close(); + onSelectThread(thread); + }} + style={({ pressed }) => [ + styles.threadRow, + { + backgroundColor: selected + ? selectedBackgroundColor + : pressed + ? pressedBackgroundColor + : backgroundColor, + }, + ]} + > + + + {thread.title} + + + {relativeTime(thread.updatedAt ?? thread.createdAt)} + + + + + )} + + ); +}); + export function ThreadNavigationSidebar(props: { readonly width: number; readonly selectedThreadKey: string | null; @@ -123,55 +213,20 @@ export function ThreadNavigationSidebar(props: { const selected = threadKey === props.selectedThreadKey; return ( - confirmDeleteThread(thread)} + onArchiveThread={archiveThread} + onDeleteThread={confirmDeleteThread} + onSelectThread={props.onSelectThread} onSwipeableClose={handleSwipeableClose} onSwipeableWillOpen={handleSwipeableWillOpen} - primaryAction={{ - accessibilityLabel: `Archive ${thread.title}`, - icon: "archivebox", - label: "Archive", - onPress: () => archiveThread(thread), - }} - threadTitle={thread.title} - > - {(close) => ( - { - close(); - props.onSelectThread(thread); - }} - style={({ pressed }) => [ - styles.threadRow, - { - backgroundColor: selected - ? selectedBackgroundColor - : pressed - ? pressedBackgroundColor - : backgroundColor, - }, - ]} - > - - - {thread.title} - - - {relativeTime(thread.updatedAt ?? thread.createdAt)} - - - - - )} - + pressedBackgroundColor={pressedBackgroundColor} + selected={selected} + selectedBackgroundColor={selectedBackgroundColor} + thread={thread} + /> ); }) )}