@exegia/corpora-ui 0.19.0 → 0.21.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 (162) hide show
  1. package/dist-lib/components/blocks/auth/__tests__/auth-state-atom.test.d.ts +1 -0
  2. package/dist-lib/components/blocks/auth/auth-flow-atom.d.ts +77 -0
  3. package/dist-lib/components/blocks/auth/auth-session-atom.d.ts +28 -0
  4. package/dist-lib/components/blocks/auth/auth-state-type.d.ts +71 -0
  5. package/dist-lib/components/blocks/auth/auth-state.d.ts +9 -0
  6. package/dist-lib/components/blocks/auth/use-auth-state.d.ts +52 -0
  7. package/dist-lib/components/blocks/nav/sidebar/__tests__/ai-sidebar-atom.test.d.ts +1 -0
  8. package/dist-lib/components/blocks/nav/sidebar/__tests__/use-ai-sidebar.test.d.ts +1 -0
  9. package/dist-lib/components/blocks/nav/sidebar/ai-sidebar-atom.d.ts +217 -0
  10. package/dist-lib/components/blocks/nav/sidebar/ai-sidebar.d.ts +11 -2
  11. package/dist-lib/components/blocks/nav/sidebar/index.d.ts +3 -1
  12. package/dist-lib/components/blocks/nav/sidebar/sidebar-context.d.ts +3 -0
  13. package/dist-lib/components/blocks/nav/sidebar/sidebar-row.d.ts +3 -1
  14. package/dist-lib/components/blocks/nav/sidebar/type.d.ts +191 -29
  15. package/dist-lib/components/blocks/nav/sidebar/use-ai-sidebar-state.d.ts +29 -0
  16. package/dist-lib/components/blocks/nav/sidebar/use-ai-sidebar.d.ts +27 -31
  17. package/dist-lib/components/blocks/nav/sidebar/utils.d.ts +6 -1
  18. package/dist-lib/components/blocks/profile/index.d.ts +7 -0
  19. package/dist-lib/components/blocks/profile/profile-card-atom.d.ts +68 -0
  20. package/dist-lib/components/blocks/profile/profile-card-block.d.ts +28 -39
  21. package/dist-lib/components/blocks/profile/type.d.ts +80 -0
  22. package/dist-lib/components/blocks/profile/use-profile-card-state.d.ts +24 -0
  23. package/dist-lib/components/blocks/profile/use-profile-card.d.ts +34 -0
  24. package/dist-lib/components/blocks/scaffold/constants.d.ts +5 -0
  25. package/dist-lib/components/blocks/scaffold/scaffold-canvas.d.ts +2 -0
  26. package/dist-lib/components/blocks/scaffold/scaffold-panel.d.ts +1 -1
  27. package/dist-lib/components/blocks/scaffold/scaffold-tab.d.ts +6 -1
  28. package/dist-lib/components/blocks/scaffold/type.d.ts +28 -0
  29. package/dist-lib/components/blocks/scaffold/use-panel-visibility.d.ts +19 -0
  30. package/dist-lib/components/blocks/scaffold/utils.d.ts +6 -0
  31. package/dist-lib/components/blocks/shell/__tests__/shell-fit-atom.test.d.ts +1 -0
  32. package/dist-lib/components/blocks/shell/__tests__/shell-metrics.test.d.ts +1 -0
  33. package/dist-lib/components/blocks/shell/animated-panel-provider.d.ts +1 -1
  34. package/dist-lib/components/blocks/shell/animated-panel.d.ts +1 -1
  35. package/dist-lib/components/blocks/shell/index.d.ts +4 -0
  36. package/dist-lib/components/blocks/shell/shell-fit-atom.d.ts +68 -0
  37. package/dist-lib/components/blocks/shell/shell-metrics.d.ts +54 -0
  38. package/dist-lib/components/blocks/shell/type.d.ts +115 -3
  39. package/dist-lib/components/blocks/shell/use-shell-fit-state.d.ts +25 -0
  40. package/dist-lib/components/blocks/shell/use-shell-fit.d.ts +14 -0
  41. package/dist-lib/components/blocks/shell/use-shell-panels.d.ts +12 -1
  42. package/dist-lib/components/blocks/shell/utils.d.ts +32 -0
  43. package/dist-lib/components/composed/tree/__tests__/tree-atom.test.d.ts +1 -0
  44. package/dist-lib/components/composed/tree/__tests__/tree.test.d.ts +1 -0
  45. package/dist-lib/components/composed/tree/__tests__/use-tree.test.d.ts +1 -0
  46. package/dist-lib/components/composed/tree/constants.d.ts +35 -0
  47. package/dist-lib/components/composed/tree/index.d.ts +6 -0
  48. package/dist-lib/components/composed/tree/tree-atom.d.ts +160 -0
  49. package/dist-lib/components/composed/tree/tree-context.d.ts +4 -0
  50. package/dist-lib/components/composed/tree/tree-node.d.ts +12 -0
  51. package/dist-lib/components/composed/tree/tree.d.ts +19 -0
  52. package/dist-lib/components/composed/tree/type.d.ts +290 -0
  53. package/dist-lib/components/composed/tree/use-tree-dnd.d.ts +18 -0
  54. package/dist-lib/components/composed/tree/use-tree-state.d.ts +25 -0
  55. package/dist-lib/components/composed/tree/use-tree.d.ts +24 -0
  56. package/dist-lib/components/composed/tree/utils.d.ts +28 -0
  57. package/dist-lib/components/composed/user-avatar.d.ts +5 -28
  58. package/dist-lib/components/user-avatar/__tests__/user-avatar.test.d.ts +1 -0
  59. package/dist-lib/components/user-avatar/audio-wave.d.ts +2 -0
  60. package/dist-lib/components/user-avatar/component.d.ts +3 -0
  61. package/dist-lib/components/user-avatar/fallback.d.ts +5 -0
  62. package/dist-lib/components/user-avatar/index.d.ts +18 -0
  63. package/dist-lib/components/user-avatar/presence-badge.d.ts +16 -0
  64. package/dist-lib/components/user-avatar/type.d.ts +71 -0
  65. package/dist-lib/components/user-avatar/use-user-avatar-state.d.ts +26 -0
  66. package/dist-lib/components/user-avatar/use-user-avatar.d.ts +32 -0
  67. package/dist-lib/components/user-avatar/user-avatar-atom.d.ts +55 -0
  68. package/dist-lib/components/user-avatar/utils.d.ts +2 -0
  69. package/dist-lib/index.d.ts +6 -2
  70. package/dist-lib/index.js +3576 -1366
  71. package/dist-lib/index.js.map +1 -1
  72. package/dist-lib/state/exegia-provider.d.ts +51 -0
  73. package/dist-lib/state/index.d.ts +4 -0
  74. package/dist-lib/state/store.d.ts +17 -0
  75. package/package.json +16 -12
  76. package/src/components/beste/piece/browser-frame.tsx +9 -6
  77. package/src/components/blocks/auth/__tests__/auth-state-atom.test.tsx +247 -0
  78. package/src/components/blocks/auth/auth-flow-atom.ts +238 -0
  79. package/src/components/blocks/auth/auth-session-atom.ts +82 -0
  80. package/src/components/blocks/auth/auth-state-type.ts +97 -0
  81. package/src/components/blocks/auth/auth-state.ts +52 -0
  82. package/src/components/blocks/auth/use-auth-state.ts +127 -0
  83. package/src/components/blocks/nav/sidebar/__tests__/ai-sidebar-atom.test.tsx +325 -0
  84. package/src/components/blocks/nav/sidebar/__tests__/use-ai-sidebar.test.tsx +310 -0
  85. package/src/components/blocks/nav/sidebar/ai-sidebar-atom.ts +698 -0
  86. package/src/components/blocks/nav/sidebar/ai-sidebar.tsx +112 -76
  87. package/src/components/blocks/nav/sidebar/index.ts +53 -1
  88. package/src/components/blocks/nav/sidebar/sidebar-context.ts +15 -0
  89. package/src/components/blocks/nav/sidebar/sidebar-row.tsx +119 -50
  90. package/src/components/blocks/nav/sidebar/type.ts +228 -29
  91. package/src/components/blocks/nav/sidebar/use-ai-sidebar-state.ts +123 -0
  92. package/src/components/blocks/nav/sidebar/use-ai-sidebar.ts +455 -225
  93. package/src/components/blocks/nav/sidebar/utils.ts +29 -1
  94. package/src/components/blocks/profile/__tests__/profile-card-block.test.tsx +194 -0
  95. package/src/components/blocks/profile/index.ts +28 -0
  96. package/src/components/blocks/profile/profile-card-atom.ts +247 -0
  97. package/src/components/blocks/profile/profile-card-block.tsx +153 -74
  98. package/src/components/blocks/profile/type.ts +95 -0
  99. package/src/components/blocks/profile/use-profile-card-state.ts +67 -0
  100. package/src/components/blocks/profile/use-profile-card.ts +126 -0
  101. package/src/components/blocks/scaffold/__tests__/scaffold.test.tsx +160 -1
  102. package/src/components/blocks/scaffold/constants.ts +7 -0
  103. package/src/components/blocks/scaffold/panel-menu-button.tsx +3 -0
  104. package/src/components/blocks/scaffold/scaffold-canvas.tsx +48 -5
  105. package/src/components/blocks/scaffold/scaffold-inspector.tsx +3 -2
  106. package/src/components/blocks/scaffold/scaffold-main.tsx +1 -1
  107. package/src/components/blocks/scaffold/scaffold-panel.tsx +41 -5
  108. package/src/components/blocks/scaffold/scaffold-root.tsx +18 -3
  109. package/src/components/blocks/scaffold/scaffold-sidebar.tsx +1 -1
  110. package/src/components/blocks/scaffold/scaffold-tab.tsx +45 -7
  111. package/src/components/blocks/scaffold/type.ts +28 -0
  112. package/src/components/blocks/scaffold/use-panel-visibility.ts +180 -0
  113. package/src/components/blocks/scaffold/utils.ts +19 -0
  114. package/src/components/blocks/shell/__tests__/shell-fit-atom.test.tsx +360 -0
  115. package/src/components/blocks/shell/__tests__/shell-layout.test.tsx +192 -3
  116. package/src/components/blocks/shell/__tests__/shell-metrics.test.ts +108 -0
  117. package/src/components/blocks/shell/animated-panel-inset.tsx +5 -1
  118. package/src/components/blocks/shell/animated-panel-provider.tsx +77 -11
  119. package/src/components/blocks/shell/animated-panel-trigger.tsx +4 -0
  120. package/src/components/blocks/shell/animated-panel.tsx +126 -29
  121. package/src/components/blocks/shell/index.ts +18 -0
  122. package/src/components/blocks/shell/shell-fit-atom.ts +243 -0
  123. package/src/components/blocks/shell/shell-layout.tsx +55 -53
  124. package/src/components/blocks/shell/shell-metrics.ts +79 -0
  125. package/src/components/blocks/shell/type.ts +130 -3
  126. package/src/components/blocks/shell/use-shell-fit-state.ts +49 -0
  127. package/src/components/blocks/shell/use-shell-fit.ts +135 -0
  128. package/src/components/blocks/shell/use-shell-panels.ts +57 -4
  129. package/src/components/blocks/shell/utils.ts +44 -4
  130. package/src/components/composed/tree/CLAUDE.md +132 -0
  131. package/src/components/composed/tree/__tests__/tree-atom.test.tsx +217 -0
  132. package/src/components/composed/tree/__tests__/tree.test.tsx +525 -0
  133. package/src/components/composed/tree/__tests__/use-tree.test.tsx +333 -0
  134. package/src/components/composed/tree/constants.ts +60 -0
  135. package/src/components/composed/tree/index.ts +51 -0
  136. package/src/components/composed/tree/tree-atom.ts +590 -0
  137. package/src/components/composed/tree/tree-context.ts +13 -0
  138. package/src/components/composed/tree/tree-node.tsx +490 -0
  139. package/src/components/composed/tree/tree.tsx +286 -0
  140. package/src/components/composed/tree/type.ts +322 -0
  141. package/src/components/composed/tree/use-tree-dnd.ts +179 -0
  142. package/src/components/composed/tree/use-tree-state.ts +105 -0
  143. package/src/components/composed/tree/use-tree.ts +307 -0
  144. package/src/components/composed/tree/utils.ts +172 -0
  145. package/src/components/composed/user-avatar.tsx +11 -96
  146. package/src/components/docs/component-preview.tsx +11 -11
  147. package/src/components/user-avatar/__tests__/user-avatar.test.tsx +245 -0
  148. package/src/components/user-avatar/audio-wave.tsx +22 -0
  149. package/src/components/user-avatar/component.tsx +152 -0
  150. package/src/components/user-avatar/fallback.tsx +26 -0
  151. package/src/components/user-avatar/index.ts +34 -0
  152. package/src/components/user-avatar/presence-badge.tsx +56 -0
  153. package/src/components/user-avatar/type.ts +85 -0
  154. package/src/components/user-avatar/use-user-avatar-state.ts +60 -0
  155. package/src/components/user-avatar/use-user-avatar.ts +144 -0
  156. package/src/components/user-avatar/user-avatar-atom.ts +198 -0
  157. package/src/components/user-avatar/utils.ts +8 -0
  158. package/src/index.css +211 -153
  159. package/src/index.ts +35 -2
  160. package/src/state/exegia-provider.tsx +79 -0
  161. package/src/state/index.ts +4 -0
  162. package/src/state/store.ts +19 -0
