From 07e86c0f20824a96d80838a8a2f4eae662e32456 Mon Sep 17 00:00:00 2001 From: Alessio Gravili Date: Tue, 31 Dec 2024 00:30:32 -0700 Subject: [PATCH] fix(richtext-lexical): throw toast error when attempting to create upload node without any upload collections enabled (#10277) Fixes https://github.com/payloadcms/payload/issues/9136 --- .../utils/EnabledRelationshipsCondition.tsx | 10 ++++--- .../features/upload/client/drawer/index.tsx | 26 ++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/richtext-lexical/src/features/relationship/client/utils/EnabledRelationshipsCondition.tsx b/packages/richtext-lexical/src/features/relationship/client/utils/EnabledRelationshipsCondition.tsx index dea958962a..9a0804572e 100644 --- a/packages/richtext-lexical/src/features/relationship/client/utils/EnabledRelationshipsCondition.tsx +++ b/packages/richtext-lexical/src/features/relationship/client/utils/EnabledRelationshipsCondition.tsx @@ -29,8 +29,12 @@ const filterRichTextCollections: FilteredCollectionsT = (collections, options) = }) } -export const EnabledRelationshipsCondition: React.FC = (props) => { - const { children, uploads = false, ...rest } = props +export const EnabledRelationshipsCondition: React.FC<{ + children: any + FallbackComponent?: React.FC + uploads?: boolean +}> = (props) => { + const { children, FallbackComponent, uploads = false, ...rest } = props const { config: { collections }, } = useConfig() @@ -44,7 +48,7 @@ export const EnabledRelationshipsCondition: React.FC = (props) => { ) if (!enabledCollectionSlugs.length) { - return null + return FallbackComponent ? : null } return React.cloneElement(children, { ...rest, enabledCollectionSlugs }) diff --git a/packages/richtext-lexical/src/features/upload/client/drawer/index.tsx b/packages/richtext-lexical/src/features/upload/client/drawer/index.tsx index 90f7c0fbb3..b3b7645f5a 100644 --- a/packages/richtext-lexical/src/features/upload/client/drawer/index.tsx +++ b/packages/richtext-lexical/src/features/upload/client/drawer/index.tsx @@ -2,6 +2,7 @@ import type { LexicalEditor } from 'lexical' import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext.js' +import { toast } from '@payloadcms/ui' import { $getNodeByKey, COMMAND_PRIORITY_EDITOR } from 'lexical' import React, { useCallback, useEffect, useState } from 'react' @@ -92,9 +93,32 @@ const UploadDrawerComponent: React.FC = ({ enabledCollectionSlugs }) => { return } +const UploadDrawerComponentFallback: React.FC = () => { + const [editor] = useLexicalComposerContext() + + useEffect(() => { + return editor.registerCommand<{ + replace: { nodeKey: string } | false + }>( + INSERT_UPLOAD_WITH_DRAWER_COMMAND, + () => { + toast.error('No upload collections enabled') + return true + }, + COMMAND_PRIORITY_EDITOR, + ) + }, [editor]) + + return null +} + export const UploadDrawer = (props: Props): React.ReactNode => { return ( - + )