Skip to content

Commit 7b6fbb8

Browse files
refactor(mobile): thread detail and shelf toggles drop layout-effect ref syncs
Shelf toggles flip the stored preference through a transform, which reads the optimistic value, instead of mirroring each flag into a ref. The thread detail reads the draft from props and updates the follow flag where it changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2cf759c commit 7b6fbb8

2 files changed

Lines changed: 44 additions & 51 deletions

File tree

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

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2+
import { AsyncResult } from "effect/reactivity";
13
import * as Dialog from "../../components/ui/Dialog";
24
import { useAtomValue } from "@effect/atom-react";
35
import { useChildThreadInputs, useThreadReportedModelSelection } from "../../state/entities";
@@ -404,10 +406,6 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
404406
const frame = requestAnimationFrame(() => composerEditorRef.current?.focus());
405407
return () => cancelAnimationFrame(frame);
406408
}, [editingRunId]);
407-
const draftMessageRef = useRef(props.draftMessage);
408-
useLayoutEffect(() => {
409-
draftMessageRef.current = props.draftMessage;
410-
});
411409
const composerOverlayRef = useRef<ViewInstance>(null);
412410
const listRef = useRef<LegendListRef>(null);
413411
const feedTouchStartRef = useRef<{ pageX: number; pageY: number } | null>(null);
@@ -660,15 +658,18 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
660658
[userInputCoverageApplies],
661659
);
662660
const { freeze, scrollMessageToEnd } = useKeyboardScrollToEnd({ listRef });
661+
// Repin timers fire after the render that scheduled them, so they read follow
662+
// state from the ref the writers below keep in step with it.
663663
const endFollowEnabledRef = useRef(true);
664-
useLayoutEffect(() => {
665-
endFollowEnabledRef.current = endFollowEnabled;
666-
});
667664
const overlayRepinTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
668665
const previousWorkingControlStateRef = useRef({
669666
threadKey: selectedThreadKey,
670667
visible: false,
671668
});
669+
const handleEndFollowEnabledChange = (enabled: boolean) => {
670+
endFollowEnabledRef.current = enabled;
671+
setEndFollowEnabled(enabled);
672+
};
672673
const scheduleOverlayRepin = (delayMs: number) => {
673674
if (overlayRepinTimerRef.current !== null) {
674675
clearTimeout(overlayRepinTimerRef.current);
@@ -836,6 +837,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
836837
setAnchorMessageId(null);
837838
setSubmittedMessageId(null);
838839
lastScrolledSubmittedMessageIdRef.current = null;
840+
endFollowEnabledRef.current = true;
839841
setEndFollowEnabled(true);
840842
freeze.set(false);
841843
}, [freeze, selectedThreadKey]);
@@ -925,34 +927,30 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
925927
};
926928

927929
const handleEditPendingMessage = async (message: QueuedThreadMessage) => {
928-
await editPendingThreadMessage(message)
929-
.then((edited) => {
930-
if (
931-
edited &&
932-
selectedThreadKeyRef.current === scopedThreadKey(message.environmentId, message.threadId)
933-
) {
934-
composerEditorRef.current?.focus();
935-
}
936-
})
937-
.catch((error: unknown) => {
938-
Dialog.alert(
939-
"Could not edit message",
940-
error instanceof Error ? error.message : "Please try again.",
941-
);
942-
});
930+
const edited = await settlePromise(() => editPendingThreadMessage(message));
931+
if (AsyncResult.isFailure(edited)) {
932+
const error = squashAtomCommandFailure(edited);
933+
Dialog.alert(
934+
"Could not edit message",
935+
error instanceof Error ? error.message : "Please try again.",
936+
);
937+
return;
938+
}
939+
if (
940+
edited.value &&
941+
selectedThreadKeyRef.current === scopedThreadKey(message.environmentId, message.threadId)
942+
) {
943+
composerEditorRef.current?.focus();
944+
}
943945
};
944946

945947
const collapseComposer = () => {
946948
composerEditorRef.current?.blur();
947949
};
948950

