Skip to content

Commit

Permalink
fix: add context defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
rpenido committed Dec 10, 2024
1 parent a4b53a3 commit 526b915
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/library-authoring/common/context/LibraryContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ComponentEditorInfo {

export type LibraryContextData = {
/** The ID of the current library */
libraryId: string;
libraryId?: undefined | string;

This comment has been minimized.

Copy link
@bradenmacdonald

bradenmacdonald Dec 10, 2024

Contributor

I would prefer that libraryId is always defined in the context, so maybe for LibraryContext we shouldn't have a default. We already have it in our test cases anyways. It's mostly for the other ones that I wanted a default.

libraryData?: ContentLibrary;
readOnly: boolean;
isLoadingLibraryData: boolean;
Expand Down Expand Up @@ -141,7 +141,20 @@ export function useLibraryContext(): LibraryContextData {
const ctx = useContext(LibraryContext);
if (ctx === undefined) {
/* istanbul ignore next */
throw new Error('useLibraryContext() was used in a component without a <LibraryProvider> ancestor.');
return {
libraryId: undefined,
readOnly: true,
isLoadingLibraryData: false,
collectionId: undefined,
setCollectionId: () => {},
showOnlyPublished: false,
isCreateCollectionModalOpen: false,
openCreateCollectionModal: () => {},
closeCreateCollectionModal: () => {},
componentBeingEdited: undefined,
openComponentEditor: () => {},
closeComponentEditor: () => {},
};
}
return ctx;
}
12 changes: 10 additions & 2 deletions src/library-authoring/common/context/SidebarContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
useMemo,
useState,
} from 'react';

Check failure on line 7 in src/library-authoring/common/context/SidebarContext.tsx

View workflow job for this annotation

GitHub Actions / tests

Expected 1 empty line after import statement not followed by another import

export enum SidebarBodyComponentId {
AddContent = 'add-content',
Info = 'info',
Expand Down Expand Up @@ -158,7 +157,16 @@ export function useSidebarContext(): SidebarContextData {
const ctx = useContext(SidebarContext);
if (ctx === undefined) {
/* istanbul ignore next */
throw new Error('useSidebarContext() was used in a component without a <SidebarProvider> ancestor.');
return {
closeLibrarySidebar: () => {},
openAddContentSidebar: () => {},
openInfoSidebar: () => {},
openComponentInfoSidebar: () => {},
openCollectionInfoSidebar: () => {},
resetSidebarAdditionalActions: () => {},
setSidebarCurrentTab: () => {},
sidebarComponentInfo: undefined,
};
}
return ctx;
}

0 comments on commit 526b915

Please sign in to comment.