@macrostrat/data-sheet 3.1.0 → 4.0.0-dev2

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 (184) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/actions/builtin.cjs +56 -0
  3. package/dist/actions/builtin.cjs.map +1 -0
  4. package/dist/actions/builtin.d.ts +15 -0
  5. package/dist/actions/builtin.js +56 -0
  6. package/dist/actions/builtin.js.map +1 -0
  7. package/dist/actions/clipboard.cjs +250 -0
  8. package/dist/actions/clipboard.cjs.map +1 -0
  9. package/dist/actions/clipboard.d.ts +42 -0
  10. package/dist/actions/clipboard.js +250 -0
  11. package/dist/actions/clipboard.js.map +1 -0
  12. package/dist/actions/filters.cjs +123 -0
  13. package/dist/actions/filters.cjs.map +1 -0
  14. package/dist/actions/filters.d.ts +6 -0
  15. package/dist/actions/filters.js +123 -0
  16. package/dist/actions/filters.js.map +1 -0
  17. package/dist/actions/index.d.ts +6 -0
  18. package/dist/actions/selection.cjs +194 -0
  19. package/dist/actions/selection.cjs.map +1 -0
  20. package/dist/actions/selection.d.ts +40 -0
  21. package/dist/actions/selection.js +192 -0
  22. package/dist/actions/selection.js.map +1 -0
  23. package/dist/actions/toolbar.cjs +171 -0
  24. package/dist/actions/toolbar.cjs.map +1 -0
  25. package/dist/actions/toolbar.d.ts +10 -0
  26. package/dist/actions/toolbar.js +171 -0
  27. package/dist/actions/toolbar.js.map +1 -0
  28. package/dist/actions/toolbar.module.sass.cjs +22 -0
  29. package/dist/actions/toolbar.module.sass.cjs.map +1 -0
  30. package/dist/actions/toolbar.module.sass.js +20 -0
  31. package/dist/actions/toolbar.module.sass.js.map +1 -0
  32. package/dist/actions/types.d.ts +143 -0
  33. package/dist/cell-renderer.cjs +44 -41
  34. package/dist/cell-renderer.cjs.map +1 -1
  35. package/dist/cell-renderer.d.ts +1 -6
  36. package/dist/cell-renderer.js +45 -42
  37. package/dist/cell-renderer.js.map +1 -1
  38. package/dist/components/actions.module.sass.cjs +3 -3
  39. package/dist/components/actions.module.sass.js +2 -2
  40. package/dist/components/colors.cjs +20 -16
  41. package/dist/components/colors.cjs.map +1 -1
  42. package/dist/components/colors.d.ts +3 -2
  43. package/dist/components/colors.js +20 -16
  44. package/dist/components/colors.js.map +1 -1
  45. package/dist/components/editor-popup.cjs +4 -3
  46. package/dist/components/editor-popup.cjs.map +1 -1
  47. package/dist/components/editor-popup.js +4 -3
  48. package/dist/components/editor-popup.js.map +1 -1
  49. package/dist/components/index.d.ts +1 -0
  50. package/dist/components/main.module.sass.cjs +12 -12
  51. package/dist/components/main.module.sass.js +11 -11
  52. package/dist/components/text-areas.cjs +23 -18
  53. package/dist/components/text-areas.cjs.map +1 -1
  54. package/dist/components/text-areas.d.ts +8 -4
  55. package/dist/components/text-areas.js +20 -13
  56. package/dist/components/text-areas.js.map +1 -1
  57. package/dist/components/view-info.cjs +18 -0
  58. package/dist/components/view-info.cjs.map +1 -0
  59. package/dist/components/view-info.d.ts +1 -0
  60. package/dist/components/view-info.js +18 -0
  61. package/dist/components/view-info.js.map +1 -0
  62. package/dist/core.cjs +184 -156
  63. package/dist/core.cjs.map +1 -1
  64. package/dist/core.d.ts +14 -1
  65. package/dist/core.js +187 -157
  66. package/dist/core.js.map +1 -1
  67. package/dist/data-sheet.css +181 -43
  68. package/dist/index.cjs +43 -13
  69. package/dist/index.cjs.map +1 -1
  70. package/dist/index.d.ts +1 -0
  71. package/dist/index.js +43 -10
  72. package/dist/index.js.map +1 -1
  73. package/dist/main.module.sass.cjs +15 -14
  74. package/dist/main.module.sass.cjs.map +1 -1
  75. package/dist/main.module.sass.js +14 -13
  76. package/dist/main.module.sass.js.map +1 -1
  77. package/dist/notifications.cjs +19 -0
  78. package/dist/notifications.cjs.map +1 -0
  79. package/dist/notifications.d.ts +9 -0
  80. package/dist/notifications.js +19 -0
  81. package/dist/notifications.js.map +1 -0
  82. package/dist/postgrest-table/data-loaders.cjs +181 -104
  83. package/dist/postgrest-table/data-loaders.cjs.map +1 -1
  84. package/dist/postgrest-table/data-loaders.d.ts +52 -28
  85. package/dist/postgrest-table/data-loaders.js +181 -104
  86. package/dist/postgrest-table/data-loaders.js.map +1 -1
  87. package/dist/postgrest-table/index.cjs +205 -79
  88. package/dist/postgrest-table/index.cjs.map +1 -1
  89. package/dist/postgrest-table/index.d.ts +13 -16
  90. package/dist/postgrest-table/index.js +186 -58
  91. package/dist/postgrest-table/index.js.map +1 -1
  92. package/dist/postgrest-table/lazy-loader-table.cjs +18 -17
  93. package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -1
  94. package/dist/postgrest-table/lazy-loader-table.d.ts +1 -1
  95. package/dist/postgrest-table/lazy-loader-table.js +17 -14
  96. package/dist/postgrest-table/lazy-loader-table.js.map +1 -1
  97. package/dist/postgrest-table/loading-utils.cjs +16 -0
  98. package/dist/postgrest-table/loading-utils.cjs.map +1 -0
  99. package/dist/postgrest-table/loading-utils.d.ts +13 -0
  100. package/dist/postgrest-table/loading-utils.js +16 -0
  101. package/dist/postgrest-table/loading-utils.js.map +1 -0
  102. package/dist/postgrest-table/main.module.sass.cjs +18 -13
  103. package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
  104. package/dist/postgrest-table/main.module.sass.js +18 -13
  105. package/dist/postgrest-table/main.module.sass.js.map +1 -1
  106. package/dist/provider.cjs +58 -26
  107. package/dist/provider.cjs.map +1 -1
  108. package/dist/provider.js +57 -23
  109. package/dist/provider.js.map +1 -1
  110. package/dist/renderers/column-header-cell-postgrest.cjs +216 -0
  111. package/dist/renderers/column-header-cell-postgrest.cjs.map +1 -0
  112. package/dist/renderers/column-header-cell-postgrest.d.ts +13 -0
  113. package/dist/renderers/column-header-cell-postgrest.js +216 -0
  114. package/dist/renderers/column-header-cell-postgrest.js.map +1 -0
  115. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +23 -0
  116. package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +1 -0
  117. package/dist/renderers/column-header-cell-postgrest.module.sass.js +22 -0
  118. package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +1 -0
  119. package/dist/renderers/column-header-cell.cjs +103 -0
  120. package/dist/renderers/column-header-cell.cjs.map +1 -0
  121. package/dist/renderers/column-header-cell.d.ts +30 -0
  122. package/dist/renderers/column-header-cell.js +101 -0
  123. package/dist/renderers/column-header-cell.js.map +1 -0
  124. package/dist/renderers/index.d.ts +2 -0
  125. package/dist/table-updates/data-parameter.d.ts +14 -0
  126. package/dist/table-updates/filter.d.ts +16 -0
  127. package/dist/table-updates/index.d.ts +19 -0
  128. package/dist/types.cjs +9 -0
  129. package/dist/types.cjs.map +1 -0
  130. package/dist/types.d.ts +58 -4
  131. package/dist/types.js +9 -0
  132. package/dist/types.js.map +1 -0
  133. package/dist/utils/column-spec.cjs +9 -1
  134. package/dist/utils/column-spec.cjs.map +1 -1
  135. package/dist/utils/column-spec.d.ts +21 -0
  136. package/dist/utils/column-spec.js +9 -1
  137. package/dist/utils/column-spec.js.map +1 -1
  138. package/dist/utils/key-handling.cjs +141 -52
  139. package/dist/utils/key-handling.cjs.map +1 -1
  140. package/dist/utils/key-handling.d.ts +2 -3
  141. package/dist/utils/key-handling.js +142 -53
  142. package/dist/utils/key-handling.js.map +1 -1
  143. package/dist/zustand-store.cjs +216 -51
  144. package/dist/zustand-store.cjs.map +1 -1
  145. package/dist/zustand-store.js +197 -32
  146. package/dist/zustand-store.js.map +1 -1
  147. package/package.json +5 -5
  148. package/src/actions/builtin.ts +65 -0
  149. package/src/actions/clipboard.ts +350 -0
  150. package/src/actions/filters.ts +150 -0
  151. package/src/actions/index.ts +6 -0
  152. package/src/actions/selection.ts +271 -0
  153. package/src/actions/toolbar.module.sass +30 -0
  154. package/src/actions/toolbar.ts +212 -0
  155. package/src/actions/types.ts +159 -0
  156. package/src/cell-renderer.ts +55 -51
  157. package/src/components/colors.ts +22 -19
  158. package/src/components/editor-popup.ts +3 -2
  159. package/src/components/index.ts +1 -0
  160. package/src/components/main.module.sass +1 -0
  161. package/src/components/text-areas.ts +34 -19
  162. package/src/components/view-info.ts +17 -0
  163. package/src/core.ts +243 -170
  164. package/src/index.ts +1 -0
  165. package/src/main.module.sass +14 -2
  166. package/src/notifications.ts +22 -0
  167. package/src/postgrest-table/data-loaders.ts +300 -201
  168. package/src/postgrest-table/index.ts +258 -84
  169. package/src/postgrest-table/lazy-loader-table.ts +18 -17
  170. package/src/postgrest-table/loading-utils.ts +49 -0
  171. package/src/postgrest-table/main.module.sass +16 -0
  172. package/src/provider.ts +63 -20
  173. package/src/renderers/column-header-cell-postgrest.module.sass +71 -0
  174. package/src/renderers/column-header-cell-postgrest.ts +282 -0
  175. package/src/renderers/column-header-cell.ts +388 -0
  176. package/src/renderers/index.ts +2 -0
  177. package/src/table-updates/data-parameter.ts +44 -0
  178. package/src/table-updates/filter.ts +124 -0
  179. package/src/table-updates/index.ts +239 -0
  180. package/src/types.ts +58 -3
  181. package/src/utils/column-spec.ts +47 -0
  182. package/src/utils/key-handling.ts +181 -72
  183. package/src/zustand-store.ts +241 -36
  184. package/dist/provider.d.ts +0 -15
