Skip to content

Commit e61e8b9

Browse files
committed
fix(chat): avoid repeating failed deferred work loads on render
1 parent 9a0ab90 commit e61e8b9

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,15 +1233,17 @@ function RunFoldButton({
12331233
.map((workAnchorId) => loadWork({ environmentId, input: { threadId, workAnchorId } })),
12341234
);
12351235
};
1236+
const loadOnExpand = useEffectEvent(() => {
1237+
void load().then((results) =>
1238+
setFailed(
1239+
results.some((result) => result._tag === "Failure" || result.value._tag === "error"),
1240+
),
1241+
);
1242+
});
12361243
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Restored expanded folds load their deferred server history on mount.
12371244
useEffect(() => {
1238-
if (entry.expanded)
1239-
void load().then((results) =>
1240-
setFailed(
1241-
results.some((result) => result._tag === "Failure" || result.value._tag === "error"),
1242-
),
1243-
);
1244-
}, [entry.expanded, load]);
1245+
if (entry.expanded && anchorsKey && environmentId && threadId) loadOnExpand();
1246+
}, [entry.expanded, anchorsKey, environmentId, threadId]);
12451247
const error = anchorsKey && failed ? "Couldn't load work. Tap to retry." : null;
12461248
const loading = entry.expanded && anchorsKey !== "" && !failed;
12471249
const toggle = () => {

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
useContext,
3535
useLayoutEffect,
3636
useEffect,
37+
useEffectEvent,
3738
useRef,
3839
useState,
3940
type ReactNode,
@@ -1570,15 +1571,17 @@ function TurnFoldTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "turn-
15701571
.map((workAnchorId) => loadWork({ environmentId, input: { threadId, workAnchorId } })),
15711572
);
15721573
};
1574+
const loadOnExpand = useEffectEvent(() => {
1575+
void load().then((results) =>
1576+
setFailed(
1577+
results.some((result) => result._tag === "Failure" || result.value._tag === "error"),
1578+
),
1579+
);
1580+
});
15731581
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Restored expanded folds load their deferred server history on mount.
15741582
useEffect(() => {
1575-
if (row.expanded)
1576-
void load().then((results) =>
1577-
setFailed(
1578-
results.some((result) => result._tag === "Failure" || result.value._tag === "error"),
1579-
),
1580-
);
1581-
}, [row.expanded, load]);
1583+
if (row.expanded && anchorsKey && environmentId && threadId) loadOnExpand();
1584+
}, [row.expanded, anchorsKey, environmentId, threadId]);
15821585
const error = anchorsKey && failed ? "Couldn't load work. Click to retry." : null;
15831586
const loading = row.expanded && anchorsKey !== "" && !failed;
15841587
const toggle = () => {

‎packages/client-runtime/src/state/threads-sync.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ describe("EnvironmentThreads", () => {
741741
},
742742
cachedHistory: { historyCursor: "older", hasMoreHistory: true },
743743
historyHttpClient: HttpClient.make((request, url) => {
744+
expect(url.searchParams.get("view")).toBe("work");
744745
const cursor = url.searchParams.get("cursor")!;
745746
requests.push(cursor);
746747
if (cursor === "second" && !failed) {

0 commit comments

Comments
 (0)