'use client' import { useWindowInfo } from '@faceless-ui/window-info' import React, { Fragment, useEffect, useId, useRef, useState } from 'react' import type { ListControlsProps } from './types.js' import { Popup, PopupList } from '../../elements/Popup/index.js' import { ChevronIcon } from '../../icons/Chevron/index.js' import { Dots } from '../../icons/Dots/index.js' import { useListQuery } from '../../providers/ListQuery/index.js' import { useTranslation } from '../../providers/Translation/index.js' import { Button } from '../Button/index.js' import { ListColumnSelectionButton } from '../ListColumnSelectionButton/index.js' import { ListGroupByButton } from '../ListGroupByButton/index.js' import { ListEmptyTrashButton } from '../ListHeader/TitleActions/index.js' import { ListWhereBuilder } from '../ListWhereBuilder/index.js' import { QueryPresetBar } from '../QueryPresets/QueryPresetBar/index.js' import { ListSearchFilter } from '../Search/ListSearchFilter/index.js' import './index.css' const baseClass = 'list-controls' /** * The ListControls component is used to render the controls (search, filter, where) * for a collection's list view. You can find those directly above the table which lists * the collection's documents. */ export const ListControls: React.FC = (props) => { const { beforeActions, collectionConfig, collectionSlug, disableQueryPresets, enableColumns = true, enableFilters = true, enableSort = false, hasCreatePermission, hasDeletePermission, isWhereOpen: isWhereOpenFromProps, layoutToggle, listMenuItems, newDocumentURL, onWhereToggle, queryPreset: activePreset, queryPresetPermissions, renderedFilters, resolvedFilterOptions, viewType, wherePanelID: wherePanelIDFromProps, } = props const instanceID = useId() const wherePanelID = wherePanelIDFromProps ?? `${baseClass}-where-${instanceID}` const isControlled = typeof onWhereToggle === 'function' const { handleSearchChange, hasActiveFilters, query, setSearchInput } = useListQuery() const { t } = useTranslation() const { breakpoints: { s: smallBreak }, } = useWindowInfo() // Simplified placeholder - just "Search" instead of "Search by X, Y, Z" const searchLabelTranslated = t('general:searchBy', { label: '' }).split(' ')[0] const hasWhereParam = useRef(Boolean(query?.where)) const [visibleDrawer, setVisibleDrawer] = useState<'columns' | 'sort' | 'where'>( hasActiveFilters ? 'where' : undefined, ) useEffect(() => { if (hasWhereParam.current && !query?.where) { hasWhereParam.current = false } else if (query?.where) { hasWhereParam.current = true } }, [setVisibleDrawer, query?.where]) const isWhereOpen = isControlled ? Boolean(isWhereOpenFromProps) : visibleDrawer === 'where' const handleWhereToggle = isControlled ? onWhereToggle : () => setVisibleDrawer(visibleDrawer !== 'where' ? 'where' : undefined) return (
{collectionConfig?.enableQueryPresets && !disableQueryPresets && ( )}
{!smallBreak && beforeActions} {enableFilters && ( )} {enableColumns && } {enableSort && ( )} {layoutToggle} {hasCreatePermission && newDocumentURL && ( )} {hasDeletePermission && collectionConfig.trash && viewType === 'trash' && ( )} {listMenuItems && Array.isArray(listMenuItems) && listMenuItems.length > 0 && ( } className={`${baseClass}__popup`} horizontalAlign="right" id="list-menu" popupType="menu" verticalAlign="bottom" > {listMenuItems.map((item, i) => ( {item} ))} )}
{!isControlled && isWhereOpen && (
setVisibleDrawer(undefined)} renderedFilters={renderedFilters} resolvedFilterOptions={resolvedFilterOptions} />
)}
) }