import React from 'react'; import AdsClickRoundedIcon from '@mui/icons-material/AdsClickRounded'; import AddCircleOutlineRoundedIcon from '@mui/icons-material/AddCircleOutlineRounded'; import PhotoLibraryRoundedIcon from '@mui/icons-material/PhotoLibraryRounded'; import { DOMAttributes } from '@react-types/shared'; import { ModalTrigger } from '@squiz/resource-browser-ui-lib'; import { ErrorState } from './States/Error'; import { LoadingState } from './States/Loading'; import { SelectedState } from './States/Selected'; import { useSelectedState } from '../Hooks/useSelectedState'; import { ResourceBrowserResource, ResourceBrowserPlugin } from '../types'; import clsx from 'clsx'; export type ResourcePickerProps = { resource: ResourceBrowserResource | null; plugin: ResourceBrowserPlugin | null; allowedTypes: string[] | undefined; error: Error | null; isLoading: boolean; isOtherSourceValue: boolean; isDisabled?: boolean; children: (onClose: () => void, titleProps: DOMAttributes) => React.ReactElement; onClear: () => void; isModalOpen: boolean; onModalStateChange(isOpen: boolean): void; }; export const ResourcePicker = ({ resource, plugin, allowedTypes, error: externalError, isLoading: isExternalLoading, isOtherSourceValue, isDisabled, children, onClear, isModalOpen, onModalStateChange, }: ResourcePickerProps) => { const { data: selectedState, error, isLoading } = useSelectedState({ resource, plugin }); const isImagePicker = allowedTypes && allowedTypes.length === 1 && allowedTypes.includes('image'); const isEmpty = resource === null && !isExternalLoading && !externalError; return (
{isImagePicker ? ( ) : ( )} {isOtherSourceValue && ( // User is selecting a new image from a different source, render a loading spinner as we can't resolve the value
{/* Need to render the ModalTrigger still as it renders the */} {children}
)} {isEmpty && !isOtherSourceValue && ( } isDisabled={isDisabled} scope="squiz-rb-scope" > {children} )} {!isEmpty && !isOtherSourceValue && (
{(isExternalLoading || isLoading) && } {(externalError || error) && ( )} {!isExternalLoading && resource && selectedState && ( )}
)}
); };