@constructive-io/ui 0.2.0 → 0.3.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/components/card.d.ts +1 -1
- package/dist/components/checkbox.js +1 -1
- package/dist/components/checkbox.js.map +1 -1
- package/dist/components/combobox.js +1 -1
- package/dist/components/combobox.js.map +1 -1
- package/dist/components/dropdown-menu.js +3 -3
- package/dist/components/dropdown-menu.js.map +1 -1
- package/dist/components/input-group.js +1 -1
- package/dist/components/input-group.js.map +1 -1
- package/dist/components/org-chart/index.d.ts +54 -0
- package/dist/components/org-chart/index.js +861 -0
- package/dist/components/org-chart/index.js.map +1 -0
- package/dist/components/record-picker.js +2 -2
- package/dist/components/record-picker.js.map +1 -1
- package/dist/index.cjs +519 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +61 -1
- package/dist/index.d.ts +61 -1
- package/dist/index.js +511 -9
- package/dist/index.js.map +1 -1
- package/package.json +13 -2
|
@@ -0,0 +1,861 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React2 from 'react';
|
|
3
|
+
import { createContext, memo, useState, useContext, useCallback, useMemo, useEffect, useRef } from 'react';
|
|
4
|
+
import { Handle, Position, ReactFlowProvider, useReactFlow, useNodesState, useEdgesState, ReactFlow, Background, BackgroundVariant, getSmoothStepPath, BaseEdge, Panel } from '@xyflow/react';
|
|
5
|
+
import '@xyflow/react/dist/style.css';
|
|
6
|
+
import { Ellipsis, Pencil, Trash2, Loader2, Network, UserPlus } from 'lucide-react';
|
|
7
|
+
import { clsx } from 'clsx';
|
|
8
|
+
import { twMerge } from 'tailwind-merge';
|
|
9
|
+
import { Tooltip } from '@base-ui/react/tooltip';
|
|
10
|
+
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
11
|
+
import { RiAddLine, RiSubtractLine, RiFocus3Line } from '@remixicon/react';
|
|
12
|
+
import { cva } from 'class-variance-authority';
|
|
13
|
+
import { Avatar as Avatar$1 } from '@base-ui/react/avatar';
|
|
14
|
+
import { Menu } from '@base-ui/react/menu';
|
|
15
|
+
|
|
16
|
+
// src/components/org-chart/org-chart.tsx
|
|
17
|
+
function cn(...inputs) {
|
|
18
|
+
return twMerge(clsx(inputs));
|
|
19
|
+
}
|
|
20
|
+
var PORTAL_ROOT_ID = "portal-root";
|
|
21
|
+
var PortalContext = React2.createContext({
|
|
22
|
+
layer: "base",
|
|
23
|
+
depth: 0,
|
|
24
|
+
floatingPortalStrategy: "root",
|
|
25
|
+
floatingZIndex: "default"
|
|
26
|
+
});
|
|
27
|
+
function useRootPortalContainer() {
|
|
28
|
+
const [container, setContainer] = React2.useState(null);
|
|
29
|
+
React2.useLayoutEffect(() => {
|
|
30
|
+
const el = document.getElementById(PORTAL_ROOT_ID);
|
|
31
|
+
setContainer(el);
|
|
32
|
+
}, []);
|
|
33
|
+
return container;
|
|
34
|
+
}
|
|
35
|
+
function useFloatingOverlayPortalProps() {
|
|
36
|
+
const rootContainer = useRootPortalContainer();
|
|
37
|
+
const { floatingPortalStrategy, floatingZIndex } = React2.useContext(PortalContext);
|
|
38
|
+
const container = floatingPortalStrategy === "nested" ? void 0 : rootContainer ?? null;
|
|
39
|
+
const zIndexClass = floatingZIndex === "elevated" ? "z-[var(--z-layer-floating-elevated)]" : "z-[var(--z-layer-floating)]";
|
|
40
|
+
return { container, zIndexClass };
|
|
41
|
+
}
|
|
42
|
+
function composeRefs(...refs) {
|
|
43
|
+
return (node) => {
|
|
44
|
+
refs.forEach((ref) => {
|
|
45
|
+
if (typeof ref === "function") {
|
|
46
|
+
ref(node);
|
|
47
|
+
} else if (ref != null) {
|
|
48
|
+
ref.current = node;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function mergeProps(slotProps, childProps) {
|
|
54
|
+
const overrideProps = { ...childProps };
|
|
55
|
+
for (const propName in childProps) {
|
|
56
|
+
if (propName === "ref") continue;
|
|
57
|
+
const slotPropValue = slotProps[propName];
|
|
58
|
+
const childPropValue = childProps[propName];
|
|
59
|
+
if (/^on[A-Z]/.test(propName) && typeof slotPropValue === "function" && typeof childPropValue === "function") {
|
|
60
|
+
overrideProps[propName] = (...args) => {
|
|
61
|
+
childPropValue(...args);
|
|
62
|
+
slotPropValue(...args);
|
|
63
|
+
};
|
|
64
|
+
} else if (propName === "className" && typeof slotPropValue === "string" && typeof childPropValue === "string") {
|
|
65
|
+
overrideProps[propName] = `${slotPropValue} ${childPropValue}`.trim();
|
|
66
|
+
} else if (propName === "style" && typeof slotPropValue === "object" && typeof childPropValue === "object") {
|
|
67
|
+
overrideProps[propName] = { ...slotPropValue, ...childPropValue };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { ...slotProps, ...overrideProps };
|
|
71
|
+
}
|
|
72
|
+
function mergePropsWithRef(parentProps, childElement) {
|
|
73
|
+
const childProps = childElement.props;
|
|
74
|
+
const parentRef = parentProps.ref;
|
|
75
|
+
const childRef = childProps.ref;
|
|
76
|
+
const mergedProps = mergeProps(parentProps, childProps);
|
|
77
|
+
if (parentRef || childRef) {
|
|
78
|
+
mergedProps.ref = composeRefs(parentRef, childRef);
|
|
79
|
+
}
|
|
80
|
+
return mergedProps;
|
|
81
|
+
}
|
|
82
|
+
function isSlottable(child) {
|
|
83
|
+
return React2.isValidElement(child);
|
|
84
|
+
}
|
|
85
|
+
var Slot = React2.forwardRef((props, forwardedRef) => {
|
|
86
|
+
const { children, ...slotProps } = props;
|
|
87
|
+
if (isSlottable(children)) {
|
|
88
|
+
const childProps = children.props;
|
|
89
|
+
const childRef = childProps.ref;
|
|
90
|
+
return React2.cloneElement(children, {
|
|
91
|
+
...mergeProps(slotProps, childProps),
|
|
92
|
+
ref: forwardedRef ? composeRefs(forwardedRef, childRef) : childRef
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (React2.Children.count(children) > 1) {
|
|
96
|
+
const childrenArray = React2.Children.toArray(children);
|
|
97
|
+
const slottableChild = childrenArray.find(
|
|
98
|
+
(child) => React2.isValidElement(child) && child.type === Slottable
|
|
99
|
+
);
|
|
100
|
+
if (slottableChild) {
|
|
101
|
+
const newElement = slottableChild.props.children;
|
|
102
|
+
if (isSlottable(newElement)) {
|
|
103
|
+
const newElementProps = newElement.props;
|
|
104
|
+
const newChildren = childrenArray.map((child) => {
|
|
105
|
+
if (child === slottableChild) {
|
|
106
|
+
return React2.Children.count(newElementProps.children) > 1 ? React2.Children.only(null) : newElementProps.children;
|
|
107
|
+
}
|
|
108
|
+
return child;
|
|
109
|
+
});
|
|
110
|
+
const childRef = newElementProps.ref;
|
|
111
|
+
return React2.cloneElement(newElement, {
|
|
112
|
+
...mergeProps(slotProps, newElementProps),
|
|
113
|
+
ref: forwardedRef ? composeRefs(forwardedRef, childRef) : childRef,
|
|
114
|
+
children: newChildren
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
120
|
+
});
|
|
121
|
+
Slot.displayName = "Slot";
|
|
122
|
+
function Slottable({ children }) {
|
|
123
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
124
|
+
}
|
|
125
|
+
function TooltipProvider({ delay = 0, delayDuration, ...props }) {
|
|
126
|
+
return /* @__PURE__ */ jsx(Tooltip.Provider, { "data-slot": "tooltip-provider", delay: delayDuration ?? delay, ...props });
|
|
127
|
+
}
|
|
128
|
+
var buttonVariants = cva(
|
|
129
|
+
"[&_svg]:-mx-0.5 relative inline-flex shrink-0 cursor-pointer items-center justify-center gap-2 whitespace-nowrap rounded-lg border font-medium text-base outline-none transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] pointer-coarse:after:absolute pointer-coarse:after:size-full pointer-coarse:after:min-h-11 pointer-coarse:after:min-w-11 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 sm:text-sm [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-4.5 sm:[&_svg:not([class*='size-'])]:size-4 [&_svg]:pointer-events-none [&_svg]:shrink-0",
|
|
130
|
+
{
|
|
131
|
+
defaultVariants: {
|
|
132
|
+
size: "default",
|
|
133
|
+
variant: "default"
|
|
134
|
+
},
|
|
135
|
+
variants: {
|
|
136
|
+
size: {
|
|
137
|
+
default: "h-9 px-[calc(--spacing(3)-1px)] sm:h-8",
|
|
138
|
+
icon: "size-9 sm:size-8",
|
|
139
|
+
"icon-lg": "size-10 sm:size-9",
|
|
140
|
+
"icon-sm": "size-8 sm:size-7",
|
|
141
|
+
"icon-xl": 'size-11 sm:size-10 [&_svg:not([class*="size-"])]:size-5 sm:[&_svg:not([class*="size-"])]:size-4.5',
|
|
142
|
+
"icon-xs": 'size-7 rounded-md before:rounded-[calc(var(--radius-md)-1px)] sm:size-6 not-in-data-[slot=input-group]:[&_svg:not([class*="size-"])]:size-4 sm:not-in-data-[slot=input-group]:[&_svg:not([class*="size-"])]:size-3.5',
|
|
143
|
+
lg: "h-10 px-[calc(--spacing(3.5)-1px)] sm:h-9",
|
|
144
|
+
sm: "h-8 gap-1.5 px-[calc(--spacing(2.5)-1px)] sm:h-7",
|
|
145
|
+
xl: 'h-11 px-[calc(--spacing(4)-1px)] text-lg sm:h-10 sm:text-base [&_svg:not([class*="size-"])]:size-5 sm:[&_svg:not([class*="size-"])]:size-4.5',
|
|
146
|
+
xs: 'h-7 gap-1 rounded-md px-[calc(--spacing(2)-1px)] text-sm before:rounded-[calc(var(--radius-md)-1px)] sm:h-6 sm:text-xs [&_svg:not([class*="size-"])]:size-4 sm:[&_svg:not([class*="size-"])]:size-3.5'
|
|
147
|
+
},
|
|
148
|
+
variant: {
|
|
149
|
+
default: "not-disabled:inset-shadow-[0_1px_--theme(--color-white/16%)] border-primary bg-primary text-primary-foreground shadow-primary/24 shadow-xs hover:bg-primary/90 [:active,[data-pressed]]:inset-shadow-[0_1px_--theme(--color-black/8%)] [:disabled,:active,[data-pressed]]:shadow-none",
|
|
150
|
+
destructive: "not-disabled:inset-shadow-[0_1px_--theme(--color-white/16%)] border-destructive bg-destructive text-white shadow-destructive/24 shadow-xs hover:bg-destructive/90 [:active,[data-pressed]]:inset-shadow-[0_1px_--theme(--color-black/8%)] [:disabled,:active,[data-pressed]]:shadow-none",
|
|
151
|
+
"destructive-outline": "border-destructive/40 bg-transparent bg-clip-padding text-destructive shadow-xs not-disabled:not-active:not-data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:bg-input/32 dark:not-in-data-[slot=group]:bg-clip-border dark:not-disabled:before:shadow-[0_-1px_--theme(--color-white/4%)] dark:not-disabled:not-active:not-data-pressed:before:shadow-[0_-1px_--theme(--color-white/8%)] [:disabled,:active,[data-pressed]]:shadow-none [:hover,[data-pressed]]:border-destructive/60 [:hover,[data-pressed]]:bg-destructive/8",
|
|
152
|
+
ghost: "border-transparent hover:bg-accent data-pressed:bg-accent",
|
|
153
|
+
link: "border-transparent underline-offset-4 hover:underline",
|
|
154
|
+
outline: "border-border bg-background bg-clip-padding shadow-xs not-disabled:not-active:not-data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:bg-input/32 dark:not-in-data-[slot=group]:bg-clip-border dark:not-disabled:before:shadow-[0_-1px_--theme(--color-white/4%)] dark:not-disabled:not-active:not-data-pressed:before:shadow-[0_-1px_--theme(--color-white/8%)] [:disabled,:active,[data-pressed]]:shadow-none [:hover,[data-pressed]]:bg-accent/50 dark:[:hover,[data-pressed]]:bg-input/64",
|
|
155
|
+
secondary: "border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/90 [:active,[data-pressed]]:bg-secondary/80"
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
);
|
|
160
|
+
var Button = React2.forwardRef(
|
|
161
|
+
({ className, variant, size, asChild, children, type = "button", ...props }, ref) => {
|
|
162
|
+
const buttonClasses = cn(buttonVariants({ className, size, variant }));
|
|
163
|
+
if (asChild && React2.isValidElement(children)) {
|
|
164
|
+
const childProps = children.props;
|
|
165
|
+
return React2.cloneElement(children, {
|
|
166
|
+
...props,
|
|
167
|
+
className: cn(buttonClasses, childProps.className),
|
|
168
|
+
"data-slot": "button"
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return /* @__PURE__ */ jsx("button", { ref, type, className: buttonClasses, "data-slot": "button", ...props, children });
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
Button.displayName = "Button";
|
|
175
|
+
var BUTTON_CLASS = "text-muted-foreground/80 hover:text-muted-foreground bg-card size-10 rounded-none shadow-none first:rounded-s-lg last:rounded-e-lg focus-visible:z-10";
|
|
176
|
+
function FlowZoomPanel({
|
|
177
|
+
className,
|
|
178
|
+
position = "bottom-right",
|
|
179
|
+
fitViewIcon = /* @__PURE__ */ jsx(RiFocus3Line, { className: "size-5", "aria-hidden": "true" }),
|
|
180
|
+
fitViewOptions
|
|
181
|
+
}) {
|
|
182
|
+
const { zoomIn, zoomOut, fitView } = useReactFlow();
|
|
183
|
+
const onFitView = useCallback(() => {
|
|
184
|
+
fitView(fitViewOptions);
|
|
185
|
+
}, [fitView, fitViewOptions]);
|
|
186
|
+
return /* @__PURE__ */ jsxs(
|
|
187
|
+
Panel,
|
|
188
|
+
{
|
|
189
|
+
"data-slot": "flow-zoom-panel",
|
|
190
|
+
position,
|
|
191
|
+
className: cn("inline-flex -space-x-px rounded-md shadow-xs rtl:space-x-reverse", className),
|
|
192
|
+
children: [
|
|
193
|
+
/* @__PURE__ */ jsx(Button, { variant: "outline", size: "icon", className: BUTTON_CLASS, onClick: () => zoomIn(), "aria-label": "Zoom in", children: /* @__PURE__ */ jsx(RiAddLine, { className: "size-5", "aria-hidden": "true" }) }),
|
|
194
|
+
/* @__PURE__ */ jsx(Button, { variant: "outline", size: "icon", className: BUTTON_CLASS, onClick: () => zoomOut(), "aria-label": "Zoom out", children: /* @__PURE__ */ jsx(RiSubtractLine, { className: "size-5", "aria-hidden": "true" }) }),
|
|
195
|
+
/* @__PURE__ */ jsx(Button, { variant: "outline", size: "icon", className: BUTTON_CLASS, onClick: onFitView, "aria-label": "Fit view", children: fitViewIcon })
|
|
196
|
+
]
|
|
197
|
+
}
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/components/org-chart/layout.ts
|
|
202
|
+
var NODE_WIDTH = 260;
|
|
203
|
+
var NODE_HEIGHT = 100;
|
|
204
|
+
var RANK_SEP = 80;
|
|
205
|
+
var NODE_SEP = 40;
|
|
206
|
+
var ROOT_KEY = "__root__";
|
|
207
|
+
function computeLayout(edges) {
|
|
208
|
+
const edgeById = /* @__PURE__ */ new Map();
|
|
209
|
+
const childCountMap = /* @__PURE__ */ new Map();
|
|
210
|
+
const childrenMap = /* @__PURE__ */ new Map();
|
|
211
|
+
for (const e of edges) {
|
|
212
|
+
edgeById.set(e.id, e);
|
|
213
|
+
if (e.parentId) {
|
|
214
|
+
childCountMap.set(e.parentId, (childCountMap.get(e.parentId) ?? 0) + 1);
|
|
215
|
+
}
|
|
216
|
+
const parent = e.parentId ?? ROOT_KEY;
|
|
217
|
+
const bucket = childrenMap.get(parent);
|
|
218
|
+
if (bucket) bucket.push(e.id);
|
|
219
|
+
else childrenMap.set(parent, [e.id]);
|
|
220
|
+
}
|
|
221
|
+
for (const children of childrenMap.values()) {
|
|
222
|
+
children.sort((a, b) => {
|
|
223
|
+
const nameA = edgeById.get(a)?.displayName?.toLowerCase() ?? "";
|
|
224
|
+
const nameB = edgeById.get(b)?.displayName?.toLowerCase() ?? "";
|
|
225
|
+
return nameA.localeCompare(nameB);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
const widthCache = /* @__PURE__ */ new Map();
|
|
229
|
+
const subtreeWidth = (nodeId) => {
|
|
230
|
+
if (widthCache.has(nodeId)) return widthCache.get(nodeId);
|
|
231
|
+
const children = childrenMap.get(nodeId);
|
|
232
|
+
if (!children || children.length === 0) {
|
|
233
|
+
widthCache.set(nodeId, NODE_WIDTH);
|
|
234
|
+
return NODE_WIDTH;
|
|
235
|
+
}
|
|
236
|
+
const totalChildWidth = children.reduce((sum, c) => sum + subtreeWidth(c), 0);
|
|
237
|
+
const gaps = (children.length - 1) * NODE_SEP;
|
|
238
|
+
const w = Math.max(NODE_WIDTH, totalChildWidth + gaps);
|
|
239
|
+
widthCache.set(nodeId, w);
|
|
240
|
+
return w;
|
|
241
|
+
};
|
|
242
|
+
const positions = /* @__PURE__ */ new Map();
|
|
243
|
+
const positionSubtree = (nodeId, centerX, y) => {
|
|
244
|
+
positions.set(nodeId, { x: centerX, y });
|
|
245
|
+
const children = childrenMap.get(nodeId);
|
|
246
|
+
if (!children || children.length === 0) return;
|
|
247
|
+
const totalWidth = children.reduce((sum, c) => sum + subtreeWidth(c), 0) + (children.length - 1) * NODE_SEP;
|
|
248
|
+
let currentX = centerX - totalWidth / 2;
|
|
249
|
+
for (const childId of children) {
|
|
250
|
+
const childW = subtreeWidth(childId);
|
|
251
|
+
positionSubtree(childId, currentX + childW / 2, y + NODE_HEIGHT + RANK_SEP);
|
|
252
|
+
currentX += childW + NODE_SEP;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
const roots = childrenMap.get(ROOT_KEY) ?? [];
|
|
256
|
+
if (roots.length === 1) {
|
|
257
|
+
positionSubtree(roots[0], 0, 0);
|
|
258
|
+
} else {
|
|
259
|
+
const totalWidth = roots.reduce((sum, r) => sum + subtreeWidth(r), 0) + (roots.length - 1) * NODE_SEP;
|
|
260
|
+
let currentX = -totalWidth / 2;
|
|
261
|
+
for (const rootId of roots) {
|
|
262
|
+
const w = subtreeWidth(rootId);
|
|
263
|
+
positionSubtree(rootId, currentX + w / 2, 0);
|
|
264
|
+
currentX += w + NODE_SEP;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const nodes = edges.map((e) => {
|
|
268
|
+
const pos = positions.get(e.id);
|
|
269
|
+
return {
|
|
270
|
+
id: e.id,
|
|
271
|
+
type: "orgChartNode",
|
|
272
|
+
width: NODE_WIDTH,
|
|
273
|
+
height: NODE_HEIGHT,
|
|
274
|
+
position: {
|
|
275
|
+
x: pos.x - NODE_WIDTH / 2,
|
|
276
|
+
y: pos.y
|
|
277
|
+
},
|
|
278
|
+
data: {
|
|
279
|
+
id: e.id,
|
|
280
|
+
displayName: e.displayName,
|
|
281
|
+
avatarUrl: e.avatarUrl,
|
|
282
|
+
positionTitle: e.positionTitle,
|
|
283
|
+
parentId: e.parentId,
|
|
284
|
+
childCount: childCountMap.get(e.id) ?? 0,
|
|
285
|
+
isRoot: !e.parentId
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
});
|
|
289
|
+
const flowEdges = edges.filter((e) => e.parentId).map((e) => ({
|
|
290
|
+
id: `edge-${e.parentId}-${e.id}`,
|
|
291
|
+
source: e.parentId,
|
|
292
|
+
target: e.id,
|
|
293
|
+
type: "orgChartEdge"
|
|
294
|
+
}));
|
|
295
|
+
return { nodes, edges: flowEdges };
|
|
296
|
+
}
|
|
297
|
+
var NodeActionsContext = createContext(null);
|
|
298
|
+
var OrgChartContext = createContext(null);
|
|
299
|
+
function OrgChartProvider({ children, editable }) {
|
|
300
|
+
const [selectedNodeId, setSelectedNodeId] = useState(null);
|
|
301
|
+
const [draggedNodeId, setDraggedNodeId] = useState(null);
|
|
302
|
+
const [dropTargetNodeId, setDropTargetNodeId] = useState(null);
|
|
303
|
+
const selectNode = useCallback((nodeId) => setSelectedNodeId(nodeId), []);
|
|
304
|
+
const clearSelection = useCallback(() => setSelectedNodeId(null), []);
|
|
305
|
+
const value = useMemo(
|
|
306
|
+
() => ({
|
|
307
|
+
selectedNodeId,
|
|
308
|
+
selectNode,
|
|
309
|
+
clearSelection,
|
|
310
|
+
editable,
|
|
311
|
+
draggedNodeId,
|
|
312
|
+
setDraggedNodeId,
|
|
313
|
+
dropTargetNodeId,
|
|
314
|
+
setDropTargetNodeId
|
|
315
|
+
}),
|
|
316
|
+
[selectedNodeId, selectNode, clearSelection, editable, draggedNodeId, dropTargetNodeId]
|
|
317
|
+
);
|
|
318
|
+
return /* @__PURE__ */ jsx(OrgChartContext.Provider, { value, children });
|
|
319
|
+
}
|
|
320
|
+
function useOrgChartContext() {
|
|
321
|
+
const ctx = useContext(OrgChartContext);
|
|
322
|
+
if (!ctx) throw new Error("useOrgChartContext must be used within OrgChartProvider");
|
|
323
|
+
return ctx;
|
|
324
|
+
}
|
|
325
|
+
function NodeActionsProvider({
|
|
326
|
+
children,
|
|
327
|
+
onEditNode,
|
|
328
|
+
onRemoveNode
|
|
329
|
+
}) {
|
|
330
|
+
const value = useMemo(() => ({ onEditNode, onRemoveNode }), [onEditNode, onRemoveNode]);
|
|
331
|
+
return /* @__PURE__ */ jsx(NodeActionsContext.Provider, { value, children });
|
|
332
|
+
}
|
|
333
|
+
function useNodeActions() {
|
|
334
|
+
const ctx = useContext(NodeActionsContext);
|
|
335
|
+
if (!ctx) throw new Error("useNodeActions must be used within NodeActionsProvider");
|
|
336
|
+
return ctx;
|
|
337
|
+
}
|
|
338
|
+
function OrgChartEdge({
|
|
339
|
+
sourceX,
|
|
340
|
+
sourceY,
|
|
341
|
+
targetX,
|
|
342
|
+
targetY,
|
|
343
|
+
sourcePosition,
|
|
344
|
+
targetPosition,
|
|
345
|
+
style = {}
|
|
346
|
+
}) {
|
|
347
|
+
const [edgePath] = getSmoothStepPath({
|
|
348
|
+
sourceX,
|
|
349
|
+
sourceY,
|
|
350
|
+
sourcePosition,
|
|
351
|
+
targetX,
|
|
352
|
+
targetY,
|
|
353
|
+
targetPosition,
|
|
354
|
+
borderRadius: 12
|
|
355
|
+
});
|
|
356
|
+
return /* @__PURE__ */ jsx(BaseEdge, { path: edgePath, style });
|
|
357
|
+
}
|
|
358
|
+
function OrgChartEmpty({ editable, onAddRoot }) {
|
|
359
|
+
return /* @__PURE__ */ jsxs("div", { className: "bg-card border-border/60 flex flex-col items-center justify-center rounded-xl border py-16", children: [
|
|
360
|
+
/* @__PURE__ */ jsx("div", { className: "bg-muted mb-4 flex size-14 items-center justify-center rounded-2xl", children: /* @__PURE__ */ jsx(Network, { className: "text-muted-foreground size-7" }) }),
|
|
361
|
+
/* @__PURE__ */ jsx("h3", { className: "text-foreground text-base font-semibold", children: "No Org Chart Yet" }),
|
|
362
|
+
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground mt-1.5 max-w-sm text-center text-sm", children: "Build your organization hierarchy by adding the first person." }),
|
|
363
|
+
editable && /* @__PURE__ */ jsxs(Button, { className: "mt-5 gap-1.5", onClick: onAddRoot, children: [
|
|
364
|
+
/* @__PURE__ */ jsx(UserPlus, { className: "size-3.5" }),
|
|
365
|
+
"Add First Member"
|
|
366
|
+
] })
|
|
367
|
+
] });
|
|
368
|
+
}
|
|
369
|
+
function Avatar({ className, ...props }) {
|
|
370
|
+
return /* @__PURE__ */ jsx(
|
|
371
|
+
Avatar$1.Root,
|
|
372
|
+
{
|
|
373
|
+
"data-slot": "avatar",
|
|
374
|
+
className: cn("relative flex size-8 shrink-0 overflow-hidden rounded-full", className),
|
|
375
|
+
...props
|
|
376
|
+
}
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
function AvatarImage({ className, ...props }) {
|
|
380
|
+
return /* @__PURE__ */ jsx(Avatar$1.Image, { "data-slot": "avatar-image", className: cn("aspect-square size-full", className), ...props });
|
|
381
|
+
}
|
|
382
|
+
function AvatarFallback({ className, delayMs, delay, ...props }) {
|
|
383
|
+
return /* @__PURE__ */ jsx(
|
|
384
|
+
Avatar$1.Fallback,
|
|
385
|
+
{
|
|
386
|
+
"data-slot": "avatar-fallback",
|
|
387
|
+
delay: delay ?? delayMs,
|
|
388
|
+
className: cn("bg-secondary flex size-full items-center justify-center rounded-[inherit] text-xs", className),
|
|
389
|
+
...props
|
|
390
|
+
}
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
var badgeVariants = cva(
|
|
394
|
+
"relative inline-flex shrink-0 items-center justify-center gap-1 whitespace-nowrap rounded-sm border border-transparent font-medium outline-none transition-shadow focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-64 [&_svg:not([class*='size-'])]:size-3 [&_svg]:pointer-events-none [&_svg]:shrink-0 [button,a&]:cursor-pointer [button,a&]:pointer-coarse:after:absolute [button,a&]:pointer-coarse:after:size-full [button,a&]:pointer-coarse:after:min-h-11 [button,a&]:pointer-coarse:after:min-w-11",
|
|
395
|
+
{
|
|
396
|
+
defaultVariants: {
|
|
397
|
+
size: "default",
|
|
398
|
+
variant: "default"
|
|
399
|
+
},
|
|
400
|
+
variants: {
|
|
401
|
+
size: {
|
|
402
|
+
default: "px-[calc(--spacing(1)-1px)] text-xs",
|
|
403
|
+
lg: "px-[calc(--spacing(1.5)-1px)] text-sm",
|
|
404
|
+
sm: "rounded-[calc(var(--radius-sm)-2px)] px-[calc(--spacing(1)-1px)] text-[.625rem]"
|
|
405
|
+
},
|
|
406
|
+
variant: {
|
|
407
|
+
default: "bg-primary text-primary-foreground [button,a&]:hover:bg-primary/90",
|
|
408
|
+
destructive: "bg-destructive text-white [button,a&]:hover:bg-destructive/90",
|
|
409
|
+
error: "bg-destructive/8 text-destructive dark:bg-destructive/16",
|
|
410
|
+
info: "bg-info/8 text-info-foreground dark:bg-info/16",
|
|
411
|
+
outline: "border-border/60 bg-transparent dark:bg-input/32 [button,a&]:hover:bg-accent/50 dark:[button,a&]:hover:bg-input/48",
|
|
412
|
+
secondary: "bg-secondary text-secondary-foreground [button,a&]:hover:bg-secondary/90",
|
|
413
|
+
success: "bg-success/8 text-success-foreground dark:bg-success/16",
|
|
414
|
+
warning: "bg-warning/8 text-warning-foreground dark:bg-warning/16"
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
);
|
|
419
|
+
var Badge = React2.forwardRef(({ className, variant, size, ...props }, ref) => /* @__PURE__ */ jsx("span", { ref, "data-slot": "badge", className: cn(badgeVariants({ variant, size }), className), ...props }));
|
|
420
|
+
Badge.displayName = "Badge";
|
|
421
|
+
function DropdownMenu({ ...props }) {
|
|
422
|
+
return /* @__PURE__ */ jsx(Menu.Root, { "data-slot": "dropdown-menu", ...props });
|
|
423
|
+
}
|
|
424
|
+
function DropdownMenuPortal({ ...props }) {
|
|
425
|
+
const { container } = useFloatingOverlayPortalProps();
|
|
426
|
+
return /* @__PURE__ */ jsx(Menu.Portal, { "data-slot": "dropdown-menu-portal", container, ...props });
|
|
427
|
+
}
|
|
428
|
+
function DropdownMenuTrigger({ asChild, nativeButton, children, ...props }) {
|
|
429
|
+
if (asChild && React2.isValidElement(children)) {
|
|
430
|
+
return /* @__PURE__ */ jsx(
|
|
431
|
+
Menu.Trigger,
|
|
432
|
+
{
|
|
433
|
+
"data-slot": "dropdown-menu-trigger",
|
|
434
|
+
nativeButton: nativeButton ?? true,
|
|
435
|
+
...props,
|
|
436
|
+
render: (triggerProps) => {
|
|
437
|
+
const { nativeButton: _, ...rest } = triggerProps;
|
|
438
|
+
return React2.cloneElement(
|
|
439
|
+
children,
|
|
440
|
+
mergePropsWithRef(rest, children)
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
return /* @__PURE__ */ jsx(Menu.Trigger, { "data-slot": "dropdown-menu-trigger", nativeButton, ...props, children });
|
|
447
|
+
}
|
|
448
|
+
function DropdownMenuContent({
|
|
449
|
+
className,
|
|
450
|
+
sideOffset = 4,
|
|
451
|
+
side = "bottom",
|
|
452
|
+
align = "start",
|
|
453
|
+
onPointerDown: _onPointerDown,
|
|
454
|
+
onPointerDownOutside: _onPointerDownOutside,
|
|
455
|
+
onCloseAutoFocus: _onCloseAutoFocus,
|
|
456
|
+
forceMount: _forceMount,
|
|
457
|
+
...props
|
|
458
|
+
}) {
|
|
459
|
+
const { zIndexClass } = useFloatingOverlayPortalProps();
|
|
460
|
+
return /* @__PURE__ */ jsx(DropdownMenuPortal, { children: /* @__PURE__ */ jsx(Menu.Positioner, { side, align, sideOffset, className: zIndexClass, children: /* @__PURE__ */ jsx(
|
|
461
|
+
Menu.Popup,
|
|
462
|
+
{
|
|
463
|
+
"data-slot": "dropdown-menu-content",
|
|
464
|
+
className: cn(
|
|
465
|
+
`bg-popover text-popover-foreground data-[open]:animate-in data-[closed]:animate-out
|
|
466
|
+
data-[closed]:fade-out-0 data-[open]:fade-in-0 data-[closed]:zoom-out-95
|
|
467
|
+
data-[open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2
|
|
468
|
+
data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 min-w-40 overflow-hidden
|
|
469
|
+
rounded-md border p-1 shadow-lg`,
|
|
470
|
+
className
|
|
471
|
+
),
|
|
472
|
+
...props
|
|
473
|
+
}
|
|
474
|
+
) }) });
|
|
475
|
+
}
|
|
476
|
+
function DropdownMenuItem({
|
|
477
|
+
className,
|
|
478
|
+
inset,
|
|
479
|
+
variant = "default",
|
|
480
|
+
asChild,
|
|
481
|
+
children,
|
|
482
|
+
onClick,
|
|
483
|
+
...props
|
|
484
|
+
}) {
|
|
485
|
+
const itemClassName = cn(
|
|
486
|
+
`data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground data-[variant=destructive]:text-destructive
|
|
487
|
+
data-[variant=destructive]:data-[highlighted]:bg-destructive/10 dark:data-[variant=destructive]:data-[highlighted]:bg-destructive/40
|
|
488
|
+
data-[variant=destructive]:data-[highlighted]:text-destructive
|
|
489
|
+
data-[variant=destructive]:*:[svg]:!text-destructive relative flex cursor-default items-center gap-2
|
|
490
|
+
rounded-sm px-2 py-1.5 text-sm outline-hidden select-none data-[disabled]:pointer-events-none
|
|
491
|
+
data-[disabled]:opacity-50 data-[inset]:pl-8 [&_svg]:pointer-events-none [&_svg]:shrink-0`,
|
|
492
|
+
className
|
|
493
|
+
);
|
|
494
|
+
if (asChild && React2.isValidElement(children)) {
|
|
495
|
+
return /* @__PURE__ */ jsx(
|
|
496
|
+
Menu.Item,
|
|
497
|
+
{
|
|
498
|
+
"data-slot": "dropdown-menu-item",
|
|
499
|
+
"data-inset": inset,
|
|
500
|
+
"data-variant": variant,
|
|
501
|
+
...props,
|
|
502
|
+
render: (itemProps) => {
|
|
503
|
+
const childProps = children.props;
|
|
504
|
+
const itemOnClick = itemProps.onClick;
|
|
505
|
+
return React2.cloneElement(children, {
|
|
506
|
+
...itemProps,
|
|
507
|
+
className: cn(itemClassName, childProps.className),
|
|
508
|
+
onClick: (e) => {
|
|
509
|
+
onClick?.(e);
|
|
510
|
+
itemOnClick?.(e);
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
return /* @__PURE__ */ jsx(
|
|
518
|
+
Menu.Item,
|
|
519
|
+
{
|
|
520
|
+
"data-slot": "dropdown-menu-item",
|
|
521
|
+
"data-inset": inset,
|
|
522
|
+
"data-variant": variant,
|
|
523
|
+
...props,
|
|
524
|
+
render: (itemProps) => {
|
|
525
|
+
const itemOnClick = itemProps.onClick;
|
|
526
|
+
return /* @__PURE__ */ jsx(
|
|
527
|
+
"div",
|
|
528
|
+
{
|
|
529
|
+
...itemProps,
|
|
530
|
+
className: cn(itemClassName, itemProps.className),
|
|
531
|
+
onClick: (e) => {
|
|
532
|
+
onClick?.(e);
|
|
533
|
+
itemOnClick?.(e);
|
|
534
|
+
},
|
|
535
|
+
children
|
|
536
|
+
}
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
function DropdownMenuSeparator({ className, ...props }) {
|
|
543
|
+
return /* @__PURE__ */ jsx(
|
|
544
|
+
Menu.Separator,
|
|
545
|
+
{
|
|
546
|
+
"data-slot": "dropdown-menu-separator",
|
|
547
|
+
className: cn("bg-border -mx-1 my-1 h-px", className),
|
|
548
|
+
...props
|
|
549
|
+
}
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// src/components/org-chart/org-chart-utils.ts
|
|
554
|
+
function getInitials(name) {
|
|
555
|
+
if (!name) return "--";
|
|
556
|
+
const parts = name.trim().split(/\s+/).filter(Boolean);
|
|
557
|
+
if (parts.length === 0) return "--";
|
|
558
|
+
if (parts.length === 1) return parts[0].slice(0, 2).toUpperCase();
|
|
559
|
+
return (parts[0][0] + parts[1][0]).toUpperCase();
|
|
560
|
+
}
|
|
561
|
+
function OrgChartNodeComponent({ id, data }) {
|
|
562
|
+
const { selectedNodeId, dropTargetNodeId, editable } = useOrgChartContext();
|
|
563
|
+
const { onEditNode, onRemoveNode } = useNodeActions();
|
|
564
|
+
const isSelected = selectedNodeId === id;
|
|
565
|
+
const isDropTarget = dropTargetNodeId === id;
|
|
566
|
+
const [isHovered, setIsHovered] = useState(false);
|
|
567
|
+
const [isMenuOpen, setIsMenuOpen] = useState(false);
|
|
568
|
+
return /* @__PURE__ */ jsxs(
|
|
569
|
+
"div",
|
|
570
|
+
{
|
|
571
|
+
style: { width: NODE_WIDTH },
|
|
572
|
+
className: cn(
|
|
573
|
+
"bg-card dark:bg-muted relative rounded-xl border shadow-sm transition-all",
|
|
574
|
+
"border-border/60",
|
|
575
|
+
isSelected && "ring-primary ring-2 ring-offset-2 dark:ring-offset-zinc-900",
|
|
576
|
+
isDropTarget && "ring-primary/50 border-primary scale-105 ring-2"
|
|
577
|
+
),
|
|
578
|
+
onMouseEnter: () => setIsHovered(true),
|
|
579
|
+
onMouseLeave: () => setIsHovered(false),
|
|
580
|
+
children: [
|
|
581
|
+
!data.isRoot && /* @__PURE__ */ jsx(Handle, { type: "target", position: Position.Top, className: "!bg-border !size-2 !rounded-full !border-0" }),
|
|
582
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3 px-4 py-3", children: [
|
|
583
|
+
/* @__PURE__ */ jsxs(Avatar, { className: "size-10 shrink-0", children: [
|
|
584
|
+
data.avatarUrl && /* @__PURE__ */ jsx(AvatarImage, { src: data.avatarUrl, alt: data.displayName ?? "" }),
|
|
585
|
+
/* @__PURE__ */ jsx(AvatarFallback, { className: "bg-primary/10 text-primary text-xs font-medium", children: getInitials(data.displayName) })
|
|
586
|
+
] }),
|
|
587
|
+
/* @__PURE__ */ jsxs("div", { className: "min-w-0 flex-1", children: [
|
|
588
|
+
/* @__PURE__ */ jsx("p", { className: "text-foreground truncate text-sm font-semibold", children: data.displayName ?? "Unknown" }),
|
|
589
|
+
data.positionTitle && /* @__PURE__ */ jsx("p", { className: "text-muted-foreground truncate text-xs", children: data.positionTitle })
|
|
590
|
+
] }),
|
|
591
|
+
editable && (isHovered || isMenuOpen) ? /* @__PURE__ */ jsx("div", { className: "nodrag nopan shrink-0", children: /* @__PURE__ */ jsxs(DropdownMenu, { open: isMenuOpen, onOpenChange: setIsMenuOpen, children: [
|
|
592
|
+
/* @__PURE__ */ jsx(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx(
|
|
593
|
+
Button,
|
|
594
|
+
{
|
|
595
|
+
variant: "ghost",
|
|
596
|
+
size: "icon",
|
|
597
|
+
className: "size-7 rounded-full",
|
|
598
|
+
onClick: (e) => e.stopPropagation(),
|
|
599
|
+
children: /* @__PURE__ */ jsx(Ellipsis, { className: "size-4" })
|
|
600
|
+
}
|
|
601
|
+
) }),
|
|
602
|
+
/* @__PURE__ */ jsxs(DropdownMenuContent, { align: "start", side: "right", className: "translate-x-1 -translate-y-3.5", children: [
|
|
603
|
+
/* @__PURE__ */ jsxs(
|
|
604
|
+
DropdownMenuItem,
|
|
605
|
+
{
|
|
606
|
+
onClick: (e) => {
|
|
607
|
+
e.stopPropagation();
|
|
608
|
+
setIsMenuOpen(false);
|
|
609
|
+
onEditNode(data);
|
|
610
|
+
},
|
|
611
|
+
children: [
|
|
612
|
+
/* @__PURE__ */ jsx(Pencil, { className: "mr-2 size-3.5" }),
|
|
613
|
+
"Edit Position"
|
|
614
|
+
]
|
|
615
|
+
}
|
|
616
|
+
),
|
|
617
|
+
/* @__PURE__ */ jsx(DropdownMenuSeparator, {}),
|
|
618
|
+
/* @__PURE__ */ jsxs(
|
|
619
|
+
DropdownMenuItem,
|
|
620
|
+
{
|
|
621
|
+
className: "text-destructive hover:bg-destructive/10 focus:bg-destructive/10 hover:text-destructive\n focus:text-destructive data-[highlighted]:bg-destructive/10 data-[highlighted]:text-destructive",
|
|
622
|
+
onClick: (e) => {
|
|
623
|
+
e.stopPropagation();
|
|
624
|
+
setIsMenuOpen(false);
|
|
625
|
+
onRemoveNode(data);
|
|
626
|
+
},
|
|
627
|
+
children: [
|
|
628
|
+
/* @__PURE__ */ jsx(Trash2, { className: "mr-2 size-3.5" }),
|
|
629
|
+
"Remove from Chart"
|
|
630
|
+
]
|
|
631
|
+
}
|
|
632
|
+
)
|
|
633
|
+
] })
|
|
634
|
+
] }) }) : data.childCount > 0 && /* @__PURE__ */ jsx(Badge, { variant: "secondary", className: "shrink-0 text-[10px]", children: data.childCount })
|
|
635
|
+
] }),
|
|
636
|
+
/* @__PURE__ */ jsx(Handle, { type: "source", position: Position.Bottom, className: "!bg-border !size-2 !rounded-full !border-0" })
|
|
637
|
+
]
|
|
638
|
+
}
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
var OrgChartNodeMemo = memo(OrgChartNodeComponent);
|
|
642
|
+
var FIT_VIEW_OPTIONS = { padding: 0.12 };
|
|
643
|
+
function getDescendantIds(nodeId, nodes) {
|
|
644
|
+
const descendants = /* @__PURE__ */ new Set();
|
|
645
|
+
const queue = [nodeId];
|
|
646
|
+
while (queue.length > 0) {
|
|
647
|
+
const current = queue.pop();
|
|
648
|
+
for (const n of nodes) {
|
|
649
|
+
if (n.data.parentId === current && !descendants.has(n.id)) {
|
|
650
|
+
descendants.add(n.id);
|
|
651
|
+
queue.push(n.id);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return descendants;
|
|
656
|
+
}
|
|
657
|
+
function mergeWithMeasured(prev, layoutNodes) {
|
|
658
|
+
const prevMap = new Map(prev.map((n) => [n.id, n]));
|
|
659
|
+
return layoutNodes.map((n) => {
|
|
660
|
+
const existing = prevMap.get(n.id);
|
|
661
|
+
if (!existing) return n;
|
|
662
|
+
return existing.data.isRoot !== n.data.isRoot ? n : { ...n, measured: existing.measured };
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
function OrgChartInner({
|
|
666
|
+
className,
|
|
667
|
+
edges: controlledEdges,
|
|
668
|
+
defaultEdges,
|
|
669
|
+
isLoading = false,
|
|
670
|
+
editable = true,
|
|
671
|
+
onReparent,
|
|
672
|
+
onAddToChart,
|
|
673
|
+
onEditNode,
|
|
674
|
+
onRemoveNode,
|
|
675
|
+
onReparentSuccess,
|
|
676
|
+
onReparentError
|
|
677
|
+
}) {
|
|
678
|
+
const isControlled = controlledEdges !== void 0;
|
|
679
|
+
const {
|
|
680
|
+
selectNode,
|
|
681
|
+
clearSelection,
|
|
682
|
+
setDraggedNodeId,
|
|
683
|
+
setDropTargetNodeId
|
|
684
|
+
} = useOrgChartContext();
|
|
685
|
+
const [internalEdges, setInternalEdges] = useState(defaultEdges ?? []);
|
|
686
|
+
const defaultEdgesHash = useMemo(
|
|
687
|
+
() => defaultEdges?.map((e) => `${e.id}:${e.parentId}:${e.positionTitle ?? ""}:${e.displayName ?? ""}`).join(",") ?? "",
|
|
688
|
+
[defaultEdges]
|
|
689
|
+
);
|
|
690
|
+
useEffect(() => {
|
|
691
|
+
if (!isControlled && defaultEdges) {
|
|
692
|
+
setInternalEdges(defaultEdges);
|
|
693
|
+
}
|
|
694
|
+
}, [defaultEdgesHash]);
|
|
695
|
+
const apiEdges = controlledEdges ?? internalEdges;
|
|
696
|
+
const layout = useMemo(() => apiEdges.length > 0 ? computeLayout(apiEdges) : null, [apiEdges]);
|
|
697
|
+
const layoutNodes = layout?.nodes ?? [];
|
|
698
|
+
const layoutEdges = layout?.edges ?? [];
|
|
699
|
+
const isEmpty = apiEdges.length === 0 && !isLoading;
|
|
700
|
+
const { fitView, getIntersectingNodes } = useReactFlow();
|
|
701
|
+
const [nodes, setNodes, onNodesChange] = useNodesState([]);
|
|
702
|
+
const [rfEdges, setRfEdges, onEdgesChange] = useEdgesState([]);
|
|
703
|
+
const nodesHash = useMemo(
|
|
704
|
+
() => layoutNodes.map((n) => `${n.id}:${n.data.parentId ?? ""}:${n.data.positionTitle ?? ""}:${n.data.displayName ?? ""}`).join(","),
|
|
705
|
+
[layoutNodes]
|
|
706
|
+
);
|
|
707
|
+
const edgesHash = useMemo(() => layoutEdges.map((e) => `${e.id}:${e.source}:${e.target}`).join(","), [layoutEdges]);
|
|
708
|
+
const fitViewRef = useRef(fitView);
|
|
709
|
+
fitViewRef.current = fitView;
|
|
710
|
+
useEffect(() => {
|
|
711
|
+
setNodes((prev) => mergeWithMeasured(prev, layoutNodes));
|
|
712
|
+
setRfEdges(layoutEdges);
|
|
713
|
+
const raf = requestAnimationFrame(() => fitViewRef.current(FIT_VIEW_OPTIONS));
|
|
714
|
+
return () => cancelAnimationFrame(raf);
|
|
715
|
+
}, [nodesHash, edgesHash]);
|
|
716
|
+
const draggedNodeRef = useRef(null);
|
|
717
|
+
const dropTargetIdRef = useRef(null);
|
|
718
|
+
const nodeTypes = useMemo(() => ({ orgChartNode: OrgChartNodeMemo }), []);
|
|
719
|
+
const edgeTypes = useMemo(() => ({ orgChartEdge: OrgChartEdge }), []);
|
|
720
|
+
const onNodeDragStart = useCallback(
|
|
721
|
+
(_, node) => {
|
|
722
|
+
if (!editable) return;
|
|
723
|
+
draggedNodeRef.current = node.id;
|
|
724
|
+
setDraggedNodeId(node.id);
|
|
725
|
+
},
|
|
726
|
+
[editable, setDraggedNodeId]
|
|
727
|
+
);
|
|
728
|
+
const onNodeDrag = useCallback(
|
|
729
|
+
(_, node) => {
|
|
730
|
+
if (!editable || !draggedNodeRef.current) return;
|
|
731
|
+
const intersecting = getIntersectingNodes(node);
|
|
732
|
+
const target = intersecting.find((n) => n.id !== node.id);
|
|
733
|
+
const targetId = target?.id ?? null;
|
|
734
|
+
if (targetId === dropTargetIdRef.current) return;
|
|
735
|
+
dropTargetIdRef.current = targetId;
|
|
736
|
+
setDropTargetNodeId(targetId);
|
|
737
|
+
},
|
|
738
|
+
[editable, getIntersectingNodes, setDropTargetNodeId]
|
|
739
|
+
);
|
|
740
|
+
const applyOptimisticReparent = useCallback((childId, newParentId) => {
|
|
741
|
+
setInternalEdges(
|
|
742
|
+
(prev) => prev.map((e) => e.id === childId ? { ...e, parentId: newParentId } : e)
|
|
743
|
+
);
|
|
744
|
+
}, []);
|
|
745
|
+
const snapBack = useCallback(() => {
|
|
746
|
+
setNodes((prev) => mergeWithMeasured(prev, layoutNodes));
|
|
747
|
+
}, [layoutNodes, setNodes]);
|
|
748
|
+
const onNodeDragStop = useCallback(
|
|
749
|
+
async (_, _node) => {
|
|
750
|
+
if (!editable || !draggedNodeRef.current) return;
|
|
751
|
+
const childId = draggedNodeRef.current;
|
|
752
|
+
const newParentId = dropTargetIdRef.current;
|
|
753
|
+
draggedNodeRef.current = null;
|
|
754
|
+
dropTargetIdRef.current = null;
|
|
755
|
+
setDraggedNodeId(null);
|
|
756
|
+
setDropTargetNodeId(null);
|
|
757
|
+
if (!newParentId || newParentId === childId) {
|
|
758
|
+
snapBack();
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
if (getDescendantIds(childId, layoutNodes).has(newParentId)) {
|
|
762
|
+
onReparentError?.("Cannot create circular reporting chain");
|
|
763
|
+
snapBack();
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
const draggedData = layoutNodes.find((n) => n.id === childId)?.data;
|
|
767
|
+
if (!draggedData || newParentId === draggedData.parentId) {
|
|
768
|
+
snapBack();
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
const targetName = layoutNodes.find((n) => n.id === newParentId)?.data.displayName;
|
|
772
|
+
const preserve = { positionTitle: draggedData.positionTitle };
|
|
773
|
+
if (!isControlled) {
|
|
774
|
+
const snapshotEdges = internalEdges;
|
|
775
|
+
applyOptimisticReparent(childId, newParentId);
|
|
776
|
+
try {
|
|
777
|
+
await onReparent?.(childId, newParentId, preserve);
|
|
778
|
+
onReparentSuccess?.(draggedData.displayName ?? "Member", targetName ?? "new manager");
|
|
779
|
+
} catch (err) {
|
|
780
|
+
console.error("[OrgChart] reparent failed", err);
|
|
781
|
+
setInternalEdges(snapshotEdges);
|
|
782
|
+
onReparentError?.("Failed to update reporting line");
|
|
783
|
+
}
|
|
784
|
+
} else {
|
|
785
|
+
snapBack();
|
|
786
|
+
try {
|
|
787
|
+
await onReparent?.(childId, newParentId, preserve);
|
|
788
|
+
onReparentSuccess?.(draggedData.displayName ?? "Member", targetName ?? "new manager");
|
|
789
|
+
} catch (err) {
|
|
790
|
+
console.error("[OrgChart] reparent failed", err);
|
|
791
|
+
onReparentError?.("Failed to update reporting line");
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
},
|
|
795
|
+
[editable, isControlled, internalEdges, layoutNodes, onReparent, onReparentSuccess, onReparentError, applyOptimisticReparent, snapBack, setDraggedNodeId, setDropTargetNodeId]
|
|
796
|
+
);
|
|
797
|
+
const onNodeClick = useCallback((_, node) => selectNode(node.id), [selectNode]);
|
|
798
|
+
const onPaneClick = useCallback(() => clearSelection(), [clearSelection]);
|
|
799
|
+
const handleEditNode = useCallback(
|
|
800
|
+
(nodeData) => onEditNode?.(nodeData),
|
|
801
|
+
[onEditNode]
|
|
802
|
+
);
|
|
803
|
+
const handleRemoveNode = useCallback(
|
|
804
|
+
(nodeData) => onRemoveNode?.(nodeData),
|
|
805
|
+
[onRemoveNode]
|
|
806
|
+
);
|
|
807
|
+
if (isLoading) {
|
|
808
|
+
return /* @__PURE__ */ jsx("div", { className: "bg-card border-border/60 flex items-center justify-center rounded-xl border p-16", children: /* @__PURE__ */ jsx(Loader2, { className: "text-muted-foreground size-6 animate-spin" }) });
|
|
809
|
+
}
|
|
810
|
+
if (isEmpty) {
|
|
811
|
+
return /* @__PURE__ */ jsx(OrgChartEmpty, { editable, onAddRoot: onAddToChart ?? (() => {
|
|
812
|
+
}) });
|
|
813
|
+
}
|
|
814
|
+
return /* @__PURE__ */ jsx(NodeActionsProvider, { onEditNode: handleEditNode, onRemoveNode: handleRemoveNode, children: /* @__PURE__ */ jsx("div", { className: cn("border-border/60 h-[600px] overflow-hidden rounded-xl border", className), children: /* @__PURE__ */ jsxs(
|
|
815
|
+
ReactFlow,
|
|
816
|
+
{
|
|
817
|
+
nodes,
|
|
818
|
+
edges: rfEdges,
|
|
819
|
+
onNodesChange,
|
|
820
|
+
onEdgesChange,
|
|
821
|
+
onNodeClick,
|
|
822
|
+
proOptions: { hideAttribution: true },
|
|
823
|
+
onPaneClick,
|
|
824
|
+
onNodeDragStart,
|
|
825
|
+
onNodeDrag,
|
|
826
|
+
onNodeDragStop,
|
|
827
|
+
nodeTypes,
|
|
828
|
+
edgeTypes,
|
|
829
|
+
fitView: true,
|
|
830
|
+
fitViewOptions: FIT_VIEW_OPTIONS,
|
|
831
|
+
minZoom: 0.3,
|
|
832
|
+
maxZoom: 1.5,
|
|
833
|
+
nodesDraggable: editable,
|
|
834
|
+
nodesConnectable: false,
|
|
835
|
+
elementsSelectable: true,
|
|
836
|
+
selectNodesOnDrag: false,
|
|
837
|
+
panOnDrag: true,
|
|
838
|
+
zoomOnScroll: true,
|
|
839
|
+
preventScrolling: true,
|
|
840
|
+
style: {
|
|
841
|
+
"--xy-background-pattern-dots-color-default": "var(--color-border)",
|
|
842
|
+
"--xy-edge-stroke-width-default": 2,
|
|
843
|
+
"--xy-edge-stroke-default": "var(--color-border)",
|
|
844
|
+
"--xy-edge-stroke-selected-default": "var(--color-foreground)",
|
|
845
|
+
"--xy-attribution-background-color-default": "transparent"
|
|
846
|
+
},
|
|
847
|
+
attributionPosition: "bottom-left",
|
|
848
|
+
children: [
|
|
849
|
+
/* @__PURE__ */ jsx(Background, { variant: BackgroundVariant.Dots, gap: 20, size: 2 }),
|
|
850
|
+
/* @__PURE__ */ jsx(FlowZoomPanel, { fitViewOptions: FIT_VIEW_OPTIONS })
|
|
851
|
+
]
|
|
852
|
+
}
|
|
853
|
+
) }) });
|
|
854
|
+
}
|
|
855
|
+
function OrgChart(props) {
|
|
856
|
+
return /* @__PURE__ */ jsx(ReactFlowProvider, { children: /* @__PURE__ */ jsx(OrgChartProvider, { editable: props.editable ?? true, children: /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsx(OrgChartInner, { ...props }) }) }) });
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
export { OrgChart };
|
|
860
|
+
//# sourceMappingURL=index.js.map
|
|
861
|
+
//# sourceMappingURL=index.js.map
|