949951
const handleUseArtifactTemplate = (template: CodexArtifactTemplate) => {
950-
const currentDraft = draftMessageRef.current;
951-
const nextDraft = appendCodexArtifactTemplateUsePrompt(currentDraft, template);
952-
if (nextDraft !== currentDraft) {
953-
draftMessageRef.current = nextDraft;
954-
props.onChangeDraftMessage(nextDraft);
955-
}
952+
const nextDraft = appendCodexArtifactTemplateUsePrompt(props.draftMessage, template);
953+
if (nextDraft !== props.draftMessage) props.onChangeDraftMessage(nextDraft);
956954
requestAnimationFrame(() => {
957955
composerEditorRef.current?.focus();
958956
composerEditorRef.current?.setSelection({ start: nextDraft.length, end: nextDraft.length });
@@ -1065,7 +1063,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
10651063
layoutVariant={layoutVariant}
10661064
usesAutomaticContentInsets={props.usesAutomaticContentInsets}
10671065
onHeaderMaterialVisibilityChange={props.onHeaderMaterialVisibilityChange}
1068-
onEndFollowEnabledChange={setEndFollowEnabled}
1066+
onEndFollowEnabledChange={handleEndFollowEnabledChange}
10691067
skills={selectedProviderSkills}
10701068
onUseArtifactTemplate={handleUseArtifactTemplate}
10711069
/>

‎apps/mobile/src/features/threads/use-thread-list-v2-shelf-preferences.ts‎

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { useAtomSet, useAtomValue } from "@effect/atom-react";
22
import { AsyncResult } from "effect/reactivity";
3-
import { useLayoutEffect, useRef } from "react";
43

54
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
65

7-
/**
8-
* Refs advance before persistence starts so consecutive presses always toggle the latest value,
9-
* even if React has not rendered the optimistic patch yet.
10-
*/
116
export function useThreadListV2ShelfPreferences() {
127
const preferencesResult = useAtomValue(mobilePreferencesAtom);
138
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
@@ -19,32 +14,32 @@ export function useThreadListV2ShelfPreferences() {
1914
const workingShelfEnabled = loaded && preferencesResult.value.workingShelfEnabled === true;
2015
const workingShelfExpanded =
2116
loaded && preferencesResult.value.threadListWorkingShelfExpanded === true;
22-
const snoozedShelfExpandedRef = useRef(snoozedShelfExpanded);
23-
const settledShelfExpandedRef = useRef(settledShelfExpanded);
24-
const workingShelfExpandedRef = useRef(workingShelfExpanded);
25-
useLayoutEffect(() => {
26-
snoozedShelfExpandedRef.current = snoozedShelfExpanded;
27-
settledShelfExpandedRef.current = settledShelfExpanded;
28-
workingShelfExpandedRef.current = workingShelfExpanded;
29-
});
3017

18+
// A transform reads the optimistic value, so two presses before the next
19+
// render still toggle twice instead of writing the same value.
3120
const toggleSnoozedShelf = () => {
3221
if (!loaded) return;
33-
const expanded = !snoozedShelfExpandedRef.current;
34-
snoozedShelfExpandedRef.current = expanded;
35-
savePreferences({ threadListSnoozedShelfExpanded: expanded });
22+
savePreferences({
23+
transform: (current) => ({
24+
threadListSnoozedShelfExpanded: current.threadListSnoozedShelfExpanded !== true,
25+
}),
26+
});
3627
};
3728
const toggleSettledShelf = () => {
3829
if (!loaded) return;
39-
const expanded = !settledShelfExpandedRef.current;
40-
settledShelfExpandedRef.current = expanded;
41-
savePreferences({ threadListSettledShelfExpanded: expanded });
30+
savePreferences({
31+
transform: (current) => ({
32+
threadListSettledShelfExpanded: current.threadListSettledShelfExpanded !== true,
33+
}),
34+
});
4235
};
4336
const toggleWorkingShelf = () => {
4437
if (!loaded) return;
45-
const expanded = !workingShelfExpandedRef.current;
46-
workingShelfExpandedRef.current = expanded;
47-
savePreferences({ threadListWorkingShelfExpanded: expanded });
38+
savePreferences({
39+
transform: (current) => ({
40+
threadListWorkingShelfExpanded: current.threadListWorkingShelfExpanded !== true,
41+
}),
42+
});
4843
};
4944

5045
return {

0 commit comments

Comments
 (0)