From d267d16bf25ded191bc5b3843cab779860ecac74 Mon Sep 17 00:00:00 2001 From: Rita Date: Thu, 26 Sep 2024 08:24:34 +0200 Subject: [PATCH] chore(form): remove imports from shouldArrayDialogOpen --- .../src/core/form/studio/FormBuilder.tsx | 15 +-- packages/sanity/src/core/form/studio/index.ts | 2 +- .../__tests__/shouldArrayDialogOpen.test.ts | 102 ------------------ .../core/form/studio/tree-editing/index.ts | 1 - .../form/studio/tree-editing/utils/index.ts | 1 - .../utils/shouldArrayDialogOpen.ts | 49 --------- .../statusBar/DocumentStatusBarActions.tsx | 16 +-- 7 files changed, 5 insertions(+), 181 deletions(-) delete mode 100644 packages/sanity/src/core/form/studio/tree-editing/__tests__/shouldArrayDialogOpen.test.ts delete mode 100644 packages/sanity/src/core/form/studio/tree-editing/utils/shouldArrayDialogOpen.ts diff --git a/packages/sanity/src/core/form/studio/FormBuilder.tsx b/packages/sanity/src/core/form/studio/FormBuilder.tsx index 98a744aefbc..58e1f1d04bf 100644 --- a/packages/sanity/src/core/form/studio/FormBuilder.tsx +++ b/packages/sanity/src/core/form/studio/FormBuilder.tsx @@ -32,12 +32,7 @@ import { import {DocumentFieldActionsProvider} from './contexts/DocumentFieldActions' import {FormBuilderInputErrorBoundary} from './FormBuilderInputErrorBoundary' import {FormProvider} from './FormProvider' -import { - shouldArrayDialogOpen, - TreeEditingDialog, - TreeEditingEnabledProvider, - useTreeEditingEnabled, -} from './tree-editing' +import {TreeEditingDialog, TreeEditingEnabledProvider, useTreeEditingEnabled} from './tree-editing' /** * @alpha @@ -307,15 +302,9 @@ interface RootInputProps { function RootInput(props: RootInputProps) { const {rootInputProps, onPathOpen, openPath, renderInput} = props const treeEditing = useTreeEditingEnabled() - - const open = useMemo( - () => shouldArrayDialogOpen(rootInputProps.schemaType, openPath), - [openPath, rootInputProps.schemaType], - ) - const isRoot = rootInputProps.id === 'root' - const arrayEditingModal = treeEditing.enabled && isRoot && open && ( + const arrayEditingModal = treeEditing.enabled && isRoot && ( { - test('should return false if its in the base document (path [])', () => { - const objectSchemaType = { - jsonType: 'object', - fields: [], - name: '', - // eslint-disable-next-line camelcase - __experimental_search: [], - } as ObjectSchemaType - - const {result} = renderHook(() => shouldArrayDialogOpen(objectSchemaType, [])) - - expect(result.current).toEqual(false) - }) - - test('should return false if array has a reference', () => { - const referenceSchemaType = { - jsonType: 'object', - fields: [ - { - name: 'referenceAuthor', - type: { - type: 'array', - of: [{type: 'reference', to: [{type: 'author'}]}], - }, - }, - ], - name: '', - // eslint-disable-next-line camelcase - __experimental_search: [], - } as unknown as ObjectSchemaType - - const {result} = renderHook(() => - shouldArrayDialogOpen(referenceSchemaType, ['referenceAuthor']), - ) - - expect(result.current).toEqual(false) - }) - - test('should return false if array is PTE', () => { - const referenceSchemaType = { - jsonType: 'object', - fields: [ - { - name: 'pte', - type: { - type: 'array', - of: [{type: 'block'}], - }, - }, - ], - name: '', - // eslint-disable-next-line camelcase - __experimental_search: [], - } as unknown as ObjectSchemaType - - const {result} = renderHook(() => shouldArrayDialogOpen(referenceSchemaType, ['pte'])) - - expect(result.current).toEqual(false) - }) - - test('should return true if its an array of plain objects - not pte, an array of references or in base document', () => { - const objectSchemaType = { - jsonType: 'object', - fields: [ - { - type: 'array', - name: 'arrayOfObjects', - of: [ - { - type: 'object', - name: 'object1', - fields: [ - { - name: 'name', - type: 'string', - title: 'name', - }, - { - name: 'age', - type: 'number', - title: 'age', - }, - ], - }, - ], - }, - ], - name: '', - // eslint-disable-next-line camelcase - __experimental_search: [], - } as unknown as ObjectSchemaType - - const {result} = renderHook(() => shouldArrayDialogOpen(objectSchemaType, ['object1'])) - - expect(result.current).toEqual(false) - }) -}) diff --git a/packages/sanity/src/core/form/studio/tree-editing/index.ts b/packages/sanity/src/core/form/studio/tree-editing/index.ts index 634ba1a809b..3e91c5effbb 100644 --- a/packages/sanity/src/core/form/studio/tree-editing/index.ts +++ b/packages/sanity/src/core/form/studio/tree-editing/index.ts @@ -1,4 +1,3 @@ export * from './components' export * from './context' export * from './hooks' -export {shouldArrayDialogOpen} from './utils' diff --git a/packages/sanity/src/core/form/studio/tree-editing/utils/index.ts b/packages/sanity/src/core/form/studio/tree-editing/utils/index.ts index 0f1f8a87d0b..c2fe986cc77 100644 --- a/packages/sanity/src/core/form/studio/tree-editing/utils/index.ts +++ b/packages/sanity/src/core/form/studio/tree-editing/utils/index.ts @@ -1,4 +1,3 @@ export * from './build-tree-editing-state' export * from './findArrayTypePaths' export * from './getSchemaField' -export * from './shouldArrayDialogOpen' diff --git a/packages/sanity/src/core/form/studio/tree-editing/utils/shouldArrayDialogOpen.ts b/packages/sanity/src/core/form/studio/tree-editing/utils/shouldArrayDialogOpen.ts deleted file mode 100644 index 53050447cfa..00000000000 --- a/packages/sanity/src/core/form/studio/tree-editing/utils/shouldArrayDialogOpen.ts +++ /dev/null @@ -1,49 +0,0 @@ -import {toString} from '@sanity/util/paths' -import { - isArrayOfBlocksSchemaType, - isArrayOfObjectsSchemaType, - type ObjectSchemaType, - type Path, -} from 'sanity' - -// import {isPortableTextSchemaType} from './asserters' -import {getRootPath} from './getRootPath' -import {getSchemaField} from './getSchemaField' - -/** - * A utility function to check if the global array editing dialog should be open. - * @param schemaType - The schema object that we are opening - * @param path - The path that we are focusing on - * @returns Returns true if the dialog should be open - * @internal - */ -export function shouldArrayDialogOpen(schemaType: ObjectSchemaType, path: Path): boolean { - // If the path is empty, we can't determine if the array dialog is open - if (path.length === 0) return false - - const rootPath = getRootPath(path) - - // Get the field for the first segments - const field = getSchemaField(schemaType, toString(rootPath)) - - // Check if the field is an array of objects - if (isArrayOfObjectsSchemaType(field?.type)) { - // Check if the array of objects is an array of references. - const isArrayOfReferences = field.type.of.every((type) => type?.hasOwnProperty('to')) - const isPortableText = isArrayOfBlocksSchemaType(field.type) - - // Return false if the array of objects is an array of references - // since these are edited inline and not in a dialog. - if (isArrayOfReferences) return false - - // Return false if the array of objects is an array of portable text - // since these are edited inline and not in a dialog. - if (isPortableText) return false - - // Else, return true if it is an array of objects - return true - } - - // Otherwise, return false - return false -} diff --git a/packages/sanity/src/structure/panes/document/statusBar/DocumentStatusBarActions.tsx b/packages/sanity/src/structure/panes/document/statusBar/DocumentStatusBarActions.tsx index 97ea27266af..7eabd5acdc1 100644 --- a/packages/sanity/src/structure/panes/document/statusBar/DocumentStatusBarActions.tsx +++ b/packages/sanity/src/structure/panes/document/statusBar/DocumentStatusBarActions.tsx @@ -5,8 +5,6 @@ import { type DocumentActionComponent, type DocumentActionDescription, Hotkeys, - shouldArrayDialogOpen, - useSource, useTimelineSelector, } from 'sanity' @@ -27,17 +25,9 @@ const DocumentStatusBarActionsInner = memo(function DocumentStatusBarActionsInne props: DocumentStatusBarActionsInnerProps, ) { const {disabled, showMenu, states} = props - const {__internal_tasks, schemaType, openPath} = useDocumentPane() + const {__internal_tasks} = useDocumentPane() const [firstActionState, ...menuActionStates] = states const [buttonElement, setButtonElement] = useState(null) - const isTreeArrayEditingEnabled = useSource().beta?.treeArrayEditing?.enabled - - // Disable the main document action if the array dialog is open - const isTreeArrayEditingEnabledOpen = useMemo(() => { - if (!isTreeArrayEditingEnabled) return false - - return shouldArrayDialogOpen(schemaType, openPath) - }, [isTreeArrayEditingEnabled, openPath, schemaType]) // TODO: This could be refactored to use the tooltip from the button if the firstAction.title was updated to a string. const tooltipContent = useMemo(() => { @@ -69,9 +59,7 @@ const DocumentStatusBarActionsInner = memo(function DocumentStatusBarActionsInne