Fix stale timelineStore.activeDocumentId when switching between loaded documents

switchToDocument called loadDocument which returned early when a document was
already in memory, skipping loadTimeline and leaving timelineStore.activeDocumentId
pointing to the previous document. Store operations like duplicateStateAsChild
then looked up state IDs in the wrong document's timeline, causing "state not found".

Fix by adding setActiveDocument to TimelineActions and calling it from
switchToDocument so both stores always stay in sync on tab switch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jan-Henrik 2026-03-31 11:39:50 +02:00
parent 4322f3cf63
commit 3c3c99f521
4 changed files with 13 additions and 0 deletions

View file

@ -111,6 +111,10 @@ export const useTimelineStore = create<TimelineStore & TimelineActions>(
console.log(`Timeline initialized for document ${documentId}`);
},
setActiveDocument: (documentId: string) => {
set({ activeDocumentId: documentId });
},
loadTimeline: (documentId: string, timeline: Timeline) => {
set((state) => {
const newTimelines = new Map(state.timelines);

View file

@ -25,6 +25,7 @@ vi.mock("./timelineStore", () => ({
getState: () => ({
timelines: new Map(),
loadTimeline: vi.fn(),
setActiveDocument: vi.fn(),
clearTimeline: vi.fn(),
}),
},

View file

@ -600,6 +600,11 @@ export const useWorkspaceStore = create<Workspace & WorkspaceActions>((set, get)
activeDocumentId: documentId,
};
});
// Always sync timelineStore.activeDocumentId — loadDocument returns early
// when the document is already in memory, so loadTimeline is never called
// and timelineStore.activeDocumentId would remain stale.
useTimelineStore.getState().setActiveDocument(documentId);
});
},

View file

@ -66,6 +66,9 @@ export interface TimelineActions {
// Load timeline from document
loadTimeline: (documentId: string, timeline: Timeline) => void;
// Set active document without reloading timeline data (for switching between already-loaded documents)
setActiveDocument: (documentId: string) => void;
// Create new state
createState: (label: string, description?: string, cloneFromCurrent?: boolean) => StateId;