Skip to content

Commit 9f45f72

Browse files
perf(mobile): recycle the default v2 home list and scope the snooze minute tick
The default Home screen rendered the v2 thread list with a bare RN FlatList: no recycling, no item equality, and the 60s snooze-minute clock rode in the list's extraData, so every visible row (each one a ReanimatedSwipeable + PR subscription) re-rendered on every tick and on every unrelated shell update. The legacy v1 list and the iPad sidebar already use LegendList with recycleItems + itemsAreEqual for the same rows; Home's default path was the outlier. Swap the v2 Home list onto the same LegendList configuration and move the clock out of extraData and onto the items: each v2-thread item now carries its precomputed row time, the snooze-menu minute (only for rows whose menu actually offers snooze presets), and its trailing-divider flag, so the recycler's equality invalidates only rows whose visible text or menu moved. The shared equality and the sidebar's wrapper now match, and the sidebar drops its own minute-tick extraData bust too. Thread rows also get reference-stable provider-instance objects, which previously broke the memoized rows' props comparison on every render.
1 parent d7819c1 commit 9f45f72

7 files changed

Lines changed: 718 additions & 86 deletions

File tree

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { useAtomSet, useAtomValue } from "@effect/atom-react";
2323
import { AsyncResult } from "effect/unstable/reactivity";
2424
import { useFocusEffect } from "@react-navigation/native";
2525
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
26-
import { ActivityIndicator, FlatList, Platform, View } from "react-native";
26+
import { ActivityIndicator, Platform, View } from "react-native";
2727
import type { SwipeableMethods } from "react-native-gesture-handler/ReanimatedSwipeable";
2828
import { useSafeAreaInsets } from "react-native-safe-area-context";
2929

