import { computed, makeObservable } from 'mobx'; import { Field, FieldsSource, SourceState } from '@wix/bex-core'; import type { ToolbarCollectionState } from '../../state'; import { CollectionFieldsSourceState } from '../../state/CollectionFieldsSourceState'; import { isFilterableFieldType } from '../../state/buildFieldFilters'; import { schemaFieldsSource } from './schemaFieldsSource'; import { schemaRendering } from './schemaRendering'; export interface SchemaStateParams { sourceState: SourceState; } /** * The collection-page side of a source: turns the loaded schema into a * source-neutral FieldsSource for the table, owns the field-management object * and the add/edit field modal, and builds the table columns from the fields. * The page-neutral holding (load, fields, field mutations) lives in the * composed core `SourceState`. */ export class SchemaState> { readonly sourceState: SourceState; /** The read contract the collection spine consumes — a plain adapter whose * getters read through to `sourceState`, so MobX tracks through it. Built * once: the spine keeps this exact instance. */ readonly fieldsSource: FieldsSource; toolbar?: ToolbarCollectionState = undefined; constructor(params: SchemaStateParams) { this.sourceState = params.sourceState; this.fieldsSource = schemaFieldsSource(this); makeObservable(this, { filterableFields: computed, }); } /** Fields whose type maps to an auto filter in the toolbar. */ get filterableFields(): Field[] { return this.sourceState.fields.filter((f) => isFilterableFieldType(f.type)); } /** Attaches this source to a table's toolbar via the shared wrapper. */ setOnTable(toolbar: ToolbarCollectionState) { this.toolbar = toolbar; CollectionFieldsSourceState.setOnTable(toolbar, { fieldsSource: this.fieldsSource, ...schemaRendering(this), }); } }