Skip to content

Commit 0efab4c

Browse files
committed
fix(web): an edit saved during dispatch no longer disappears (#40 A5)
The drain consults the editing hold only when it PICKS a head (`useThreadOutboxDrain.ts` — the `editingQueuedMessageIds` check sits in the selection loop, above `beginDispatchingQueuedMessage`). A hold taken after that point does not stop the send already under way, and `threadOutboxManager.update` correctly returns `false` for a message that has left the queue — but `saveEditing` ignored the return and closed the editor as if it had saved. Net effect: the message went out with its ORIGINAL text, the user's revision was gone from the queue, the editor, and the composer, and nothing anywhere said so. Closed from both sides: * `beginEditing` refuses to open the editor for the row currently being dispatched. Selection and `beginDispatchingQueuedMessage` are synchronous within the same effect, so the atom covers the whole async send with no gap. * `saveEditing` honours the boolean. On `false` it logs through the module's own `[thread-outbox]` breadcrumb helper and raises a warning toast saying the message already went out with its original text. The row unmounts either way — it renders from the queue — so the news has to leave the component. `logOutboxMutationFailure` gains `"update"` beside `"reorder"`; a no-op update is exactly as invisible to the user as a rejected reorder, which is what that helper already exists for. Both changed files are fork-owned; the upstream-owned file count and every deletion count are unchanged. Verified: `vp test run apps/web/src/outbox` 41 passed; `vp lint` clean; `tsgo --noEmit -p apps/web` 0 errors. Not unit-tested at the component level — apps/web has 34 component tests and every one is `renderToStaticMarkup`, so there is no way to drive a click here without adding an interaction-test dependency. The contract this leans on (`update` resolving `false` once the message has left the queue) is already pinned by threadOutbox.logic.test.ts:442.
1 parent 70e7b05 commit 0efab4c

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎apps/web/src/components/chat/ThreadOutboxQueueList.tsx‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useAtomValue } from "@effect/atom-react";
12
import type { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
23
import { ArrowDownIcon, ArrowUpIcon, CheckIcon, PencilIcon, Trash2Icon, XIcon } from "lucide-react";
34
import { useCallback, useEffect, useRef, useState } from "react";
@@ -12,9 +13,11 @@ import {
1213
} from "~/outbox/threadOutbox";
1314
import type { QueuedThreadMessage } from "~/outbox/threadOutbox.logic";
1415
import { logOutboxMutationFailure } from "~/outbox/outboxDiagnostics";
16+
import { dispatchingQueuedMessageIdAtom } from "~/outbox/useThreadOutboxDrain";
1517
import { cn } from "~/lib/utils";
1618
import { Button } from "../ui/button";
1719
import { 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);

‎apps/web/src/outbox/outboxDiagnostics.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ export function logComposerDispatch(input: ComposerDispatchDiagnostics): void {
4747
});
4848
}
4949

50-
/** A queue mutation that failed after the user asked for it. */
50+
/**
51+
* A queue mutation that failed after the user asked for it.
52+
*
53+
* `update` covers the no-op case as well as a thrown one: the manager returns
54+
* `false` when the message has already left the queue, and that outcome is just
55+
* as invisible to the user as a rejection would be.
56+
*/
5157
export function logOutboxMutationFailure(
52-
operation: "reorder",
58+
operation: "reorder" | "update",
5359
fields: Readonly<Record<string, unknown>>,
5460
error: unknown,
5561
): void {

0 commit comments

Comments
 (0)