Skip to content

Commit 099303b

Browse files
fix(web): dictation finishing after a composer remount still lands and blocks Send
A recording detached on an owner change or unmount lived only in the old hook instance, so a remounted composer for the same draft did not see it: Send went out without the transcript, which the stale check then dropped, and the mic showed "Another voice recording is already active". The detached recording now lives in one module-level slot that every hook instance reads through useSyncExternalStore, replacing the per-instance ref. The editor showing the recording's own draft shows it as finishing and queues Send until its transcript commits; others get the "Finishing an earlier dictation" notice. A detached commit now needs only audio consent, like the attached path, and a destination change that drops a detached recording says so. Toast copy no longer assumes a thread switch. AI-assisted; verified with the tests and tools listed above.
1 parent 8dc0414 commit 099303b

2 files changed

Lines changed: 109 additions & 60 deletions

File tree

‎apps/web/src/speech/useEnvironmentSpeechInput.browser.test.tsx‎

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -484,21 +484,35 @@ it("does not start or cancel from another thread while the previous one finishes
484484
expect(drafts["thread-b"]).toBe("");
485485
});
486486

487-
it("waits for the transcript when Send is pressed after returning to its thread", async () => {
488-
setup({ destination: "env-a" });
489-
const resolveTranscript = pendingTranscript();
490-
const drafts: Record<string, string> = { "thread-a": "Fix", "thread-b": "" };
491-
await using screen = await render(<ThreadProbe owner="thread-a" drafts={drafts} />);
492-
await act(() => mocks.voice.start());
493-
await screen.rerender(<ThreadProbe owner="thread-b" drafts={drafts} />);
494-
await screen.rerender(<ThreadProbe owner="thread-a" drafts={drafts} />);
495-
expect(mocks.voice.freezesEditor).toBe(true);
496-
const submit = vi.fn();
497-
await act(() => mocks.voice.submitAfterDictation(submit));
498-
expect(submit).not.toHaveBeenCalled();
499-
await act(async () => resolveTranscript("the login bug"));
500-
await vi.waitFor(() => expect(submit).toHaveBeenCalledExactlyOnceWith("Fix the login bug"));
501-
});
487+
it.each([
488+
["the same editor", false],
489+
["a remounted editor", true],
490+
])(
491+
"waits for the transcript when Send is pressed after returning to its thread in %s",
492+
async (_, remount) => {
493+
setup({ destination: "env-a" });
494+
const resolveTranscript = pendingTranscript();
495+
const drafts: Record<string, string> = { "thread-a": "Fix", "thread-b": "" };
496+
await using screen = await render(<ThreadProbe key="a" owner="thread-a" drafts={drafts} />);
497+
await act(() => mocks.voice.start());
498+
await screen.rerender(
499+
<ThreadProbe key={remount ? "b" : "a"} owner="thread-b" drafts={drafts} />,
500+
);
501+
vi.mocked(toastManager.add).mockClear();
502+
await act(() => mocks.voice.start());
503+
expect(toastManager.add).toHaveBeenCalledWith(expect.objectContaining({ type: "info" }));
504+
expect(mocks.voice.state.phase).toBe("idle");
505+
await screen.rerender(
506+
<ThreadProbe key={remount ? "a-again" : "a"} owner="thread-a" drafts={drafts} />,
507+
);
508+
expect(mocks.voice.freezesEditor).toBe(true);
509+
const submit = vi.fn();
510+
await act(() => mocks.voice.submitAfterDictation(submit));
511+
expect(submit).not.toHaveBeenCalled();
512+
await act(async () => resolveTranscript("the login bug"));
513+
await vi.waitFor(() => expect(submit).toHaveBeenCalledExactlyOnceWith("Fix the login bug"));
514+
},
515+
);
502516

