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 { TableState } from '../state'; import type { FieldsSourceProvider } from '../components/fieldsSourceProvider'; import { useTableCollection as useOpenTableCollection, type TableCollectionConfig, } from '../hooks/useTableCollection'; import { SchemaState } from '../components/SchemaSource/SchemaState'; import { schemaCollectionConfig, type SchemaCollectionOptions, } from './schemaCollectionOptions'; import { attachesItself } from './usePlatformizedSource'; /** A table collection driven by a source: the source supplies the query and * the fields area, and both attach when the state is created. */ export function useTableCollection< T, F extends FiltersMap = OptionalFiltersMap, >( source: SchemaSource, options?: SchemaCollectionOptions, ): TableState { const container = useWixPatternsContainer(); const [fieldsProvider] = useState(() => attachesItself(source) ? source : new SchemaState({ sourceState: new SourceState({ container, source }), }), ); const state = useOpenTableCollection( schemaCollectionConfig(source, options) as TableCollectionConfig< T, F >, ); useState(() => fieldsProvider.setOnTable(state.toolbar)); return state; }