Skip to content

Commit

Permalink
fix(richtext-lexical): throw toast error when attempting to create up…
Browse files Browse the repository at this point in the history
…load node without any upload collections enabled (#10277)

Fixes #9136
  • Loading branch information
AlessioGr authored Dec 31, 2024
1 parent 182eaa3 commit 07e86c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ const filterRichTextCollections: FilteredCollectionsT = (collections, options) =
})
}

export const EnabledRelationshipsCondition: React.FC<any> = (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()
Expand All @@ -44,7 +48,7 @@ export const EnabledRelationshipsCondition: React.FC<any> = (props) => {
)

if (!enabledCollectionSlugs.length) {
return null
return FallbackComponent ? <FallbackComponent {...rest} /> : null
}

return React.cloneElement(children, { ...rest, enabledCollectionSlugs })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -92,9 +93,32 @@ const UploadDrawerComponent: React.FC<Props> = ({ enabledCollectionSlugs }) => {
return <ListDrawer onSelect={onSelect} />
}

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 (
<EnabledRelationshipsCondition {...props} uploads>
<EnabledRelationshipsCondition
{...props}
FallbackComponent={UploadDrawerComponentFallback}
uploads
>
<UploadDrawerComponent {...props} />
</EnabledRelationshipsCondition>
)
Expand Down

0 comments on commit 07e86c0

Please sign in to comment.