Skip to content

Commit 82e9d33

Browse files
committed
fix(mobile): chat feed no longer re-derives on every render
1 parent e64766c commit 82e9d33

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,33 @@ function ThreadFeedPlaceholder(props: {
17341734
);
17351735
}
17361736

1737+
// A hook's return value is frozen to the compiler, so ThreadFeed's later
1738+
// hooks can't drag this derivation into an uncached scope.
1739+
function useThreadFeedPresentation(
1740+
props: ThreadFeedProps,
1741+
expandedTurnIds: ReadonlySet<RunId>,
1742+
expandedWorkGroups: Record<string, boolean>,
1743+
) {
1744+
const [feedCache] = useState(createThreadFeedCache);
1745+
return appendPendingThreadMessages(
1746+
deriveThreadFeedPresentation(
1747+
props.feed,
1748+
props.latestRun,
1749+
expandedTurnIds,
1750+
new Set(
1751+
Object.entries(expandedWorkGroups)
1752+
.filter(([, expanded]) => expanded)
1753+
.map(([groupId]) => groupId),
1754+
),
1755+
props.activeWorkStartedAt,
1756+
props.runlessWorkActive ?? false,
1757+
feedCache,
1758+
),
1759+
props.feed,
1760+
props.queuedMessages,
1761+
);
1762+
}
1763+
17371764
export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
17381765
const { listRef, onEndFollowEnabledChange, onHeaderMaterialVisibilityChange } = props;
17391766
const workflowOpening = useWorkflowOpening(props.environmentId, props.threadId);
@@ -1759,7 +1786,6 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
17591786
const disclosureSettleFrameRef = useRef<number | null>(null);
17601787
const disclosureSettleSecondFrameRef = useRef<number | null>(null);
17611788
const disclosureAnchorKeyRef = useRef<string | null>(null);
1762-
const [feedCache] = useState(createThreadFeedCache);
17631789
const previousLatestTurnRef = useRef(props.latestRun);
17641790
const userScrollSettleTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
17651791
const headerMaterialVisibleRef = useRef(false);
@@ -2315,23 +2341,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
23152341
reportHeaderMaterialVisibility(false);
23162342
}, [feedThreadKey, reportHeaderMaterialVisibility]);
23172343

2318-
const presentedFeed = appendPendingThreadMessages(
2319-
deriveThreadFeedPresentation(
2320-
props.feed,
2321-
props.latestRun,
2322-
expandedTurnIds,
2323-
new Set(
2324-
Object.entries(expandedWorkGroups)
2325-
.filter(([, expanded]) => expanded)
2326-
.map(([groupId]) => groupId),
2327-
),
2328-
props.activeWorkStartedAt,
2329-
props.runlessWorkActive ?? false,
2330-
feedCache,
2331-
),
2332-
props.feed,
2333-
props.queuedMessages,
2334-
);
2344+
const presentedFeed = useThreadFeedPresentation(props, expandedTurnIds, expandedWorkGroups);
23352345
const setupAnchorIndex = presentedFeed.findIndex(
23362346
(entry) => entry.type === "message" && entry.message.role === "user",
23372347
);

0 commit comments

Comments
 (0)