The best way to control Claude Code, Codex and more from any computer or phone
Clean up orphaned composer drafts and harden draft checks
- Remove stale `draftsByThreadId` entries when project-thread mappings are replaced or cleared - Preserve drafts when a thread is still mapped to another project - Parse persisted image `dataUrl` payloads more defensively and use `Object.hasOwn` for draft-thread existence checks
Julius Marminge committed
Mar 1, 2026 at 19:01 UTC
d9cce6402ae875b24d9ddbe73866eee94ec16ddb
3 files changed
+58
-4
apps/web/src/composerDraftStore.test.ts
+34
@@ -156,6 +156,7 @@ describe("composerDraftStore clearComposerContent", () => {
156
157
describe("composerDraftStore project draft thread mapping", () => {
158
const projectId = ProjectId.makeUnsafe("project-a");
159
+ const otherProjectId = ProjectId.makeUnsafe("project-b");
160
const threadId = ThreadId.makeUnsafe("thread-a");
161
const otherThreadId = ThreadId.makeUnsafe("thread-b");
162
@@ -195,6 +196,7 @@ describe("composerDraftStore project draft thread mapping", () => {
196
it("clears only matching project draft mapping entries", () => {
197
const store = useComposerDraftStore.getState();
198
store.setProjectDraftThreadId(projectId, threadId);
199
+ store.setPrompt(threadId, "hello");
200
201
store.clearProjectDraftThreadById(projectId, otherThreadId);
202
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
@@ -204,14 +206,46 @@ describe("composerDraftStore project draft thread mapping", () => {
206
store.clearProjectDraftThreadById(projectId, threadId);
207
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
208
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
209
+ expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
210
});
211
212
it("clears project draft mapping by project id", () => {
213
const store = useComposerDraftStore.getState();
214
store.setProjectDraftThreadId(projectId, threadId);
215
+ store.setPrompt(threadId, "hello");
216
store.clearProjectDraftThreadId(projectId);
217
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
218
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
219
+ expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
220
+ });
221
+
222
+ it("clears orphaned composer drafts when remapping a project to a new draft thread", () => {
223
+ const store = useComposerDraftStore.getState();
224
+ store.setProjectDraftThreadId(projectId, threadId);
225
+ store.setPrompt(threadId, "orphan me");
226
+
227
+ store.setProjectDraftThreadId(projectId, otherThreadId);
228
+
229
+ expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
230
+ otherThreadId,
231
+ );
232
+ expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
233
+ expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
234
+ });
235
+
236
+ it("keeps composer drafts when the thread is still mapped by another project", () => {
237
+ const store = useComposerDraftStore.getState();
238
+ store.setProjectDraftThreadId(projectId, threadId);
239
+ store.setProjectDraftThreadId(otherProjectId, threadId);
240
+ store.setPrompt(threadId, "keep me");
241
+
242
+ store.clearProjectDraftThreadId(projectId);
243
+
244
+ expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
245
+ expect(useComposerDraftStore.getState().getDraftThreadByProjectId(otherProjectId)?.threadId).toBe(
246
+ threadId,
247
+ );
248
+ expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.prompt).toBe("keep me");
249
});
250
251
it("clears draft registration independently", () => {
apps/web/src/composerDraftStore.ts
+23
-3
@@ -355,9 +355,11 @@ function readPersistedAttachmentIdsFromStorage(threadId: ThreadId): string[] {
355
function hydreatePersistedComposerImageAttachment(
356
attachment: PersistedComposerImageAttachment,
357
): File | null {
358
- const [rawHeader, payload] = attachment.dataUrl.split(",", 2);
359
- const header = rawHeader ?? "";
360
- if (!payload) {
358
+ const commaIndex = attachment.dataUrl.indexOf(",");
359
+ const header =
360
+ commaIndex === -1 ? attachment.dataUrl : attachment.dataUrl.slice(0, commaIndex);
361
+ const payload = commaIndex === -1 ? "" : attachment.dataUrl.slice(commaIndex + 1);
362
+ if (payload.length === 0) {
363
return null;
364
}
365
try {
@@ -484,14 +486,20 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
486
...state.draftThreadsByThreadId,
487
[threadId]: nextDraftThread,
488
};
489
+ let nextDraftsByThreadId = state.draftsByThreadId;
490
if (
491
previousThreadIdForProject &&
492
previousThreadIdForProject !== threadId &&
493
!Object.values(nextProjectDraftThreadIdByProjectId).includes(previousThreadIdForProject)
494
) {
495
delete nextDraftThreadsByThreadId[previousThreadIdForProject];
496
+ if (state.draftsByThreadId[previousThreadIdForProject] !== undefined) {
497
+ nextDraftsByThreadId = { ...state.draftsByThreadId };
498
+ delete nextDraftsByThreadId[previousThreadIdForProject];
499
+ }
500
}
501
return {
502
+ draftsByThreadId: nextDraftsByThreadId,
503
draftThreadsByThreadId: nextDraftThreadsByThreadId,
504
projectDraftThreadIdByProjectId: nextProjectDraftThreadIdByProjectId,
505
};
@@ -563,10 +571,16 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
571
const nextDraftThreadsByThreadId: Record<ThreadId, DraftThreadState> = {
572
...state.draftThreadsByThreadId,
573
};
574
+ let nextDraftsByThreadId = state.draftsByThreadId;
575
if (!Object.values(restProjectMappings).includes(threadId)) {
576
delete nextDraftThreadsByThreadId[threadId];
577
+ if (state.draftsByThreadId[threadId] !== undefined) {
578
+ nextDraftsByThreadId = { ...state.draftsByThreadId };
579
+ delete nextDraftsByThreadId[threadId];
580
+ }
581
}
582
return {
583
+ draftsByThreadId: nextDraftsByThreadId,
584
draftThreadsByThreadId: nextDraftThreadsByThreadId,
585
projectDraftThreadIdByProjectId: restProjectMappings,
586
};
@@ -586,10 +600,16 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
600
const nextDraftThreadsByThreadId: Record<ThreadId, DraftThreadState> = {
601
...state.draftThreadsByThreadId,
602
};
603
+ let nextDraftsByThreadId = state.draftsByThreadId;
604
if (!Object.values(restProjectMappings).includes(threadId)) {
605
delete nextDraftThreadsByThreadId[threadId];
606
+ if (state.draftsByThreadId[threadId] !== undefined) {
607
+ nextDraftsByThreadId = { ...state.draftsByThreadId };
608
+ delete nextDraftsByThreadId[threadId];
609
+ }
610
}
611
return {
612
+ draftsByThreadId: nextDraftsByThreadId,
613
draftThreadsByThreadId: nextDraftThreadsByThreadId,
614
projectDraftThreadIdByProjectId: restProjectMappings,
615
};
apps/web/src/routes/_chat.$threadId.tsx
+1
-1
@@ -141,7 +141,7 @@ function ChatThreadRouteView() {
141
const search = Route.useSearch();
142
const threadExists = state.threads.some((thread) => thread.id === threadId);
143
const draftThreadExists = useComposerDraftStore(
144
- (store) => store.draftThreadsByThreadId[threadId] !== undefined,
144
+ (store) => Object.hasOwn(store.draftThreadsByThreadId, threadId),
145
);
146
const routeThreadExists = threadExists || draftThreadExists;
147
const diffOpen = search.diff === "1";