Skip to content

Commit

Permalink
Add missing null check for content frame in ExportAnnotations
Browse files Browse the repository at this point in the history
The `store.defaultContentFrame` selector had an incorrect return type, `Frame`
instead of `Frame|null`, because `frames[0]` has the static type `Frame`, even
though this can actually return undefined at runtime. As a result, TypeScript
didn't catch a missing null check when generating the default filename for the
export.

Fixes #5818
  • Loading branch information
robertknight committed Sep 13, 2023
1 parent c01e96e commit 42858d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/sidebar/components/ShareDialog/ExportAnnotations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function ExportAnnotations({
const defaultFilename = useMemo(
() =>
suggestedFilename({
documentMetadata: contentFrame.metadata,
documentMetadata: contentFrame?.metadata,
groupName: group?.name,
}),
[contentFrame, group],
Expand Down
11 changes: 10 additions & 1 deletion src/sidebar/store/modules/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,16 @@ const mainFrame = createSelector(
*/
const defaultContentFrame = createSelector(
(state: State) => state.frames,
frames => firstFrameWithoutId(frames) ?? frames[0] ?? null,
frames => {
const mainFrame = firstFrameWithoutId(frames);
if (mainFrame) {
return mainFrame;
} else if (frames.length > 0) {
return frames[0];
} else {
return null;
}
},
);

function searchUrisForFrame(frame: Frame): string[] {
Expand Down

0 comments on commit 42858d0

Please sign in to comment.