From a42d3f111eaeef268d42f482a55c64035bd1d884 Mon Sep 17 00:00:00 2001 From: Alexander Zakharov Date: Thu, 18 Jul 2024 17:40:00 +0200 Subject: [PATCH] fix: wrap components with a 'withToolOptions' HOC --- src/components/Tool/index.tsx | 15 +++++---------- src/contexts/ToolOptionsContext.tsx | 16 +++++++++++++--- src/index.ts | 25 ++++++++++++++++--------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/components/Tool/index.tsx b/src/components/Tool/index.tsx index 942aaa74..91adb7e6 100644 --- a/src/components/Tool/index.tsx +++ b/src/components/Tool/index.tsx @@ -1,17 +1,12 @@ import {Flex} from '@sanity/ui' -import React, {ComponentProps} from 'react' +import React from 'react' import Browser from '../Browser' -import {ToolOptionsProvider} from '../../contexts/ToolOptionsContext' -import {Tool as SanityTool} from 'sanity' -import {MediaToolOptions} from '@types' -const Tool = ({tool: {options}}: ComponentProps['component']>) => { +const Tool = () => { return ( - - - - - + + + ) } diff --git a/src/contexts/ToolOptionsContext.tsx b/src/contexts/ToolOptionsContext.tsx index 69f3379e..75f631a7 100644 --- a/src/contexts/ToolOptionsContext.tsx +++ b/src/contexts/ToolOptionsContext.tsx @@ -1,5 +1,5 @@ import {MediaToolOptions} from '@types' -import React, {PropsWithChildren, createContext, useContext, useMemo} from 'react' +import React, {ComponentType, PropsWithChildren, createContext, useContext, useMemo} from 'react' import {DropzoneOptions} from 'react-dropzone' type ContextProps = { @@ -9,10 +9,10 @@ type ContextProps = { const ToolOptionsContext = createContext(null) type Props = { - options?: MediaToolOptions + options?: MediaToolOptions | void } -export const ToolOptionsProvider = ({options, children}: PropsWithChildren) => { +const ToolOptionsProvider = ({options, children}: PropsWithChildren) => { const value = useMemo( () => ({dropzone: {maxSize: options?.maximumUploadSize}}), [options?.maximumUploadSize] @@ -31,4 +31,14 @@ export const useToolOptions = () => { return context } +export const withToolOptions = + (options: MediaToolOptions | void) => +

(Component: ComponentType

) => { + return (props: P) => ( + + + + ) + } + export default ToolOptionsContext diff --git a/src/index.ts b/src/index.ts index e9414345..deab83b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,10 @@ import {definePlugin} from 'sanity' import {ImageIcon} from '@sanity/icons' -import type {AssetSource} from 'sanity' import FormBuilderTool from './components/FormBuilderTool' import Tool from './components/Tool' import mediaTag from './schemas/tag' import {MediaToolOptions} from '@types' +import {withToolOptions} from './contexts/ToolOptionsContext' const plugin = { icon: ImageIcon, @@ -12,22 +12,29 @@ const plugin = { title: 'Media' } -export const mediaAssetSource: AssetSource = { - ...plugin, - component: FormBuilderTool -} - export const media = definePlugin(options => ({ name: 'media', form: { file: { assetSources: prev => { - return [...prev, mediaAssetSource] + return [ + ...prev, + { + ...plugin, + component: withToolOptions(options)(FormBuilderTool) + } + ] } }, image: { assetSources: prev => { - return [...prev, mediaAssetSource] + return [ + ...prev, + { + ...plugin, + component: withToolOptions(options)(FormBuilderTool) + } + ] } } }, @@ -40,7 +47,7 @@ export const media = definePlugin(options => ({ { ...plugin, options, - component: Tool + component: withToolOptions(options)(Tool) } ] }