package/src/provider.ts CHANGED
@@ -1,37 +1,48 @@
1
1
  import { SetStateAction, useEffect, useRef, useState } from "react";
2
2
  import h from "@macrostrat/hyper";
3
3
  import { createStore, StoreApi, useStore } from "zustand";
4
- import type { Region, Table } from "@blueprintjs/table";
4
+ import type { Table } from "@blueprintjs/table";
5
5
  import { generateColumnSpec } from "./utils";
6
6
  import { createScopedStore } from "@macrostrat/data-components";
7
7
  import {
8
8
  DataSheetProviderProps,
9
+ DataSheetState,
9
10
  DataSheetStore,
10
- VisibleCells,
11
11
  } from "./types.ts";
12
12
  import { createZustandStore } from "./zustand-store.ts";
13
13
  import { atomWithStore } from "jotai-zustand";
14
+ import { toasterAtom } from "./notifications.ts";
15
+ import { TableAction } from "./actions";
14
16
  import { atom } from "jotai";
15
17
 
16
18
  /** Create a Jotai scoped store */
17
- const scope = createScopedStore();
18
- const { useAtom, useAtomValue, useSetAtom } = scope;
19
- export { useAtom, useAtomValue, useSetAtom };
20
- export { atom };
19
+ export const ctx = createScopedStore();
21
20
 
