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 <[email protected]>
This commit is contained in:
1 parent
a896942bc7
commit
d353476417
2 files changed
+125
-67
No files matched your search
@@ -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 (
|
||||
<AdaptiveWorkspaceContext.Provider value={contextValue}>
|
||||
|
||||
@@ -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 (
|
||||
<ThreadSwipeable
|
||||
backgroundColor={backgroundColor}
|
||||
containerStyle={styles.threadRowContainer}
|
||||
fullSwipeWidth={fullSwipeWidth}
|
||||
onDelete={handleDelete}
|
||||
onSwipeableClose={onSwipeableClose}
|
||||
onSwipeableWillOpen={onSwipeableWillOpen}
|
||||
primaryAction={primaryAction}
|
||||
threadTitle={thread.title}
|
||||
>
|
||||
{(close) => (
|
||||
<Pressable
|
||||
accessibilityHint="Swipe left for archive and delete actions"
|
||||
accessibilityLabel={thread.title}
|
||||
accessibilityRole="button"
|
||||
accessibilityState={{ selected }}
|
||||
onPress={() => {
|
||||
close();
|
||||
onSelectThread(thread);
|
||||
}}
|
||||
style={({ pressed }) => [
|
||||
styles.threadRow,
|
||||
{
|
||||
backgroundColor: selected
|
||||
? selectedBackgroundColor
|
||||
: pressed
|
||||
? pressedBackgroundColor
|
||||
: backgroundColor,
|
||||
},
|
||||
]}
|
||||
>
|
||||
<View style={styles.threadText}>
|
||||
<Text className="text-base font-t3-medium" numberOfLines={1}>
|
||||
{thread.title}
|
||||
</Text>
|
||||
<Text className="text-xs text-foreground-muted" numberOfLines={1}>
|
||||
{relativeTime(thread.updatedAt ?? thread.createdAt)}
|
||||
</Text>
|
||||
</View>
|
||||
<StatusPill {...threadStatusTone(thread)} size="compact" />
|
||||
</Pressable>
|
||||
)}
|
||||
</ThreadSwipeable>
|
||||
);
|
||||
});
|
||||
|
||||
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 (
|
||||
<ThreadSwipeable
|
||||
<ThreadNavigationRow
|
||||
key={threadKey}
|
||||
backgroundColor={backgroundColor}
|
||||
containerStyle={styles.threadRowContainer}
|
||||
fullSwipeWidth={props.width - 20}
|
||||
onDelete={() => 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) => (
|
||||
<Pressable
|
||||
accessibilityHint="Swipe left for archive and delete actions"
|
||||
accessibilityLabel={thread.title}
|
||||
accessibilityRole="button"
|
||||
accessibilityState={{ selected }}
|
||||
onPress={() => {
|
||||
close();
|
||||
props.onSelectThread(thread);
|
||||
}}
|
||||
style={({ pressed }) => [
|
||||
styles.threadRow,
|
||||
{
|
||||
backgroundColor: selected
|
||||
? selectedBackgroundColor
|
||||
: pressed
|
||||
? pressedBackgroundColor
|
||||
: backgroundColor,
|
||||
},
|
||||
]}
|
||||
>
|
||||
<View style={styles.threadText}>
|
||||
<Text className="text-base font-t3-medium" numberOfLines={1}>
|
||||
{thread.title}
|
||||
</Text>
|
||||
<Text className="text-xs text-foreground-muted" numberOfLines={1}>
|
||||
{relativeTime(thread.updatedAt ?? thread.createdAt)}
|
||||
</Text>
|
||||
</View>
|
||||
<StatusPill {...threadStatusTone(thread)} size="compact" />
|
||||
</Pressable>
|
||||
)}
|
||||
</ThreadSwipeable>
|
||||
pressedBackgroundColor={pressedBackgroundColor}
|
||||
selected={selected}
|
||||
selectedBackgroundColor={selectedBackgroundColor}
|
||||
thread={thread}
|
||||
/>
|
||||
);
|
||||
})
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user