Fix types

This commit is contained in:
Adria Navarro 2024-12-20 13:58:56 +01:00
parent acc9da3876
commit fc76f573aa
2 changed files with 28 additions and 18 deletions

View File

@ -9,7 +9,7 @@ interface ColumnStore {
interface DerivedColumnStore { interface DerivedColumnStore {
tableColumns: Readable<Column[]> tableColumns: Readable<Column[]>
displayColumn: Readable<Column> displayColumn: Readable<Column | undefined>
columnLookupMap: Readable<Record<string, Column>> columnLookupMap: Readable<Record<string, Column>>
visibleColumns: Readable<Column[]> visibleColumns: Readable<Column[]>
scrollableColumns: Readable<Column[]> scrollableColumns: Readable<Column[]>
@ -37,7 +37,7 @@ type Column = FieldSchema & {
} }
export const createStores = (): ColumnStore => { export const createStores = (): ColumnStore => {
const columns = writable([]) const columns = writable<Column[]>([])
// Enrich columns with metadata about their display position // Enrich columns with metadata about their display position
const enrichedColumns = derived(columns, $columns => { const enrichedColumns = derived(columns, $columns => {
@ -51,7 +51,7 @@ export const createStores = (): ColumnStore => {
} }
if (col.visible) { if (col.visible) {
idx++ idx++
offset += col.width offset += col.width ?? 0
} }
return enriched return enriched
}) })
@ -70,7 +70,7 @@ export const deriveStores = (context: StoreContext): DerivedColumnStore => {
// Derive a lookup map for all columns by name // Derive a lookup map for all columns by name
const columnLookupMap = derived(columns, $columns => { const columnLookupMap = derived(columns, $columns => {
let map = {} let map: Record<string, Column> = {}
$columns.forEach(column => { $columns.forEach(column => {
map[column.name] = column map[column.name] = column
}) })
@ -164,7 +164,7 @@ export const initialise = (context: StoreContext) => {
const { definition, columns, displayColumn, enrichedSchema } = context const { definition, columns, displayColumn, enrichedSchema } = context
// Merge new schema fields with existing schema in order to preserve widths // Merge new schema fields with existing schema in order to preserve widths
const processColumns = $enrichedSchema => { const processColumns = ($enrichedSchema: any) => {
if (!$enrichedSchema) { if (!$enrichedSchema) {
columns.set([]) columns.set([])
return return
@ -174,7 +174,7 @@ export const initialise = (context: StoreContext) => {
const $displayColumn = get(displayColumn) const $displayColumn = get(displayColumn)
// Find primary display // Find primary display
let primaryDisplay let primaryDisplay: string
const candidatePD = $definition.primaryDisplay || $displayColumn?.name const candidatePD = $definition.primaryDisplay || $displayColumn?.name
if (candidatePD && $enrichedSchema[candidatePD]) { if (candidatePD && $enrichedSchema[candidatePD]) {
primaryDisplay = candidatePD primaryDisplay = candidatePD

View File

@ -50,33 +50,43 @@ const DependencyOrderedStores = [
Cache, Cache,
] ]
export type Store = Columns.Store & { export interface BaseStore {}
// TODO while typing the rest of stores
datasource: any
definition: Writable<any>
displayColumn: Writable<any>
enrichedSchema: any
}
export const attachStores = (context): Store => { export type Store = BaseStore &
Columns.Store & {
// TODO while typing the rest of stores
datasource: any
definition: Writable<any>
enrichedSchema: any
}
export const attachStores = (context: Store): Store => {
// Atomic store creation // Atomic store creation
for (let store of DependencyOrderedStores) { for (let store of DependencyOrderedStores) {
context = { ...context, ...store.createStores?.(context) } if ("createStores" in store) {
context = { ...context, ...store.createStores?.(context) }
}
} }
// Derived store creation // Derived store creation
for (let store of DependencyOrderedStores) { for (let store of DependencyOrderedStores) {
context = { ...context, ...store.deriveStores?.(context) } if ("deriveStores" in store) {
context = { ...context, ...store.deriveStores?.(context) }
}
} }
// Action creation // Action creation
for (let store of DependencyOrderedStores) { for (let store of DependencyOrderedStores) {
context = { ...context, ...store.createActions?.(context) } if ("createActions" in store) {
context = { ...context, ...store.createActions?.(context) }
}
} }
// Initialise any store logic // Initialise any store logic
for (let store of DependencyOrderedStores) { for (let store of DependencyOrderedStores) {
store.initialise?.(context) if ("initialise" in store) {
store.initialise?.(context)
}
} }
return context return context