Skip to content

Commit dab2d34

Browse files
fix(web): composer context undo keeps its release-on-switch cleanup
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5595cf6 commit dab2d34

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

‎apps/web/src/components/chat/composerContextUndo.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { PreviewAnnotationPayload, ThreadContextRecord } from "@t3tools/contracts";
2-
import { useState } from "react";
2+
import { useEffect, useState } from "react";
33

44
import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
55
import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords";
@@ -32,8 +32,7 @@ function createRetainedContextPayloads(): RetainedContextPayloads {
3232
};
3333
}
3434

35-
// Deleted chips keep their payloads until their draft's undo history ends. Switching drafts
36-
// swaps in fresh maps; the dropped ones are unreachable and collected with that history.
35+
// Deleted chips keep their payloads until their draft's undo history ends.
3736
export function useRetainedContextPayloads(draftTargetKey: string): RetainedContextPayloads {
3837
const [draft, setDraft] = useState(() => ({
3938
targetKey: draftTargetKey,
@@ -42,7 +41,14 @@ export function useRetainedContextPayloads(draftTargetKey: string): RetainedCont
4241
if (draft.targetKey !== draftTargetKey) {
4342
setDraft({ targetKey: draftTargetKey, payloads: createRetainedContextPayloads() });
4443
}
45-
return draft.payloads;
44+
const retained = draft.payloads;
45+
useEffect(
46+
() => () => {
47+
for (const payloads of Object.values(retained)) payloads.clear();
48+
},
49+
[retained],
50+
);
51+
return retained;
4652
}
4753

4854
export function reconcileAttachmentContextReferences(input: {

0 commit comments

Comments
 (0)