22
21
  export const storeAPIAtom = atom<StoreApi<DataSheetStore<any>>>();
23
22
 
24
23
  const storeWrapperAtom = atom((get) => {
25
- return atomWithStore(get(storeAPIAtom));
24
+ const _storeAPIAtom = get(storeAPIAtom);
25
+ if (_storeAPIAtom == null) {
26
+ return undefined;
27
+ }
28
+ return atomWithStore(_storeAPIAtom);
26
29
  });
27
30
 
28
31
  /** This is the basis for all other atoms that manipulate the store. */
29
32
  export const storeAtom = atom(
30
33
  (get) => {
31
- return get(get(storeWrapperAtom));
34
+ const storeWrapper = get(storeWrapperAtom);
35
+ if (storeWrapper == null) {
36
+ return undefined;
37
+ }
38
+ return get(storeWrapper);
32
39
  },
33
40
  (get, set, action: SetStateAction<T>) => {
34
- return set(get(storeWrapperAtom), action);
41
+ const storeWrapper = get(storeWrapperAtom);
42
+ if (storeWrapper == null) {
43
+ throw new Error("Missing DataSheetProvider");
44
+ }
45
+ return set(storeWrapper, action);
35
46
  },
36
47
  );
37
48
 
@@ -47,16 +58,22 @@ const initializeStoreAtom = atom(
47
58
  },
