Skip to content

Commit b2a9ca4

Browse files
fix(ui): release attachments from cancelled queue edits
1 parent e68043c commit b2a9ca4

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎ui/src/pages/chat/queued-message-edit.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ describe("queued message edit round-trip", () => {
116116
unsubscribe();
117117
});
118118

119+
it("releases images added during an abandoned edit", () => {
120+
const original = stageQueuedImage("att-original");
121+
const added = stageQueuedImage("att-added");
122+
const { host, unsubscribe } = queueHost([{ attachments: [original] }]);
123+
beginQueuedMessageEdit(host as never, "queued-1");
124+
host.chatAttachments = [original, added];
125+
126+
expect(cancelQueuedMessageEdit(host as never)).toBe(true);
127+
128+
expect(storedOrder(host)).toEqual(["message 1"]);
129+
expect(getChatAttachmentDataUrl(original)).not.toBeNull();
130+
expect(getChatAttachmentDataUrl(added)).toBeNull();
131+
unsubscribe();
132+
});
133+
119134
it("replaces the row in the same slot when the edited message is sent", async () => {
120135
const { host, unsubscribe } = queueHost([{}, {}, {}]);
121136
beginQueuedMessageEdit(host as never, "queued-2");

‎ui/src/pages/chat/queued-message-edit.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ type QueuedMessageEditHost = ChatQueueScopedSessionHost & {
3131
chatQueuedEdit?: QueuedMessageEdit | null;
3232
};
3333

34+
function releaseUnretainedAttachments(
35+
owned: readonly ChatAttachment[],
36+
retained: readonly ChatAttachment[],
37+
): void {
38+
const retainedIds = new Set(retained.map((attachment) => attachment.id));
39+
releaseChatAttachmentPayloads(owned.filter((attachment) => !retainedIds.has(attachment.id)));
40+
}
41+
3442
/** Closed outcomes so the page owns the operator-visible wording. */
3543
type QueuedMessageEditResult = "started" | "unavailable" | "composer-busy";
3644

@@ -94,9 +102,13 @@ export function beginQueuedMessageEdit(
94102

95103
/** Cancel touches storage not at all: the row never left the queue. */
96104
export function cancelQueuedMessageEdit(host: QueuedMessageEditHost): boolean {
97-
if (!activeQueuedMessageEdit(host)) {
105+
const edit = activeQueuedMessageEdit(host);
106+
if (!edit) {
98107
return false;
99108
}
109+
// The durable row still owns its original payloads, but anything attached in
110+
// the composer during the edit has no owner after cancellation.
111+
releaseUnretainedAttachments(host.chatAttachments, edit.attachments);
100112
host.chatQueuedEdit = null;
101113
host.chatMessage = "";
102114
host.chatAttachments = [];
@@ -128,8 +140,5 @@ export function retireEditedQueuedMessageSource(
128140
// ones the replacement still carries must survive, so release only the rest.
129141
// The payloads come from the token: a successful write already retired the row
130142
// and told every pane, so re-reading it here would find nothing to release.
131-
const retained = new Set(nextAttachments.map((attachment) => attachment.id));
132-
releaseChatAttachmentPayloads(
133-
edit.attachments.filter((attachment) => !retained.has(attachment.id)),
134-
);
143+
releaseUnretainedAttachments(edit.attachments, nextAttachments);
135144
}

0 commit comments

Comments
 (0)