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
This commit is contained in:
1 parent
508872e04b
commit
d9cce6402a
3 files changed
+58
-4
No files matched your search
@@ -156,6 +156,7 @@ describe("composerDraftStore clearComposerContent", () => {
|
||||
|
||||
describe("composerDraftStore project draft thread mapping", () => {
|
||||
const projectId = ProjectId.makeUnsafe("project-a");
|
||||
const otherProjectId = ProjectId.makeUnsafe("project-b");
|
||||
const threadId = ThreadId.makeUnsafe("thread-a");
|
||||
const otherThreadId = ThreadId.makeUnsafe("thread-b");
|
||||
|
||||
@@ -195,6 +196,7 @@ describe("composerDraftStore project draft thread mapping", () => {
|
||||
it("clears only matching project draft mapping entries", () => {
|
||||
const store = useComposerDraftStore.getState();
|
||||
store.setProjectDraftThreadId(projectId, threadId);
|
||||
store.setPrompt(threadId, "hello");
|
||||
|
||||
store.clearProjectDraftThreadById(projectId, otherThreadId);
|
||||
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
|
||||
@@ -204,14 +206,46 @@ describe("composerDraftStore project draft thread mapping", () => {
|
||||
store.clearProjectDraftThreadById(projectId, threadId);
|
||||
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
|
||||
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
|
||||
expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
|
||||
});
|
||||
|
||||
it("clears project draft mapping by project id", () => {
|
||||
const store = useComposerDraftStore.getState();
|
||||
store.setProjectDraftThreadId(projectId, threadId);
|
||||
store.setPrompt(threadId, "hello");
|
||||
store.clearProjectDraftThreadId(projectId);
|
||||
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
|
||||
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
|
||||
expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
|
||||
});
|
||||
|
||||
it("clears orphaned composer drafts when remapping a project to a new draft thread", () => {
|
||||
const store = useComposerDraftStore.getState();
|
||||
store.setProjectDraftThreadId(projectId, threadId);
|
||||
store.setPrompt(threadId, "orphan me");
|
||||
|
||||
store.setProjectDraftThreadId(projectId, otherThreadId);
|
||||
|
||||
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
|
||||
otherThreadId,
|
||||
);
|
||||
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
|
||||
expect(useComposerDraftStore.getState().draftsByThreadId[threadId]).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps composer drafts when the thread is still mapped by another project", () => {
|
||||
const store = useComposerDraftStore.getState();
|
||||
store.setProjectDraftThreadId(projectId, threadId);
|
||||
store.setProjectDraftThreadId(otherProjectId, threadId);
|
||||
store.setPrompt(threadId, "keep me");
|
||||
|
||||
store.clearProjectDraftThreadId(projectId);
|
||||
|
||||
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
|
||||
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(otherProjectId)?.threadId).toBe(
|
||||
threadId,
|
||||
);
|
||||
expect(useComposerDraftStore.getState().draftsByThreadId[threadId]?.prompt).toBe("keep me");
|
||||
});
|
||||
|
||||
it("clears draft registration independently", () => {
|
||||
|
||||
@@ -355,9 +355,11 @@ function readPersistedAttachmentIdsFromStorage(threadId: ThreadId): string[] {
|
||||
function hydreatePersistedComposerImageAttachment(
|
||||
attachment: PersistedComposerImageAttachment,
|
||||
): File | null {
|
||||
const [rawHeader, payload] = attachment.dataUrl.split(",", 2);
|
||||
const header = rawHeader ?? "";
|
||||
if (!payload) {
|
||||
const commaIndex = attachment.dataUrl.indexOf(",");
|
||||
const header =
|
||||
commaIndex === -1 ? attachment.dataUrl : attachment.dataUrl.slice(0, commaIndex);
|
||||
const payload = commaIndex === -1 ? "" : attachment.dataUrl.slice(commaIndex + 1);
|
||||
if (payload.length === 0) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
@@ -484,14 +486,20 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
|
||||
...state.draftThreadsByThreadId,
|
||||
[threadId]: nextDraftThread,
|
||||
};
|
||||
let nextDraftsByThreadId = state.draftsByThreadId;
|
||||
if (
|
||||
previousThreadIdForProject &&
|
||||
previousThreadIdForProject !== threadId &&
|
||||
!Object.values(nextProjectDraftThreadIdByProjectId).includes(previousThreadIdForProject)
|
||||
) {
|
||||
delete nextDraftThreadsByThreadId[previousThreadIdForProject];
|
||||
if (state.draftsByThreadId[previousThreadIdForProject] !== undefined) {
|
||||
nextDraftsByThreadId = { ...state.draftsByThreadId };
|
||||
delete nextDraftsByThreadId[previousThreadIdForProject];
|
||||
}
|
||||
}
|
||||
return {
|
||||
draftsByThreadId: nextDraftsByThreadId,
|
||||
draftThreadsByThreadId: nextDraftThreadsByThreadId,
|
||||
projectDraftThreadIdByProjectId: nextProjectDraftThreadIdByProjectId,
|
||||
};
|
||||
@@ -563,10 +571,16 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
|
||||
const nextDraftThreadsByThreadId: Record<ThreadId, DraftThreadState> = {
|
||||
...state.draftThreadsByThreadId,
|
||||
};
|
||||
let nextDraftsByThreadId = state.draftsByThreadId;
|
||||
if (!Object.values(restProjectMappings).includes(threadId)) {
|
||||
delete nextDraftThreadsByThreadId[threadId];
|
||||
if (state.draftsByThreadId[threadId] !== undefined) {
|
||||
nextDraftsByThreadId = { ...state.draftsByThreadId };
|
||||
delete nextDraftsByThreadId[threadId];
|
||||
}
|
||||
}
|
||||
return {
|
||||
draftsByThreadId: nextDraftsByThreadId,
|
||||
draftThreadsByThreadId: nextDraftThreadsByThreadId,
|
||||
projectDraftThreadIdByProjectId: restProjectMappings,
|
||||
};
|
||||
@@ -586,10 +600,16 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
|
||||
const nextDraftThreadsByThreadId: Record<ThreadId, DraftThreadState> = {
|
||||
...state.draftThreadsByThreadId,
|
||||
};
|
||||
let nextDraftsByThreadId = state.draftsByThreadId;
|
||||
if (!Object.values(restProjectMappings).includes(threadId)) {
|
||||
delete nextDraftThreadsByThreadId[threadId];
|
||||
if (state.draftsByThreadId[threadId] !== undefined) {
|
||||
nextDraftsByThreadId = { ...state.draftsByThreadId };
|
||||
delete nextDraftsByThreadId[threadId];
|
||||
}
|
||||
}
|
||||
return {
|
||||
draftsByThreadId: nextDraftsByThreadId,
|
||||
draftThreadsByThreadId: nextDraftThreadsByThreadId,
|
||||
projectDraftThreadIdByProjectId: restProjectMappings,
|
||||
};
|
||||
|
||||
@@ -141,7 +141,7 @@ function ChatThreadRouteView() {
|
||||
const search = Route.useSearch();
|
||||
const threadExists = state.threads.some((thread) => thread.id === threadId);
|
||||
const draftThreadExists = useComposerDraftStore(
|
||||
(store) => store.draftThreadsByThreadId[threadId] !== undefined,
|
||||
(store) => Object.hasOwn(store.draftThreadsByThreadId, threadId),
|
||||
);
|
||||
const routeThreadExists = threadExists || draftThreadExists;
|
||||
const diffOpen = search.diff === "1";
|
||||
|
||||
Reference in new issue
Block a user