From 526b915cae0b31c2fd9014cc792e97d0137e71ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Penido?= Date: Tue, 10 Dec 2024 14:45:34 -0300 Subject: [PATCH] fix: add context defaults --- .../common/context/LibraryContext.tsx | 17 +++++++++++++++-- .../common/context/SidebarContext.tsx | 12 ++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/library-authoring/common/context/LibraryContext.tsx b/src/library-authoring/common/context/LibraryContext.tsx index 9612a9285..68ba0ea8c 100644 --- a/src/library-authoring/common/context/LibraryContext.tsx +++ b/src/library-authoring/common/context/LibraryContext.tsx @@ -19,7 +19,7 @@ export interface ComponentEditorInfo { export type LibraryContextData = { /** The ID of the current library */ - libraryId: string; + libraryId?: undefined | string; libraryData?: ContentLibrary; readOnly: boolean; isLoadingLibraryData: boolean; @@ -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 ancestor.'); + return { + libraryId: undefined, + readOnly: true, + isLoadingLibraryData: false, + collectionId: undefined, + setCollectionId: () => {}, + showOnlyPublished: false, + isCreateCollectionModalOpen: false, + openCreateCollectionModal: () => {}, + closeCreateCollectionModal: () => {}, + componentBeingEdited: undefined, + openComponentEditor: () => {}, + closeComponentEditor: () => {}, + }; } return ctx; } diff --git a/src/library-authoring/common/context/SidebarContext.tsx b/src/library-authoring/common/context/SidebarContext.tsx index d17ee6f9e..b962dd3e9 100644 --- a/src/library-authoring/common/context/SidebarContext.tsx +++ b/src/library-authoring/common/context/SidebarContext.tsx @@ -5,7 +5,6 @@ import { useMemo, useState, } from 'react'; - export enum SidebarBodyComponentId { AddContent = 'add-content', Info = 'info', @@ -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 ancestor.'); + return { + closeLibrarySidebar: () => {}, + openAddContentSidebar: () => {}, + openInfoSidebar: () => {}, + openComponentInfoSidebar: () => {}, + openCollectionInfoSidebar: () => {}, + resetSidebarAdditionalActions: () => {}, + setSidebarCurrentTab: () => {}, + sidebarComponentInfo: undefined, + }; } return ctx; }