import React, { useCallback, useState } from 'react'; import { DOMAttributes, FocusableElement } from '@react-types/shared'; import SourceList from '../SourceList/SourceList'; import ResourceLauncher from '../ResourceLauncher/ResourceLauncher'; import SourceDropdownContainer from '../SourceDropdownContainer/SourceDropdownContainer'; import SourceDropdown from '../SourceDropdown/SourceDropdown'; import { ResourceBrowserPlugin, ResourceBrowserSource, ResourceBrowserResource, ResourceBrowserSourceWithPlugin, PluginLaunchMode, PluginLaunchModeType, ResourceBrowserSearchUIArgs, ResourceBrowserUIArgs, } from '../types'; interface MainContainerProps { title: string; titleAriaProps: DOMAttributes; allowedTypes: string[] | undefined; sources: ResourceBrowserSourceWithPlugin[]; selectedSource: ResourceBrowserSource | null; onSourceSelect(source: ResourceBrowserSource, mode?: PluginLaunchMode): void; onChange(resource: ResourceBrowserResource | null): void; onClose: () => void; preselectedResource?: ResourceBrowserResource | null; plugin: ResourceBrowserPlugin | null; pluginMode: PluginLaunchMode | null; searchEnabled: boolean; } function MainContainer({ title, titleAriaProps, allowedTypes, sources, selectedSource, onSourceSelect, onChange, onClose, preselectedResource, plugin, pluginMode, searchEnabled, }: MainContainerProps) { const [headerPortal, setHeaderPortal] = useState(null); const pluginToRender = pluginMode ? pluginMode.type : PluginLaunchModeType.Browse; // Cant default a 'null' property in signature so set it here const SourceBrowser = plugin?.sourceBrowserComponent(); const SourceSearch = plugin?.sourceSearchComponent(); // Can't use a useRef as it wont update on change when a source is selected, so need to use a ref callback to store in state const setHeaderPortalRef = useCallback( (node: HTMLDivElement) => { if (node !== null) { setHeaderPortal(node); } }, [setHeaderPortal], ); // MainContainer will either render the source list view if no source is set or the plugins UI if a source has been selected return (

{title?.charAt(0).toUpperCase() + title?.slice(1).toLowerCase()}

{plugin && selectedSource && ( <> {/* Custom header if plugin supports it */} {plugin.createHeaderPortal &&
} {/* Default header if plugin does not support */} {!plugin.createHeaderPortal && sources.length > 1 && ( {}}>
)} )}
{plugin && selectedSource && (
{pluginToRender === PluginLaunchModeType.Browse && SourceBrowser && ( { onChange(resource); onClose(); }} /> )} {pluginToRender === PluginLaunchModeType.Search && SourceSearch && ( { onChange(resource); onClose(); }} /> )}
)} {!selectedSource && searchEnabled && } {!selectedSource && !searchEnabled && }
); } export default MainContainer;