Skip to content

Commit

Permalink
fix: add optional library context hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Oct 21, 2024
1 parent 56ff971 commit 6a52f3c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/editors/containers/TextEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as hooks from './hooks';
import messages from './messages';
import TinyMceWidget from '../../sharedComponents/TinyMceWidget';
import { prepareEditorRef, replaceStaticWithAsset } from '../../sharedComponents/TinyMceWidget/hooks';
import { useLibraryContext } from '../../../library-authoring/common/context';
import { useOptionalLibraryContext } from '../../../library-authoring/common/context';

const TextEditor = ({
onClose,
Expand All @@ -43,9 +43,9 @@ const TextEditor = ({
});
const editorContent = newContent || initialContent;
let documentURL;
const { sidebarComponentUsageKey: usageKey } = useLibraryContext();
if (isLibrary) {
documentURL = `${getConfig().STUDIO_BASE_URL }/library_assets/blocks/${ usageKey }/`;
const ctx = useOptionalLibraryContext();
if (isLibrary && ctx !== undefined) {
documentURL = `${getConfig().STUDIO_BASE_URL }/library_assets/blocks/${ ctx.sidebarComponentUsageKey }/`;
}

if (!refReady) { return null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports[`TinyMceWidget snapshots ImageUploadModal is not rendered 1`] = `
{
"clearSelection": [MockFunction hooks.selectedImage.clearSelection],
"content": undefined,
"documentURL": undefined,
"editorContentHtml": undefined,
"editorRef": {
"current": {
Expand Down Expand Up @@ -87,6 +88,7 @@ exports[`TinyMceWidget snapshots SourcecodeModal is not rendered 1`] = `
{
"clearSelection": [MockFunction hooks.selectedImage.clearSelection],
"content": undefined,
"documentURL": undefined,
"editorContentHtml": undefined,
"editorRef": {
"current": {
Expand Down Expand Up @@ -166,6 +168,7 @@ exports[`TinyMceWidget snapshots renders as expected with default behavior 1`] =
{
"clearSelection": [MockFunction hooks.selectedImage.clearSelection],
"content": undefined,
"documentURL": undefined,
"editorContentHtml": undefined,
"editorRef": {
"current": {
Expand Down
4 changes: 4 additions & 0 deletions src/library-authoring/common/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,7 @@ export function useLibraryContext(): LibraryContextData {
}
return ctx;
}

export function useOptionalLibraryContext(): LibraryContextData | undefined {
return useContext(LibraryContext);
}

0 comments on commit 6a52f3c

Please sign in to comment.