1+ import { useAtomValue } from "@effect/atom-react" ;
12import type { EnvironmentId , MessageId , ThreadId } from "@t3tools/contracts" ;
23import { ArrowDownIcon , ArrowUpIcon , CheckIcon , PencilIcon , Trash2Icon , XIcon } from "lucide-react" ;
34import { useCallback , useEffect , useRef , useState } from "react" ;
@@ -12,9 +13,11 @@ import {
1213} from "~/outbox/threadOutbox" ;
1314import type { QueuedThreadMessage } from "~/outbox/threadOutbox.logic" ;
1415import { logOutboxMutationFailure } from "~/outbox/outboxDiagnostics" ;
16+ import { dispatchingQueuedMessageIdAtom } from "~/outbox/useThreadOutboxDrain" ;
1517import { cn } from "~/lib/utils" ;
1618import { Button } from "../ui/button" ;
1719import { Textarea } from "../ui/textarea" ;
20+ import { stackedThreadToast , toastManager } from "../ui/toast" ;
1821
1922/**
2023 * The visible pending-queue for the active thread. It renders *inside* the
@@ -101,11 +104,29 @@ function QueuedMessageRow({
101104 } ;
102105 } , [ ] ) ;
103106
107+ // The drain's editing hold is consulted only when it PICKS a head, so a hold
108+ // taken after `beginDispatchingQueuedMessage` does not stop the send that is
109+ // already under way (radroid/t3code#40 A5). Refusing to open the editor for
110+ // the row being dispatched closes that window from the other side; the
111+ // matching `false` return in `saveEditing` covers a dispatch that starts
112+ // while an editor is already open.
113+ const isDispatching = useAtomValue ( dispatchingQueuedMessageIdAtom ) === message . messageId ;
114+
104115 const beginEditing = useCallback ( ( ) => {
116+ if ( isDispatching ) {
117+ toastManager . add (
118+ stackedThreadToast ( {
119+ type : "info" ,
120+ title : "This message is being sent" ,
121+ description : "It left the queue before the editor opened, so it can no longer be edited." ,
122+ } ) ,
123+ ) ;
124+ return ;
125+ }
105126 holdEditingThreadOutboxMessage ( message . messageId ) ;
106127 setDraft ( message . text ) ;
107128 setIsEditing ( true ) ;
108- } , [ message . messageId , message . text ] ) ;
129+ } , [ isDispatching , message . messageId , message . text ] ) ;
109130
110131 const cancelEditing = useCallback ( ( ) => {
111132 setIsEditing ( false ) ;
@@ -121,7 +142,28 @@ function QueuedMessageRow({
121142 }
122143 setBusy ( true ) ;
123144 try {
124- await updateThreadOutboxMessage ( { ...message , text : nextText } ) ;
145+ // `update` is a deliberate no-op once the message has left the queue, and
146+ // it says so in its return value. Ignoring that closed the editor as if
147+ // the edit had saved, so a message that started dispatching mid-edit went
148+ // out with its ORIGINAL text and the user's revision vanished with no
149+ // trace at all (radroid/t3code#40 A5). The row unmounts either way — it is
150+ // rendered from the queue — so the news has to be carried out of here.
151+ const saved = await updateThreadOutboxMessage ( { ...message , text : nextText } ) ;
152+ if ( ! saved ) {
153+ logOutboxMutationFailure (
154+ "update" ,
155+ { messageId : message . messageId , threadId : message . threadId } ,
156+ new Error ( "queued message left the queue before the edit was saved" ) ,
157+ ) ;
158+ toastManager . add (
159+ stackedThreadToast ( {
160+ type : "warning" ,
161+ title : "Edit not applied" ,
162+ description :
163+ "The message was already sent or removed, so it went out with its original text. Send the revision as a new message." ,
164+ } ) ,
165+ ) ;
166+ }
125167 } finally {
126168 setBusy ( false ) ;
127169 setIsEditing ( false ) ;
0 commit comments