@exegia/corpora-ui 0.20.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.
- package/dist-lib/components/blocks/auth/__tests__/auth-state-atom.test.d.ts +1 -0
- package/dist-lib/components/blocks/auth/auth-flow-atom.d.ts +77 -0
- package/dist-lib/components/blocks/auth/auth-session-atom.d.ts +28 -0
- package/dist-lib/components/blocks/auth/auth-state-type.d.ts +71 -0
- package/dist-lib/components/blocks/auth/auth-state.d.ts +9 -0
- package/dist-lib/components/blocks/auth/use-auth-state.d.ts +52 -0
- package/dist-lib/components/blocks/nav/sidebar/__tests__/ai-sidebar-atom.test.d.ts +1 -0
- package/dist-lib/components/blocks/nav/sidebar/ai-sidebar-atom.d.ts +217 -0
- package/dist-lib/components/blocks/nav/sidebar/index.d.ts +3 -1
- package/dist-lib/components/blocks/nav/sidebar/sidebar-context.d.ts +3 -0
- package/dist-lib/components/blocks/nav/sidebar/sidebar-row.d.ts +3 -1
- package/dist-lib/components/blocks/nav/sidebar/type.d.ts +109 -29
- package/dist-lib/components/blocks/nav/sidebar/use-ai-sidebar-state.d.ts +29 -0
- package/dist-lib/components/blocks/nav/sidebar/use-ai-sidebar.d.ts +6 -1
- package/dist-lib/components/blocks/nav/sidebar/utils.d.ts +1 -1
- package/dist-lib/components/blocks/profile/index.d.ts +7 -0
- package/dist-lib/components/blocks/profile/profile-card-atom.d.ts +68 -0
- package/dist-lib/components/blocks/profile/profile-card-block.d.ts +28 -39
- package/dist-lib/components/blocks/profile/type.d.ts +80 -0
- package/dist-lib/components/blocks/profile/use-profile-card-state.d.ts +24 -0
- package/dist-lib/components/blocks/profile/use-profile-card.d.ts +34 -0
- package/dist-lib/components/blocks/shell/__tests__/shell-fit-atom.test.d.ts +1 -0
- package/dist-lib/components/blocks/shell/__tests__/shell-metrics.test.d.ts +1 -0
- package/dist-lib/components/blocks/shell/animated-panel-provider.d.ts +1 -1
- package/dist-lib/components/blocks/shell/animated-panel.d.ts +1 -1
- package/dist-lib/components/blocks/shell/index.d.ts +4 -0
- package/dist-lib/components/blocks/shell/shell-fit-atom.d.ts +68 -0
- package/dist-lib/components/blocks/shell/shell-metrics.d.ts +54 -0
- package/dist-lib/components/blocks/shell/type.d.ts +115 -3
- package/dist-lib/components/blocks/shell/use-shell-fit-state.d.ts +25 -0
- package/dist-lib/components/blocks/shell/use-shell-fit.d.ts +14 -0
- package/dist-lib/components/blocks/shell/use-shell-panels.d.ts +12 -1
- package/dist-lib/components/blocks/shell/utils.d.ts +32 -0
- package/dist-lib/components/composed/tree/__tests__/tree-atom.test.d.ts +1 -0
- package/dist-lib/components/composed/tree/constants.d.ts +4 -4
- package/dist-lib/components/composed/tree/index.d.ts +3 -1
- package/dist-lib/components/composed/tree/tree-atom.d.ts +160 -0
- package/dist-lib/components/composed/tree/tree-node.d.ts +6 -1
- package/dist-lib/components/composed/tree/type.d.ts +116 -18
- package/dist-lib/components/composed/tree/use-tree-dnd.d.ts +18 -7
- package/dist-lib/components/composed/tree/use-tree-state.d.ts +25 -0
- package/dist-lib/components/composed/tree/use-tree.d.ts +4 -0
- package/dist-lib/components/composed/user-avatar.d.ts +5 -28
- package/dist-lib/components/user-avatar/__tests__/user-avatar.test.d.ts +1 -0
- package/dist-lib/components/user-avatar/component.d.ts +1 -8
- package/dist-lib/components/user-avatar/index.d.ts +5 -0
- package/dist-lib/components/user-avatar/presence-badge.d.ts +16 -0
- package/dist-lib/components/user-avatar/type.d.ts +45 -0
- package/dist-lib/components/user-avatar/use-user-avatar-state.d.ts +26 -0
- package/dist-lib/components/user-avatar/use-user-avatar.d.ts +32 -0
- package/dist-lib/components/user-avatar/user-avatar-atom.d.ts +55 -0
- package/dist-lib/index.d.ts +5 -2
- package/dist-lib/index.js +3004 -1796
- package/dist-lib/index.js.map +1 -1
- package/dist-lib/state/exegia-provider.d.ts +51 -0
- package/dist-lib/state/index.d.ts +4 -0
- package/dist-lib/state/store.d.ts +17 -0
- package/package.json +15 -12
- package/src/components/blocks/auth/__tests__/auth-state-atom.test.tsx +247 -0
- package/src/components/blocks/auth/auth-flow-atom.ts +238 -0
- package/src/components/blocks/auth/auth-session-atom.ts +82 -0
- package/src/components/blocks/auth/auth-state-type.ts +97 -0
- package/src/components/blocks/auth/auth-state.ts +52 -0
- package/src/components/blocks/auth/use-auth-state.ts +127 -0
- package/src/components/blocks/nav/sidebar/__tests__/ai-sidebar-atom.test.tsx +325 -0
- package/src/components/blocks/nav/sidebar/ai-sidebar-atom.ts +698 -0
- package/src/components/blocks/nav/sidebar/ai-sidebar.tsx +48 -42
- package/src/components/blocks/nav/sidebar/index.ts +53 -1
- package/src/components/blocks/nav/sidebar/sidebar-context.ts +15 -0
- package/src/components/blocks/nav/sidebar/sidebar-row.tsx +119 -50
- package/src/components/blocks/nav/sidebar/type.ts +125 -30
- package/src/components/blocks/nav/sidebar/use-ai-sidebar-state.ts +123 -0
- package/src/components/blocks/nav/sidebar/use-ai-sidebar.ts +301 -248
- package/src/components/blocks/nav/sidebar/utils.ts +1 -1
- package/src/components/blocks/profile/__tests__/profile-card-block.test.tsx +194 -0
- package/src/components/blocks/profile/index.ts +28 -0
- package/src/components/blocks/profile/profile-card-atom.ts +247 -0
- package/src/components/blocks/profile/profile-card-block.tsx +153 -74
- package/src/components/blocks/profile/type.ts +95 -0
- package/src/components/blocks/profile/use-profile-card-state.ts +67 -0
- package/src/components/blocks/profile/use-profile-card.ts +126 -0
- package/src/components/blocks/shell/__tests__/shell-fit-atom.test.tsx +360 -0
- package/src/components/blocks/shell/__tests__/shell-layout.test.tsx +192 -3
- package/src/components/blocks/shell/__tests__/shell-metrics.test.ts +108 -0
- package/src/components/blocks/shell/animated-panel-inset.tsx +5 -1
- package/src/components/blocks/shell/animated-panel-provider.tsx +77 -11
- package/src/components/blocks/shell/animated-panel-trigger.tsx +4 -0
- package/src/components/blocks/shell/animated-panel.tsx +126 -29
- package/src/components/blocks/shell/index.ts +18 -0
- package/src/components/blocks/shell/shell-fit-atom.ts +243 -0
- package/src/components/blocks/shell/shell-layout.tsx +55 -53
- package/src/components/blocks/shell/shell-metrics.ts +79 -0
- package/src/components/blocks/shell/type.ts +130 -3
- package/src/components/blocks/shell/use-shell-fit-state.ts +49 -0
- package/src/components/blocks/shell/use-shell-fit.ts +135 -0
- package/src/components/blocks/shell/use-shell-panels.ts +57 -4
- package/src/components/blocks/shell/utils.ts +44 -4
- package/src/components/composed/tree/CLAUDE.md +132 -0
- package/src/components/composed/tree/__tests__/tree-atom.test.tsx +217 -0
- package/src/components/composed/tree/__tests__/tree.test.tsx +162 -37
- package/src/components/composed/tree/__tests__/use-tree.test.tsx +2 -1
- package/src/components/composed/tree/constants.ts +4 -4
- package/src/components/composed/tree/index.ts +38 -0
- package/src/components/composed/tree/tree-atom.ts +590 -0
- package/src/components/composed/tree/tree-node.tsx +117 -78
- package/src/components/composed/tree/tree.tsx +24 -16
- package/src/components/composed/tree/type.ts +125 -15
- package/src/components/composed/tree/use-tree-dnd.ts +82 -51
- package/src/components/composed/tree/use-tree-state.ts +105 -0
- package/src/components/composed/tree/use-tree.ts +185 -184
- package/src/components/composed/user-avatar.tsx +11 -99
- package/src/components/user-avatar/__tests__/user-avatar.test.tsx +245 -0
- package/src/components/user-avatar/component.tsx +121 -22
- package/src/components/user-avatar/index.ts +19 -0
- package/src/components/user-avatar/presence-badge.tsx +56 -0
- package/src/components/user-avatar/type.ts +50 -0
- package/src/components/user-avatar/use-user-avatar-state.ts +60 -0
- package/src/components/user-avatar/use-user-avatar.ts +144 -0
- package/src/components/user-avatar/user-avatar-atom.ts +198 -0
- package/src/index.ts +34 -2
- package/src/state/exegia-provider.tsx +79 -0
- package/src/state/index.ts +4 -0
- package/src/state/store.ts +19 -0
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-instance Jotai state for `Tree`.
|
|
3
|
+
*
|
|
4
|
+
* Every atom is a module-level family keyed by a tree id, so one
|
|
5
|
+
* `ExegiaProvider` at the app root is enough: many trees coexist in one store
|
|
6
|
+
* without a provider each, and any component can drive a tree by id without
|
|
7
|
+
* holding its controller.
|
|
8
|
+
*
|
|
9
|
+
* Controlled props stay authoritative. `useTree` projects them into these
|
|
10
|
+
* 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`, `controlsCollapsed`)
|
|
13
|
+
* are what keep the store from overwriting a prop.
|
|
14
|
+
*/
|
|
15
|
+
import { atom } from "jotai"
|
|
16
|
+
import type { Atom, Getter, Setter } from "jotai"
|
|
17
|
+
|
|
18
|
+
import { playCue } from "@/lib/sound"
|
|
19
|
+
import type {
|
|
20
|
+
TreeConfig,
|
|
21
|
+
TreeDropPosition,
|
|
22
|
+
TreeDropTarget,
|
|
23
|
+
TreeHandlers,
|
|
24
|
+
TreeInstanceId,
|
|
25
|
+
TreeNode,
|
|
26
|
+
TreeSeed,
|
|
27
|
+
TreeState,
|
|
28
|
+
TreeVariant,
|
|
29
|
+
} from "./type"
|
|
30
|
+
import {
|
|
31
|
+
ancestorIdsOf,
|
|
32
|
+
expandableIdsOf,
|
|
33
|
+
findNode,
|
|
34
|
+
hasThreeLevels,
|
|
35
|
+
initialExpandedIds,
|
|
36
|
+
moveNode,
|
|
37
|
+
renameNode,
|
|
38
|
+
} from "./utils"
|
|
39
|
+
|
|
40
|
+
const NO_ITEMS: TreeNode[] = []
|
|
41
|
+
const NO_IDS: ReadonlySet<string> = new Set<string>()
|
|
42
|
+
const NO_HANDLERS: TreeHandlers = {}
|
|
43
|
+
|
|
44
|
+
/** What a tree reads as before `useTree` publishes its options. */
|
|
45
|
+
export const DEFAULT_TREE_CONFIG: TreeConfig = {
|
|
46
|
+
variant: "navigation",
|
|
47
|
+
sound: true,
|
|
48
|
+
controlsItems: false,
|
|
49
|
+
controlsActiveId: false,
|
|
50
|
+
controlsCollapsed: false,
|
|
51
|
+
managesItems: true,
|
|
52
|
+
hasRenameHandler: false,
|
|
53
|
+
hasMoveHandler: false,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const DEFAULT_TREE_SEED: TreeSeed = { items: NO_ITEMS, collapsed: false }
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A string-keyed atom family.
|
|
60
|
+
*
|
|
61
|
+
* `jotai/utils`' `atomFamily` is deprecated for Jotai v3, and we need only
|
|
62
|
+
* the string-keyed case with a `remove` — so this stays in-house rather than
|
|
63
|
+
* adding `jotai-family` as a second Jotai package to keep version-aligned.
|
|
64
|
+
* Dropping a key lets the store's WeakMap release that instance's state.
|
|
65
|
+
*/
|
|
66
|
+
type Family<AtomType> = ((id: TreeInstanceId) => AtomType) & {
|
|
67
|
+
remove: (id: TreeInstanceId) => void
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Every family, so `removeTreeInstance` can drop an id from all of them. */
|
|
71
|
+
const families: { remove: (id: TreeInstanceId) => void }[] = []
|
|
72
|
+
|
|
73
|
+
function keyed<AtomType>(create: (id: TreeInstanceId) => AtomType) {
|
|
74
|
+
const cache = new Map<TreeInstanceId, AtomType>()
|
|
75
|
+
const family = ((id: TreeInstanceId) => {
|
|
76
|
+
let instance = cache.get(id)
|
|
77
|
+
if (instance === undefined) {
|
|
78
|
+
instance = create(id)
|
|
79
|
+
cache.set(id, instance)
|
|
80
|
+
}
|
|
81
|
+
return instance
|
|
82
|
+
}) as Family<AtomType>
|
|
83
|
+
family.remove = (id: TreeInstanceId) => {
|
|
84
|
+
cache.delete(id)
|
|
85
|
+
}
|
|
86
|
+
families.push(family)
|
|
87
|
+
return family
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function stateFamily<Value>(name: string, initialValue: Value) {
|
|
91
|
+
return keyed((id) => {
|
|
92
|
+
const instance = atom(initialValue)
|
|
93
|
+
instance.debugLabel = `tree/${id}/${name}`
|
|
94
|
+
return instance
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function readFamily<Value>(
|
|
99
|
+
name: string,
|
|
100
|
+
read: (get: Getter, id: TreeInstanceId) => Value
|
|
101
|
+
) {
|
|
102
|
+
return keyed((id) => {
|
|
103
|
+
const instance = atom((get) => read(get, id))
|
|
104
|
+
instance.debugLabel = `tree/${id}/${name}`
|
|
105
|
+
return instance
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function actionFamily<Args extends unknown[]>(
|
|
110
|
+
name: string,
|
|
111
|
+
write: (get: Getter, set: Setter, id: TreeInstanceId, ...args: Args) => void
|
|
112
|
+
) {
|
|
113
|
+
return keyed((id) => {
|
|
114
|
+
const instance = atom(null, (get, set, ...args: Args) =>
|
|
115
|
+
write(get, set, id, ...args)
|
|
116
|
+
)
|
|
117
|
+
instance.debugLabel = `tree/${id}/${name}`
|
|
118
|
+
return instance
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A per-node derived atom, keyed by tree and then node id.
|
|
124
|
+
*
|
|
125
|
+
* This is where Jotai earns its place: a row subscribed to
|
|
126
|
+
* `treeNodeExpandedAtom(treeId, node.id)` re-renders when *its* branch opens,
|
|
127
|
+
* and stays put when a sibling's does. Reading the whole controller through
|
|
128
|
+
* context instead re-rendered every row in the tree on every toggle.
|
|
129
|
+
*
|
|
130
|
+
* The outer family registers for cleanup, so `removeTreeInstance` drops all
|
|
131
|
+
* of a tree's node atoms with it.
|
|
132
|
+
*/
|
|
133
|
+
function nodeFamily<Value>(
|
|
134
|
+
name: string,
|
|
135
|
+
read: (get: Getter, id: TreeInstanceId, nodeId: string) => Value
|
|
136
|
+
) {
|
|
137
|
+
const outer = keyed((id: TreeInstanceId) => {
|
|
138
|
+
const inner = new Map<string, Atom<Value>>()
|
|
139
|
+
return (nodeId: string) => {
|
|
140
|
+
let instance = inner.get(nodeId)
|
|
141
|
+
if (instance === undefined) {
|
|
142
|
+
instance = atom((get) => read(get, id, nodeId))
|
|
143
|
+
instance.debugLabel = `tree/${id}/${name}/${nodeId}`
|
|
144
|
+
inner.set(nodeId, instance)
|
|
145
|
+
}
|
|
146
|
+
return instance
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
return (id: TreeInstanceId, nodeId: string) => outer(id)(nodeId)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ── state ──────────────────────────────────────────────────────────────────
|
|
153
|
+
|
|
154
|
+
/** @internal */
|
|
155
|
+
export const treeConfigAtom = stateFamily<TreeConfig>(
|
|
156
|
+
"config",
|
|
157
|
+
DEFAULT_TREE_CONFIG
|
|
158
|
+
)
|
|
159
|
+
/** @internal Refreshed every commit; nothing subscribes, so it is free. */
|
|
160
|
+
export const treeHandlersAtom = stateFamily<TreeHandlers>(
|
|
161
|
+
"handlers",
|
|
162
|
+
NO_HANDLERS
|
|
163
|
+
)
|
|
164
|
+
const treeSeedAtom = stateFamily<TreeSeed>("seed", DEFAULT_TREE_SEED)
|
|
165
|
+
const treeInitializedAtom = stateFamily<boolean>("initialized", false)
|
|
166
|
+
|
|
167
|
+
/** The tree's current data — hook-owned, or the projection of a controlled
|
|
168
|
+
* `items` prop. */
|
|
169
|
+
export const treeItemsAtom = stateFamily<TreeNode[]>("items", NO_ITEMS)
|
|
170
|
+
export const treeActiveIdAtom = stateFamily<string | undefined>(
|
|
171
|
+
"activeId",
|
|
172
|
+
undefined
|
|
173
|
+
)
|
|
174
|
+
export const treeExpandedIdsAtom = stateFamily<ReadonlySet<string>>(
|
|
175
|
+
"expandedIds",
|
|
176
|
+
NO_IDS
|
|
177
|
+
)
|
|
178
|
+
/** Raw rail fold. Read `treeCollapsedAtom` for the variant-masked value. */
|
|
179
|
+
export const treeRailCollapsedAtom = stateFamily<boolean>(
|
|
180
|
+
"railCollapsed",
|
|
181
|
+
false
|
|
182
|
+
)
|
|
183
|
+
export const treeRenamingIdAtom = stateFamily<string | null>("renamingId", null)
|
|
184
|
+
export const treeDraggedIdAtom = stateFamily<string | null>("draggedId", null)
|
|
185
|
+
export const treeDropTargetAtom = stateFamily<TreeDropTarget | null>(
|
|
186
|
+
"dropTarget",
|
|
187
|
+
null
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
// ── derived ────────────────────────────────────────────────────────────────
|
|
191
|
+
|
|
192
|
+
/** @internal The only items atom `useTree` may subscribe to: empty while a
|
|
193
|
+
* controlled `items` prop owns the data, so the projection write never feeds
|
|
194
|
+
* back into the render that produced it. An inline `items={[…]}` array is a
|
|
195
|
+
* new reference every render, and a subscribed round-trip would loop. */
|
|
196
|
+
export const treeOwnedItemsAtom = readFamily("ownedItems", (get, id) =>
|
|
197
|
+
get(treeConfigAtom(id)).controlsItems ? NO_ITEMS : get(treeItemsAtom(id))
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
/** Rail folded to icons — always `false` outside `sidebar`. */
|
|
201
|
+
export const treeCollapsedAtom = readFamily("collapsed", (get, id) =>
|
|
202
|
+
get(treeConfigAtom(id)).variant === "sidebar"
|
|
203
|
+
? get(treeRailCollapsedAtom(id))
|
|
204
|
+
: false
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
export const treeSectionedAtom = readFamily(
|
|
208
|
+
"sectioned",
|
|
209
|
+
(get, id) =>
|
|
210
|
+
get(treeConfigAtom(id)).variant === "navigation" &&
|
|
211
|
+
hasThreeLevels(get(treeItemsAtom(id)))
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
export const treeSectionIdsAtom = readFamily("sectionIds", (get, id) =>
|
|
215
|
+
get(treeSectionedAtom(id))
|
|
216
|
+
? get(treeItemsAtom(id)).map((node) => node.id)
|
|
217
|
+
: []
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
export const treeCanRenameAtom = readFamily("canRename", (get, id) => {
|
|
221
|
+
const config = get(treeConfigAtom(id))
|
|
222
|
+
return (
|
|
223
|
+
config.variant === "files" &&
|
|
224
|
+
(config.hasRenameHandler || config.managesItems)
|
|
225
|
+
)
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
export const treeCanMoveAtom = readFamily("canMove", (get, id) => {
|
|
229
|
+
const config = get(treeConfigAtom(id))
|
|
230
|
+
return (
|
|
231
|
+
config.variant === "files" && (config.hasMoveHandler || config.managesItems)
|
|
232
|
+
)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
export const treeVariantAtom = readFamily(
|
|
236
|
+
"variant",
|
|
237
|
+
(get, id) => get(treeConfigAtom(id)).variant
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
export const treeSoundAtom = readFamily(
|
|
241
|
+
"sound",
|
|
242
|
+
(get, id) => get(treeConfigAtom(id)).sound
|
|
243
|
+
)
|
|
244
|
+
|
|
245
|
+
/** Whether this one node's branch is open. */
|
|
246
|
+
export const treeNodeExpandedAtom = nodeFamily("expanded", (get, id, nodeId) =>
|
|
247
|
+
get(treeExpandedIdsAtom(id)).has(nodeId)
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
/** Whether this one node is the current entry. */
|
|
251
|
+
export const treeNodeActiveAtom = nodeFamily(
|
|
252
|
+
"active",
|
|
253
|
+
(get, id, nodeId) => get(treeActiveIdAtom(id)) === nodeId
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
/** Whether this one node is in rename mode. */
|
|
257
|
+
export const treeNodeRenamingAtom = nodeFamily(
|
|
258
|
+
"renaming",
|
|
259
|
+
(get, id, nodeId) => get(treeRenamingIdAtom(id)) === nodeId
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
/** Whether this one node is the row in flight. */
|
|
263
|
+
export const treeNodeDraggingAtom = nodeFamily(
|
|
264
|
+
"dragging",
|
|
265
|
+
(get, id, nodeId) => get(treeDraggedIdAtom(id)) === nodeId
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
/** Where a drop on this one node would land, `null` when it is not hovered. */
|
|
269
|
+
export const treeNodeDropAtom = nodeFamily<TreeDropPosition | null>(
|
|
270
|
+
"drop",
|
|
271
|
+
(get, id, nodeId) => {
|
|
272
|
+
const target = get(treeDropTargetAtom(id))
|
|
273
|
+
return target?.id === nodeId ? target.position : null
|
|
274
|
+
}
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
/** The whole state of one tree. Components reading a single field should
|
|
278
|
+
* subscribe to that field's atom instead — this one changes on every edit. */
|
|
279
|
+
export const treeStateAtom = readFamily<TreeState>("state", (get, id) => ({
|
|
280
|
+
variant: get(treeConfigAtom(id)).variant,
|
|
281
|
+
items: get(treeItemsAtom(id)),
|
|
282
|
+
activeId: get(treeActiveIdAtom(id)),
|
|
283
|
+
expandedIds: get(treeExpandedIdsAtom(id)),
|
|
284
|
+
collapsed: get(treeCollapsedAtom(id)),
|
|
285
|
+
renamingId: get(treeRenamingIdAtom(id)),
|
|
286
|
+
sectioned: get(treeSectionedAtom(id)),
|
|
287
|
+
sectionIds: get(treeSectionIdsAtom(id)),
|
|
288
|
+
canRename: get(treeCanRenameAtom(id)),
|
|
289
|
+
canMove: get(treeCanMoveAtom(id)),
|
|
290
|
+
draggedId: get(treeDraggedIdAtom(id)),
|
|
291
|
+
dropTarget: get(treeDropTargetAtom(id)),
|
|
292
|
+
}))
|
|
293
|
+
|
|
294
|
+
// ── shared write helpers ───────────────────────────────────────────────────
|
|
295
|
+
|
|
296
|
+
function cue(get: Getter, id: TreeInstanceId): void {
|
|
297
|
+
if (get(treeConfigAtom(id)).sound) playCue("toggle")
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function commitExpanded(
|
|
301
|
+
get: Getter,
|
|
302
|
+
set: Setter,
|
|
303
|
+
id: TreeInstanceId,
|
|
304
|
+
next: Set<string>
|
|
305
|
+
): void {
|
|
306
|
+
set(treeExpandedIdsAtom(id), next)
|
|
307
|
+
get(treeHandlersAtom(id)).onExpandedChange?.([...next])
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** The atom only when the hook owns the data; the callback always. */
|
|
311
|
+
function commitItems(
|
|
312
|
+
get: Getter,
|
|
313
|
+
set: Setter,
|
|
314
|
+
id: TreeInstanceId,
|
|
315
|
+
next: TreeNode[]
|
|
316
|
+
): void {
|
|
317
|
+
if (!get(treeConfigAtom(id)).controlsItems) set(treeItemsAtom(id), next)
|
|
318
|
+
get(treeHandlersAtom(id)).onItemsChange?.(next)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** `null` when every ancestor of `nodeId` is already open. */
|
|
322
|
+
function withAncestors(
|
|
323
|
+
get: Getter,
|
|
324
|
+
id: TreeInstanceId,
|
|
325
|
+
nodeId: string
|
|
326
|
+
): Set<string> | null {
|
|
327
|
+
const ancestors = ancestorIdsOf(get(treeItemsAtom(id)), nodeId)
|
|
328
|
+
const expanded = get(treeExpandedIdsAtom(id))
|
|
329
|
+
if (!ancestors.some((ancestor) => !expanded.has(ancestor))) return null
|
|
330
|
+
return new Set([...expanded, ...ancestors])
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function seedInstance(
|
|
334
|
+
set: Setter,
|
|
335
|
+
id: TreeInstanceId,
|
|
336
|
+
seed: TreeSeed,
|
|
337
|
+
variant: TreeVariant
|
|
338
|
+
): void {
|
|
339
|
+
set(treeItemsAtom(id), seed.items)
|
|
340
|
+
set(treeActiveIdAtom(id), seed.activeId)
|
|
341
|
+
set(treeRailCollapsedAtom(id), seed.collapsed)
|
|
342
|
+
set(treeRenamingIdAtom(id), null)
|
|
343
|
+
set(treeDraggedIdAtom(id), null)
|
|
344
|
+
set(treeDropTargetAtom(id), null)
|
|
345
|
+
const expanded = seed.expandedIds
|
|
346
|
+
? new Set(seed.expandedIds)
|
|
347
|
+
: initialExpandedIds(seed.items, seed.activeId)
|
|
348
|
+
// Section names default open — they are headings over their run of items,
|
|
349
|
+
// not drawers; `defaultOpen: false` still starts one closed.
|
|
350
|
+
if (
|
|
351
|
+
!seed.expandedIds &&
|
|
352
|
+
variant === "navigation" &&
|
|
353
|
+
hasThreeLevels(seed.items)
|
|
354
|
+
)
|
|
355
|
+
for (const node of seed.items)
|
|
356
|
+
if (node.defaultOpen !== false) expanded.add(node.id)
|
|
357
|
+
set(treeExpandedIdsAtom(id), expanded)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// ── expansion ──────────────────────────────────────────────────────────────
|
|
361
|
+
|
|
362
|
+
export const expandTreeNodeAtom = actionFamily<[nodeId: string]>(
|
|
363
|
+
"expand",
|
|
364
|
+
(get, set, id, nodeId) => {
|
|
365
|
+
const expanded = get(treeExpandedIdsAtom(id))
|
|
366
|
+
if (expanded.has(nodeId)) return
|
|
367
|
+
commitExpanded(get, set, id, new Set([...expanded, nodeId]))
|
|
368
|
+
cue(get, id)
|
|
369
|
+
}
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
export const collapseTreeNodeAtom = actionFamily<[nodeId: string]>(
|
|
373
|
+
"collapse",
|
|
374
|
+
(get, set, id, nodeId) => {
|
|
375
|
+
const expanded = get(treeExpandedIdsAtom(id))
|
|
376
|
+
if (!expanded.has(nodeId)) return
|
|
377
|
+
const next = new Set(expanded)
|
|
378
|
+
next.delete(nodeId)
|
|
379
|
+
commitExpanded(get, set, id, next)
|
|
380
|
+
cue(get, id)
|
|
381
|
+
}
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
export const toggleTreeNodeAtom = actionFamily<[nodeId: string]>(
|
|
385
|
+
"toggleExpanded",
|
|
386
|
+
(get, set, id, nodeId) => {
|
|
387
|
+
const next = new Set(get(treeExpandedIdsAtom(id)))
|
|
388
|
+
if (next.has(nodeId)) next.delete(nodeId)
|
|
389
|
+
else next.add(nodeId)
|
|
390
|
+
commitExpanded(get, set, id, next)
|
|
391
|
+
cue(get, id)
|
|
392
|
+
}
|
|
393
|
+
)
|
|
394
|
+
|
|
395
|
+
export const expandAllTreeNodesAtom = actionFamily<[]>(
|
|
396
|
+
"expandAll",
|
|
397
|
+
(get, set, id) => {
|
|
398
|
+
const filesVariant = get(treeConfigAtom(id)).variant === "files"
|
|
399
|
+
const ids = expandableIdsOf(get(treeItemsAtom(id)), filesVariant)
|
|
400
|
+
commitExpanded(get, set, id, new Set(ids))
|
|
401
|
+
}
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
export const collapseAllTreeNodesAtom = actionFamily<[]>(
|
|
405
|
+
"collapseAll",
|
|
406
|
+
(get, set, id) => {
|
|
407
|
+
commitExpanded(get, set, id, new Set())
|
|
408
|
+
}
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
/** Open every ancestor of `nodeId` without touching the rest of the tree. */
|
|
412
|
+
export const revealTreeNodeAtom = actionFamily<[nodeId: string]>(
|
|
413
|
+
"reveal",
|
|
414
|
+
(get, set, id, nodeId) => {
|
|
415
|
+
const next = withAncestors(get, id, nodeId)
|
|
416
|
+
if (next) commitExpanded(get, set, id, next)
|
|
417
|
+
}
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
/** @internal The same, silent. `useTree` runs it whenever the active id
|
|
421
|
+
* changes so a nested entry is never folded away — the hook has never
|
|
422
|
+
* reported that implicit reveal through `onExpandedChange`. */
|
|
423
|
+
export const revealTreeAncestorsAtom = actionFamily<[nodeId: string]>(
|
|
424
|
+
"revealAncestors",
|
|
425
|
+
(get, set, id, nodeId) => {
|
|
426
|
+
const next = withAncestors(get, id, nodeId)
|
|
427
|
+
if (next) set(treeExpandedIdsAtom(id), next)
|
|
428
|
+
}
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
// ── rail ───────────────────────────────────────────────────────────────────
|
|
432
|
+
|
|
433
|
+
export const setTreeCollapsedAtom = actionFamily<[collapsed: boolean]>(
|
|
434
|
+
"setCollapsed",
|
|
435
|
+
(get, set, id, collapsed) => {
|
|
436
|
+
if (!get(treeConfigAtom(id)).controlsCollapsed)
|
|
437
|
+
set(treeRailCollapsedAtom(id), collapsed)
|
|
438
|
+
get(treeHandlersAtom(id)).onCollapsedChange?.(collapsed)
|
|
439
|
+
cue(get, id)
|
|
440
|
+
}
|
|
441
|
+
)
|
|
442
|
+
|
|
443
|
+
export const toggleTreeCollapsedAtom = actionFamily<[]>(
|
|
444
|
+
"toggleCollapsed",
|
|
445
|
+
(get, set, id) => {
|
|
446
|
+
set(setTreeCollapsedAtom(id), !get(treeCollapsedAtom(id)))
|
|
447
|
+
}
|
|
448
|
+
)
|
|
449
|
+
|
|
450
|
+
// ── selection ──────────────────────────────────────────────────────────────
|
|
451
|
+
|
|
452
|
+
/** Runs the node's own `onSelect` first, then `onNavigate` — the order a row
|
|
453
|
+
* press follows. Disabled and unknown nodes are inert. */
|
|
454
|
+
export const selectTreeNodeAtom = actionFamily<[nodeId: string]>(
|
|
455
|
+
"select",
|
|
456
|
+
(get, set, id, nodeId) => {
|
|
457
|
+
const node = findNode(get(treeItemsAtom(id)), nodeId)
|
|
458
|
+
if (!node || node.disabled) return
|
|
459
|
+
if (!get(treeConfigAtom(id)).controlsActiveId)
|
|
460
|
+
set(treeActiveIdAtom(id), nodeId)
|
|
461
|
+
node.onSelect?.()
|
|
462
|
+
get(treeHandlersAtom(id)).onNavigate?.(node)
|
|
463
|
+
}
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
// ── rename ─────────────────────────────────────────────────────────────────
|
|
467
|
+
|
|
468
|
+
export const startTreeRenameAtom = actionFamily<[nodeId: string]>(
|
|
469
|
+
"startRename",
|
|
470
|
+
(get, set, id, nodeId) => {
|
|
471
|
+
// An id from elsewhere (a stale selection, another tree) would arm rename
|
|
472
|
+
// mode against a row that never renders.
|
|
473
|
+
if (!get(treeCanRenameAtom(id))) return
|
|
474
|
+
if (!findNode(get(treeItemsAtom(id)), nodeId)) return
|
|
475
|
+
set(treeRenamingIdAtom(id), nodeId)
|
|
476
|
+
}
|
|
477
|
+
)
|
|
478
|
+
|
|
479
|
+
export const cancelTreeRenameAtom = actionFamily<[]>(
|
|
480
|
+
"cancelRename",
|
|
481
|
+
(_get, set, id) => {
|
|
482
|
+
set(treeRenamingIdAtom(id), null)
|
|
483
|
+
}
|
|
484
|
+
)
|
|
485
|
+
|
|
486
|
+
/** Commit a rename and leave rename mode. Blank or unchanged labels are
|
|
487
|
+
* dropped. */
|
|
488
|
+
export const renameTreeNodeAtom = actionFamily<[nodeId: string, label: string]>(
|
|
489
|
+
"rename",
|
|
490
|
+
(get, set, id, nodeId, label) => {
|
|
491
|
+
set(treeRenamingIdAtom(id), null)
|
|
492
|
+
const next = label.trim()
|
|
493
|
+
if (!get(treeCanRenameAtom(id)) || !next) return
|
|
494
|
+
const items = get(treeItemsAtom(id))
|
|
495
|
+
const node = findNode(items, nodeId)
|
|
496
|
+
if (!node || node.label === next) return
|
|
497
|
+
get(treeHandlersAtom(id)).onRename?.(nodeId, next)
|
|
498
|
+
if (get(treeConfigAtom(id)).managesItems)
|
|
499
|
+
commitItems(get, set, id, renameNode(items, nodeId, next))
|
|
500
|
+
}
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
// ── data ───────────────────────────────────────────────────────────────────
|
|
504
|
+
|
|
505
|
+
export const moveTreeNodeAtom = actionFamily<
|
|
506
|
+
[nodeId: string, parentId: string | null, index: number]
|
|
507
|
+
>("move", (get, set, id, nodeId, parentId, index) => {
|
|
508
|
+
if (!get(treeCanMoveAtom(id))) return
|
|
509
|
+
get(treeHandlersAtom(id)).onMove?.(nodeId, parentId, index)
|
|
510
|
+
if (get(treeConfigAtom(id)).managesItems)
|
|
511
|
+
commitItems(
|
|
512
|
+
get,
|
|
513
|
+
set,
|
|
514
|
+
id,
|
|
515
|
+
moveNode(get(treeItemsAtom(id)), nodeId, parentId, index)
|
|
516
|
+
)
|
|
517
|
+
})
|
|
518
|
+
|
|
519
|
+
/** Replace the data. Reports through `onItemsChange`; writes the atom only
|
|
520
|
+
* when no controlled `items` prop owns it. */
|
|
521
|
+
export const setTreeItemsAtom = actionFamily<[items: TreeNode[]]>(
|
|
522
|
+
"setItems",
|
|
523
|
+
(get, set, id, items) => {
|
|
524
|
+
commitItems(get, set, id, items)
|
|
525
|
+
}
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
// ── drag state ─────────────────────────────────────────────────────────────
|
|
529
|
+
|
|
530
|
+
export const endTreeDragAtom = actionFamily<[]>("endDrag", (_get, set, id) => {
|
|
531
|
+
set(treeDraggedIdAtom(id), null)
|
|
532
|
+
set(treeDropTargetAtom(id), null)
|
|
533
|
+
})
|
|
534
|
+
|
|
535
|
+
// ── lifecycle ──────────────────────────────────────────────────────────────
|
|
536
|
+
|
|
537
|
+
/** @internal Publish the latest options; seed the instance once. */
|
|
538
|
+
export const mountTreeAtom = actionFamily<[config: TreeConfig, seed: TreeSeed]>(
|
|
539
|
+
"mount",
|
|
540
|
+
(get, set, id, config, seed) => {
|
|
541
|
+
set(treeConfigAtom(id), config)
|
|
542
|
+
if (get(treeInitializedAtom(id))) return
|
|
543
|
+
set(treeInitializedAtom(id), true)
|
|
544
|
+
set(treeSeedAtom(id), seed)
|
|
545
|
+
seedInstance(set, id, seed, config.variant)
|
|
546
|
+
}
|
|
547
|
+
)
|
|
548
|
+
|
|
549
|
+
/** @internal */
|
|
550
|
+
export const setTreeHandlersAtom = actionFamily<[handlers: TreeHandlers]>(
|
|
551
|
+
"setHandlers",
|
|
552
|
+
(_get, set, id, handlers) => {
|
|
553
|
+
set(treeHandlersAtom(id), handlers)
|
|
554
|
+
}
|
|
555
|
+
)
|
|
556
|
+
|
|
557
|
+
/** @internal One-way projections of controlled props — no callbacks fire. */
|
|
558
|
+
export const projectTreeItemsAtom = actionFamily<[items: TreeNode[]]>(
|
|
559
|
+
"projectItems",
|
|
560
|
+
(_get, set, id, items) => {
|
|
561
|
+
set(treeItemsAtom(id), items)
|
|
562
|
+
}
|
|
563
|
+
)
|
|
564
|
+
|
|
565
|
+
/** @internal */
|
|
566
|
+
export const projectTreeActiveIdAtom = actionFamily<
|
|
567
|
+
[activeId: string | undefined]
|
|
568
|
+
>("projectActiveId", (_get, set, id, activeId) => {
|
|
569
|
+
set(treeActiveIdAtom(id), activeId)
|
|
570
|
+
})
|
|
571
|
+
|
|
572
|
+
/** @internal */
|
|
573
|
+
export const projectTreeCollapsedAtom = actionFamily<[collapsed: boolean]>(
|
|
574
|
+
"projectCollapsed",
|
|
575
|
+
(_get, set, id, collapsed) => {
|
|
576
|
+
set(treeRailCollapsedAtom(id), collapsed)
|
|
577
|
+
}
|
|
578
|
+
)
|
|
579
|
+
|
|
580
|
+
/** Back to the values the instance mounted with. */
|
|
581
|
+
export const resetTreeAtom = actionFamily<[]>("reset", (get, set, id) => {
|
|
582
|
+
seedInstance(set, id, get(treeSeedAtom(id)), get(treeConfigAtom(id)).variant)
|
|
583
|
+
})
|
|
584
|
+
|
|
585
|
+
/** Drop every atom for `id`. `useTree` calls this on unmount for trees it
|
|
586
|
+
* keyed itself; an explicit `treeId` outlives its component, so a rail's fold
|
|
587
|
+
* survives a route change. */
|
|
588
|
+
export function removeTreeInstance(id: TreeInstanceId): void {
|
|
589
|
+
for (const family of families) family.remove(id)
|
|
590
|
+
}
|