|
| 1 | +import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 2 | +import { AsyncResult } from "effect/reactivity"; |
1 | 3 | import * as Dialog from "../../components/ui/Dialog"; |
2 | 4 | import { useAtomValue } from "@effect/atom-react"; |
3 | 5 | import { useChildThreadInputs, useThreadReportedModelSelection } from "../../state/entities"; |
@@ -404,10 +406,6 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai |
404 | 406 | const frame = requestAnimationFrame(() => composerEditorRef.current?.focus()); |
405 | 407 | return () => cancelAnimationFrame(frame); |
406 | 408 | }, [editingRunId]); |
407 | | - const draftMessageRef = useRef(props.draftMessage); |
408 | | - useLayoutEffect(() => { |
409 | | - draftMessageRef.current = props.draftMessage; |
410 | | - }); |
411 | 409 | const composerOverlayRef = useRef<ViewInstance>(null); |
412 | 410 | const listRef = useRef<LegendListRef>(null); |
413 | 411 | const feedTouchStartRef = useRef<{ pageX: number; pageY: number } | null>(null); |
@@ -660,15 +658,18 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai |
660 | 658 | [userInputCoverageApplies], |
661 | 659 | ); |
662 | 660 | 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. |
663 | 663 | const endFollowEnabledRef = useRef(true); |
664 | | - useLayoutEffect(() => { |
665 | | - endFollowEnabledRef.current = endFollowEnabled; |
666 | | - }); |
667 | 664 | const overlayRepinTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
668 | 665 | const previousWorkingControlStateRef = useRef({ |
669 | 666 | threadKey: selectedThreadKey, |
670 | 667 | visible: false, |
671 | 668 | }); |
| 669 | + const handleEndFollowEnabledChange = (enabled: boolean) => { |
| 670 | + endFollowEnabledRef.current = enabled; |
| 671 | + setEndFollowEnabled(enabled); |
| 672 | + }; |
672 | 673 | const scheduleOverlayRepin = (delayMs: number) => { |
673 | 674 | if (overlayRepinTimerRef.current !== null) { |
674 | 675 | clearTimeout(overlayRepinTimerRef.current); |
@@ -836,6 +837,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai |
836 | 837 | setAnchorMessageId(null); |
837 | 838 | setSubmittedMessageId(null); |
838 | 839 | lastScrolledSubmittedMessageIdRef.current = null; |
| 840 | + endFollowEnabledRef.current = true; |
839 | 841 | setEndFollowEnabled(true); |
840 | 842 | freeze.set(false); |
841 | 843 | }, [freeze, selectedThreadKey]); |
@@ -925,34 +927,30 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai |
925 | 927 | }; |
926 | 928 |
|
927 | 929 | 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 | + } |
943 | 945 | }; |
944 | 946 |
|
945 | 947 | const collapseComposer = () => { |
946 | 948 | composerEditorRef.current?.blur(); |
947 | 949 | }; |
948 | 950 |
|
949 | 951 | 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); |
956 | 954 | requestAnimationFrame(() => { |
957 | 955 | composerEditorRef.current?.focus(); |
958 | 956 | composerEditorRef.current?.setSelection({ start: nextDraft.length, end: nextDraft.length }); |
@@ -1065,7 +1063,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai |
1065 | 1063 | layoutVariant={layoutVariant} |
1066 | 1064 | usesAutomaticContentInsets={props.usesAutomaticContentInsets} |
1067 | 1065 | onHeaderMaterialVisibilityChange={props.onHeaderMaterialVisibilityChange} |
1068 | | - onEndFollowEnabledChange={setEndFollowEnabled} |
| 1066 | + onEndFollowEnabledChange={handleEndFollowEnabledChange} |
1069 | 1067 | skills={selectedProviderSkills} |
1070 | 1068 | onUseArtifactTemplate={handleUseArtifactTemplate} |
1071 | 1069 | /> |
|
0 commit comments