48
59
  );
49
60
 
61
+ export const tableActionsAtom = atom<TableAction[]>([]);
62
+
50
63
  export function DataSheetProvider<T>(props: DataSheetProviderProps<T>) {
64
+ const { toaster, ...rest } = props;
51
65
  const [store] = useState(() => {
52
66
  return createStore<DataSheetStore<T>>(createZustandStore);
53
67
  });
54
68
  return h(
55
- scope.Provider,
69
+ ctx.Provider,
56
70
  {
57
- atoms: [[storeAPIAtom, store]],
71
+ atoms: [
72
+ [storeAPIAtom, store],
73
+ [toasterAtom, toaster],
74
+ ],
58
75
  },
59
- h(DataSheetProviderInner, props),
76
+ h(DataSheetProviderInner, rest),
60
77
  );
61
78
  }
62
79
 
@@ -69,14 +86,9 @@ export function DataSheetProviderInner<T>({
69
86
  enableColumnReordering,
70
87
  defaultColumnWidth = 150,
71
88
  }: DataSheetProviderProps<T>) {
72
- const visibleCellsRef = useRef<VisibleCells>({
73
- rowIndexStart: 0,
74
- rowIndexEnd: 0,
75
- });
76
-
77
89
  const tableRef = useRef<Table>(null);
78
90
 
79
- const initializeStore = scope.useSetAtom(initializeStoreAtom);
91
+ const initializeStore = ctx.useSet(initializeStoreAtom);
80
92
 
81
93
  // Not sure how required this initialization is
82
94
  useEffect(() => {
@@ -87,7 +99,6 @@ export function DataSheetProviderInner<T>({
87
99
  data,
88
100
  defaultColumnWidth,
89
101
  tableRef,
90
- visibleCellsRef,
91
102
  });
92
103
  }, [data, editable, columnSpec, columnSpecOptions, enableColumnReordering]);
93
104
 
@@ -95,7 +106,7 @@ export function DataSheetProviderInner<T>({
95
106
  }
96
107
 
97
108
  export function useStoreAPI<T>(): StoreApi<DataSheetStore<T>> {
98
- const store = scope.useAtomValue(storeAPIAtom);
109
+ const store = ctx.useValue(storeAPIAtom);
99
110
  if (!store) {
100
111
  throw new Error("Missing DataSheetProvider");
101
112
  }
@@ -108,3 +119,35 @@ export function useSelector<T = any, A = any>(
108
119
  const store = useStoreAPI<T>();
109
120
  return useStore(store, selector);
110
121
  }
122
+ /** Atoms for efficient sub-selection of state */
123
+
124
+ export const columnSpecAtom = atom((get) => get(storeAtom)?.columnSpec ?? []);
125
+
126
+ export const tableDataAtom = atom(
127
+ (get) => {
128
+ return get(storeAtom)?.data ?? [];
129
+ },
130
+ (get, set, newData: any[]) => {
131
+ set(storeAtom, (state: DataSheetState<any>): DataSheetState<any> => {
132
+ console.log("Updating table data", newData);
133
+ if (
134
+ state.data.length == 0 &&
135
+ newData.length > 0 &&
136
+ state.columnSpec.length == 0
137
+ ) {
138
+ console.log("Generating column spec from data");
139
+ /** We haven't yet generated the column spec, and we need to do so. TODO: we may be able to forestall this with loading state */
140
+ return {
141
+ ...state,
142
+ columnSpec: generateColumnSpec(newData, state.columnSpecOptions),
143
+ data: newData,
144
+ };
145
+ }
146
+
147
+ return {
148
+ ...state,
149
+ data: newData,
150
+ };
151
+ });
152
+ },
153
+ );
@@ -0,0 +1,71 @@
1
+ .column-header-cell
2
+ display: flex
3
+ align-items: center
4
+ width: 100%
5
+
6
+ .column-name
7
+ flex: 1
8
+ overflow: hidden
9
+ text-overflow: ellipsis
10
+ white-space: nowrap
11
+
12
+ .column-indicators
13
+ display: flex
14
+ align-items: center
15
+ gap: 2px
16
+ margin-left: 4px
17
+ flex-shrink: 0
18
+
19
+ :global(.bp6-icon)
20
+ opacity: 0.7
21
+ color: var(--text-color)
22
+
23
+ .sort-indicator
24
+ :global(.bp6-icon)
25
+ color: var(--intent-primary, #2965cc)
26
+ opacity: 1
27
+
28
+ .filter-indicator
29
+ :global(.bp6-icon)
30
+ color: var(--intent-warning, #d99e0b)
31
+ opacity: 1
32
+
33
+ .column-actions-popover
34
+ padding: 12px
35
+ min-width: 220px
36
+
37
+ .popover-section
38
+ margin-bottom: 12px
39
+ &:last-child
40
+ margin-bottom: 0
41
+
42
+ .popover-section-title
43
+ font-weight: 600
44
+ font-size: 11px
45
+ text-transform: uppercase
46
+ letter-spacing: 0.5px
47
+ color: var(--secondary-color, #5c7080)
48
+ margin-bottom: 6px
49
+
50
+ .sort-buttons
51
+ display: flex
52
+ gap: 4px
53
+
54
+ .filter-row
55
+ display: flex
56
+ gap: 4px
57
+ align-items: center
58
+
59
+ .filter-operator-select
60
+ flex-shrink: 0
61
+ width: 80px
62
+
63
+ .filter-value-input
64
+ flex: 1
65
+
66
+ .clear-section
67
+ display: flex
68
+ justify-content: flex-end
69
+ padding-top: 4px
70
+ border-top: 1px solid var(--divider-color, rgba(16, 22, 26, 0.15))
71
+
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Column header with server-side sort and filter controls.
3
+ *
4
+ * Uses Blueprint's ColumnHeaderCell `menuRenderer` prop to provide a
5
+ * dropdown menu with sort direction toggles and a filter input.
6
+ * The `nameRenderer` prop shows the column name plus visual indicators
7
+ * for any active sort or filter.
8
+ */
9
+ import { ColumnHeaderCell, ColumnHeaderCellProps } from "@blueprintjs/table";
10
+ import {
11
+ Button,
12
+ ButtonGroup,
13
+ HTMLSelect,
14
+ Icon,
15
+ InputGroup,
16
+ Menu,
17
+ } from "@blueprintjs/core";
18
+ import { useCallback, useRef, useState } from "react";
19
+ import h from "./column-header-cell-postgrest.module.sass";
20
+ import type { ColumnSpec, ColumnDataType } from "../utils/column-spec";
21
+ import type {
22
+ ColumnSortEntry,
23
+ PostgrestColumnFilter,
24
+ PostgrestFilterOperator,
25
+ } from "../postgrest-table/data-loaders";
26
+ import { ColumnHeaderRendererProps } from "./column-header-cell";
27
+
28
+ /** Operators appropriate for string columns. */
29
+ const STRING_FILTER_OPERATORS: PostgrestFilterOperator[] = [
30
+ "eq",
31
+ "neq",
32
+ "ilike",
33
+ ];
34
+
35
+ /** Operators appropriate for numeric / integer columns. */
36
+ const NUMERIC_FILTER_OPERATORS: PostgrestFilterOperator[] = [
37
+ "eq",
38
+ "neq",
39
+ "gt",
40
+ "lt",
41
+ "gte",
42
+ "lte",
43
+ ];
44
+
45
+ /** Operators appropriate for boolean columns. */
46
+ const BOOLEAN_FILTER_OPERATORS: PostgrestFilterOperator[] = ["eq", "neq"];
47
+
48
+ /** Fallback operators when no type information is available. */
49
+ const DEFAULT_FILTER_OPERATORS: PostgrestFilterOperator[] = [
50
+ "eq",
51
+ "neq",
52
+ "ilike",
53
+ "gt",
54
+ "lt",
55
+ "gte",
56
+ "lte",
57
+ ];
58
+
59
+ /** Map from data type to the appropriate set of filter operators. */
60
+ const OPERATORS_BY_TYPE: Record<string, PostgrestFilterOperator[]> = {
61
+ string: STRING_FILTER_OPERATORS,
62
+ number: NUMERIC_FILTER_OPERATORS,
63
+ integer: NUMERIC_FILTER_OPERATORS,
64
+ boolean: BOOLEAN_FILTER_OPERATORS,
65
+ };
66
+
67
+ export const OPERATOR_LABELS: Record<PostgrestFilterOperator, string> = {
68
+ eq: "=",
69
+ neq: "≠",
70
+ like: "like",
71
+ ilike: "contains",
72
+ gt: ">",
73
+ lt: "<",
74
+ gte: "≥",
75
+ lte: "≤",
76
+ is: "is",
77
+ };
78
+
79
+ export interface ColumnHeaderActions {
80
+ onSetSort: (key: string, ascending: boolean | null) => void;
81
+ onSetFilter: (
82
+ key: string,
83
+ operator: PostgrestFilterOperator | null,
84
+ value: string,
85
+ ) => void;
86
+ onClearColumn: (key: string) => void;
87
+ }
88
+
89
+ /** The main column header cell renderer for PostgREST tables. */
90
+ export function renderPostgRESTColumnHeaderCell(
91
+ props: ColumnHeaderRendererProps & { actions: ColumnHeaderActions },
92
+ ): React.ReactElement<ColumnHeaderCellProps> | null {
93
+ const { col, activeSort, activeFilter, actions } = props;
94
+
95
+ const isSortable = col.sortable === true;
96
+ const isFilterable =
97
+ col.filterable === true || typeof col.filterable === "object";
98
+
99
+ // If the column has no sort/filter capabilities, render a plain header
100
+ if (!isSortable && !isFilterable) {
101
+ return h(ColumnHeaderCell, { name: col.name });
102
+ }
103
+
104
+ return h(ColumnHeaderCell, {
105
+ name: col.name,
106
+ nameRenderer: (): any =>
107
+ h(ColumnHeaderName, { col, activeFilter, activeSort }),
108
+ menuRenderer: () =>
109
+ h(ColumnActionsMenu, {
110
+ col,
111
+ isSortable,
112
+ isFilterable,
113
+ activeSort,
114
+ activeFilter,
115
+ actions,
116
+ }),
117
+ });
118
+ }
119
+
120
+ /** Column name with active sort/filter indicator icons. */
121
+ function ColumnHeaderName({ col, activeSort, activeFilter }) {
122
+ const hasSortActive = activeSort != null;
123
+ const hasFilterActive = activeFilter != null;
124
+ return h("div.column-header-cell", [
125
+ h("span.column-name", col.name),
126
+ h.if(hasSortActive || hasFilterActive)("span.column-indicators", [
127
+ h.if(hasSortActive)(
128
+ "span.sort-indicator",
129
+ h(Icon, {
130
+ icon: activeSort?.ascending ? "sort-asc" : "sort-desc",
131
+ size: 12,
132
+ }),
133
+ ),
134
+ h.if(hasFilterActive)(
135
+ "span.filter-indicator",
136
+ h(Icon, { icon: "filter", size: 12 }),
137
+ ),
138
+ ]),
139
+ ]);
140
+ }
141
+
142
+ /** Resolve filter operators for a column, considering explicit config,
143
+ * inferred data type, and fallback defaults. */
144
+ function getOperatorsForColumn(col: ColumnSpec): PostgrestFilterOperator[] {
145
+ // Explicit operators in filterable config take priority
146
+ if (typeof col.filterable === "object" && col.filterable.operators?.length) {
147
+ return col.filterable.operators as PostgrestFilterOperator[];
148
+ }
149
+ // Otherwise, pick operators based on the inferred data type
150
+ if (col.dataType != null) {
151
+ return OPERATORS_BY_TYPE[col.dataType] ?? DEFAULT_FILTER_OPERATORS;
152
+ }
153
+ return DEFAULT_FILTER_OPERATORS;
154
+ }
155
+
156
+ /** Menu content rendered inside ColumnHeaderCell's built-in dropdown. */
157
+ function ColumnActionsMenu({
158
+ col,
159
+ isSortable,
160
+ isFilterable,
161
+ activeSort,
162
+ activeFilter,
163
+ actions,
164
+ }) {
165
+ const operators = getOperatorsForColumn(col);
166
+
167
+ const [filterOperator, setFilterOperator] = useState<PostgrestFilterOperator>(
168
+ activeFilter?.operator ?? operators[0],
169
+ );
170
+ const filterInputRef = useRef<HTMLInputElement>(null);
171
+
172
+ const applyFilter = useCallback(() => {
173
+ const val = filterInputRef.current?.value ?? "";
174
+ if (val.trim() === "") {
175
+ actions.onSetFilter(col.key, null, "");
176
+ } else {
177
+ actions.onSetFilter(col.key, filterOperator, val);
178
+ }
179
+ }, [col.key, filterOperator, actions]);
180
+
181
+ const hasAnyActive =
182
+ activeSort != null ||
183
+ (activeFilter != null &&
184
+ activeFilter.value !== "" &&
185
+ activeFilter.value != null);
186
+
187
+ return h(Menu, { className: "column-actions-menu" }, [
188
+ h("div.column-actions-popover", [
189
+ h.if(isSortable)("div.popover-section", [
190
+ h("div.popover-section-title", "Sort"),
191
+ h("div.sort-buttons", [
192
+ h(ButtonGroup, { minimal: true }, [
193
+ h(
194
+ Button,
195
+ {
196
+ icon: "sort-asc",
197
+ active: activeSort?.ascending === true,
198
+ intent: activeSort?.ascending === true ? "primary" : "none",
199
+ onClick() {
200
+ if (activeSort?.ascending === true) {
201
+ actions.onSetSort(col.key, null);
202
+ } else {
203
+ actions.onSetSort(col.key, true);
204
+ }
205
+ },
206
+ },
207
+ "A→Z",
208
+ ),
209
+ h(
210
+ Button,
211
+ {
212
+ icon: "sort-desc",
213
+ active: activeSort?.ascending === false,
214
+ intent: activeSort?.ascending === false ? "primary" : "none",
215
+ onClick() {
216
+ if (activeSort?.ascending === false) {
217
+ actions.onSetSort(col.key, null);
218
+ } else {
219
+ actions.onSetSort(col.key, false);
220
+ }
221
+ },
222
+ },
223
+ "Z→A",
224
+ ),
225
+ ]),
226
+ ]),
227
+ ]),
228
+ h.if(isFilterable)("div.popover-section", [
229
+ h("div.popover-section-title", "Filter"),
230
+ h("div.filter-row", [
231
+ h(HTMLSelect, {
232
+ className: "filter-operator-select",
233
+ value: filterOperator,
234
+ options: operators.map((op) => ({
235
+ value: op,
236
+ label: OPERATOR_LABELS[op] ?? op,
237
+ })),
238
+ onChange(e) {
239
+ setFilterOperator(e.target.value as PostgrestFilterOperator);
240
+ },
241
+ }),
242
+ h(InputGroup, {
243
+ className: "filter-value-input",
244
+ small: true,
245
+ placeholder: "Value…",
246
+ defaultValue: activeFilter?.value ?? "",
247
+ inputRef: filterInputRef,
248
+ onKeyDown(e) {
249
+ if (e.key === "Enter") {
250
+ applyFilter();
251
+ }
252
+ },
253
+ rightElement: h(Button, {
254
+ icon: "arrow-right",
255
+ minimal: true,
256
+ small: true,
257
+ onClick: applyFilter,
258
+ }),
259
+ }),
260
+ ]),
261
+ ]),
262
+ h.if(hasAnyActive)("div.clear-section", [
263
+ h(
264
+ Button,
265
+ {
266
+ small: true,
267
+ minimal: true,
268
+ intent: "danger",
269
+ icon: "cross",
270
+ onClick() {
271
+ actions.onClearColumn(col.key);
272
+ if (filterInputRef.current) {
273
+ filterInputRef.current.value = "";
274
+ }
275
+ },
276
+ },
277
+ "Clear all",
278
+ ),
279
+ ]),
280
+ ]),
281
+ ]);
282
+ }