@dxos/react-ui-table 0.8.4-main.5ea62a8 → 0.8.4-main.60689f5b1c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/chunk-4URYOFDL.mjs +69 -0
  3. package/dist/lib/browser/chunk-4URYOFDL.mjs.map +7 -0
  4. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  5. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  6. package/dist/lib/browser/chunk-YDXPC7HA.mjs +1695 -0
  7. package/dist/lib/browser/chunk-YDXPC7HA.mjs.map +7 -0
  8. package/dist/lib/browser/index.mjs +1139 -1198
  9. package/dist/lib/browser/index.mjs.map +4 -4
  10. package/dist/lib/browser/meta.json +1 -1
  11. package/dist/lib/browser/testing/index.mjs +35 -42
  12. package/dist/lib/browser/testing/index.mjs.map +3 -3
  13. package/dist/lib/browser/translations.mjs +36 -0
  14. package/dist/lib/browser/translations.mjs.map +7 -0
  15. package/dist/lib/browser/types/index.mjs +4 -3
  16. package/dist/lib/node-esm/chunk-2AN3QQ3D.mjs +1697 -0
  17. package/dist/lib/node-esm/chunk-2AN3QQ3D.mjs.map +7 -0
  18. package/dist/lib/node-esm/chunk-B2SZYAEC.mjs +70 -0
  19. package/dist/lib/node-esm/chunk-B2SZYAEC.mjs.map +7 -0
  20. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  21. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  22. package/dist/lib/node-esm/index.mjs +1139 -1198
  23. package/dist/lib/node-esm/index.mjs.map +4 -4
  24. package/dist/lib/node-esm/meta.json +1 -1
  25. package/dist/lib/node-esm/testing/index.mjs +35 -42
  26. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  27. package/dist/lib/node-esm/translations.mjs +37 -0
  28. package/dist/lib/node-esm/translations.mjs.map +7 -0
  29. package/dist/lib/node-esm/types/index.mjs +4 -3
  30. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts +1 -2
  31. package/dist/types/src/components/Table/ColumnActionsMenu.d.ts.map +1 -1
  32. package/dist/types/src/components/Table/ColumnSettings.d.ts +2 -2
  33. package/dist/types/src/components/Table/ColumnSettings.d.ts.map +1 -1
  34. package/dist/types/src/components/Table/DynamicTable.d.ts +6 -7
  35. package/dist/types/src/components/Table/DynamicTable.d.ts.map +1 -1
  36. package/dist/types/src/components/Table/DynamicTable.stories.d.ts +23 -39
  37. package/dist/types/src/components/Table/DynamicTable.stories.d.ts.map +1 -1
  38. package/dist/types/src/components/Table/Relations.stories.d.ts +96 -29
  39. package/dist/types/src/components/Table/Relations.stories.d.ts.map +1 -1
  40. package/dist/types/src/components/Table/RowActionsMenu.d.ts.map +1 -1
  41. package/dist/types/src/components/Table/Table.d.ts +20 -18
  42. package/dist/types/src/components/Table/Table.d.ts.map +1 -1
  43. package/dist/types/src/components/Table/Table.stories.d.ts +96 -31
  44. package/dist/types/src/components/Table/Table.stories.d.ts.map +1 -1
  45. package/dist/types/src/components/Table/TableContent.d.ts +18 -0
  46. package/dist/types/src/components/Table/TableContent.d.ts.map +1 -0
  47. package/dist/types/src/components/Table/TableToolbar.d.ts +21 -0
  48. package/dist/types/src/components/Table/TableToolbar.d.ts.map +1 -0
  49. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts +1 -1
  50. package/dist/types/src/components/TableCellEditor/CellValidationMessage.d.ts.map +1 -1
  51. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts +12 -12
  52. package/dist/types/src/components/TableCellEditor/FormCellEditor.d.ts.map +1 -1
  53. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts +7 -7
  54. package/dist/types/src/components/TableCellEditor/TableCellEditor.d.ts.map +1 -1
  55. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts +25 -28
  56. package/dist/types/src/components/TableCellEditor/TableCellEditor.stories.d.ts.map +1 -1
  57. package/dist/types/src/components/TableCellEditor/index.d.ts +1 -0
  58. package/dist/types/src/components/TableCellEditor/index.d.ts.map +1 -1
  59. package/dist/types/src/components/index.d.ts +0 -1
  60. package/dist/types/src/components/index.d.ts.map +1 -1
  61. package/dist/types/src/components/testing.d.ts +23 -16
  62. package/dist/types/src/components/testing.d.ts.map +1 -1
  63. package/dist/types/src/hooks/index.d.ts +2 -1
  64. package/dist/types/src/hooks/index.d.ts.map +1 -1
  65. package/dist/types/src/hooks/useAddRow.d.ts +5 -6
  66. package/dist/types/src/hooks/useAddRow.d.ts.map +1 -1
  67. package/dist/types/src/hooks/useProjectionModel.d.ts +6 -0
  68. package/dist/types/src/hooks/useProjectionModel.d.ts.map +1 -0
  69. package/dist/types/src/hooks/useTableModel.d.ts +9 -9
  70. package/dist/types/src/hooks/useTableModel.d.ts.map +1 -1
  71. package/dist/types/src/index.d.ts +0 -2
  72. package/dist/types/src/index.d.ts.map +1 -1
  73. package/dist/types/src/model/modal-controller.d.ts +5 -8
  74. package/dist/types/src/model/modal-controller.d.ts.map +1 -1
  75. package/dist/types/src/model/selection-model.d.ts +18 -9
  76. package/dist/types/src/model/selection-model.d.ts.map +1 -1
  77. package/dist/types/src/model/table-model.d.ts +105 -26
  78. package/dist/types/src/model/table-model.d.ts.map +1 -1
  79. package/dist/types/src/model/table-presentation.d.ts +3 -1
  80. package/dist/types/src/model/table-presentation.d.ts.map +1 -1
  81. package/dist/types/src/playwright/TableManager.d.ts +2 -1
  82. package/dist/types/src/playwright/TableManager.d.ts.map +1 -1
  83. package/dist/types/src/playwright/playwright.config.d.ts.map +1 -1
  84. package/dist/types/src/testing/useTestTableModel.d.ts +18 -29
  85. package/dist/types/src/testing/useTestTableModel.d.ts.map +1 -1
  86. package/dist/types/src/translations.d.ts +21 -23
  87. package/dist/types/src/translations.d.ts.map +1 -1
  88. package/dist/types/src/types/Table.d.ts +25 -0
  89. package/dist/types/src/types/Table.d.ts.map +1 -0
  90. package/dist/types/src/types/index.d.ts +1 -1
  91. package/dist/types/src/types/index.d.ts.map +1 -1
  92. package/dist/types/src/util/dynamic-table.d.ts +14 -12
  93. package/dist/types/src/util/dynamic-table.d.ts.map +1 -1
  94. package/dist/types/src/util/dynamic-table.test.d.ts +2 -0
  95. package/dist/types/src/util/dynamic-table.test.d.ts.map +1 -0
  96. package/dist/types/src/util/index.d.ts +2 -0
  97. package/dist/types/src/util/index.d.ts.map +1 -1
  98. package/dist/types/src/util/query-ast.d.ts +7 -0
  99. package/dist/types/src/util/query-ast.d.ts.map +1 -0
  100. package/dist/types/src/util/schema.d.ts +1 -1
  101. package/dist/types/src/util/schema.d.ts.map +1 -1
  102. package/dist/types/src/util/signals.d.ts.map +1 -1
  103. package/dist/types/src/util/sort.d.ts +12 -0
  104. package/dist/types/src/util/sort.d.ts.map +1 -0
  105. package/dist/types/src/util/table-buttons.d.ts +20 -20
  106. package/dist/types/src/util/table-buttons.d.ts.map +1 -1
  107. package/dist/types/src/util/table-controls.d.ts +4 -4
  108. package/dist/types/src/util/table-controls.d.ts.map +1 -1
  109. package/dist/types/src/util/tag.d.ts.map +1 -1
  110. package/dist/types/tsconfig.tsbuildinfo +1 -1
  111. package/package.json +69 -78
  112. package/src/components/Table/ColumnActionsMenu.tsx +17 -12
  113. package/src/components/Table/ColumnSettings.tsx +8 -9
  114. package/src/components/Table/DynamicTable.stories.tsx +35 -34
  115. package/src/components/Table/DynamicTable.tsx +32 -36
  116. package/src/components/Table/Relations.stories.tsx +195 -48
  117. package/src/components/Table/RowActionsMenu.tsx +27 -17
  118. package/src/components/Table/Table.stories.tsx +145 -129
  119. package/src/components/Table/Table.tsx +43 -403
  120. package/src/components/Table/TableContent.tsx +425 -0
  121. package/src/components/Table/TableToolbar.tsx +111 -0
  122. package/src/components/TableCellEditor/CellValidationMessage.tsx +5 -6
  123. package/src/components/TableCellEditor/FormCellEditor.tsx +125 -131
  124. package/src/components/TableCellEditor/TableCellEditor.stories.tsx +32 -56
  125. package/src/components/TableCellEditor/TableCellEditor.tsx +47 -140
  126. package/src/components/TableCellEditor/index.ts +1 -0
  127. package/src/components/index.ts +0 -1
  128. package/src/components/testing.ts +44 -18
  129. package/src/hooks/index.ts +2 -1
  130. package/src/hooks/useAddRow.ts +8 -10
  131. package/src/hooks/useProjectionModel.ts +39 -0
  132. package/src/hooks/useTableModel.ts +36 -34
  133. package/src/index.ts +0 -4
  134. package/src/model/modal-controller.ts +23 -40
  135. package/src/model/selection-model.ts +75 -44
  136. package/src/model/table-model.test.ts +93 -56
  137. package/src/model/table-model.ts +435 -163
  138. package/src/model/table-presentation.test.ts +67 -56
  139. package/src/model/table-presentation.ts +45 -42
  140. package/src/playwright/TableManager.ts +13 -6
  141. package/src/playwright/playwright.config.ts +1 -1
  142. package/src/playwright/smoke.spec.ts +59 -13
  143. package/src/testing/useTestTableModel.ts +49 -39
  144. package/src/translations.ts +20 -22
  145. package/src/types/Table.ts +78 -0
  146. package/src/types/index.ts +1 -1
  147. package/src/util/dynamic-table.test.ts +70 -0
  148. package/src/util/dynamic-table.ts +42 -30
  149. package/src/util/index.ts +2 -0
  150. package/src/util/query-ast.ts +22 -0
  151. package/src/util/schema.ts +4 -4
  152. package/src/util/sort.ts +90 -0
  153. package/src/util/table-buttons.ts +1 -1
  154. package/src/util/table-controls.ts +2 -2
  155. package/dist/lib/browser/chunk-63UUF4IG.mjs +0 -66
  156. package/dist/lib/browser/chunk-63UUF4IG.mjs.map +0 -7
  157. package/dist/lib/browser/chunk-RQYXNWWN.mjs +0 -1822
  158. package/dist/lib/browser/chunk-RQYXNWWN.mjs.map +0 -7
  159. package/dist/lib/node-esm/chunk-K734DJB7.mjs +0 -67
  160. package/dist/lib/node-esm/chunk-K734DJB7.mjs.map +0 -7
  161. package/dist/lib/node-esm/chunk-QNDUGIS7.mjs +0 -1823
  162. package/dist/lib/node-esm/chunk-QNDUGIS7.mjs.map +0 -7
  163. package/dist/types/src/components/Table/CreateRefPanel.d.ts +0 -9
  164. package/dist/types/src/components/Table/CreateRefPanel.d.ts.map +0 -1
  165. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts +0 -21
  166. package/dist/types/src/components/TableToolbar/TableToolbar.d.ts.map +0 -1
  167. package/dist/types/src/components/TableToolbar/index.d.ts +0 -2
  168. package/dist/types/src/components/TableToolbar/index.d.ts.map +0 -1
  169. package/dist/types/src/model/table-sorting.d.ts +0 -53
  170. package/dist/types/src/model/table-sorting.d.ts.map +0 -1
  171. package/dist/types/src/types/schema.d.ts +0 -28
  172. package/dist/types/src/types/schema.d.ts.map +0 -1
  173. package/src/components/Table/CreateRefPanel.tsx +0 -103
  174. package/src/components/TableToolbar/TableToolbar.tsx +0 -102
  175. package/src/components/TableToolbar/index.ts +0 -5
  176. package/src/model/table-sorting.ts +0 -216
  177. package/src/types/schema.ts +0 -73
