Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/src/stores/portfolio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function createPortfolioStore(subscriptions: SubscriptionsRouter, editor:
subscriptions.subscribeFrontendMessage("UpdateOpenDocumentsList", (data) => {
update((state) => {
state.documents = data.openDocuments;
if (state.documents.length === 0) state.activeDocumentIndex = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While resetting the index to 0 when the list is empty fixes the reported issue, it is more robust to ensure the index is always within valid bounds whenever the document list changes. This prevents potential UI crashes or glitches if the active document is closed but other documents remain, causing the index to temporarily point out of bounds before the next UpdateActiveDocument message arrives.

Using Math.max(0, Math.min(..., length - 1)) correctly handles both the empty list case (resetting to 0) and the out-of-bounds case.

Suggested change
if (state.documents.length === 0) state.activeDocumentIndex = 0;
state.activeDocumentIndex = Math.max(0, Math.min(state.activeDocumentIndex, state.documents.length - 1));

return state;
});
});
Expand Down
Loading