Skip to content

Commit 4d1c135

Browse files
committed
fix(web): deleted composer chips release their payloads on thread switch
1 parent 397a461 commit 4d1c135

3 files changed

Lines changed: 108 additions & 24 deletions

File tree

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ import {
144144
import { ComposerActivityRow } from "./ComposerActivityStatus";
145145
import {
146146
reconcileAttachmentContextReferences,
147-
type RetainedAttachmentContextPayloads,
147+
useRetainedContextPayloads,
148148
} from "./composerContextUndo";
149149
import type { ThreadSyncPhase } from "../../threadSync";
150150
import { ComposerBanner } from "./ComposerBanner";
@@ -3709,20 +3709,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
37093709
setIsComposerScrollCollapsed(false);
37103710
}, [setIsComposerScrollCollapsed]);
37113711

3712-
/**
3713-
* Payloads for chips the prompt no longer references. History undo restores the
3714-
* reference text but knows nothing about the draft records behind it, so a delete keeps its
3715-
* payload here and an undo puts it back rather than leaving a dangling chip.
3716-
*/
3717-
const removedContextPayloadsRef = useRef<{
3718-
terminals: Map<string, TerminalContextDraft>;
3719-
reviewComments: Map<string, ReviewCommentContext>;
3720-
threads: Map<string, ThreadContextRecord>;
3721-
}>({ terminals: new Map(), reviewComments: new Map(), threads: new Map() });
3722-
const removedAttachmentContextPayloadsRef = useRef<RetainedAttachmentContextPayloads>({
3723-
files: new Map(),
3724-
previewAnnotations: new Map(),
3725-
});
3712+
const retainedContextPayloads = useRetainedContextPayloads(composerDraftTargetKey);
37263713

37273714
const onPromptChange = useCallback(
37283715
(
@@ -3758,15 +3745,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
37583745
promptHistoryPositionRef.current = null;
37593746
}
37603747
const referenced = new Set(contextIds);
3761-
const retained = removedContextPayloadsRef.current;
37623748

37633749
// An undone delete brings the reference back; restore the payload it points at.
37643750
const liveTerminalIds = new Set<string>(
37653751
composerTerminalContexts.map((context) => terminalContextReference(context).contextId),
37663752
);
37673753
const restoredTerminals = [...referenced].flatMap((contextId) => {
37683754
if (liveTerminalIds.has(contextId)) return [];
3769-
const context = retained.terminals.get(contextId);
3755+
const context = retainedContextPayloads.terminals.get(contextId);
37703756
return context ? [context] : [];
37713757
});
37723758
const nextTerminals = [
@@ -3777,7 +3763,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
37773763
];
37783764
for (const context of composerTerminalContexts) {
37793765
const contextId = terminalContextReference(context).contextId;
3780-
if (!referenced.has(contextId)) retained.terminals.set(contextId, context);
3766+
if (!referenced.has(contextId)) retainedContextPayloads.terminals.set(contextId, context);
37813767
}
37823768
if (
37833769
nextTerminals.length !== composerTerminalContexts.length ||
@@ -3791,15 +3777,16 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
37913777
);
37923778
const restoredThreads = [...referenced].flatMap((contextId) => {
37933779
if (liveThreadIds.has(contextId)) return [];
3794-
const record = retained.threads.get(contextId);
3780+
const record = retainedContextPayloads.threads.get(contextId);
37953781
return record ? [record] : [];
37963782
});
37973783
const nextThreads = [
37983784
...composerThreadContexts.filter((record) => referenced.has(record.contextId)),
37993785
...restoredThreads,
38003786
];
38013787
for (const record of composerThreadContexts) {
3802-
if (!referenced.has(record.contextId)) retained.threads.set(record.contextId, record);
3788+
if (!referenced.has(record.contextId))
3789+
retainedContextPayloads.threads.set(record.contextId, record);
38033790
}
38043791
if (nextThreads.length !== composerThreadContexts.length || restoredThreads.length > 0) {
38053792
setComposerDraftThreadContexts(composerDraftTarget, nextThreads);
@@ -3808,7 +3795,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38083795
for (const comment of composerReviewComments) {
38093796
const contextId = reviewCommentContextId(comment.id);
38103797
if (!referenced.has(contextId)) {
3811-
retained.reviewComments.set(contextId, comment);
3798+
retainedContextPayloads.reviewComments.set(contextId, comment);
38123799
removeComposerDraftReviewComment(composerDraftTarget, comment.id);
38133800
}
38143801
}
@@ -3817,7 +3804,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38173804
);
38183805
for (const contextId of referenced) {
38193806
if (liveReviewIds.has(contextId)) continue;
3820-
const comment = retained.reviewComments.get(contextId);
3807+
const comment = retainedContextPayloads.reviewComments.get(contextId);
38213808
if (comment) {
38223809
addComposerDraftReviewComment(composerDraftTarget, comment, { appendReference: false });
38233810
}
@@ -3828,7 +3815,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38283815
files: composerFiles,
38293816
images: composerImages,
38303817
previewAnnotations: composerPreviewAnnotations,
3831-
retained: removedAttachmentContextPayloadsRef.current,
3818+
retained: retainedContextPayloads,
38323819
});
38333820
for (const annotationId of attachmentChanges.annotationIdsToRemove) {
38343821
// Keep the upload queue entry alive: undo restores the image that owns it.
@@ -3878,6 +3865,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38783865
addComposerDraftImages,
38793866
addComposerDraftFiles,
38803867
attachmentDraftTarget,
3868+
retainedContextPayloads,
38813869
],
38823870
);
38833871

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// @vitest-environment jsdom
2+
import { act, useEffect } from "react";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, beforeEach, expect, it } from "vite-plus/test";
5+
6+
import type { ComposerFileAttachment } from "../../composerDraftStore";
7+
import { type RetainedContextPayloads, useRetainedContextPayloads } from "./composerContextUndo";
8+
9+
const file = {
10+
type: "file",
11+
id: "file-1",
12+
name: "notes.txt",
13+
mimeType: "text/plain",
14+
sizeBytes: 5,
15+
file: new File(["notes"], "notes.txt", { type: "text/plain" }),
16+
} satisfies ComposerFileAttachment;
17+
18+
let container: HTMLDivElement;
19+
let root: Root;
20+
let current: RetainedContextPayloads;
21+
22+
function Probe({ targetKey }: { targetKey: string }) {
23+
const retained = useRetainedContextPayloads(targetKey);
24+
useEffect(() => {
25+
current = retained;
26+
});
27+
return null;
28+
}
29+
30+
beforeEach(() => {
31+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
32+
container = document.createElement("div");
33+
root = createRoot(container);
34+
});
35+
36+
afterEach(() => {
37+
act(() => root.unmount());
38+
});
39+
40+
it("keeps deleted payloads for the open draft and releases them when the draft changes", () => {
41+
act(() => root.render(<Probe targetKey="thread-a" />));
42+
const retained = current;
43+
retained.files.set("file:file-1", file);
44+
45+
act(() => root.render(<Probe targetKey="thread-a" />));
46+
expect(current.files.get("file:file-1")).toBe(file);
47+
48+
act(() => root.render(<Probe targetKey="thread-b" />));
49+
expect(retained.files.size).toBe(0);
50+
expect(current.files.size).toBe(0);
51+
});
52+
53+
it("releases deleted payloads when the composer unmounts", () => {
54+
act(() => root.render(<Probe targetKey="thread-a" />));
55+
const retained = current;
56+
retained.files.set("file:file-1", file);
57+
58+
act(() => root.unmount());
59+
expect(retained.files.size).toBe(0);
60+
root = createRoot(container);
61+
});

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import type { PreviewAnnotationPayload } from "@t3tools/contracts";
1+
import type { PreviewAnnotationPayload, ThreadContextRecord } from "@t3tools/contracts";
2+
import { useEffect, useState } from "react";
23

