import React, { useMemo, useRef } from 'react'; import { observer } from 'mobx-react-lite'; import { Box, DropdownPropsControlled as DropdownProps, Input, Loader, } from '@wix/design-system'; import { CollectionViewsState } from '../../state'; import { FiltersMap } from '@wix/bex-core'; import { ViewsDropdownModals } from './ViewsDropdownModals'; import { View } from '../../model'; import { classes, st } from './ViewsDropdown.st.css.js'; import { ManageViewPopover, ManageViewPopoverHandle, } from './ManageViewPopover'; import { buildCategoryOption, buildViewOption, getCategoryName, getViewName, } from './viewsDropdownRenderHelpers'; import { AutoCompleteReadonly } from './AutoCompleteReadonly'; import { SaveToNewViewFooter } from './SaveToNewViewFooter'; import { ViewsDropdownPrefix } from './ViewsDropdownPrefix'; import { useWixPatternsContainer } from '@wix/bex-core/react'; import { useBmHarmonyTheme } from '../../hooks/useBmHarmonyTheme'; export const VIEWS_REDESIGN_EXPERIMENT = 'specs.cairo.ViewsRedesign'; export type ViewsDropdownProps = { state: CollectionViewsState; onSelect?: (view: View) => void; dataHook: string; showTotal?: boolean; } & Omit; function _ViewsDropdown({ onSelect, dataHook, state, showTotal = true, dropdownWidth = '200px', ...dropdownProps }: ViewsDropdownProps) { const container = useWixPatternsContainer(); const isRedesign = container.internalExperiments?.enabled( VIEWS_REDESIGN_EXPERIMENT, ); const isBmHarmonyTheme = useBmHarmonyTheme(); const { views: viewsState } = state; const { currentView, collection, localSearchState } = viewsState; const { filteredCategoriesWithViews } = localSearchState; const { translate: t } = collection; const showLoader = collection.result.status.isIdle || collection.result.status.isLoading; const manageViewRef = useRef(null); const viewsOptions = useMemo(() => { const { viewsWithoutCategory, categories } = filteredCategoriesWithViews; const buildOption = (view: (typeof viewsWithoutCategory)[number]) => { const isSelected = currentView?.id === view.id; const option = buildViewOption({ view, viewsState, suffix: isRedesign && isSelected ? ( ) : undefined, }); return option; }; return [ ...viewsWithoutCategory.map(buildOption), ...categories.flatMap((category) => [ ...(getCategoryName({ category, viewsState }) ? [buildCategoryOption({ category, viewsState })] : []), ...category.views.map(buildOption), ]), ]; }, [ filteredCategoriesWithViews, container.initTask.status, currentView?.id, isRedesign, ]); const { options, fixedFooter } = showLoader ? { options: [], fixedFooter: ( ), } : { options: viewsOptions, fixedFooter: isRedesign ? ( ) : undefined, }; const { table: { collection: mainCollection }, } = state; const shouldShowTotal = showTotal && (mainCollection.totalStatus === 'success' || mainCollection.showRefreshLoader); return ( localSearchState.setInputValueAndScheduleSearch(e.target.value) } clearInput={() => localSearchState.clearSearch()} dropdownWidth={isRedesign ? '264px' : dropdownWidth} popoverProps={{ placement: 'bottom-start' }} {...dropdownProps} onDrillIn={ isRedesign ? (option) => { if (option.id === currentView?.id) { manageViewRef.current?.focusTrigger(); } } : undefined } onInputClicked={(e) => { state.viewsStateBIReporter.clickOnViewDropdownBI(); dropdownProps?.onInputClicked?.(e); }} fixedFooter={fixedFooter} border={isBmHarmonyTheme ? 'standard' : 'round'} selectedId={currentView?.id} selectedLabel={ currentView ? getViewName({ view: currentView, viewsState }) : undefined } textOverflow="ellipsis" prefix={
viewsState.autoCompleteReadonlyState.focusInput()} style={{ cursor: 'pointer', display: 'inline-flex' }} >
} suffix={ shouldShowTotal ? (
viewsState.autoCompleteReadonlyState.focusInput() } style={{ cursor: 'pointer', display: 'inline-flex' }} > ({state.table.collection.result.total})
) : undefined } className={st(classes.root)} dataHook="collection-views-dropdown" onSelect={(option) => { viewsState.events.emit('beforeSelectView'); const id = option.id.toString(); const view = viewsState.getView(id); if (view) { onSelect?.(view); viewsState.selectView({ id }); viewsState.events.emit('afterSelectView', { view }); } localSearchState.clearSearch(); }} options={options} placeholder={t('cairo.viewDropdown.unsavedView.status')} />
{!isRedesign && ( )}
); } export const ViewsDropdown = observer(_ViewsDropdown);