import { useCallback, useState } from 'react'; import { FiltersMap, OptionalFiltersMap } from '@wix/bex-core'; import { PickerStandaloneEvents, PickerStandaloneState } from '../../state'; import { CollectionConfigWithConditionals, useWixPatternsContainer, useWixPatternsContainerOverrides, useCreateCollection, } from '@wix/bex-core/react'; export type LogoType = 'wix' | 'editorx'; export type PickerStandaloneConfigBase = { /** * URL to navigate to when there are no items available in the collection. */ noItemsAvailableDestinationUrl?: string; /** * A predicate for an item to be selected in the picker. * Pass `true` to select the first item. */ initialSelect?: true | ((item: T, index: number) => boolean); /** * A destination URL or callback to redirect to when the user selects an item. * Will be used when no query param override was provided. */ defaultDestinationUrl: string | ((item: T) => string); /** * Map of query params to object properties to allow for URL filtering. * For example - `urlSegmentPropertyMap: { msid: ['metaSiteId'] }` * will add support for `?msid=123` query params to filter `metaSiteId`. */ urlSegmentPropertyMap?: Record; /** * Optional callbacks for various events of the picker & collection state lifecycle:
* - `onSingleItemAutoNavigation` - when the picker is opened and auto-selects the only item in the collection
* - `onContinueToDestinationUrl` - when the user selects an item and submits
* - `onReady` - fires when the picker is ready to be interacted with
* - `onInitialPageFetched` - when the first page of items is ready
* - `onSearch` - when fetching new items as a result new search
* - `onSearchResults` - when the result of a search is ready
* - `onNewPageStart` - when starting to load the next page of items
* - `onNewPageAdded` - when the result of the next page is ready */ events?: PickerStandaloneEvents; /** * The type of logo to display in the picker. */ logoType?: LogoType; shouldFilterNotAffectTotalCount?: (filterName: keyof F) => boolean; }; export type PickerStandaloneConfig< T, F extends FiltersMap, > = CollectionConfigWithConditionals & PickerStandaloneConfigBase; export type PickerStandaloneFactory = ( config: PickerStandaloneConfig, ) => PickerStandaloneState; export function useCreatePickerStandalone(): PickerStandaloneFactory { const { window, errorMonitor, createBILogger } = useWixPatternsContainer(); const containerOverrides = useWixPatternsContainerOverrides(); const createCollection = useCreateCollection(); return useCallback((config) => { const { noItemsAvailableDestinationUrl, initialSelect, logoType = 'wix', defaultDestinationUrl, urlSegmentPropertyMap, events, shouldFilterNotAffectTotalCount, ...collectionConfig } = config; const collection = createCollection({ persistQueryToUrl: true, ...collectionConfig, events, }); return new PickerStandaloneState({ collection, noItemsAvailableDestinationUrl, initialSelect, defaultDestinationUrl, urlSegmentPropertyMap, window: containerOverrides.window ?? window, errorMonitor, logoType, shouldFilterNotAffectTotalCount, events, createBILogger, }); }, []); } export function usePickerStandalone< T, F extends FiltersMap = OptionalFiltersMap, >(config: PickerStandaloneConfig): PickerStandaloneState { const createPickerStandalone = useCreatePickerStandalone(); const [state] = useState(() => createPickerStandalone(config)); return state; }