503517
it("discards a detached transcript when voice input is turned off after the editor unmounts", async () => {
504518
setup({ destination: "env-a" });

‎apps/web/src/speech/useEnvironmentSpeechInput.ts‎

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
2626
import { chooseSetupSpeechModel, resolveSpeechVocabulary } from "@t3tools/shared/speech";
2727
import { useNavigate } from "@tanstack/react-router";
2828
import * as Option from "effect/Option";
29-
import { useEffect, useRef, useState } from "react";
29+
import { useEffect, useRef, useState, useSyncExternalStore } from "react";
3030

3131
import { useStableCallback } from "~/hooks/useStableCallback";
3232
import { toastManager } from "../components/ui/toast";
@@ -80,29 +80,59 @@ type DraftAccess = {
8080
) => void;
8181
};
8282

83-
type DetachedDraft = { readonly snapshot: VoiceDraftSnapshot; readonly access: DraftAccess };
83+
type DetachedRecording = {
84+
readonly controller: VoiceInputController;
85+
readonly snapshot: VoiceDraftSnapshot;
86+
readonly access: DraftAccess;
87+
state: VoiceInputState;
88+
committedText?: string;
89+
};
90+
91+
// A recording finishing into its owner's draft after its editor moved to another owner or
92+
// unmounted. Every editor reads it: the owner's shows it, the rest wait for it. The global voice
93+
// session allows one recording at a time, so one slot is enough.
94+
let detachedRecording: DetachedRecording | null = null;
95+
const detachedListeners = new Set<() => void>();
96+
function publishDetached(next: DetachedRecording | null) {
97+
detachedRecording = next;
98+
for (const listener of detachedListeners) listener();
99+
}
100+
function subscribeDetached(listener: () => void) {
101+
detachedListeners.add(listener);
102+
return () => {
103+
detachedListeners.delete(listener);
104+
};
105+
}
84106

85107
/** Finishes an active recording into its owner's draft while the editor shows another owner, or is gone. */
86108
function detachRecording(
87109
controller: VoiceInputController,
88-
detachedRef: { current: DetachedDraft | null },
89110
snapshot: VoiceDraftSnapshot | null,
90111
{ ownerKey, detachDraft }: HookInput,
91112
): boolean {
92-
const phase = controller.currentState.phase;
113+
const state = controller.currentState;
93114
if (
94115
!detachDraft ||
95116
snapshot?.ownerKey !== ownerKey ||
96-
(phase !== "recording" && phase !== "transcribing" && phase !== "post-processing")
117+
(state.phase !== "recording" &&
118+
state.phase !== "transcribing" &&
119+
state.phase !== "post-processing")
97120
)
98121
return false;
99-
detachedRef.current = { snapshot, access: detachDraft() };
122+
publishDetached({ controller, snapshot, access: detachDraft(), state });
100123
void controller.stop();
101124
return true;
102125
}
103126

127+
const DETACHED_CONSENT_LOST =
128+
"Voice input was turned off or changed before the transcript was ready.";
129+
104130
function failDetached(description: string | null) {
105-
toastManager.add({ type: "error", title: "Dictation for the last thread failed", description });
131+
toastManager.add({
132+
type: "error",
133+
title: "Dictation couldn't be added to its draft",
134+
description,
135+
});
106136
}
107137

108138
function cleanupContext({ text, selection }: DraftInput): DraftInput {
@@ -230,19 +260,24 @@ export function useEnvironmentSpeechInput(input: HookInput) {
230260
setQueuedStart(null);
231261
setSetup(INITIAL_SETUP);
232262
}
263+
const detachedState = useSyncExternalStore(subscribeDetached, () =>
264+
detachedRecording?.snapshot.ownerKey === input.ownerKey ? detachedRecording.state : null,
265+
);
233266
const state: VoiceInputState =
234-
queuedStart === prepared && prepared
267+
detachedState ??
268+
(queuedStart === prepared && prepared
235269
? WAITING_STATE
236270
: controllerState.prepared === prepared
237271
? controllerState.value
238-
: INITIAL_STATE;
272+
: INITIAL_STATE);
239273

240274
const controllerRef = useRef<VoiceInputController | null>(null);
241275
const startRequestRef = useRef(0);
242276
const pendingSubmissionRef = useRef<{
243277
ownerKey: string;
244278
submit: (text: string) => void;
245279
text?: string;
280+
recording?: DetachedRecording;
246281
} | null>(null);
247282
const latestRef = useRef({ input, clientSettings, cleanupPrepared, projectWords });
248283
const previousInputRef = useRef(input);
@@ -252,7 +287,6 @@ export function useEnvironmentSpeechInput(input: HookInput) {
252287
latestRef.current = { input, clientSettings, cleanupPrepared, projectWords };
253288
});
254289
const lastSnapshotRef = useRef<VoiceDraftSnapshot | null>(null);
255-
const detachedRef = useRef<DetachedDraft | null>(null);
256290
const unmountingRef = useRef(false);
257291
const draftRevisionRef = useRef({ ownerKey: input.ownerKey, text: input.draftText, revision: 0 });
258292
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- tracks committed draft revisions so a late transcript cannot overwrite an edited draft
@@ -291,9 +325,11 @@ export function useEnvironmentSpeechInput(input: HookInput) {
291325
readonly target: PreparedConnection | null;
292326
}
293327
| undefined;
328+
const detachedSelf = () =>
329+
detachedRecording?.controller === controller ? detachedRecording : null;
294330

295331
const readDraft = (): VoiceDraftSnapshot | null => {
296-
const detached = detachedRef.current;
332+
const detached = detachedSelf();
297333
if (detached) {
298334
const draft = detached.access.readDraft();
299335
const { ownerKey, revision } = detached.snapshot;
@@ -373,7 +409,7 @@ export function useEnvironmentSpeechInput(input: HookInput) {
373409
options,
374410
) => {
375411
const preferences = recordingPreferences;
376-
const detached = detachedRef.current;
412+
const detached = detachedSelf();
377413
const { input: current, cleanupPrepared } = latestRef.current;
378414
if (
379415
!preferences?.cleanup ||
@@ -420,24 +456,23 @@ export function useEnvironmentSpeechInput(input: HookInput) {
420456
}),
421457
readDraft,
422458
commitDraft: (text, selection) => {
423-
if (detachedRef.current) {
459+
const detached = detachedSelf();
460+
if (detached) {
424461
// Unmounted, consent changes no longer reach this controller through its effect.
425-
const cleanupGrant = recordingPreferences?.cleanupGrant;
426-
if (hasAudioConsent() && (!cleanupGrant || cleanupGrant === readCleanupGrant()))
427-
detachedRef.current.access.commitDraft(text, selection);
428-
else
429-
failDetached("Voice input was turned off or changed before the transcript was ready.");
462+
if (!hasAudioConsent()) return failDetached(DETACHED_CONSENT_LOST);
463+
detached.access.commitDraft(text, selection);
464+
detached.committedText = text;
430465
return;
431466
}
432467
if (latestRef.current.input.disabled) return;
433468
latestRef.current.input.commitDraft(text, selection);
434469
if (pendingSubmissionRef.current) pendingSubmissionRef.current.text = text;
435470
},
436471
onStateChange: (value) => {
437-
const detached = detachedRef.current;
472+
const detached = detachedSelf();
438473
if (detached) {
439-
if (value.phase !== "idle" && value.phase !== "error") return;
440-
detachedRef.current = null;
474+
detached.state = value;
475+
publishDetached(value.phase === "idle" || value.phase === "error" ? null : detached);
441476
if (value.phase === "error") {
442477
failDetached(value.error);
443478
// Drops the audio kept for Retry; no editor shows this recording to offer it.
@@ -458,14 +493,15 @@ export function useEnvironmentSpeechInput(input: HookInput) {
458493
startRequestRef.current += 1;
459494
setQueuedStart(null);
460495
if (controllerRef.current === controller) controllerRef.current = null;
461-
const last = latestRef.current.input;
462-
if (
496+
if (detachedSelf()) {
497+
if (unmountingRef.current) return;
498+
failDetached(DETACHED_CONSENT_LOST);
499+
publishDetached(null);
500+
} else if (
463501
unmountingRef.current &&
464-
(detachedRef.current ||
465-
detachRecording(controller, detachedRef, lastSnapshotRef.current, last))
502+
detachRecording(controller, lastSnapshotRef.current, latestRef.current.input)
466503
)
467504
return;
468-
detachedRef.current = null;
469505
controller.dispose();
470506
};
471507
}, [prepared, grantId, cloudProvider, cloudModel]);
@@ -522,19 +558,10 @@ export function useEnvironmentSpeechInput(input: HookInput) {
522558
pendingSubmissionRef.current = null;
523559
startRequestRef.current += 1;
524560
const controller = controllerRef.current;
525-
const detached = detachedRef.current;
526-
if (!controller) return;
527-
if (detached) {
528-
if (detached.snapshot.ownerKey !== input.ownerKey || input.disabled) return;
529-
// Back on the recording's own draft: the editor shows it again and Send waits for it.
530-
detachedRef.current = null;
531-
draftRevisionRef.current = detached.snapshot;
532-
setControllerState((current) => ({ ...current, value: controller.currentState }));
533-
return;
534-
}
561+
if (!controller || detachedRecording?.controller === controller) return;
535562
if (
536563
previousOwner === input.ownerKey ||
537-
!detachRecording(controller, detachedRef, lastSnapshotRef.current, previousInputRef.current)
564+
!detachRecording(controller, lastSnapshotRef.current, previousInputRef.current)
538565
)
539566
controller.ownerChanged();
540567
else setControllerState((current) => ({ ...current, value: INITIAL_STATE }));
@@ -545,19 +572,22 @@ export function useEnvironmentSpeechInput(input: HookInput) {
545572
const pending = pendingSubmissionRef.current;
546573
if (state.phase !== "idle" || !pending) return;
547574
pendingSubmissionRef.current = null;
548-
if (!input.disabled && pending.ownerKey === input.ownerKey && pending.text !== undefined)
549-
pending.submit(pending.text);
575+
const text = pending.text ?? pending.recording?.committedText;
576+
if (!input.disabled && pending.ownerKey === input.ownerKey && text !== undefined)
577+
pending.submit(text);
550578
}, [state, input.ownerKey, input.disabled]);
551579

552580
const submitAfterDictation = useStableCallback((submit: (text: string) => void) => {
553581
if (latestRef.current.input.disabled || state.phase === "preparing") return;
554-
const controller = controllerRef.current;
582+
const ownerKey = latestRef.current.input.ownerKey;
583+
const recording = detachedRecording?.snapshot.ownerKey === ownerKey ? detachedRecording : null;
584+
const controller = recording?.controller ?? controllerRef.current;
555585
if (!controller || !voiceInputBlocksSubmission(state)) {
556586
submit(latestRef.current.input.readDraft().text);
557587
return;
558588
}
559589
if (pendingSubmissionRef.current) return;
560-
pendingSubmissionRef.current = { ownerKey: latestRef.current.input.ownerKey, submit };
590+
pendingSubmissionRef.current = { ownerKey, submit, ...(recording ? { recording } : {}) };
561591
if (controller.currentState.phase === "recording") void controller.stop();
562592
});
563593

@@ -589,10 +619,10 @@ export function useEnvironmentSpeechInput(input: HookInput) {
589619

590620
const start = useStableCallback(async () => {
591621
if (latestRef.current.input.disabled || (setup.open && setup.downloading)) return;
592-
if (detachedRef.current) {
622+
if (detachedRecording) {
593623
toastManager.add({
594624
type: "info",
595-
title: "Finishing dictation for the previous thread",
625+
title: "Finishing an earlier dictation",
596626
description: "Try again in a moment.",
597627
});
598628
return;
@@ -710,8 +740,13 @@ export function useEnvironmentSpeechInput(input: HookInput) {
710740
}
711741
});
712742

713-
// A detached recording belongs to another draft; this editor's controls must not reach it.
714-
const attachedController = () => (detachedRef.current ? null : controllerRef.current);
743+
// A detached recording answers only to the editor showing its own draft.
744+
const attachedController = () =>
745+
detachedRecording
746+
? detachedRecording.snapshot.ownerKey === input.ownerKey
747+
? detachedRecording.controller
748+
: null
749+
: controllerRef.current;
715750

716751
const destinationReady =
717752
grantId === null

0 commit comments

Comments
 (0)