Skip to content

Commit 9d2a71d

Browse files
committed
fix(web): clean up background drafts after navigation
1 parent 7e08e54 commit 9d2a71d

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8015,8 +8015,12 @@ export default function ChatView(props: ChatViewProps) {
80158015
}
80168016
acknowledgeActiveThreadWoke();
80178017
if (backgroundThreadRef) {
8018-
if (backgroundDraftOpened) {
8018+
if (backgroundDraftOpened || currentRouteThreadKeyRef.current !== routeThreadKey) {
80198019
finalizePromotedDraftThreadByRef(backgroundThreadRef);
8020+
} else {
8021+
clearBackgroundDraftSubmissionByRef(backgroundThreadRef);
8022+
}
8023+
if (backgroundDraftOpened) {
80208024
toastManager.add(
80218025
stackedThreadToast({
80228026
type: "success",
@@ -8033,8 +8037,6 @@ export default function ChatView(props: ChatViewProps) {
80338037
},
80348038
}),
80358039
);
8036-
} else {
8037-
clearBackgroundDraftSubmissionByRef(backgroundThreadRef);
80388040
}
80398041
}
80408042
}

‎apps/web/src/composerDraftStore.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
6666

6767
import {
6868
COMPOSER_DRAFT_STORAGE_KEY,
69+
beginBackgroundDraftSubmissionByRef,
6970
clearComposerDraftsEnvironment,
7071
composerDraftHasUserContent,
7172
finalizePromotedDraftThreadByRef,
@@ -1628,6 +1629,28 @@ describe("composerDraftStore project draft thread mapping", () => {
16281629
expect(draftFor(threadId, TEST_ENVIRONMENT_ID)?.prompt).toBe("typed during setup");
16291630
});
16301631

1632+
it("cleans up a completed background draft without replacing the active draft", () => {
1633+
const store = useComposerDraftStore.getState();
1634+
const threadRef = scopeThreadRef(TEST_ENVIRONMENT_ID, threadId);
1635+
const nextDraftId = DraftId.make("next-draft");
1636+
store.setProjectDraftThreadId(projectRef, draftId, { threadId });
1637+
beginBackgroundDraftSubmissionByRef(threadRef);
1638+
markPromotedDraftThreadByRef(threadRef);
1639+
store.setProjectDraftThreadId(projectRef, nextDraftId, {
1640+
threadId: ThreadId.make("next-thread"),
1641+
});
1642+
store.setPrompt(nextDraftId, "Keep my next task");
1643+
1644+
finalizePromotedDraftThreadByRef(threadRef);
1645+
1646+
expect(store.getDraftSession(draftId)).toBeNull();
1647+
expect(store.getDraftThreadByProjectRef(projectRef)?.draftId).toBe(nextDraftId);
1648+
expect(store.getComposerDraft(nextDraftId)?.prompt).toBe("Keep my next task");
1649+
expect(
1650+
useComposerDraftStore.getState().backgroundSubmissionThreadKeys[scopedThreadKey(threadRef)],
1651+
).toBeUndefined();
1652+
});
1653+
16311654
it("finalizes a matching materialized draft even when promotion was not pre-marked", () => {
16321655
const store = useComposerDraftStore.getState();
16331656
store.setProjectDraftThreadId(projectRef, draftId, { threadId });

0 commit comments

Comments
 (0)