@@ -1,102 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { Rx } from '@effect-rx/rx-react';
6
- import React, { useMemo } from 'react';
7
-
8
- import { live } from '@dxos/live-object';
9
- import { type ThemedClassName } from '@dxos/react-ui';
10
- import {
11
- type ActionGraphEdges,
12
- type ActionGraphNodes,
13
- type ActionGraphProps,
14
- type MenuAction,
15
- MenuProvider,
16
- ToolbarMenu,
17
- createGapSeparator,
18
- createMenuAction,
19
- useMenuActions,
20
- } from '@dxos/react-ui-menu';
21
- import { rxFromSignal } from '@dxos/react-ui-menu';
22
-
23
- import { translationKey } from '../../translations';
24
-
25
- export type TableToolbarActionProperties = { type: TableToolbarActionType };
26
-
27
- export type TableToolbarAction = MenuAction<TableToolbarActionProperties>;
28
-
29
- export type TableToolbarActionType = 'add-row' | 'comment' | 'save-view';
30
-
31
- type TableToolbarState = Partial<{ viewDirty: boolean }>;
32
-
33
- export type TableToolbarProps = ThemedClassName<
34
- TableToolbarState & {
35
- onAdd: () => void;
36
- onSave: () => void;
37
- attendableId?: string;
38
- customActions?: Rx.Rx<ActionGraphProps>;
39
- }
40
- >;
41
-
42
- const createTableToolbarActions = ({
43
- state,
44
- onAdd,
45
- onSave,
46
- customActions,
47
- }: {
48
- state: TableToolbarState;
49
- onAdd: () => void;
50
- onSave: () => void;
51
- customActions?: Rx.Rx<ActionGraphProps>;
52
- }) =>
53
- Rx.make((get) => {
54
- const add = createMenuAction<TableToolbarActionProperties>('add-row', onAdd, {
55
- type: 'add-row' as const,
56
- icon: 'ph--plus--regular',
57
- label: ['add row', { ns: translationKey }],
58
- testId: 'table.toolbar.add-row',
59
- });
60
- const save = createMenuAction<TableToolbarActionProperties>('save-view', onSave, {
61
- type: 'save-view' as const,
62
- icon: 'ph--floppy-disk--regular',
63
- label: ['save view label', { ns: translationKey }],
64
- testId: 'table.toolbar.save-view',
65
- iconOnly: false,
66
- hidden: get(rxFromSignal(() => !state.viewDirty)),
67
- });
68
- const gap = createGapSeparator();
69
- const nodes: ActionGraphNodes = [add, save, ...gap.nodes];
70
- const edges: ActionGraphEdges = nodes.map(({ id: target }) => ({ source: 'root', target }));
71
- if (customActions) {
72
- const custom = get(customActions);
73
- nodes.push(...custom.nodes);
74
- edges.push(...custom.edges);
75
- }
76
- return {
77
- nodes,
78
- edges,
79
- };
80
- });
81
-
82
- export const TableToolbar = ({
83
- classNames,
84
- viewDirty,
85
- attendableId,
86
- onAdd,
87
- onSave,
88
- customActions,
89
- }: TableToolbarProps) => {
90
- const state = useMemo(() => live<TableToolbarState>({ viewDirty }), []);
91
- const actionsCreator = useMemo(
92
- () => createTableToolbarActions({ state, onAdd, onSave, customActions }),
93
- [state, onAdd, onSave, customActions],
94
- );
95
- const menu = useMenuActions(actionsCreator);
96
-
97
- return (
98
- <MenuProvider {...menu} attendableId={attendableId}>
99
- <ToolbarMenu classNames={classNames} />
100
- </MenuProvider>
101
- );
102
- };
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- export * from './TableToolbar';
@@ -1,216 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { type ReadonlySignal, type Signal, computed, signal } from '@preact/signals-core';
6
- import orderBy from 'lodash.orderby';
7
-
8
- import { type FieldSortType, FormatEnum, type SortDirectionType, TypeEnum, getValue } from '@dxos/echo-schema';
9
- import { formatForDisplay } from '@dxos/react-ui-form';
10
- import type { DataType, FieldType, ProjectionModel, PropertyType } from '@dxos/schema';
11
-
12
- import { type TableRow } from './table-model';
13
-
14
- /**
15
- * Represents the local sort state.
16
- * - undefined: No local sort set, fallback to view sort.
17
- * - cleared: Sort explicitly cleared by user.
18
- * - active: Active local sort with sort configuration.
19
- */
20
- type LocalSort = { type: 'cleared' } | { type: 'active'; sort: FieldSortType } | undefined;
21
-
22
- /**
23
- * Manages table sorting functionality through display and data index mapping.
24
- *
25
- * When a table is sorted, rows visually reorder but we need to maintain references
26
- * to the original data positions. This is handled through two types of indices:
27
- *
28
- * - Display indices: The visual positions after sorting
29
- * - Data indices: The original positions in the source data
30
- *
31
- * The _displayToDataIndex map maintains these relationships, enabling lookups
32
- * between visual and source positions.
33
- *
34
- * Example mapping between display and data indices:
35
- *
36
- * Original Data: Sorted Display: Mapping: (Display → Data)
37
- * ┌──────────────┐ ┌──────────────┐ ┌────────┐
38
- * │ 0: "Banana" │ => │ 0: "Apple" │ => │ 0 => 1 │
39
- * │ 1: "Apple" │ => │ 1: "Banana" │ => │ 1 => 0 │
40
- * │ 2: "Cherry" │ => │ 2: "Cherry" │ => │ 2 => 2 │
41
- * └──────────────┘ └──────────────┘ └────────┘
42
- */
43
- export class TableSorting<T extends TableRow> {
44
- private readonly _displayToDataIndex = new Map<number, number>();
45
- private readonly _rows: Signal<T[]>;
46
- private readonly _localSort = signal<LocalSort>(undefined);
47
- private readonly _isDirty: ReadonlySignal<boolean>;
48
- public readonly sortedRows: ReadonlySignal<T[]>;
49
-
50
- constructor(
51
- rows: Signal<T[]>,
52
- private readonly _view: DataType.View,
53
- private readonly _projection: ProjectionModel,
54
- ) {
55
- this._rows = rows;
56
- this._isDirty = computed(() => {
57
- const local = this._localSort.value;
58
- const viewSort = this._view.query.sort?.[0];
59
- if (local?.type === 'cleared') {
60
- return viewSort !== undefined;
61
- }
62
- if (local?.type === 'active') {
63
- if (!viewSort) {
64
- return true;
65
- }
66
- return local.sort.fieldId !== viewSort.fieldId || local.sort.direction !== viewSort.direction;
67
- }
68
- return false;
69
- });
70
- this.sortedRows = this.initialiseSortedRows();
71
- }
72
-
73
- /**
74
- * @reactive
75
- * @returns Local sort if present, falls back to view sort.
76
- */
77
- public get sorting(): FieldSortType | undefined {
78
- const local = this._localSort.value;
79
- if (local?.type === 'cleared') {
80
- return undefined;
81
- }
82
- if (local?.type === 'active') {
83
- return local.sort;
84
- }
85
-
86
- return this._view.query.sort?.[0];
87
- }
88
-
89
- /**
90
- * @reactive
91
- * @returns Whether local sort differs from view sort.
92
- */
93
- public get isDirty(): boolean {
94
- return this._isDirty.value;
95
- }
96
-
97
- public getDataIndex(displayIndex: number): number {
98
- return this._displayToDataIndex.get(displayIndex) ?? displayIndex;
99
- }
100
-
101
- public setSort(fieldId: string, direction: SortDirectionType): void {
102
- this._localSort.value = {
103
- type: 'active',
104
- sort: { fieldId, direction },
105
- };
106
- }
107
-
108
- public toggleSort(fieldId: string): void {
109
- if (!this.sorting || this.sorting.fieldId !== fieldId) {
110
- return;
111
- }
112
-
113
- switch (this.sorting.direction) {
114
- case 'asc': {
115
- this.setSort(fieldId, 'desc');
116
- break;
117
- }
118
- case 'desc': {
119
- this.setSort(fieldId, 'asc');
120
- break;
121
- }
122
- }
123
- }
124
-
125
- public clearSort(): void {
126
- this._localSort.value = { type: 'cleared' };
127
- }
128
-
129
- public save(): void {
130
- if (this._localSort.value !== undefined) {
131
- if (this._localSort.value.type === 'active') {
132
- this._view.query.sort = [this._localSort.value.sort];
133
- } else {
134
- this._view.query.sort = [];
135
- }
136
- this._localSort.value = undefined;
137
- }
138
- }
139
-
140
- //
141
- // Initialisation.
142
- //
143
-
144
- private initialiseSortedRows(): ReadonlySignal<T[]> {
145
- return computed(() => {
146
- this._displayToDataIndex.clear();
147
- const sort = this.sorting;
148
- if (!sort) {
149
- return this._rows.value;
150
- }
151
-
152
- const field = this._projection.fields.find((f) => f.id === sort.fieldId);
153
- if (!field) {
154
- return this._rows.value;
155
- }
156
-
157
- const { props } = this._projection.getFieldProjection(field.id);
158
-
159
- const dataWithIndices = this._rows.value.map((item, index) => {
160
- const sortValue = this.getSortValue(props, field, item);
161
- const isEmpty = !(props.type === TypeEnum.Boolean) && (sortValue == null || sortValue === '');
162
-
163
- return { item, index, value: sortValue, isEmpty };
164
- });
165
-
166
- const ordered = orderBy(dataWithIndices, ['isEmpty', 'value'], ['asc', sort.direction]);
167
-
168
- ordered.forEach(({ index: dataIndex }, displayIndex) => {
169
- if (displayIndex !== dataIndex) {
170
- this._displayToDataIndex.set(displayIndex, dataIndex);
171
- }
172
- });
173
-
174
- return ordered.map(({ item }) => item);
175
- });
176
- }
177
-
178
- private getSortValue(props: PropertyType, field: FieldType, item: T) {
179
- const { type, format } = props;
180
- const value = getValue(item, field.path);
181
-
182
- if (type === TypeEnum.Boolean) {
183
- return !!value; // Coerce null/undefined to false for booleans.
184
- }
185
- if (type === TypeEnum.Number) {
186
- return value;
187
- }
188
- if (value == null) {
189
- return value;
190
- }
191
-
192
- /**
193
- * For single select fields, the sort value is determined by the index of the option in the
194
- * options array. This ensures sort order matches the configured option order.
195
- */
196
- if (format === FormatEnum.SingleSelect) {
197
- const options = props.options;
198
- if (!options) {
199
- return undefined;
200
- }
201
-
202
- const option = options.find((option) => option.id === value);
203
- return option ? options.indexOf(option) : undefined;
204
- }
205
-
206
- // TODO(ZaymonFC): Should we format this returned value for display based on the referenced prop format?
207
- // Right now we're going to be sorting based on the raw values.
208
- // Maybe we need to recurse this function with the referenced prop format and field?
209
- if (format === FormatEnum.Ref && field.referencePath) {
210
- const refValue = getValue(value.target, field.referencePath);
211
- return refValue;
212
- }
213
-
214
- return formatForDisplay({ type, format, value });
215
- }
216
- }
@@ -1,73 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Schema } from 'effect';
6
- import { Match } from 'effect';
7
-
8
- import { Obj, Type } from '@dxos/echo';
9
- import { JsonPath, type JsonSchemaType, toEffectSchema } from '@dxos/echo-schema';
10
- import { ViewAnnotation } from '@dxos/echo-schema';
11
- import { type SimpleType } from '@dxos/effect';
12
- import { type CreateViewFromSpaceProps, type DataType, createViewFromSpace, getSchemaProperties } from '@dxos/schema';
13
-
14
- export const Table = Schema.Struct({
15
- sizes: Schema.Record({
16
- key: JsonPath,
17
- value: Schema.Number,
18
- }).pipe(Schema.mutable),
19
- }).pipe(
20
- Type.Obj({
21
- typename: 'dxos.org/type/Table',
22
- version: '0.1.0',
23
- }),
24
- ViewAnnotation.set(true),
25
- );
26
-
27
- export type Table = Schema.Schema.Type<typeof Table>;
28
-
29
- /**
30
- * Make a table view.
31
- */
32
- export const make = (props: Partial<Obj.MakeProps<typeof Table>> = {}) => Obj.make(Table, { sizes: {}, ...props });
33
-
34
- type MakeViewProps = Omit<CreateViewFromSpaceProps, 'presentation'> & {
35
- sizes?: Record<string, number>;
36
- };
37
-
38
- export const makeView = async ({
39
- sizes,
40
- ...props
41
- }: MakeViewProps): Promise<{
42
- jsonSchema: JsonSchemaType;
43
- view: DataType.View;
44
- schema: ReturnType<typeof toEffectSchema>;
45
- }> => {
46
- const table = Obj.make(Table, { sizes: {} });
47
- const { jsonSchema, view } = await createViewFromSpace({ ...props, presentation: table });
48
-
49
- // Preset sizes.
50
- const schema = toEffectSchema(jsonSchema);
51
- const shouldIncludeId = props.fields?.find((field) => field === 'id') !== undefined;
52
- const properties = getSchemaProperties(schema.ast, {}, shouldIncludeId);
53
- for (const property of properties) {
54
- if (sizes?.[property.name]) {
55
- table.sizes[property.name as JsonPath] = sizes[property.name];
56
- continue;
57
- }
58
-
59
- Match.type<SimpleType>().pipe(
60
- Match.when('boolean', () => {
61
- table.sizes[property.name as JsonPath] = 100;
62
- }),
63
- Match.when('number', () => {
64
- table.sizes[property.name as JsonPath] = 100;
65
- }),
66
- Match.orElse(() => {
67
- // Noop.
68
- }),
69
- )(property.type);
70
- }
71
-
72
- return { jsonSchema, schema, view };
73
- };