Skip to content

Commit f3c3782

Browse files
fix(mobile): rebuild dictated draft review on the current composer
1 parent 7392aa4 commit f3c3782

2 files changed

Lines changed: 59 additions & 7 deletions

File tree

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ export const COMPOSER_COLLAPSED_CHROME = 60;
128128

129129
export const COMPOSER_EXPANDED_CHROME = 156;
130130

131+
const EXPANDED_EDITOR_MIN_HEIGHT = 72;
132+
const EXPANDED_EDITOR_MAX_HEIGHT = 160;
133+
134+
export const COMPOSER_DICTATION_REVIEW_CHROME =
135+
COMPOSER_EXPANDED_CHROME + EXPANDED_EDITOR_MAX_HEIGHT - EXPANDED_EDITOR_MIN_HEIGHT;
136+
131137
export interface ThreadComposerProps {
132138
readonly canOperateThread: boolean;
133139
readonly placeholder: string;
@@ -156,6 +162,8 @@ export interface ThreadComposerProps {
156162
readonly canSteerActiveTurn: boolean;
157163
readonly editorRef?: RefObject<ComposerEditorHandle | null>;
158164
readonly onChangeDraftMessage: (value: string) => void;
165+
readonly isReviewingDictation: boolean;
166+
readonly onDictationComplete: () => void;
159167
readonly onPickDraftMedia: () => Promise<void>;
160168
readonly onPickDraftFiles: () => Promise<void>;
161169
readonly onNativePasteImages: (uris: ReadonlyArray<string>) => Promise<void>;
@@ -457,15 +465,19 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
457465
readDraftMessage: () => getComposerDraftSnapshot(composerDraftKey).text,
458466
subscribeToDraftChanges: (onChange) => appAtomRegistry.subscribe(composerDraftsAtom, onChange),
459467
selection: composerMenu.selection,
460-
onChangeDraftMessage: (text) => setComposerDraftText(composerDraftKey, text),
468+
onChangeDraftMessage: (text) => {
469+
setComposerDraftText(composerDraftKey, text);
470+
props.onDictationComplete();
471+
},
461472
onChangeSelection: composerMenu.onSelectionChange,
462473
});
463474
const voicePresentation = resolveVoiceComposerPresentation(
464475
voiceInput.state,
465476
voiceInput.elapsedSeconds,
466477
);
467478
const isVoiceInputPresented = voicePresentation.statusLabel !== null;
468-
const isExpanded = isFocused || settingsSheetPresentation.keepsComposerExpanded;
479+
const isExpanded =
480+
isFocused || props.isReviewingDictation || settingsSheetPresentation.keepsComposerExpanded;
469481
const showsCompactDictation = isVoiceInputPresented && !isExpanded;
470482
const isToolbarVisible = isExpanded || isVoiceInputPresented;
471483
const attachmentBlockReason = composerAttachmentUploadBlockReason({
@@ -489,7 +501,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
489501
sendBlockedReason === null &&
490502
!modelUnavailable;
491503

492-
useEffect(() => {
504+
useLayoutEffect(() => {
493505
onExpandedChange?.(isExpanded);
494506
}, [isExpanded, onExpandedChange]);
495507

@@ -902,8 +914,10 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
902914
style={
903915
isExpanded
904916
? {
905-
minHeight: 72,
906-
maxHeight: 160,
917+
minHeight: props.isReviewingDictation
918+
? EXPANDED_EDITOR_MAX_HEIGHT
919+
: EXPANDED_EDITOR_MIN_HEIGHT,
920+
maxHeight: EXPANDED_EDITOR_MAX_HEIGHT,
907921
paddingVertical: 4,
908922
}
909923
: {

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ import {
136136
} from "./pendingUserInputLayout";
137137
import {
138138
COMPOSER_COLLAPSED_CHROME,
139+
COMPOSER_DICTATION_REVIEW_CHROME,
139140
COMPOSER_EXPANDED_CHROME,
140141
COMPOSER_LAYOUT_TRANSITION,
141142
COMPOSER_TRANSITION_DURATION_MS,
@@ -411,7 +412,25 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
411412
const lastScrolledSubmittedMessageIdRef = useRef<MessageId | null>(null);
412413
const [composerExpanded, setComposerExpanded] = useState(false);
413414
const [composerFocused, setComposerFocused] = useState(false);
415+
const composerFocusedThreadKeyRef = useRef<string | null>(null);
416+
const [dictationReviewThreadKey, setDictationReviewThreadKey] = useState<string | null>(null);
417+
const isReviewingDictation = dictationReviewThreadKey === selectedThreadKey && !composerFocused;
418+
const dismissDictationReview = () =>
419+
setDictationReviewThreadKey((current) => (current === selectedThreadKey ? null : current));
420+
const handleDictationComplete = () => {
421+
// The voice session retains its startup callback. Focus must stay current and
422+
// belong to that thread, even when another composer is now on screen.
423+
if (composerFocusedThreadKeyRef.current !== selectedThreadKey) {
424+
setDictationReviewThreadKey(selectedThreadKey);
425+
}
426+
};
414427
const handleComposerFocusChange = (focused: boolean) => {
428+
if (focused) {
429+
composerFocusedThreadKeyRef.current = selectedThreadKey;
430+
dismissDictationReview();
431+
} else if (composerFocusedThreadKeyRef.current === selectedThreadKey) {
432+
composerFocusedThreadKeyRef.current = null;
433+
}
415434
setComposerFocused(focused);
416435
handleOwnedInputFocusChange(focused);
417436
};
@@ -424,7 +443,9 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
424443
// the hide animation, so the composer would ride down flush to the screen edge and then snap up
425444
// into the inset.
426445
const composerBottomInset = (
427-
Platform.OS === "android" ? isKeyboardVisible : composerExpanded || composerFocused
446+
Platform.OS === "android"
447+
? isKeyboardVisible
448+
: !isReviewingDictation && (composerExpanded || composerFocused)
428449
)
429450
? 0
430451
: Math.max(insets.bottom, 12);
@@ -536,7 +557,11 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
536557
) ||
537558
(props.historyControls?.hasMoreHistory === true &&
538559
props.selectedThread.latestUserMessageAt !== null);
539-
const composerChrome = composerExpanded ? COMPOSER_EXPANDED_CHROME : COMPOSER_COLLAPSED_CHROME;
560+
const composerChrome = isReviewingDictation
561+
? COMPOSER_DICTATION_REVIEW_CHROME
562+
: composerExpanded
563+
? COMPOSER_EXPANDED_CHROME
564+
: COMPOSER_COLLAPSED_CHROME;
540565
const composerOverlapHeight = composerChrome + composerBottomInset;
541566
const [collapsedUserInputRequestId, setCollapsedUserInputRequestId] =
542567
useState<RuntimeRequestId | null>(null);
@@ -779,7 +804,9 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
779804

780805
useLayoutEffect(() => {
781806
selectedThreadKeyRef.current = selectedThreadKey;
807+
composerEditorRef.current?.blur();
782808
// A replaced or unmounted native editor may not emit a blur event.
809+
composerFocusedThreadKeyRef.current = null;
783810
setComposerFocused(false);
784811
}, [selectedThreadKey, showContent]);
785812

@@ -900,10 +927,18 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
900927

901928
const handleSendMessage = async (followUp?: ActiveTurnComposerAction) => {
902929
const targetThreadKey = selectedThreadKey;
930+
const targetDraftKey = props.composerDraftKey ?? targetThreadKey;
903931
const hasUserMessage = selectedThreadFeed.some(
904932
(entry) => entry.type === "message" && entry.message.role === "user",
905933
);
906934
const messageId = await props.onSendMessage(followUp);
935+
const remainingDraft = getComposerDraftSnapshot(targetDraftKey);
936+
if (
937+
messageId !== null ||
938+
(remainingDraft.text.length === 0 && remainingDraft.attachments.length === 0)
939+
) {
940+
dismissDictationReview();
941+
}
907942
if (messageId === null || selectedThreadKeyRef.current !== targetThreadKey) {
908943
return messageId;
909944
}
@@ -943,6 +978,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
943978
};
944979

945980
const collapseComposer = () => {
981+
dismissDictationReview();
946982
composerEditorRef.current?.blur();
947983
};
948984

@@ -1337,6 +1373,8 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
13371373
}
13381374
bottomInset={composerBottomInset}
13391375
onChangeDraftMessage={props.onChangeDraftMessage}
1376+
isReviewingDictation={isReviewingDictation}
1377+
onDictationComplete={handleDictationComplete}
13401378
onPickDraftMedia={props.onPickDraftMedia}
13411379
onPickDraftFiles={props.onPickDraftFiles}
13421380
onNativePasteImages={props.onNativePasteImages}

0 commit comments

Comments
 (0)