@@ -55,11 +55,12 @@ import {
5555
ThreadListV2ShowMoreRow,
5656
ThreadListV2SnoozedShelfHeader,
5757
} from "../threads/thread-list-v2-items";
58-
import { resolveThreadProviderInstance } from "../threads/thread-provider-instance";
58+
import { useThreadRowProviderInstanceResolver } from "../threads/thread-provider-instance";
5959
import {
6060
buildThreadListV2Items,
6161
getThreadListV2OrderedSection,
6262
buildThreadListV2ListItems,
63+
threadListV2ListItemsAreEqual,
6364
THREAD_LIST_V2_SETTLED_INITIAL_COUNT,
6465
THREAD_LIST_V2_SETTLED_PAGE_COUNT,
6566
type ThreadListV2ListItem,
@@ -138,6 +139,10 @@ interface HomeScreenProps {
138139
/* ─── Layout constants ───────────────────────────────────────────────── */
139140

140141
const ESTIMATED_THREAD_ROW_HEIGHT = 72;
142+
// v2 cards run taller than v1's compact rows (title can take two lines plus
143+
// the branch/PR footer); settled slim rows run shorter. The estimate only
144+
// sizes the recycler's initial container pool, so the taller bias is safe.
145+
const ESTIMATED_THREAD_LIST_V2_ROW_HEIGHT = 92;
141146
const PRE_LIQUID_GLASS_BOTTOM_TOOLBAR_HEIGHT = 44;
142147
/**
143148
* Top spacing between the list and the Android custom header. The Android
@@ -655,6 +660,9 @@ export function HomeScreen(props: HomeScreenProps) {
655660
),
656661
[serverConfigs],
657662
);
663+
// Reference-stable provider glyphs: a fresh object per render would break
664+
// the memoized rows' props comparison on every parent render.
665+
const resolveProviderInstance = useThreadRowProviderInstanceResolver(serverConfigs);
658666
const pendingOrder = usePendingThreadOrder(nowMinute, snoozeWakeTick);
659667
const threadMovePlanners = useMemo(() => {
660668
const sectionPlanner = (section: "pinned" | "active") =>
@@ -783,8 +791,16 @@ export function HomeScreen(props: HomeScreenProps) {
783791
settledShelfExpanded,
784792
settledShelfHeaderIndex: threadListV2Layout.settledShelfHeaderIndex,
785793
snoozeLabelNow: `${nowMinute}:00.000Z`,
794+
snoozeEnvironmentIds,
786795
}),
787-
[settledShelfExpanded, snoozedShelfExpanded, threadListV2Layout, v2PendingTasks],
796+
[
797+
nowMinute,
798+
settledShelfExpanded,
799+
snoozedShelfExpanded,
800+
snoozeEnvironmentIds,
801+
threadListV2Layout,
802+
v2PendingTasks,
803+
],
788804
);
789805

790806
useThreadJumpShortcuts(
@@ -793,11 +809,7 @@ export function HomeScreen(props: HomeScreenProps) {
793809
);
794810

795811
const renderV2Item = useCallback(
796-
({ item, index }: { readonly item: ThreadListV2ListItem; readonly index: number }) => {
797-
const nextItem = threadListV2Items[index + 1];
798-
const showTrailingDivider =
799-
nextItem?.type === "v2-thread" ||
800-
(nextItem?.type === "v2-pending" && !nextItem.showPendingDivider);
812+
({ item }: { readonly item: ThreadListV2ListItem }) => {
801813
if (item.type === "v2-pending") {
802814
const pendingScopeKey = scopedProjectKey(
803815
item.pendingTask.environmentId,
@@ -816,7 +828,7 @@ export function HomeScreen(props: HomeScreenProps) {
816828
}
817829
environmentMachine={machineByEnvironmentId.get(item.pendingTask.environmentId)}
818830
showPendingDivider={item.showPendingDivider}
819-
showTrailingDivider={showTrailingDivider}
831+
showTrailingDivider={item.showTrailingDivider}
820832
onSelectPendingTask={props.onSelectPendingTask}
821833
onDeletePendingTask={props.onDeletePendingTask}
822834
/>
@@ -853,16 +865,17 @@ export function HomeScreen(props: HomeScreenProps) {
853865
hasQueuedMessages={queuedThreadKeys.has(movedId)}
854866
snoozed={item.item.snoozed}
855867
pinned={item.item.pinned}
856-
snoozePresetMinute={nowMinute}
868+
snoozePresetMinute={item.snoozePresetMinute ?? ""}
857869
snoozeWakeLabelText={item.snoozeWakeLabelText}
858-
showTrailingDivider={showTrailingDivider}
870+
timeLabel={item.timeLabel}
871+
showTrailingDivider={item.showTrailingDivider}
859872
project={
860873
projectByKey.get(scopedProjectKey(thread.environmentId, thread.projectId)) ?? null
861874
}
862875
projectTitle={v2ProjectTitleByProjectKey.get(
863876
scopedProjectKey(thread.environmentId, thread.projectId),
864877
)}
865-
providerInstance={resolveThreadProviderInstance(serverConfigs, thread)}
878+
providerInstance={resolveProviderInstance(thread)}
866879
environmentLabel={
867880
Object.keys(props.savedConnectionsById).length > 1
868881
? (props.savedConnectionsById[thread.environmentId]?.environmentLabel ?? null)
@@ -931,41 +944,38 @@ export function HomeScreen(props: HomeScreenProps) {
931944
props.onSelectThread,
932945
props.onNewThreadOnBranch,
933946
props.savedConnectionsById,
934-
serverConfigs,
947+
resolveProviderInstance,
935948
shelfPreferencesLoaded,
936949
settlementEnvironmentIds,
937950
snoozeEnvironmentIds,
938-
threadListV2Items,
939951
threadSearchMatchByKey,
940952
titleRegenerationEnvironmentIds,
941953
toggleSettledShelf,
942954
toggleSnoozedShelf,
943955
v2ProjectTitleByProjectKey,
944956
props.searchQuery,
945-
nowMinute,
946957
],
947958
);
948959
const v2KeyExtractor = useCallback((item: ThreadListV2ListItem) => item.key, []);
949960

950-
// FlatList treats a changed extraData identity as "re-render every visible
951-
// row", so an inline object literal would invalidate all rows on every
952-
// HomeScreen render.
961+
// FlatList/LegendList treat a changed extraData identity as "re-render every
962+
// visible row", so an inline object literal would invalidate all rows on
963+
// every HomeScreen render — and the minute clock must stay out of it for
964+
// the same reason: the clock text is precomputed per item instead.
953965
const v2ExtraData = useMemo(
954966
() => ({
955967
projectByKey,
956968
projectTitleByProjectKey: v2ProjectTitleByProjectKey,
957969
serverConfigs,
958970
savedConnectionsById: props.savedConnectionsById,
959971
searchQuery: props.searchQuery,
960-
snoozePresetMinute: nowMinute,
961972
threadSearchMatchByKey,
962973
}),
963974
[
964975
projectByKey,
965976
props.searchQuery,
966977
props.savedConnectionsById,
967978
serverConfigs,
968-
nowMinute,
969979
threadSearchMatchByKey,
970980
v2ProjectTitleByProjectKey,
971981
],
@@ -1225,11 +1235,19 @@ export function HomeScreen(props: HomeScreenProps) {
12251235
: "flex-1 bg-screen"
12261236
}
12271237
>
1238+
{/* Same recycler the iPad sidebar and the legacy list use: cells are
1239+
reused across data rebuilds and `itemsAreEqual` keeps a minute tick
1240+
(or an unrelated shell update) from re-rendering untouched rows. */}
12281241
<SwipeableScrollGateProvider enabled={swipeEnabled}>
1229-
<FlatList
1242+
<LegendList
12301243
data={threadListV2Items}
12311244
renderItem={renderV2Item}
12321245
keyExtractor={v2KeyExtractor}
1246+
getItemType={(item) => item.type}
1247+
itemsAreEqual={threadListV2ListItemsAreEqual}
1248+
estimatedItemSize={ESTIMATED_THREAD_LIST_V2_ROW_HEIGHT}
1249+
drawDistance={500}
1250+
recycleItems
12331251
extraData={v2ExtraData}
12341252
ListHeaderComponent={v2ListHeader}
12351253
ListFooterComponent={

‎apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx‎

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@ import {
8484
ThreadListV2ShowMoreRow,
8585
ThreadListV2SnoozedShelfHeader,
8686
} from "./thread-list-v2-items";
87-
import { resolveThreadProviderInstance } from "./thread-provider-instance";
87+
import { useThreadRowProviderInstanceResolver } from "./thread-provider-instance";
8888
import {
8989
buildThreadListV2Items,
9090
getThreadListV2OrderedSection,
9191
buildThreadListV2ListItems,
92+
isThreadListV2ListItem,
93+
threadListV2ListItemsAreEqual,
9294
THREAD_LIST_V2_SETTLED_INITIAL_COUNT,
9395
THREAD_LIST_V2_SETTLED_PAGE_COUNT,
9496
type ThreadListV2ListItem,
@@ -483,6 +485,9 @@ function ThreadNavigationSidebarPane(
483485
),
484486
[serverConfigs],
485487
);
488+
// Reference-stable provider glyphs: a fresh object per render would break
489+
// the memoized rows' props comparison on every parent render.
490+
const resolveProviderInstance = useThreadRowProviderInstanceResolver(serverConfigs);
486491
const pendingOrder = usePendingThreadOrder(nowMinute, snoozeWakeTick);
487492
const threadMovePlanners = useMemo(() => {
488493
const sectionPlanner = (section: "pinned" | "active") =>
@@ -607,6 +612,7 @@ function ThreadNavigationSidebarPane(
607612
settledShelfExpanded,
608613
settledShelfHeaderIndex: threadListV2Layout.settledShelfHeaderIndex,
609614
snoozeLabelNow: `${nowMinute}:00.000Z`,
615+
snoozeEnvironmentIds,
610616
});
611617
if (settledShelfExpanded && threadListV2Layout.hiddenSettledCount > 0) {
612618
items.push({
@@ -625,6 +631,7 @@ function ThreadNavigationSidebarPane(
625631
selectedProjectRefs,
626632
settledShelfExpanded,
627633
snoozedShelfExpanded,
634+
snoozeEnvironmentIds,
628635
threadListV2Enabled,
629636
threadListV2Layout,
630637
]);
@@ -789,17 +796,18 @@ function ThreadNavigationSidebarPane(
789796
onScroll: onMaterialFabScroll,
790797
onScrollBeginDrag: handleScrollBeginDrag,
791798
});
792-
// Project shells load after the first rows draw, so the maps they feed have
793-
// to bust the recycler's memoization — otherwise a row keeps the blank
794-
// favicon and fallback title it was first rendered with.
799+
// The sticky header's project shells and search maps feed row props, so
800+
// they have to bust the recycler's memoization — otherwise a row keeps the
801+
// blank favicon and fallback title it was first rendered with. The minute
802+
// clock deliberately stays out: its per-row text lives on the items, so a
803+
// tick only re-renders rows whose displayed text actually moved.
795804
const listExtraData = useMemo(
796805
() => ({
797806
selectedThreadKey: props.selectedThreadKey ?? "",
798807
projectByKey,
799808
projectTitleByProjectKey,
800809
savedConnectionsById,
801810
serverConfigs,
802-
snoozePresetMinute: nowMinute,
803811
threadSearchMatchByKey,
804812
}),
805813
[
@@ -808,49 +816,23 @@ function ThreadNavigationSidebarPane(
808816
projectTitleByProjectKey,
809817
savedConnectionsById,
810818
serverConfigs,
811-
nowMinute,
812819
threadSearchMatchByKey,
813820
],
814821
);
815822
useThreadJumpShortcuts(listItems, handleSelectThread);
816823
const sidebarItemsAreEqual = useCallback(
817824
(previous: SidebarListItem, item: SidebarListItem): boolean => {
818-
if (previous.type === "v2-thread" && item.type === "v2-thread") {
819-
return (
820-
previous.key === item.key &&
821-
previous.item.thread === item.item.thread &&
822-
previous.item.variant === item.item.variant &&
823-
previous.item.snoozed === item.item.snoozed &&
824-
previous.item.pinned === item.item.pinned &&
825-
previous.snoozeWakeLabelText === item.snoozeWakeLabelText
826-
);
825+
if (isThreadListV2ListItem(previous) && isThreadListV2ListItem(item)) {
826+
return threadListV2ListItemsAreEqual(previous, item);
827827
}
828828
if (previous.type === "v2-show-more" && item.type === "v2-show-more") {
829829
return previous.hiddenCount === item.hiddenCount;
830830
}
831-
if (previous.type === "v2-pending" && item.type === "v2-pending") {
832-
return (
833-
previous.pendingTask === item.pendingTask &&
834-
previous.showPendingDivider === item.showPendingDivider
835-
);
836-
}
837-
if (previous.type === "v2-snoozed-shelf" && item.type === "v2-snoozed-shelf") {
838-
return previous.count === item.count && previous.expanded === item.expanded;
839-
}
840-
if (previous.type === "v2-settled-shelf" && item.type === "v2-settled-shelf") {
841-
return previous.count === item.count && previous.expanded === item.expanded;
842-
}
843831
if (
844-
previous.type === "v2-thread" ||
832+
isThreadListV2ListItem(previous) ||
845833
previous.type === "v2-show-more" ||
846-
previous.type === "v2-pending" ||
847-
previous.type === "v2-snoozed-shelf" ||
848-
previous.type === "v2-settled-shelf" ||
849-
item.type === "v2-thread" ||
850-
item.type === "v2-show-more" ||
851-
item.type === "v2-pending" ||
852-
item.type === "v2-snoozed-shelf" ||
853-
item.type === "v2-settled-shelf"
834+
isThreadListV2ListItem(item) ||
835+
item.type === "v2-show-more"
854836
) {
855837
return false;
856838
}
@@ -917,11 +899,12 @@ function ThreadNavigationSidebarPane(
917899
hasQueuedMessages={queuedThreadKeys.has(`${thread.environmentId}:${thread.id}`)}
918900
snoozed={item.item.snoozed}
919901
pinned={item.item.pinned}
920-
snoozePresetMinute={nowMinute}
902+
snoozePresetMinute={item.snoozePresetMinute ?? ""}
921903
snoozeWakeLabelText={item.snoozeWakeLabelText}
904+
timeLabel={item.timeLabel}
922905
project={projectByKey.get(scopeKey) ?? null}
923906
projectTitle={projectTitleByProjectKey.get(scopeKey)}
924-
providerInstance={resolveThreadProviderInstance(serverConfigs, thread)}
907+
providerInstance={resolveProviderInstance(thread)}
925908
environmentLabel={
926909
Object.keys(savedConnectionsById).length > 1
927910
? (savedConnectionsById[thread.environmentId]?.environmentLabel ?? null)
@@ -1104,7 +1087,6 @@ function ThreadNavigationSidebarPane(
11041087
props.selectedThreadKey,
11051088
props.width,
11061089
savedConnectionsById,
1107-
serverConfigs,
11081090
shelfPreferencesLoaded,
11091091
threadSearchMatchByKey,
11101092
titleRegenerationEnvironmentIds,
@@ -1114,7 +1096,7 @@ function ThreadNavigationSidebarPane(
11141096
sidebarScrollGesture,
11151097
snoozeEnvironmentIds,
11161098
snoozeThread,
1117-
nowMinute,
1099+
resolveProviderInstance,
11181100
toggleSettledShelf,
11191101
toggleSnoozedShelf,
11201102
unpinThread,

‎apps/mobile/src/features/threads/thread-list-v2-items.tsx‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import type {
1818
import type { EnvironmentThreadSearchMatch } from "@t3tools/client-runtime/state/thread-search";
1919
import type { EnvironmentMachineKind } from "@t3tools/contracts";
2020
import { canSnooze, resolveSnoozePresets } from "@t3tools/client-runtime/state/thread-settled";
21-
import { resolveSettledThreadTimestamp } from "@t3tools/client-runtime/state/thread-sort";
2221
import type { MenuAction } from "@react-native-menu/menu";
2322
import { memo, useCallback, useEffect, useMemo, useState, type ComponentProps } from "react";
2423
import { Alert, Pressable, useWindowDimensions, View } from "react-native";
@@ -33,7 +32,6 @@ import { ProviderInstanceIcon } from "../../components/ProviderIcon";
3332
import type { ThreadRowProviderInstance } from "./thread-provider-instance";
3433
import { cn } from "../../lib/cn";
3534
import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
36-
import { relativeTime } from "../../lib/time";
3735
import { useUniwindTheme } from "../../lib/useUniwindTheme";
3836
import type { PendingNewTask } from "../../state/use-pending-new-tasks";
3937
import { useThreadPr } from "../../state/use-thread-pr";
@@ -69,10 +67,6 @@ const STATUS_LABEL_BY_STATUS: Partial<
6967
failed: { label: "Failed", className: "text-danger-foreground" },
7068
};
7169

72-
function threadTimeLabel(thread: EnvironmentThreadShell): string {
73-
return relativeTime(thread.latestUserMessageAt ?? thread.updatedAt ?? thread.createdAt);
74-
}
75-
7670
// Menus keep lifecycle and title regeneration together. Archive keeps its
7771
// own surface (thread screen / settings) rather than crowding v2 rows.
7872
const CARD_MENU_ACTIONS: MenuAction[] = [
@@ -453,8 +447,15 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
453447
/** Preformatted against the parent minute tick so this memoized row's
454448
countdown keeps moving. */
455449
readonly snoozeWakeLabelText?: string;
456-
/** Parent minute tick passed as a prop so this memoized row refreshes its
457-
native snooze menu while mounted. */
450+
/** Preformatted against the parent clock (row order timestamp: settle stamp
451+
on settled rows, latest activity otherwise). Blank while a status label
452+
or the wake countdown owns that slot. Precomputed per row — not via the
453+
list's extraData — so the minute tick re-renders only rows whose
454+
displayed text moved. */
455+
readonly timeLabel: string;
456+
/** Parent minute tick carried on the row's list item, present only when the
457+
row's menu offers snooze presets, so those menus refresh while mounted
458+
without invalidating every other row. */
458459
readonly snoozePresetMinute: string;
459460
readonly project: EnvironmentProject | null;
460461
readonly projectTitle?: string;
@@ -549,13 +550,9 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
549550

550551
const status = resolveThreadListV2Status(thread);
551552
const statusLabel = STATUS_LABEL_BY_STATUS[status];
552-
// Settled rows label by the same stamp they sort by, so order and label
553-
// can't disagree. updatedAt is always present, so the resolver never
554-
// returns null here.
555-
const settledTimestamp =
556-
variant === "slim" && !snoozedRow ? resolveSettledThreadTimestamp(thread) : null;
557-
const timeLabel =
558-
settledTimestamp !== null ? relativeTime(settledTimestamp) : threadTimeLabel(thread);
553+
// The timestamp is precomputed on the list item (same stamps the settled
554+
// tail sorts by) so a minute tick only re-renders rows that draw it.
555+
const timeLabel = props.timeLabel;
559556

560557
const handleDelete = useCallback(() => onDeleteThread(thread), [onDeleteThread, thread]);
561558
const handleRename = useCallback(() => onRenameThread(thread), [onRenameThread, thread]);

0 commit comments

Comments
 (0)