@alepha/ui 0.39.0 → 0.40.0

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 (187) hide show
  1. package/README.md +4 -2
  2. package/dist/admin/AdminJobExecutionSheet.d.ts.map +1 -1
  3. package/dist/admin/AdminJobExecutionSheet.js +7 -0
  4. package/dist/admin/AdminJobExecutionSheet.js.map +1 -1
  5. package/dist/admin/analytics/RequestDialog.js +1 -1
  6. package/dist/admin/analytics/RequestDialog.js.map +1 -1
  7. package/dist/ai/AiChat.d.ts +6 -3
  8. package/dist/ai/AiChat.d.ts.map +1 -1
  9. package/dist/ai/AiChat.js +34 -5
  10. package/dist/ai/AiChat.js.map +1 -1
  11. package/dist/ai/AiChatConfirmCard.d.ts.map +1 -1
  12. package/dist/ai/AiChatConfirmCard.js +5 -1
  13. package/dist/ai/AiChatConfirmCard.js.map +1 -1
  14. package/dist/ai/AiChatFloat.d.ts +14 -0
  15. package/dist/ai/AiChatFloat.d.ts.map +1 -0
  16. package/dist/ai/AiChatFloat.js +30 -0
  17. package/dist/ai/AiChatFloat.js.map +1 -0
  18. package/dist/ai/AiChatMessage.js +2 -2
  19. package/dist/ai/AiChatMessage.js.map +1 -1
  20. package/dist/ai/AiChatPanel.d.ts +19 -0
  21. package/dist/ai/AiChatPanel.d.ts.map +1 -1
  22. package/dist/ai/AiChatPanel.js +65 -19
  23. package/dist/ai/AiChatPanel.js.map +1 -1
  24. package/dist/ai/AiChatToolStepDetails.d.ts +14 -0
  25. package/dist/ai/AiChatToolStepDetails.d.ts.map +1 -0
  26. package/dist/ai/AiChatToolStepDetails.js +34 -0
  27. package/dist/ai/AiChatToolStepDetails.js.map +1 -0
  28. package/dist/ai/AiChatToolTrace.d.ts +50 -0
  29. package/dist/ai/AiChatToolTrace.d.ts.map +1 -0
  30. package/dist/ai/AiChatToolTrace.js +91 -0
  31. package/dist/ai/AiChatToolTrace.js.map +1 -0
  32. package/dist/ai/index.d.ts +4 -2
  33. package/dist/ai/index.d.ts.map +1 -1
  34. package/dist/ai/index.js +3 -1
  35. package/dist/core/Button.d.ts.map +1 -1
  36. package/dist/core/Button.js +17 -1
  37. package/dist/core/Button.js.map +1 -1
  38. package/dist/core/Dialog.d.ts.map +1 -1
  39. package/dist/core/Dialog.js +1 -1
  40. package/dist/core/Dialog.js.map +1 -1
  41. package/dist/core/TimeAgo.d.ts +3 -4
  42. package/dist/core/TimeAgo.d.ts.map +1 -1
  43. package/dist/core/TimeAgo.js +3 -4
  44. package/dist/core/TimeAgo.js.map +1 -1
  45. package/dist/i18n/en/index.d.ts +22 -0
  46. package/dist/i18n/en/index.d.ts.map +1 -0
  47. package/dist/i18n/en/index.js +1 -0
  48. package/dist/i18n/en/uiEn.d.ts +37 -0
  49. package/dist/i18n/en/uiEn.d.ts.map +1 -0
  50. package/dist/i18n/en/uiEn.js +1468 -0
  51. package/dist/i18n/en/uiEn.js.map +1 -0
  52. package/dist/i18n/fr/uiFr.d.ts +3 -2
  53. package/dist/i18n/fr/uiFr.d.ts.map +1 -1
  54. package/dist/i18n/fr/uiFr.js +7 -2
  55. package/dist/i18n/fr/uiFr.js.map +1 -1
  56. package/dist/markdown/MarkdownView.d.ts +14 -0
  57. package/dist/markdown/MarkdownView.d.ts.map +1 -1
  58. package/dist/markdown/MarkdownView.js +16 -3
  59. package/dist/markdown/MarkdownView.js.map +1 -1
  60. package/dist/markdown/highlightLines.d.ts +27 -0
  61. package/dist/markdown/highlightLines.d.ts.map +1 -0
  62. package/dist/markdown/highlightLines.js +91 -0
  63. package/dist/markdown/highlightLines.js.map +1 -0
  64. package/dist/markdown/index.d.ts +5 -2
  65. package/dist/markdown/index.d.ts.map +1 -1
  66. package/dist/markdown/index.js +2 -1
  67. package/dist/shell/ButtonInbox.d.ts +8 -0
  68. package/dist/shell/ButtonInbox.d.ts.map +1 -1
  69. package/dist/shell/ButtonInbox.js +3 -1
  70. package/dist/shell/ButtonInbox.js.map +1 -1
  71. package/dist/shell/PlateLayout.d.ts.map +1 -1
  72. package/dist/shell/PlateLayout.js +2 -2
  73. package/dist/shell/PlateLayout.js.map +1 -1
  74. package/dist/sortable/index.d.ts +17 -0
  75. package/dist/sortable/index.d.ts.map +1 -0
  76. package/dist/sortable/index.js +4 -0
  77. package/dist/sortable/useSortableItem.d.ts +32 -0
  78. package/dist/sortable/useSortableItem.d.ts.map +1 -0
  79. package/dist/sortable/useSortableItem.js +35 -0
  80. package/dist/sortable/useSortableItem.js.map +1 -0
  81. package/dist/sortable/useSortableList.d.ts +85 -0
  82. package/dist/sortable/useSortableList.d.ts.map +1 -0
  83. package/dist/sortable/useSortableList.js +125 -0
  84. package/dist/sortable/useSortableList.js.map +1 -0
  85. package/dist/table/DataTable.d.ts.map +1 -1
  86. package/dist/table/DataTable.js +9 -6
  87. package/dist/table/DataTable.js.map +1 -1
  88. package/dist/table/DataTableRowActionGroupItem.d.ts +23 -0
  89. package/dist/table/DataTableRowActionGroupItem.d.ts.map +1 -0
  90. package/dist/table/DataTableRowActionGroupItem.js +38 -0
  91. package/dist/table/DataTableRowActionGroupItem.js.map +1 -0
  92. package/dist/table/DataTableRowActionItem.d.ts.map +1 -1
  93. package/dist/table/DataTableRowActionItem.js +15 -2
  94. package/dist/table/DataTableRowActionItem.js.map +1 -1
  95. package/dist/table/DataTableRowActionsInline.d.ts.map +1 -1
  96. package/dist/table/DataTableRowActionsInline.js +2 -1
  97. package/dist/table/DataTableRowActionsInline.js.map +1 -1
  98. package/dist/table/DataTableRowActionsMenu.d.ts +3 -2
  99. package/dist/table/DataTableRowActionsMenu.d.ts.map +1 -1
  100. package/dist/table/DataTableRowActionsMenu.js +12 -14
  101. package/dist/table/DataTableRowActionsMenu.js.map +1 -1
  102. package/dist/table/dataTableBaseProps.d.ts +16 -2
  103. package/dist/table/dataTableBaseProps.d.ts.map +1 -1
  104. package/dist/table/dataTablePersistence.d.ts +53 -11
  105. package/dist/table/dataTablePersistence.d.ts.map +1 -1
  106. package/dist/table/dataTablePersistence.js +66 -29
  107. package/dist/table/dataTablePersistence.js.map +1 -1
  108. package/dist/table/dataTableTypes.d.ts +20 -3
  109. package/dist/table/dataTableTypes.d.ts.map +1 -1
  110. package/dist/table/index.d.ts +8 -1
  111. package/dist/table/index.d.ts.map +1 -1
  112. package/dist/table/index.js +2 -1
  113. package/dist/table/useDataTableColumns.d.ts.map +1 -1
  114. package/dist/table/useDataTableColumns.js +4 -4
  115. package/dist/table/useDataTableColumns.js.map +1 -1
  116. package/dist/table/useDataTableData.d.ts.map +1 -1
  117. package/dist/table/useDataTableData.js +3 -3
  118. package/dist/table/useDataTableData.js.map +1 -1
  119. package/dist/table/useDataTableFilterForm.d.ts.map +1 -1
  120. package/dist/table/useDataTableFilterForm.js +6 -2
  121. package/dist/table/useDataTableFilterForm.js.map +1 -1
  122. package/dist/table/useDataTableFilterVisibility.d.ts +5 -0
  123. package/dist/table/useDataTableFilterVisibility.d.ts.map +1 -1
  124. package/dist/table/useDataTableFilterVisibility.js +16 -8
  125. package/dist/table/useDataTableFilterVisibility.js.map +1 -1
  126. package/dist/table/useDataTableRefresh.d.ts.map +1 -1
  127. package/dist/table/useDataTableRefresh.js +3 -2
  128. package/dist/table/useDataTableRefresh.js.map +1 -1
  129. package/dist/table/useDataTableSummary.d.ts +5 -0
  130. package/dist/table/useDataTableSummary.d.ts.map +1 -1
  131. package/dist/table/useDataTableSummary.js +6 -6
  132. package/dist/table/useDataTableSummary.js.map +1 -1
  133. package/docs/packages-alepha-ui-ai.md +3 -3
  134. package/docs/packages-alepha-ui-i18n-en.md +35 -0
  135. package/docs/packages-alepha-ui-markdown.md +3 -1
  136. package/docs/packages-alepha-ui-overview.md +4 -2
  137. package/docs/packages-alepha-ui-sortable.md +34 -0
  138. package/docs/packages-alepha-ui-table.md +7 -1
  139. package/package.json +21 -3
  140. package/src/admin/AdminJobExecutionSheet.tsx +9 -0
  141. package/src/admin/analytics/RequestDialog.tsx +1 -1
  142. package/src/ai/AiChat.tsx +44 -6
  143. package/src/ai/AiChatConfirmCard.tsx +7 -1
  144. package/src/ai/AiChatFloat.tsx +46 -0
  145. package/src/ai/AiChatMessage.tsx +2 -2
  146. package/src/ai/AiChatPanel.tsx +154 -47
  147. package/src/ai/AiChatToolStepDetails.tsx +42 -0
  148. package/src/ai/AiChatToolTrace.tsx +167 -0
  149. package/src/ai/__tests__/AiChat.browser.spec.tsx +270 -2
  150. package/src/ai/index.ts +4 -2
  151. package/src/core/Button.tsx +26 -1
  152. package/src/core/Dialog.tsx +6 -2
  153. package/src/core/TimeAgo.tsx +3 -4
  154. package/src/core/__tests__/Button.browser.spec.tsx +57 -0
  155. package/src/i18n/en/__tests__/uiEn.spec.ts +110 -0
  156. package/src/i18n/en/index.ts +22 -0
  157. package/src/i18n/en/uiEn.ts +1647 -0
  158. package/src/i18n/fr/uiFr.ts +7 -2
  159. package/src/markdown/MarkdownView.tsx +30 -1
  160. package/src/markdown/__tests__/highlightLines.spec.ts +45 -0
  161. package/src/markdown/highlightLines.ts +120 -0
  162. package/src/markdown/index.ts +14 -2
  163. package/src/shell/ButtonInbox.tsx +11 -1
  164. package/src/shell/PlateLayout.tsx +18 -9
  165. package/src/shell/__tests__/ButtonInbox.browser.spec.tsx +40 -0
  166. package/src/shell/__tests__/PlateLayout.browser.spec.tsx +19 -2
  167. package/src/sortable/__tests__/useSortableList.browser.spec.tsx +157 -0
  168. package/src/sortable/index.ts +22 -0
  169. package/src/sortable/useSortableItem.ts +65 -0
  170. package/src/sortable/useSortableList.ts +231 -0
  171. package/src/table/DataTable.tsx +8 -5
  172. package/src/table/DataTableRowActionGroupItem.tsx +75 -0
  173. package/src/table/DataTableRowActionItem.tsx +6 -0
  174. package/src/table/DataTableRowActionsInline.tsx +3 -1
  175. package/src/table/DataTableRowActionsMenu.tsx +33 -49
  176. package/src/table/__tests__/DataTablePersistenceStorage.browser.spec.tsx +204 -0
  177. package/src/table/__tests__/DataTableRowActionGroup.browser.spec.tsx +118 -1
  178. package/src/table/dataTableBaseProps.ts +16 -2
  179. package/src/table/dataTablePersistence.ts +118 -32
  180. package/src/table/dataTableTypes.ts +20 -3
  181. package/src/table/index.ts +14 -1
  182. package/src/table/useDataTableColumns.ts +9 -4
  183. package/src/table/useDataTableData.ts +8 -3
  184. package/src/table/useDataTableFilterForm.ts +6 -2
  185. package/src/table/useDataTableFilterVisibility.ts +20 -7
  186. package/src/table/useDataTableRefresh.ts +4 -2
  187. package/src/table/useDataTableSummary.ts +25 -8
