import { useState } from 'react'; import { SourceState } from '@wix/bex-core'; import type { FiltersMap, OptionalFiltersMap, SchemaSource, } from '@wix/bex-core'; import { useWixPatternsContainer } from '@wix/bex-core/react'; import type { GridState } from '../state'; import type { FieldsSourceProvider } from '../components/fieldsSourceProvider'; import { useGridCollection as useOpenGridCollection } from '../hooks/useGridCollection'; import { SchemaState } from '../components/SchemaSource/SchemaState'; import { schemaCollectionConfig, type SchemaCollectionOptions, } from './schemaCollectionOptions'; import { attachesItself } from './usePlatformizedSource'; /** A grid collection driven by a source — the source's filters and field * management attach to the toolbar even though a grid has no columns. */ export function useGridCollection( source: SchemaSource, options?: SchemaCollectionOptions, ): GridState { const container = useWixPatternsContainer(); const [fieldsProvider] = useState(() => attachesItself(source) ? source : new SchemaState({ sourceState: new SourceState({ container, source }), }), ); const state = useOpenGridCollection( schemaCollectionConfig(source, options), ); useState(() => fieldsProvider.setOnTable(state.toolbar)); return state; }