34
import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
45
import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords";
6+
import type { TerminalContextDraft } from "../../lib/terminalContext";
7+
import type { ReviewCommentContext } from "../../reviewCommentContext";
58

69
interface RetainedPreviewAnnotation {
710
annotation: PreviewAnnotationPayload;
@@ -13,6 +16,38 @@ export interface RetainedAttachmentContextPayloads {
1316
previewAnnotations: Map<string, RetainedPreviewAnnotation>;
1417
}
1518

19+
export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads {
20+
terminals: Map<string, TerminalContextDraft>;
21+
reviewComments: Map<string, ReviewCommentContext>;
22+
threads: Map<string, ThreadContextRecord>;
23+
}
24+
25+
/**
26+
* Payloads for chips the prompt no longer references. History undo restores the
27+
* reference text but knows nothing about the draft records behind it, so a delete keeps its
28+
* payload here and an undo puts it back rather than leaving a dangling chip.
29+
*
30+
* Scoped to one draft: switching drafts or unmounting releases them, so deleted files do not
31+
* stay reachable for the composer's whole lifetime.
32+
*/
33+
export function useRetainedContextPayloads(draftTargetKey: string): RetainedContextPayloads {
34+
const [retained] = useState<RetainedContextPayloads>(() => ({
35+
terminals: new Map(),
36+
reviewComments: new Map(),
37+
threads: new Map(),
38+
files: new Map(),
39+
previewAnnotations: new Map(),
40+
}));
41+
useEffect(
42+
() => () => {
43+
for (const payloads of Object.values(retained)) payloads.clear();
44+
},
45+
// oxlint-disable-next-line react/exhaustive-effect-dependencies -- Switching drafts releases the previous draft's payloads.
46+
[draftTargetKey, retained],
47+
);
48+
return retained;
49+
}
50+
1651
export function reconcileAttachmentContextReferences(input: {
1752
referencedContextIds: ReadonlySet<string>;
1853
files: ReadonlyArray<ComposerFileAttachment>;

0 commit comments

Comments
 (0)