@@ -0,0 +1,698 @@
1
+ /**
2
+ * Per-instance Jotai state for the AI sidebar.
3
+ *
4
+ * Every atom is a module-level family keyed by a sidebar id, so one
5
+ * `ExegiaProvider` at the app root is enough: many sidebars coexist in one
6
+ * store without a provider each, and any component can drive a sidebar by id
7
+ * without holding its controller.
8
+ *
9
+ * Controlled props stay authoritative. `useAISidebar` projects them into
10
+ * these atoms on every commit so the action atoms and remote readers work off
11
+ * current data — a one-way projection, not a second source of truth. The
12
+ * write gates below (`controlsItems`, `controlsActiveId`,
13
+ * `controlsExpandedIds`) are what keep the store from overwriting a prop.
14
+ *
15
+ * DOM nodes are deliberately absent: the roving-focus ref map belongs to the
16
+ * block that renders the rows, not to the store.
17
+ */
18
+ import { atom } from "jotai";
19
+ import type { Atom, Getter, Setter } from "jotai";
20
+
21
+ import type {
22
+ AISidebarConfig,
23
+ AISidebarHandlers,
24
+ AISidebarInstanceId,
25
+ AISidebarSeed,
26
+ AISidebarState,
27
+ DropTarget,
28
+ FlatResource,
29
+ SidebarResource,
30
+ SidebarResourceDropPosition,
31
+ SidebarResourceMove,
32
+ } from "./type.ts";
33
+ import {
34
+ ancestorIdsOf,
35
+ expandableIdsOf,
36
+ findResource,
37
+ flattenResources,
38
+ moveResource,
39
+ renameResource,
40
+ } from "./utils.ts";
41
+
42
+ const NO_ITEMS: SidebarResource[] = [];
43
+ const NO_ROWS: FlatResource[] = [];
44
+ const NO_IDS: ReadonlySet<string> = new Set<string>();
45
+ const NO_HANDLERS: AISidebarHandlers = {};
46
+
47
+ /** What a sidebar reads as before `useAISidebar` publishes its options. */
48
+ export const DEFAULT_AI_SIDEBAR_CONFIG: AISidebarConfig = {
49
+ controlsItems: false,
50
+ controlsActiveId: false,
51
+ controlsExpandedIds: false,
52
+ };
53
+
54
+ const DEFAULT_AI_SIDEBAR_SEED: AISidebarSeed = {
55
+ items: NO_ITEMS,
56
+ activeId: null,
57
+ focusedId: null,
58
+ expandedIds: [],
59
+ };
60
+
61
+ /**
62
+ * A string-keyed atom family.
63
+ *
64
+ * `jotai/utils`' `atomFamily` is deprecated for Jotai v3, and we need only
65
+ * the string-keyed case with a `remove` — so this stays in-house rather than
66
+ * adding `jotai-family` as a second Jotai package to keep version-aligned.
67
+ * Dropping a key lets the store's WeakMap release that instance's state.
68
+ */
69
+ type Family<AtomType> = ((id: AISidebarInstanceId) => AtomType) & {
70
+ remove: (id: AISidebarInstanceId) => void;
71
+ };
72
+
73
+ /** Every family, so `removeAISidebarInstance` can drop an id from all. */
74
+ const families: { remove: (id: AISidebarInstanceId) => void }[] = [];
75
+
76
+ function keyed<AtomType>(create: (id: AISidebarInstanceId) => AtomType) {
77
+ const cache = new Map<AISidebarInstanceId, AtomType>();
78
+ const family = ((id: AISidebarInstanceId) => {
79
+ let instance = cache.get(id);
80
+ if (instance === undefined) {
81
+ instance = create(id);
82
+ cache.set(id, instance);
83
+ }
84
+ return instance;
85
+ }) as Family<AtomType>;
86
+ family.remove = (id: AISidebarInstanceId) => {
87
+ cache.delete(id);
88
+ };
89
+ families.push(family);
90
+ return family;
91
+ }
92
+
93
+ function stateFamily<Value>(name: string, initialValue: Value) {
94
+ return keyed((id) => {
95
+ const instance = atom(initialValue);
96
+ instance.debugLabel = `ai-sidebar/${id}/${name}`;
97
+ return instance;
98
+ });
99
+ }
100
+
101
+ function readFamily<Value>(
102
+ name: string,
103
+ read: (get: Getter, id: AISidebarInstanceId) => Value
104
+ ) {
105
+ return keyed((id) => {
106
+ const instance = atom((get) => read(get, id));
107
+ instance.debugLabel = `ai-sidebar/${id}/${name}`;
108
+ return instance;
109
+ });
110
+ }
111
+
112
+ function actionFamily<Args extends unknown[], Result = void>(
113
+ name: string,
114
+ write: (
115
+ get: Getter,
116
+ set: Setter,
117
+ id: AISidebarInstanceId,
118
+ ...args: Args
119
+ ) => Result
120
+ ) {
121
+ return keyed((id) => {
122
+ const instance = atom(null, (get, set, ...args: Args) =>
123
+ write(get, set, id, ...args)
124
+ );
125
+ instance.debugLabel = `ai-sidebar/${id}/${name}`;
126
+ return instance;
127
+ });
128
+ }
129
+
130
+ /**
131
+ * A per-row derived atom, keyed by sidebar and then row id.
132
+ *
133
+ * This is where Jotai earns its place: a row subscribed to
134
+ * `aiSidebarRowExpandedAtom(sidebarId, row.item.id)` re-renders when *its*
135
+ * branch opens and stays put when a sibling's does. Reading the whole
136
+ * controller through props instead re-rendered every row on every hover.
137
+ *
138
+ * The outer family registers for cleanup, so `removeAISidebarInstance` drops
139
+ * all of a sidebar's row atoms with it.
140
+ */
141
+ function rowFamily<Value>(
142
+ name: string,
143
+ read: (get: Getter, id: AISidebarInstanceId, rowId: string) => Value
144
+ ) {
145
+ const outer = keyed((id: AISidebarInstanceId) => {
146
+ const inner = new Map<string, Atom<Value>>();
147
+ return (rowId: string) => {
148
+ let instance = inner.get(rowId);
149
+ if (instance === undefined) {
150
+ instance = atom((get) => read(get, id, rowId));
151
+ instance.debugLabel = `ai-sidebar/${id}/${name}/${rowId}`;
152
+ inner.set(rowId, instance);
153
+ }
154
+ return instance;
155
+ };
156
+ });
157
+ return (id: AISidebarInstanceId, rowId: string) => outer(id)(rowId);
158
+ }
159
+
160
+ // ── state ──────────────────────────────────────────────────────────────────
161
+
162
+ /** @internal */
163
+ export const aiSidebarConfigAtom = stateFamily<AISidebarConfig>(
164
+ "config",
165
+ DEFAULT_AI_SIDEBAR_CONFIG
166
+ );
167
+ /** @internal Refreshed every commit; nothing subscribes, so it is free. */
168
+ export const aiSidebarHandlersAtom = stateFamily<AISidebarHandlers>(
169
+ "handlers",
170
+ NO_HANDLERS
171
+ );
172
+ const aiSidebarSeedAtom = stateFamily<AISidebarSeed>(
173
+ "seed",
174
+ DEFAULT_AI_SIDEBAR_SEED
175
+ );
176
+ const aiSidebarInitializedAtom = stateFamily<boolean>("initialized", false);
177
+
178
+ /** The sidebar's current data — hook-owned, or the projection of a
179
+ * controlled `items` prop. */
180
+ export const aiSidebarItemsAtom = stateFamily<SidebarResource[]>(
181
+ "items",
182
+ NO_ITEMS
183
+ );
184
+ /** @internal The hook-owned selection. `select` writes it only while no
185
+ * `activeId` prop owns the selection — read `aiSidebarActiveIdAtom` for the
186
+ * value the sidebar actually renders. */
187
+ export const aiSidebarOwnActiveIdAtom = stateFamily<string | null>(
188
+ "ownActiveId",
189
+ null
190
+ );
191
+ /** @internal Projection of a controlled `activeId` prop; `undefined` while
192
+ * the selection is hook-owned. */
193
+ export const aiSidebarControlledActiveIdAtom = stateFamily<
194
+ string | null | undefined
195
+ >("controlledActiveId", undefined);
196
+ /** Open rows — hook-owned, or the projection of a controlled `expandedIds`
197
+ * prop. */
198
+ export const aiSidebarExpandedIdsAtom = stateFamily<ReadonlySet<string>>(
199
+ "expandedIds",
200
+ NO_IDS
201
+ );
202
+ /** Roving-focus target. DOM focus is moved by the block, which owns the row
203
+ * refs; this is only which row is tabbable. */
204
+ export const aiSidebarFocusedIdAtom = stateFamily<string | null>(
205
+ "focusedId",
206
+ null
207
+ );
208
+ export const aiSidebarDraggingIdAtom = stateFamily<string | null>(
209
+ "draggingId",
210
+ null
211
+ );
212
+ export const aiSidebarDropTargetAtom = stateFamily<DropTarget | null>(
213
+ "dropTarget",
214
+ null
215
+ );
216
+ export const aiSidebarMenuOpenIdAtom = stateFamily<string | null>(
217
+ "menuOpenId",
218
+ null
219
+ );
220
+ export const aiSidebarRenamingIdAtom = stateFamily<string | null>(
221
+ "renamingId",
222
+ null
223
+ );
224
+ export const aiSidebarHoveredIdAtom = stateFamily<string | null>(
225
+ "hoveredId",
226
+ null
227
+ );
228
+ /** Live-region text for the last move or rename outcome. */
229
+ export const aiSidebarAnnouncementAtom = stateFamily<string>(
230
+ "announcement",
231
+ ""
232
+ );
233
+ /** A move is in flight. Read synchronously by `moveAISidebarRowAtom` before
234
+ * it starts another — the store needs no ref to see its own latest write. */
235
+ export const aiSidebarMovePendingAtom = stateFamily<boolean>(
236
+ "movePending",
237
+ false
238
+ );
239
+
240
+ // ── derived ────────────────────────────────────────────────────────────────
241
+
242
+ /** @internal The only items atom `useAISidebar` may subscribe to: empty
243
+ * while a controlled `items` prop owns the data, so the projection write
244
+ * never feeds back into the render that produced it. An inline `items={[…]}`
245
+ * array is a new reference every render, and a subscribed round-trip would
246
+ * loop. */
247
+ export const aiSidebarOwnedItemsAtom = readFamily("ownedItems", (get, id) =>
248
+ get(aiSidebarConfigAtom(id)).controlsItems
249
+ ? NO_ITEMS
250
+ : get(aiSidebarItemsAtom(id))
251
+ );
252
+
253
+ /** @internal The same guard for `expandedIds`, which is controllable here
254
+ * and arrives as an array — just as unstable an identity as `items`. */
255
+ export const aiSidebarOwnedExpandedIdsAtom = readFamily(
256
+ "ownedExpandedIds",
257
+ (get, id) =>
258
+ get(aiSidebarConfigAtom(id)).controlsExpandedIds
259
+ ? NO_IDS
260
+ : get(aiSidebarExpandedIdsAtom(id))
261
+ );
262
+
263
+ /** The selected row: the controlled `activeId` when there is one, the
264
+ * hook-owned selection otherwise. Kept as two atoms because a controlled
265
+ * `activeId` of `null` falls through to the hook-owned value — the block has
266
+ * always read `activeId ?? internalActiveId`. */
267
+ export const aiSidebarActiveIdAtom = readFamily(
268
+ "activeId",
269
+ (get, id) =>
270
+ get(aiSidebarControlledActiveIdAtom(id)) ??
271
+ get(aiSidebarOwnActiveIdAtom(id))
272
+ );
273
+
274
+ /** Visible rows in order, each with its depth and parent id. */
275
+ export const aiSidebarFlatAtom = readFamily("flat", (get, id) => {
276
+ const items = get(aiSidebarItemsAtom(id));
277
+ if (items.length === 0) return NO_ROWS;
278
+ return flattenResources(items, get(aiSidebarExpandedIdsAtom(id)));
279
+ });
280
+
281
+ /** Some row is hovered and no drag is running — mounts the pill wrapper. */
282
+ export const aiSidebarHoverActiveAtom = readFamily(
283
+ "hoverActive",
284
+ (get, id) =>
285
+ get(aiSidebarHoveredIdAtom(id)) !== null &&
286
+ get(aiSidebarDraggingIdAtom(id)) === null
287
+ );
288
+
289
+ /** Whether this one row is the current selection. */
290
+ export const aiSidebarRowSelectedAtom = rowFamily(
291
+ "rowSelected",
292
+ (get, id, rowId) => get(aiSidebarActiveIdAtom(id)) === rowId
293
+ );
294
+
295
+ /** Whether this one row is open. */
296
+ export const aiSidebarRowExpandedAtom = rowFamily(
297
+ "rowExpanded",
298
+ (get, id, rowId) => get(aiSidebarExpandedIdsAtom(id)).has(rowId)
299
+ );
300
+
301
+ /** Whether this one row is the roving-focus target. */
302
+ export const aiSidebarRowFocusedAtom = rowFamily(
303
+ "rowFocused",
304
+ (get, id, rowId) => get(aiSidebarFocusedIdAtom(id)) === rowId
305
+ );
306
+
307
+ /** Whether this one row is in rename mode. */
308
+ export const aiSidebarRowRenamingAtom = rowFamily(
309
+ "rowRenaming",
310
+ (get, id, rowId) => get(aiSidebarRenamingIdAtom(id)) === rowId
311
+ );
312
+
313
+ /** Whether this one row's action menu is open. */
314
+ export const aiSidebarRowMenuOpenAtom = rowFamily(
315
+ "rowMenuOpen",
316
+ (get, id, rowId) => get(aiSidebarMenuOpenIdAtom(id)) === rowId
317
+ );
318
+
319
+ /** Whether this one row is the row in flight. */
320
+ export const aiSidebarRowDraggingAtom = rowFamily(
321
+ "rowDragging",
322
+ (get, id, rowId) => get(aiSidebarDraggingIdAtom(id)) === rowId
323
+ );
324
+
325
+ /** Where a drop on this one row would land, `null` when it is not hovered
326
+ * by a drag. */
327
+ export const aiSidebarRowDropAtom =
328
+ rowFamily<SidebarResourceDropPosition | null>("rowDrop", (get, id, rowId) => {
329
+ const target = get(aiSidebarDropTargetAtom(id));
330
+ return target?.id === rowId ? target.position : null;
331
+ });
332
+
333
+ /** Whether this one row owns the sliding hover pill. */
334
+ export const aiSidebarRowHoveredAtom = rowFamily(
335
+ "rowHovered",
336
+ (get, id, rowId) =>
337
+ get(aiSidebarHoveredIdAtom(id)) === rowId &&
338
+ get(aiSidebarDraggingIdAtom(id)) === null
339
+ );
340
+
341
+ /** The whole state of one sidebar. Components reading a single field should
342
+ * subscribe to that field's atom instead — this one changes on every edit. */
343
+ export const aiSidebarStateAtom = readFamily<AISidebarState>(
344
+ "state",
345
+ (get, id) => ({
346
+ items: get(aiSidebarItemsAtom(id)),
347
+ flat: get(aiSidebarFlatAtom(id)),
348
+ selectedId: get(aiSidebarActiveIdAtom(id)),
349
+ expandedIds: get(aiSidebarExpandedIdsAtom(id)),
350
+ focusedId: get(aiSidebarFocusedIdAtom(id)),
351
+ renamingId: get(aiSidebarRenamingIdAtom(id)),
352
+ menuOpenId: get(aiSidebarMenuOpenIdAtom(id)),
353
+ draggingId: get(aiSidebarDraggingIdAtom(id)),
354
+ dropTarget: get(aiSidebarDropTargetAtom(id)),
355
+ hoveredId: get(aiSidebarHoveredIdAtom(id)),
356
+ movePending: get(aiSidebarMovePendingAtom(id)),
357
+ announcement: get(aiSidebarAnnouncementAtom(id)),
358
+ })
359
+ );
360
+
361
+ // ── shared write helpers ───────────────────────────────────────────────────
362
+
363
+ /** The atom only when the hook owns the data; the callback always. */
364
+ function commitItems(
365
+ get: Getter,
366
+ set: Setter,
367
+ id: AISidebarInstanceId,
368
+ next: SidebarResource[]
369
+ ): void {
370
+ if (!get(aiSidebarConfigAtom(id)).controlsItems)
371
+ set(aiSidebarItemsAtom(id), next);
372
+ get(aiSidebarHandlersAtom(id)).onItemsChange?.(next);
373
+ }
374
+
375
+ function commitExpanded(
376
+ get: Getter,
377
+ set: Setter,
378
+ id: AISidebarInstanceId,
379
+ next: Set<string>
380
+ ): void {
381
+ if (!get(aiSidebarConfigAtom(id)).controlsExpandedIds)
382
+ set(aiSidebarExpandedIdsAtom(id), next);
383
+ get(aiSidebarHandlersAtom(id)).onExpandedChange?.([...next]);
384
+ }
385
+
386
+ function seedInstance(
387
+ set: Setter,
388
+ id: AISidebarInstanceId,
389
+ seed: AISidebarSeed
390
+ ): void {
391
+ set(aiSidebarItemsAtom(id), seed.items);
392
+ set(aiSidebarOwnActiveIdAtom(id), seed.activeId);
393
+ set(aiSidebarFocusedIdAtom(id), seed.focusedId);
394
+ set(aiSidebarExpandedIdsAtom(id), new Set(seed.expandedIds));
395
+ set(aiSidebarRenamingIdAtom(id), null);
396
+ set(aiSidebarMenuOpenIdAtom(id), null);
397
+ set(aiSidebarDraggingIdAtom(id), null);
398
+ set(aiSidebarDropTargetAtom(id), null);
399
+ set(aiSidebarHoveredIdAtom(id), null);
400
+ set(aiSidebarAnnouncementAtom(id), "");
401
+ set(aiSidebarMovePendingAtom(id), false);
402
+ }
403
+
404
+ // ── expansion ──────────────────────────────────────────────────────────────
405
+
406
+ export const expandAISidebarRowAtom = actionFamily<[rowId: string]>(
407
+ "expand",
408
+ (get, set, id, rowId) => {
409
+ const expanded = get(aiSidebarExpandedIdsAtom(id));
410
+ if (expanded.has(rowId)) return;
411
+ commitExpanded(get, set, id, new Set(expanded).add(rowId));
412
+ }
413
+ );
414
+
415
+ export const collapseAISidebarRowAtom = actionFamily<[rowId: string]>(
416
+ "collapse",
417
+ (get, set, id, rowId) => {
418
+ const expanded = get(aiSidebarExpandedIdsAtom(id));
419
+ if (!expanded.has(rowId)) return;
420
+ const next = new Set(expanded);
421
+ next.delete(rowId);
422
+ commitExpanded(get, set, id, next);
423
+ }
424
+ );
425
+
426
+ export const toggleAISidebarRowAtom = actionFamily<[rowId: string]>(
427
+ "toggleExpanded",
428
+ (get, set, id, rowId) => {
429
+ const next = new Set(get(aiSidebarExpandedIdsAtom(id)));
430
+ if (next.has(rowId)) next.delete(rowId);
431
+ else next.add(rowId);
432
+ commitExpanded(get, set, id, next);
433
+ }
434
+ );
435
+
436
+ export const expandAllAISidebarRowsAtom = actionFamily<[]>(
437
+ "expandAll",
438
+ (get, set, id) => {
439
+ commitExpanded(
440
+ get,
441
+ set,
442
+ id,
443
+ new Set(expandableIdsOf(get(aiSidebarItemsAtom(id))))
444
+ );
445
+ }
446
+ );
447
+
448
+ export const collapseAllAISidebarRowsAtom = actionFamily<[]>(
449
+ "collapseAll",
450
+ (get, set, id) => {
451
+ commitExpanded(get, set, id, new Set());
452
+ }
453
+ );
454
+
455
+ /** Open every ancestor of `rowId` without touching the rest of the tree. */
456
+ export const revealAISidebarRowAtom = actionFamily<[rowId: string]>(
457
+ "reveal",
458
+ (get, set, id, rowId) => {
459
+ const ancestors = ancestorIdsOf(get(aiSidebarItemsAtom(id)), rowId);
460
+ const expanded = get(aiSidebarExpandedIdsAtom(id));
461
+ if (!ancestors.some((ancestor) => !expanded.has(ancestor))) return;
462
+ commitExpanded(get, set, id, new Set([...expanded, ...ancestors]));
463
+ }
464
+ );
465
+
466
+ // ── selection and focus ────────────────────────────────────────────────────
467
+
468
+ export const selectAISidebarRowAtom = actionFamily<[rowId: string]>(
469
+ "select",
470
+ (get, set, id, rowId) => {
471
+ if (!get(aiSidebarConfigAtom(id)).controlsActiveId)
472
+ set(aiSidebarOwnActiveIdAtom(id), rowId);
473
+ get(aiSidebarHandlersAtom(id)).onActiveChange?.(rowId);
474
+ }
475
+ );
476
+
477
+ /** The store half of `focus`: which row is tabbable. Moving DOM focus is the
478
+ * block's job — it owns the row refs. */
479
+ export const focusAISidebarRowAtom = actionFamily<[rowId: string]>(
480
+ "focus",
481
+ (_get, set, id, rowId) => {
482
+ set(aiSidebarFocusedIdAtom(id), rowId);
483
+ }
484
+ );
485
+
486
+ /** @internal Keeps the roving target on a row that still renders: a focused
487
+ * row that folds away hands focus to the first visible row instead of
488
+ * pointing at nothing. `useAISidebar` runs it after every projection. */
489
+ export const normalizeAISidebarFocusAtom = actionFamily<[]>(
490
+ "normalizeFocus",
491
+ (get, set, id) => {
492
+ const flat = get(aiSidebarFlatAtom(id));
493
+ const focusedId = get(aiSidebarFocusedIdAtom(id));
494
+ if (focusedId !== null && flat.some((row) => row.item.id === focusedId))
495
+ return;
496
+ const first = flat[0]?.item.id ?? null;
497
+ if (focusedId !== first) set(aiSidebarFocusedIdAtom(id), first);
498
+ }
499
+ );
500
+
501
+ // ── rename ─────────────────────────────────────────────────────────────────
502
+
503
+ export const startAISidebarRenameAtom = actionFamily<[rowId: string]>(
504
+ "startRename",
505
+ (get, set, id, rowId) => {
506
+ // An id from elsewhere (a stale selection, another sidebar) would arm
507
+ // rename mode against a row that never renders.
508
+ if (!findResource(get(aiSidebarItemsAtom(id)), rowId)) return;
509
+ set(aiSidebarRenamingIdAtom(id), rowId);
510
+ }
511
+ );
512
+
513
+ export const cancelAISidebarRenameAtom = actionFamily<[]>(
514
+ "cancelRename",
515
+ (_get, set, id) => {
516
+ set(aiSidebarRenamingIdAtom(id), null);
517
+ }
518
+ );
519
+
520
+ /** Commit a rename and leave rename mode. Applied optimistically and rolled
521
+ * back if `onRename` rejects. Blank and unchanged labels are dropped. */
522
+ export const renameAISidebarRowAtom = actionFamily<
523
+ [rowId: string, label: string]
524
+ >("rename", (get, set, id, rowId, label) => {
525
+ const trimmed = label.trim();
526
+ set(aiSidebarRenamingIdAtom(id), null);
527
+ const before = get(aiSidebarItemsAtom(id));
528
+ const item = findResource(before, rowId);
529
+ if (!item || !trimmed || trimmed === item.label) return;
530
+ commitItems(get, set, id, renameResource(before, rowId, trimmed));
531
+ void Promise.resolve(
532
+ get(aiSidebarHandlersAtom(id)).onRename?.(item, trimmed)
533
+ ).catch(() => {
534
+ commitItems(get, set, id, before);
535
+ set(
536
+ aiSidebarAnnouncementAtom(id),
537
+ `Rename failed. ${item.label} was restored.`
538
+ );
539
+ });
540
+ });
541
+
542
+ // ── menu ───────────────────────────────────────────────────────────────────
543
+
544
+ export const openAISidebarMenuAtom = actionFamily<[rowId: string]>(
545
+ "openMenu",
546
+ (get, set, id, rowId) => {
547
+ if (!findResource(get(aiSidebarItemsAtom(id)), rowId)) return;
548
+ set(aiSidebarMenuOpenIdAtom(id), rowId);
549
+ }
550
+ );
551
+
552
+ /** Closes the menu and hands the roving target back to the row that owned
553
+ * it, then returns that id so the block can move DOM focus there — otherwise
554
+ * focus falls back to the body and the keyboard user loses their place. */
555
+ export const closeAISidebarMenuAtom = actionFamily<[], string | null>(
556
+ "closeMenu",
557
+ (get, set, id) => {
558
+ const current = get(aiSidebarMenuOpenIdAtom(id));
559
+ set(aiSidebarMenuOpenIdAtom(id), null);
560
+ if (current) set(aiSidebarFocusedIdAtom(id), current);
561
+ return current;
562
+ }
563
+ );
564
+
565
+ // ── data ───────────────────────────────────────────────────────────────────
566
+
567
+ /**
568
+ * Reorder, applied optimistically and rolled back if `onMove` rejects.
569
+ *
570
+ * The pending flag is read straight back out of the store, which is why this
571
+ * needs no ref to guard overlapping moves the way React state did: the write
572
+ * is visible to the next call immediately, not after a commit.
573
+ */
574
+ export const moveAISidebarRowAtom = actionFamily<
575
+ [move: SidebarResourceMove],
576
+ Promise<void>
577
+ >("move", async (get, set, id, move) => {
578
+ if (get(aiSidebarMovePendingAtom(id))) {
579
+ set(aiSidebarAnnouncementAtom(id), "Wait for the current move to finish.");
580
+ return;
581
+ }
582
+ const before = get(aiSidebarItemsAtom(id));
583
+ const next = moveResource(before, move);
584
+ if (!next || next === before) return;
585
+
586
+ set(aiSidebarMovePendingAtom(id), true);
587
+ commitItems(get, set, id, next);
588
+ set(aiSidebarDropTargetAtom(id), null);
589
+ set(aiSidebarDraggingIdAtom(id), null);
590
+ const moved = findResource(before, move.itemId);
591
+ const target = move.targetId ? findResource(before, move.targetId) : null;
592
+ set(
593
+ aiSidebarAnnouncementAtom(id),
594
+ target
595
+ ? `Moved ${moved?.label ?? "item"} ${move.position} ${target.label}.`
596
+ : `Moved ${moved?.label ?? "item"} to the top level.`
597
+ );
598
+
599
+ try {
600
+ await get(aiSidebarHandlersAtom(id)).onMove?.(move);
601
+ } catch (error) {
602
+ commitItems(get, set, id, before);
603
+ set(
604
+ aiSidebarAnnouncementAtom(id),
605
+ `Move failed. ${moved?.label ?? "Item"} was restored.`
606
+ );
607
+ get(aiSidebarHandlersAtom(id)).onMoveError?.(error, move);
608
+ } finally {
609
+ set(aiSidebarMovePendingAtom(id), false);
610
+ }
611
+ });
612
+
613
+ /** Replace the data. Reports through `onItemsChange`; writes the atom only
614
+ * when no controlled `items` prop owns it. */
615
+ export const setAISidebarItemsAtom = actionFamily<[items: SidebarResource[]]>(
616
+ "setItems",
617
+ (get, set, id, items) => {
618
+ commitItems(get, set, id, items);
619
+ }
620
+ );
621
+
622
+ // ── hover and drag state ───────────────────────────────────────────────────
623
+
624
+ export const setAISidebarHoveredAtom = actionFamily<
625
+ [rowId: string, hovered: boolean]
626
+ >("setHovered", (get, set, id, rowId, hovered) => {
627
+ const current = get(aiSidebarHoveredIdAtom(id));
628
+ const next = hovered ? rowId : current === rowId ? null : current;
629
+ if (next !== current) set(aiSidebarHoveredIdAtom(id), next);
630
+ });
631
+
632
+ export const clearAISidebarHoverAtom = actionFamily<[]>(
633
+ "clearHover",
634
+ (_get, set, id) => {
635
+ set(aiSidebarHoveredIdAtom(id), null);
636
+ }
637
+ );
638
+
639
+ export const endAISidebarDragAtom = actionFamily<[]>(
640
+ "endDrag",
641
+ (_get, set, id) => {
642
+ set(aiSidebarDraggingIdAtom(id), null);
643
+ set(aiSidebarDropTargetAtom(id), null);
644
+ }
645
+ );
646
+
647
+ // ── lifecycle ──────────────────────────────────────────────────────────────
648
+
649
+ /** @internal Publish the latest options; seed the instance once. */
650
+ export const mountAISidebarAtom = actionFamily<
651
+ [config: AISidebarConfig, seed: AISidebarSeed]
652
+ >("mount", (get, set, id, config, seed) => {
653
+ set(aiSidebarConfigAtom(id), config);
654
+ if (get(aiSidebarInitializedAtom(id))) return;
655
+ set(aiSidebarInitializedAtom(id), true);
656
+ set(aiSidebarSeedAtom(id), seed);
657
+ seedInstance(set, id, seed);
658
+ });
659
+
660
+ /** @internal */
661
+ export const setAISidebarHandlersAtom = actionFamily<
662
+ [handlers: AISidebarHandlers]
663
+ >("setHandlers", (_get, set, id, handlers) => {
664
+ set(aiSidebarHandlersAtom(id), handlers);
665
+ });
666
+
667
+ /** @internal One-way projections of controlled props — no callbacks fire. */
668
+ export const projectAISidebarItemsAtom = actionFamily<
669
+ [items: SidebarResource[]]
670
+ >("projectItems", (_get, set, id, items) => {
671
+ set(aiSidebarItemsAtom(id), items);
672
+ });
673
+
674
+ /** @internal */
675
+ export const projectAISidebarActiveIdAtom = actionFamily<
676
+ [activeId: string | null]
677
+ >("projectActiveId", (_get, set, id, activeId) => {
678
+ set(aiSidebarControlledActiveIdAtom(id), activeId);
679
+ });
680
+
681
+ /** @internal */
682
+ export const projectAISidebarExpandedIdsAtom = actionFamily<
683
+ [expandedIds: string[]]
684
+ >("projectExpandedIds", (_get, set, id, expandedIds) => {
685
+ set(aiSidebarExpandedIdsAtom(id), new Set(expandedIds));
686
+ });
687
+
688
+ /** Back to the values the instance mounted with. */
689
+ export const resetAISidebarAtom = actionFamily<[]>("reset", (get, set, id) => {
690
+ seedInstance(set, id, get(aiSidebarSeedAtom(id)));
691
+ });
692
+
693
+ /** Drop every atom for `id`. `useAISidebar` calls this on unmount for
694
+ * sidebars it keyed itself; an explicit `sidebarId` outlives its component,
695
+ * so an app-named sidebar keeps its open rows across a route change. */
696
+ export function removeAISidebarInstance(id: AISidebarInstanceId): void {
697
+ for (const family of families) family.remove(id);
698
+ }