@@ -0,0 +1,204 @@
1
+ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
2
+ import { Alepha } from "alepha";
3
+ import { AlephaContext } from "alepha/react";
4
+ import { AlephaReactI18n } from "alepha/react/i18n";
5
+ import { AlephaReactRouter } from "alepha/react/router";
6
+ import { setupJsdomMocks } from "alepha/testing/react";
7
+ import { afterEach, beforeAll, describe, expect, it } from "vitest";
8
+
9
+ import { DataTable } from "../DataTable.tsx";
10
+ import {
11
+ type DataTablePersistenceStorage,
12
+ localStorageDataTableStorage,
13
+ readPersisted,
14
+ writePersisted,
15
+ } from "../dataTablePersistence.ts";
16
+ import type { DataTableFilterFields } from "../dataTableTypes.ts";
17
+
18
+ interface Row {
19
+ id: number;
20
+ title: string;
21
+ }
22
+
23
+ const columns = {
24
+ title: { label: "Title", sortable: true, cell: (r: Row) => r.title },
25
+ extra: { label: "Extra", defaultHidden: true, cell: () => "x" },
26
+ };
27
+
28
+ const filterFields = {
29
+ search: { preset: "search" },
30
+ } satisfies DataTableFilterFields;
31
+
32
+ const pageOf = (rows: Row[]) => ({
33
+ content: rows,
34
+ page: {
35
+ number: 0,
36
+ size: 20,
37
+ offset: 0,
38
+ numberOfElements: rows.length,
39
+ totalElements: rows.length,
40
+ totalPages: 1,
41
+ isEmpty: rows.length === 0,
42
+ isFirst: true,
43
+ isLast: true,
44
+ },
45
+ });
46
+
47
+ /**
48
+ * A storage that is not `localStorage`: a plain map, the shape a store
49
+ * loaded from a server takes once it answers from memory.
50
+ */
51
+ const memoryStorage = (
52
+ seed: Record<string, unknown> = {},
53
+ ): DataTablePersistenceStorage & { values: Map<string, unknown> } => {
54
+ const values = new Map<string, unknown>(Object.entries(seed));
55
+ return {
56
+ values,
57
+ read: (key, suffix) => values.get(`${key}/${suffix}`),
58
+ write: (key, suffix, value) => {
59
+ if (value === undefined) values.delete(`${key}/${suffix}`);
60
+ else values.set(`${key}/${suffix}`, value);
61
+ },
62
+ };
63
+ };
64
+
65
+ /**
66
+ * `persistenceStorage` (#Q4417): where a table keeps what it remembers is a
67
+ * storage it is handed, so the same state can live in a user's preferences
68
+ * on a server rather than in one browser. `localStorage` stays the default.
69
+ */
70
+ describe("DataTable (persistence storage)", () => {
71
+ let alepha: Alepha | undefined;
72
+
73
+ beforeAll(() => {
74
+ setupJsdomMocks();
75
+ });
76
+
77
+ afterEach(async () => {
78
+ await alepha?.stop();
79
+ alepha = undefined;
80
+ window.localStorage.clear();
81
+ });
82
+
83
+ const mount = async (ui: React.ReactNode) => {
84
+ alepha = Alepha.create().with(AlephaReactRouter).with(AlephaReactI18n);
85
+ await alepha.start();
86
+ return render(
87
+ <AlephaContext.Provider value={alepha}>{ui}</AlephaContext.Provider>,
88
+ );
89
+ };
90
+
91
+ const table = (
92
+ seen: Array<{ sort?: string; filters?: Record<string, any> }>,
93
+ storage?: DataTablePersistenceStorage,
94
+ ) => (
95
+ <DataTable<Row, typeof filterFields>
96
+ persistenceKey="probe"
97
+ {...(storage ? { persistenceStorage: storage } : {})}
98
+ columns={columns}
99
+ filters={{
100
+ fields: filterFields,
101
+ render: (form) => (
102
+ <button type="button" onClick={() => form.input.search.set("typed")}>
103
+ set search
104
+ </button>
105
+ ),
106
+ }}
107
+ fetch={async ({ sort, filters }) => {
108
+ seen.push({ sort, filters });
109
+ return pageOf([{ id: 1, title: "Alpha" }]);
110
+ }}
111
+ />
112
+ );
113
+
114
+ it("restores filters, sort and columns from the storage it is given, on the first fetch", async () => {
115
+ // What another device wrote: nothing in this browser's localStorage.
116
+ const storage = memoryStorage({
117
+ "probe/filters": { search: "elsewhere" },
118
+ "probe/sort": { field: "title", direction: "desc" },
119
+ "probe/columns": ["title", "extra"],
120
+ });
121
+ const seen: Array<{ sort?: string; filters?: Record<string, any> }> = [];
122
+ await mount(table(seen, storage));
123
+
124
+ await waitFor(() => expect(screen.getByText("Alpha")).toBeTruthy());
125
+ // The very first request already carries them: no fetch with the
126
+ // defaults went out before the stored state was applied.
127
+ expect(seen[0]?.filters?.search).toBe("elsewhere");
128
+ expect(seen[0]?.sort).toBe("-title");
129
+ expect(screen.getByText("Extra")).toBeTruthy();
130
+ });
131
+
132
+ it("writes the reader's changes to that storage, and nothing to localStorage", async () => {
133
+ const storage = memoryStorage();
134
+ const seen: Array<{ sort?: string; filters?: Record<string, any> }> = [];
135
+ await mount(table(seen, storage));
136
+
137
+ await waitFor(() => expect(screen.getByText("Alpha")).toBeTruthy());
138
+ fireEvent.click(screen.getByText("set search"));
139
+ fireEvent.click(screen.getByRole("button", { name: /Title/ }));
140
+
141
+ await waitFor(() =>
142
+ expect(storage.values.get("probe/filters")).toEqual({ search: "typed" }),
143
+ );
144
+ expect(storage.values.get("probe/sort")).toEqual({
145
+ field: "title",
146
+ direction: "asc",
147
+ });
148
+ expect(window.localStorage.length).toBe(0);
149
+ });
150
+
151
+ it("keeps localStorage as the default, under `<key>.<suffix>`", async () => {
152
+ window.localStorage.setItem(
153
+ "probe.sort",
154
+ JSON.stringify({ field: "title", direction: "desc" }),
155
+ );
156
+ const seen: Array<{ sort?: string; filters?: Record<string, any> }> = [];
157
+ await mount(table(seen));
158
+
159
+ await waitFor(() => expect(screen.getByText("Alpha")).toBeTruthy());
160
+ expect(seen[0]?.sort).toBe("-title");
161
+ fireEvent.click(screen.getByText("set search"));
162
+ await waitFor(() =>
163
+ expect(JSON.parse(window.localStorage.getItem("probe.filters")!)).toEqual(
164
+ {
165
+ search: "typed",
166
+ },
167
+ ),
168
+ );
169
+ });
170
+ });
171
+
172
+ describe("readPersisted / writePersisted with a storage", () => {
173
+ afterEach(() => window.localStorage.clear());
174
+
175
+ it("turns an empty object, an empty array or null into a delete", () => {
176
+ const storage = memoryStorage({ "k/a": 1, "k/b": 2, "k/c": 3 });
177
+ writePersisted("k", "a", {}, storage);
178
+ writePersisted("k", "b", [], storage);
179
+ writePersisted("k", "c", null, storage);
180
+ expect(storage.values.size).toBe(0);
181
+ });
182
+
183
+ it("answers undefined rather than throwing when the storage throws", () => {
184
+ const broken: DataTablePersistenceStorage = {
185
+ read: () => {
186
+ throw new Error("down");
187
+ },
188
+ write: () => {
189
+ throw new Error("down");
190
+ },
191
+ };
192
+ expect(readPersisted("k", "a", broken)).toBeUndefined();
193
+ expect(() => writePersisted("k", "a", 1, broken)).not.toThrow();
194
+ });
195
+
196
+ it("reads and writes localStorage when no storage is named", () => {
197
+ writePersisted("k", "a", { x: 1 });
198
+ expect(window.localStorage.getItem("k.a")).toBe('{"x":1}');
199
+ expect(readPersisted("k", "a")).toEqual({ x: 1 });
200
+ expect(localStorageDataTableStorage.read("k", "a")).toEqual({ x: 1 });
201
+ writePersisted("k", "a", undefined);
202
+ expect(window.localStorage.getItem("k.a")).toBeNull();
203
+ });
204
+ });
@@ -20,7 +20,7 @@ const columns = {
20
20
 
21
21
  /**
22
22
  * A row action may be a group: a label, an icon and the actions it holds,
23
- * rendered as a submenu one level deep.
23
+ * rendered as a submenu, and a group inside a group as a submenu inside it.
24
24
  *
25
25
  * Two behaviours are guarded here. The submenu itself, and the rule that a
26
26
  * group with no children does not count towards the menu existing: a row
@@ -150,6 +150,50 @@ describe("DataTable (row action groups)", () => {
150
150
  ).toBeTruthy();
151
151
  });
152
152
 
153
+ it("draws a separator under an entry that asks for one, above a group too", async () => {
154
+ await mount(
155
+ <DataTable<Row>
156
+ data={[{ id: 1, title: "Alpha" }]}
157
+ columns={columns}
158
+ rowActions={() => [
159
+ { label: "Mark as ready", separatorAfter: true, onClick: () => {} },
160
+ {
161
+ label: "Prompts",
162
+ icon: Bot,
163
+ children: [{ label: "Work on it", onClick: () => {} }],
164
+ },
165
+ { label: "Shelve", onClick: () => {} },
166
+ ]}
167
+ />,
168
+ );
169
+
170
+ await openRowMenu();
171
+ const separators = document.querySelectorAll(
172
+ '[data-slot="dropdown-menu-separator"]',
173
+ );
174
+ expect(separators.length).toBe(1);
175
+ // Between the first entry and the group, nowhere else.
176
+ expect(separators[0]?.parentElement?.textContent).toContain("Prompts");
177
+ });
178
+
179
+ it("draws no separator under the last entry, whatever it asks", async () => {
180
+ await mount(
181
+ <DataTable<Row>
182
+ data={[{ id: 1, title: "Alpha" }]}
183
+ columns={columns}
184
+ rowActions={() => [
185
+ { label: "Shelve", onClick: () => {} },
186
+ { label: "Mark as ready", separatorAfter: true, onClick: () => {} },
187
+ ]}
188
+ />,
189
+ );
190
+
191
+ await openRowMenu();
192
+ expect(
193
+ document.querySelector('[data-slot="dropdown-menu-separator"]'),
194
+ ).toBeNull();
195
+ });
196
+
153
197
  it("opens the submenu and calls the child handler with the row", async () => {
154
198
  let clicked: Row | undefined;
155
199
  await mount(
@@ -199,4 +243,77 @@ describe("DataTable (row action groups)", () => {
199
243
  await waitFor(() => expect(clicked).toBeTruthy());
200
244
  expect(clicked?.id).toBe(7);
201
245
  });
246
+
247
+ const menuItem = (label: string) =>
248
+ waitFor(() => {
249
+ const found = Array.from(
250
+ document.querySelectorAll('[role="menuitem"]'),
251
+ ).find((it) => (it.textContent ?? "").trim() === label);
252
+ expect(found).toBeTruthy();
253
+ return found as HTMLElement;
254
+ });
255
+
256
+ it("opens a group inside a group and calls the inner action with the row", async () => {
257
+ let clicked: Row | undefined;
258
+ await mount(
259
+ <DataTable<Row>
260
+ data={[{ id: 3, title: "Alpha" }]}
261
+ columns={columns}
262
+ rowActions={() => [
263
+ {
264
+ label: "Agent prompts",
265
+ icon: Bot,
266
+ children: [
267
+ {
268
+ label: "Review Epic",
269
+ children: [
270
+ { label: "Copy", onClick: () => {} },
271
+ {
272
+ label: "Run",
273
+ onClick: (row) => {
274
+ clicked = row;
275
+ },
276
+ },
277
+ ],
278
+ },
279
+ // Empty: left out of the submenu, as an empty group is.
280
+ { label: "Nothing", children: [] },
281
+ ],
282
+ },
283
+ ]}
284
+ />,
285
+ );
286
+
287
+ await openRowMenu();
288
+ fireEvent.click(await menuItem("Agent prompts"));
289
+ fireEvent.click(await menuItem("Review Epic"));
290
+ expect(
291
+ Array.from(document.querySelectorAll('[role="menuitem"]')).some(
292
+ (it) => (it.textContent ?? "").trim() === "Nothing",
293
+ ),
294
+ ).toBe(false);
295
+ fireEvent.click(await menuItem("Run"));
296
+ await waitFor(() => expect(clicked?.id).toBe(3));
297
+ });
298
+
299
+ it("renders no menu when a group holds only empty groups", async () => {
300
+ await mount(
301
+ <DataTable<Row>
302
+ data={[{ id: 1, title: "Alpha" }]}
303
+ columns={columns}
304
+ rowActions={() => [
305
+ {
306
+ label: "Agent prompts",
307
+ icon: Bot,
308
+ children: [{ label: "Review Epic", children: [] }],
309
+ },
310
+ ]}
311
+ />,
312
+ );
313
+
314
+ await waitFor(() => expect(screen.getByText("Alpha")).toBeTruthy());
315
+ expect(screen.queryByRole("button", { name: "Open row actions" })).toBe(
316
+ null,
317
+ );
318
+ });
202
319
  });
@@ -1,6 +1,7 @@
1
1
  import type { ReactNode } from "react";
2
2
 
3
3
  import type { DataTableCellPadding } from "./dataTableCellPadding.ts";
4
+ import type { DataTablePersistenceStorage } from "./dataTablePersistence.ts";
4
5
  import type { DataTableSquareRight } from "./dataTableSquareRight.ts";
5
6
  import type {
6
7
  DataTableEmptyState,
@@ -84,7 +85,8 @@ export interface DataTableBaseProps<
84
85
  * item. Each `onClick` receives `(item, { refresh, clearSelection })`.
85
86
  *
86
87
  * An entry may also be a {@link RowActionGroup}: a label, an icon and
87
- * the actions it holds, rendered as a submenu one level deep.
88
+ * the actions it holds, rendered as a submenu; a group inside a group is a
89
+ * submenu inside it.
88
90
  */
89
91
  rowActions?: (item: T) => RowActionEntry<T>[];
90
92
  /**
@@ -261,13 +263,25 @@ export interface DataTableBaseProps<
261
263
  filters?: DataTableFilters<F>;
262
264
  /**
263
265
  * When set, filter values, the filters on the bar, column visibility, and
264
- * sort state are persisted to `localStorage` under this key. Pick a key that's
266
+ * sort state are persisted under this key, to `localStorage` unless
267
+ * {@link persistenceStorage} names another store. Pick a key that's
265
268
  * unique per page and per scope (e.g. `"admin.users"`,
266
269
  * `\`lor.board.${campaignId}\``).
267
270
  *
268
271
  * Narrow what it stores with {@link persist}.
269
272
  */
270
273
  persistenceKey?: string;
274
+ /**
275
+ * Where {@link persistenceKey} is stored: `localStorage` when absent. Pass
276
+ * a {@link DataTablePersistenceStorage} to keep the table's state somewhere
277
+ * else, for instance in the user's preferences on the server so it follows
278
+ * them to another device.
279
+ *
280
+ * The storage answers synchronously, so a remote one is loaded before the
281
+ * table mounts. Keep its identity for the table's lifetime: it is read on
282
+ * mount and when the key changes.
283
+ */
284
+ persistenceStorage?: DataTablePersistenceStorage;
271
285
  /**
272
286
  * Which facets {@link persistenceKey} stores. All three by default, so this
273
287
  * is only ever worth passing to turn one OFF.
@@ -7,16 +7,91 @@ import type {
7
7
  } from "./dataTableTypes.ts";
8
8
 
9
9
  /**
10
- * Synchronous localStorage read. Returns undefined on miss or error.
10
+ * Where a table keeps what it persists: the filters, the filters on the bar,
11
+ * the columns and their order, the sort, the page size and the summary
12
+ * panel, each under its `persistenceKey` and a suffix naming the facet.
13
+ *
14
+ * Synchronous on both sides, and that is the contract: the table reads its
15
+ * state in `useState` initializers, so a store reached over the network has
16
+ * to be loaded before the table mounts and answer from memory after that.
17
+ * `write` with `undefined` deletes the value; `writePersisted` turns an empty
18
+ * object or array into `undefined` before it gets here.
19
+ *
20
+ * The default is {@link localStorageDataTableStorage}. Pass another one as a
21
+ * table's `persistenceStorage` to keep its state somewhere else, a user's
22
+ * preferences on the server for one. It should keep one identity for the
23
+ * table's lifetime: it is read when the table mounts and when its
24
+ * `persistenceKey` changes, not when the storage does.
25
+ */
26
+ export interface DataTablePersistenceStorage {
27
+ /**
28
+ * The value stored for one facet of one key, or `undefined` when there is
29
+ * none. Never throws.
30
+ */
31
+ read(key: string, suffix: string): unknown;
32
+ /**
33
+ * Store one facet of one key; `undefined` deletes it. Never throws.
34
+ */
35
+ write(key: string, suffix: string, value: unknown): void;
36
+ }
37
+
38
+ /**
39
+ * Every suffix a DataTable stores under its `persistenceKey`, one per facet.
40
+ * What a storage moving a table's state from one place to another (say, from
41
+ * `localStorage` to a server) has to carry over.
42
+ */
43
+ export const DATA_TABLE_PERSISTED_SUFFIXES = [
44
+ "filters",
45
+ "filterVisibility",
46
+ "columns",
47
+ "columnOrder",
48
+ "sort",
49
+ "size",
50
+ "summaryOpen",
51
+ ] as const;
52
+
53
+ /**
54
+ * The default storage: `localStorage`, under `<key>.<suffix>`, as JSON.
55
+ * Answers nothing on the server, and skips a write the browser refuses
56
+ * (private mode, quota).
57
+ */
58
+ export const localStorageDataTableStorage: DataTablePersistenceStorage = {
59
+ read: (key, suffix) => {
60
+ if (typeof window === "undefined") return undefined;
61
+ try {
62
+ const raw = window.localStorage.getItem(`${key}.${suffix}`);
63
+ return raw == null ? undefined : JSON.parse(raw);
64
+ } catch {
65
+ return undefined;
66
+ }
67
+ },
68
+ write: (key, suffix, value) => {
69
+ if (typeof window === "undefined") return;
70
+ const fullKey = `${key}.${suffix}`;
71
+ try {
72
+ if (value === undefined) {
73
+ window.localStorage.removeItem(fullKey);
74
+ return;
75
+ }
76
+ window.localStorage.setItem(fullKey, JSON.stringify(value));
77
+ } catch {
78
+ // localStorage may be unavailable (private mode, quota). Skip.
79
+ }
80
+ },
81
+ };
82
+
83
+ /**
84
+ * Synchronous read of `<key>.<suffix>` from `storage`, `localStorage` by
85
+ * default. Returns undefined on miss or error, and on the server.
11
86
  */
12
87
  export const readPersisted = <V>(
13
88
  key: string,
14
89
  suffix: string,
90
+ storage: DataTablePersistenceStorage = localStorageDataTableStorage,
15
91
  ): V | undefined => {
16
- if (typeof window === "undefined") return undefined;
17
92
  try {
18
- const raw = window.localStorage.getItem(`${key}.${suffix}`);
19
- return raw == null ? undefined : (JSON.parse(raw) as V);
93
+ const value = storage.read(key, suffix);
94
+ return value === undefined || value === null ? undefined : (value as V);
20
95
  } catch {
21
96
  return undefined;
22
97
  }
@@ -25,7 +100,7 @@ export const readPersisted = <V>(
25
100
  /**
26
101
  * Reshape persisted filter values that no longer match the schema.
27
102
  *
28
- * Filters are stored in localStorage per `persistenceKey`, so a filter whose
103
+ * Filters are stored per `persistenceKey` (in localStorage by default), so a filter whose
29
104
  * shape changes meets values written by the previous shape - on the machine
30
105
  * of anyone who has used the page, and only there, which is precisely the
31
106
  * class of bug no test environment contains. The first case was the Quests
@@ -67,34 +142,27 @@ export const reconcilePersistedFilters = (
67
142
  };
68
143
 
69
144
  /**
70
- * Synchronous localStorage write. Empty objects/null delete the key.
145
+ * Synchronous write of `<key>.<suffix>` to `storage`, `localStorage` by
146
+ * default. Empty objects, empty arrays and null delete the key.
71
147
  */
72
148
  export const writePersisted = (
73
149
  key: string,
74
150
  suffix: string,
75
151
  value: unknown,
152
+ storage: DataTablePersistenceStorage = localStorageDataTableStorage,
76
153
  ): void => {
77
- if (typeof window === "undefined") return;
78
- const fullKey = `${key}.${suffix}`;
154
+ const isEmptyObject =
155
+ value !== null &&
156
+ typeof value === "object" &&
157
+ !Array.isArray(value) &&
158
+ Object.keys(value as object).length === 0;
159
+ const isEmptyArray = Array.isArray(value) && value.length === 0;
160
+ const empty =
161
+ value === undefined || value === null || isEmptyObject || isEmptyArray;
79
162
  try {
80
- const isEmptyObject =
81
- value !== null &&
82
- typeof value === "object" &&
83
- !Array.isArray(value) &&
84
- Object.keys(value as object).length === 0;
85
- const isEmptyArray = Array.isArray(value) && value.length === 0;
86
- if (
87
- value === undefined ||
88
- value === null ||
89
- isEmptyObject ||
90
- isEmptyArray
91
- ) {
92
- window.localStorage.removeItem(fullKey);
93
- return;
94
- }
95
- window.localStorage.setItem(fullKey, JSON.stringify(value));
163
+ storage.write(key, suffix, empty ? undefined : value);
96
164
  } catch {
97
- // localStorage may be unavailable (private mode, quota). Skip.
165
+ // A storage that throws loses the write, never the table.
98
166
  }
99
167
  };
100
168
 
@@ -110,16 +178,20 @@ export const writePersisted = (
110
178
  export const persistedSort = (
111
179
  key: string | undefined,
112
180
  fallback: SortState | null | undefined,
181
+ storage?: DataTablePersistenceStorage,
113
182
  ): SortState | null => {
114
- const stored = key ? readPersisted<SortState>(key, "sort") : undefined;
183
+ const stored = key
184
+ ? readPersisted<SortState>(key, "sort", storage)
185
+ : undefined;
115
186
  return stored ?? fallback ?? null;
116
187
  };
117
188
 
118
189
  export const persistedSize = (
119
190
  key: string | undefined,
120
191
  fallback: number | undefined,
192
+ storage?: DataTablePersistenceStorage,
121
193
  ): number => {
122
- const stored = key ? readPersisted<number>(key, "size") : undefined;
194
+ const stored = key ? readPersisted<number>(key, "size", storage) : undefined;
123
195
  return stored ?? fallback ?? 20;
124
196
  };
125
197
 
@@ -129,19 +201,27 @@ export const persistedSize = (
129
201
  * Stored with the key whatever `persist` says, like the page size: it is how
130
202
  * the reader arranged the page, not a question they asked it.
131
203
  */
132
- export const persistedSummaryOpen = (key: string | undefined): boolean => {
133
- const stored = key ? readPersisted<unknown>(key, "summaryOpen") : undefined;
204
+ export const persistedSummaryOpen = (
205
+ key: string | undefined,
206
+ storage?: DataTablePersistenceStorage,
207
+ ): boolean => {
208
+ const stored = key
209
+ ? readPersisted<unknown>(key, "summaryOpen", storage)
210
+ : undefined;
134
211
  return typeof stored === "boolean" ? stored : true;
135
212
  };
136
213
 
137
214
  export const persistedColumns = <T>(
138
215
  key: string | undefined,
139
216
  columns: Record<string, ColumnDef<T>>,
217
+ storage?: DataTablePersistenceStorage,
140
218
  ): Set<string> => {
141
219
  const declared = new Set(
142
220
  Object.keys(columns).filter((k) => !columns[k]!.defaultHidden),
143
221
  );
144
- const stored = key ? readPersisted<string[]>(key, "columns") : undefined;
222
+ const stored = key
223
+ ? readPersisted<string[]>(key, "columns", storage)
224
+ : undefined;
145
225
  if (!Array.isArray(stored)) return declared;
146
226
  const kept = new Set(stored.filter((k) => k in columns));
147
227
  // A stored set that names none of today's columns is a snapshot of an
@@ -185,9 +265,10 @@ export const reconcileOrder = (
185
265
  export const persistedOrder = <T>(
186
266
  key: string | undefined,
187
267
  columns: Record<string, ColumnDef<T>>,
268
+ storage?: DataTablePersistenceStorage,
188
269
  ): string[] =>
189
270
  reconcileOrder(
190
- key ? readPersisted<string[]>(key, "columnOrder") : undefined,
271
+ key ? readPersisted<string[]>(key, "columnOrder", storage) : undefined,
191
272
  Object.keys(columns),
192
273
  );
193
274
 
@@ -223,10 +304,15 @@ export const declaredShownFilters = (
223
304
  export const persistedShownFilters = (
224
305
  key: string | undefined,
225
306
  modes: Record<string, DataTableFilterMode>,
307
+ storage?: DataTablePersistenceStorage,
226
308
  ): string[] => {
227
309
  const declared = declaredShownFilters(modes);
228
310
  const stored = key
229
- ? readPersisted<Partial<PersistedFilterVisibility>>(key, "filterVisibility")
311
+ ? readPersisted<Partial<PersistedFilterVisibility>>(
312
+ key,
313
+ "filterVisibility",
314
+ storage,
315
+ )
230
316
  : undefined;
231
317
  if (!stored) return declared;
232
318
  const listOf = (value: unknown): string[] =>
@@ -118,6 +118,13 @@ export interface RowAction<T> {
118
118
  onClick: (item: T, ctx: RowActionContext) => unknown;
119
119
  destructive?: boolean;
120
120
  disabled?: (item: T) => boolean;
121
+ /**
122
+ * A short muted note drawn after the label, most often why a `disabled`
123
+ * entry cannot be picked ("No Agent host ready"). A disabled menu item
124
+ * takes no pointer events, so a `title` tooltip on it would never show:
125
+ * the reason has to be on the entry itself.
126
+ */
127
+ hint?: (item: T) => string | undefined;
121
128
  /**
122
129
  * Whether this entry is the row's current value, for a group that picks
123
130
  * one of several - which release the row ships in, which area it sits in.
@@ -134,11 +141,20 @@ export interface RowAction<T> {
134
141
  * out of a list is also a delete.
135
142
  */
136
143
  checked?: (item: T) => boolean;
144
+ /**
145
+ * Draw a separator under this entry, when another entry of the menu
146
+ * follows it: a decision that leads the menu, set apart from the verbs
147
+ * under it. The separator above the first destructive entry is drawn
148
+ * without asking. Read by the three-dots menu only, not inside a submenu
149
+ * nor by `rowActionsDisplay: "inline"`.
150
+ */
151
+ separatorAfter?: boolean;
137
152
  }
138
153
 
139
154
  /**
140
155
  * A row action that is a submenu: a label, an icon, and the actions it
141
- * holds. One level only: a group's children are actions, never groups.
156
+ * holds. A child may be a group itself, drawn as a submenu inside the
157
+ * submenu: `Agent prompts > Review Epic > Copy`.
142
158
  *
143
159
  * The discriminant is the presence of `children`, so every caller that
144
160
  * returns a plain `RowAction[]` keeps compiling unchanged.
@@ -146,12 +162,13 @@ export interface RowAction<T> {
146
162
  * ⚠️ A group with no children renders nothing, and does not count towards
147
163
  * the row menu existing at all: a row whose only entry is an empty group
148
164
  * gets no three-dots trigger. That lets a caller build `children`
149
- * conditionally without checking whether anything survived.
165
+ * conditionally without checking whether anything survived. A group whose
166
+ * children are all empty groups is empty too, at any depth.
150
167
  */
151
168
  export interface RowActionGroup<T> {
152
169
  label: string;
153
170
  icon?: IconType;
154
- children: RowAction<T>[];
171
+ children: RowActionEntry<T>[];
155
172
  }
156
173
 
157
174
  export type RowActionEntry<T> = RowAction<T> | RowActionGroup<T>;