Skip to content

Commit 4e1f44d

Browse files
authored
Merge pull request #82 from radroid/t3x/outbox-edit-race
fix(web): an edit saved during dispatch no longer disappears (#40 A5)
2 parents e5a7147 + cff1185 commit 4e1f44d

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)