@plexusone/shell 0.0.0-stage → 0.2.3

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/index.mjs ADDED
@@ -0,0 +1,967 @@
1
+ // src/ShellContext.tsx
2
+ import {
3
+ createContext,
4
+ useContext,
5
+ useState,
6
+ useCallback,
7
+ useMemo,
8
+ useEffect
9
+ } from "react";
10
+ import { jsx } from "react/jsx-runtime";
11
+ var ShellContext = createContext(null);
12
+ var COLLAPSED_KEY = "systemforge:sidebar-collapsed";
13
+ function ShellProvider({
14
+ children,
15
+ defaultCollapsed = false,
16
+ onNavigate,
17
+ onCollapseChange
18
+ }) {
19
+ const [sidebarCollapsed, setSidebarCollapsedState] = useState(() => {
20
+ if (typeof window !== "undefined") {
21
+ const stored = localStorage.getItem(COLLAPSED_KEY);
22
+ return stored ? stored === "true" : defaultCollapsed;
23
+ }
24
+ return defaultCollapsed;
25
+ });
26
+ const [sidebarVisible, setSidebarVisible] = useState(false);
27
+ const [currentPath, setCurrentPath] = useState(() => {
28
+ if (typeof window !== "undefined") {
29
+ return window.location.pathname;
30
+ }
31
+ return "/";
32
+ });
33
+ const toggleSidebar = useCallback(() => {
34
+ setSidebarCollapsedState((prev) => {
35
+ const next = !prev;
36
+ if (typeof window !== "undefined") {
37
+ localStorage.setItem(COLLAPSED_KEY, String(next));
38
+ }
39
+ onCollapseChange?.(next);
40
+ return next;
41
+ });
42
+ }, [onCollapseChange]);
43
+ const setSidebarCollapsed = useCallback(
44
+ (collapsed) => {
45
+ setSidebarCollapsedState(collapsed);
46
+ if (typeof window !== "undefined") {
47
+ localStorage.setItem(COLLAPSED_KEY, String(collapsed));
48
+ }
49
+ onCollapseChange?.(collapsed);
50
+ },
51
+ [onCollapseChange]
52
+ );
53
+ const toggleSidebarVisible = useCallback(() => {
54
+ setSidebarVisible((prev) => !prev);
55
+ }, []);
56
+ const navigate = useCallback(
57
+ (href) => {
58
+ if (onNavigate) {
59
+ onNavigate(href);
60
+ } else {
61
+ window.location.assign(href);
62
+ }
63
+ setCurrentPath(href);
64
+ setSidebarVisible(false);
65
+ },
66
+ [onNavigate]
67
+ );
68
+ useEffect(() => {
69
+ const handlePopState = () => {
70
+ setCurrentPath(window.location.pathname);
71
+ };
72
+ window.addEventListener("popstate", handlePopState);
73
+ return () => window.removeEventListener("popstate", handlePopState);
74
+ }, []);
75
+ const value = useMemo(
76
+ () => ({
77
+ sidebarCollapsed,
78
+ toggleSidebar,
79
+ setSidebarCollapsed,
80
+ sidebarVisible,
81
+ toggleSidebarVisible,
82
+ currentPath,
83
+ setCurrentPath,
84
+ navigate
85
+ }),
86
+ [
87
+ sidebarCollapsed,
88
+ toggleSidebar,
89
+ setSidebarCollapsed,
90
+ sidebarVisible,
91
+ toggleSidebarVisible,
92
+ currentPath,
93
+ navigate
94
+ ]
95
+ );
96
+ return /* @__PURE__ */ jsx(ShellContext.Provider, { value, children });
97
+ }
98
+ function useShell() {
99
+ const context = useContext(ShellContext);
100
+ if (!context) {
101
+ throw new Error("useShell must be used within a ShellProvider");
102
+ }
103
+ return context;
104
+ }
105
+ function useShellOptional() {
106
+ return useContext(ShellContext);
107
+ }
108
+
109
+ // src/Sidebar.tsx
110
+ import { ROLE_LEVELS } from "@plexusone/auth";
111
+ import { useTenantOptional } from "@plexusone/tenant";
112
+ import {
113
+ Badge,
114
+ Button,
115
+ ExternalLinkIcon,
116
+ PanelLeftCloseIcon,
117
+ PanelLeftOpenIcon,
118
+ cn
119
+ } from "@plexusone/ui";
120
+ import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
121
+ function mayView(tenant, roles) {
122
+ if (!tenant) return false;
123
+ if (tenant.isPlatformAdmin) return true;
124
+ if (!tenant.membership) return false;
125
+ const required = Math.min(...roles.map((r) => ROLE_LEVELS[r]));
126
+ return ROLE_LEVELS[tenant.membership.role] >= required;
127
+ }
128
+ function NavItemComponent({
129
+ item,
130
+ isCollapsed,
131
+ onNavigate,
132
+ currentPath
133
+ }) {
134
+ const tenant = useTenantOptional();
135
+ if (item.roles && !mayView(tenant, item.roles)) {
136
+ return null;
137
+ }
138
+ const isActive = item.isActive ?? currentPath === item.href;
139
+ const handleClick = (event) => {
140
+ if (item.external) return;
141
+ event.preventDefault();
142
+ if (onNavigate) {
143
+ onNavigate(item.href);
144
+ } else {
145
+ window.location.assign(item.href);
146
+ }
147
+ };
148
+ return /* @__PURE__ */ jsxs(
149
+ "a",
150
+ {
151
+ href: item.href,
152
+ onClick: handleClick,
153
+ target: item.external ? "_blank" : void 0,
154
+ rel: item.external ? "noopener noreferrer" : void 0,
155
+ title: isCollapsed ? item.label : void 0,
156
+ className: cn(
157
+ "flex items-center gap-3 mb-0.5 no-underline rounded-md text-sm transition-colors",
158
+ isCollapsed ? "p-2.5 justify-center" : "px-3 py-2.5",
159
+ isActive ? "bg-accent text-primary font-medium" : "text-muted-foreground hover:bg-accent hover:text-accent-foreground"
160
+ ),
161
+ children: [
162
+ item.icon && /* @__PURE__ */ jsx2("span", { className: "flex items-center justify-center w-5 h-5 flex-shrink-0", children: item.icon }),
163
+ !isCollapsed && /* @__PURE__ */ jsxs(Fragment, { children: [
164
+ /* @__PURE__ */ jsx2("span", { className: "flex-1", children: item.label }),
165
+ item.badge !== void 0 && /* @__PURE__ */ jsx2(Badge, { variant: "secondary", className: "px-1.5 py-0.5 text-xs", children: item.badge }),
166
+ item.external && /* @__PURE__ */ jsx2(ExternalLinkIcon, { className: "h-3 w-3 text-muted-foreground" })
167
+ ] })
168
+ ]
169
+ }
170
+ );
171
+ }
172
+ function NavSectionComponent({
173
+ section,
174
+ isCollapsed,
175
+ onNavigate,
176
+ currentPath
177
+ }) {
178
+ return /* @__PURE__ */ jsxs("div", { className: "mb-4", children: [
179
+ section.title && !isCollapsed && /* @__PURE__ */ jsx2("div", { className: "px-3 py-2 text-xs font-semibold text-muted-foreground uppercase tracking-wider", children: section.title }),
180
+ section.items.map((item) => /* @__PURE__ */ jsx2(
181
+ NavItemComponent,
182
+ {
183
+ item,
184
+ isCollapsed,
185
+ onNavigate,
186
+ currentPath
187
+ },
188
+ item.id
189
+ ))
190
+ ] });
191
+ }
192
+ function Sidebar({
193
+ navigation,
194
+ isCollapsed,
195
+ onToggleCollapse,
196
+ logo,
197
+ logoCollapsed,
198
+ onNavigate,
199
+ currentPath
200
+ }) {
201
+ return /* @__PURE__ */ jsxs(
202
+ "aside",
203
+ {
204
+ className: cn(
205
+ "h-screen bg-background border-r border-border flex flex-col transition-[width] duration-200 overflow-hidden flex-shrink-0",
206
+ isCollapsed ? "w-16" : "w-64"
207
+ ),
208
+ children: [
209
+ /* @__PURE__ */ jsx2(
210
+ "div",
211
+ {
212
+ className: cn(
213
+ "p-4 border-b border-border flex items-center h-16",
214
+ isCollapsed ? "justify-center" : "justify-start"
215
+ ),
216
+ children: isCollapsed ? logoCollapsed || logo : logo
217
+ }
218
+ ),
219
+ /* @__PURE__ */ jsx2("nav", { className: "flex-1 p-2 overflow-y-auto overflow-x-hidden", children: navigation.map((section, index) => /* @__PURE__ */ jsx2(
220
+ NavSectionComponent,
221
+ {
222
+ section,
223
+ isCollapsed,
224
+ onNavigate,
225
+ currentPath
226
+ },
227
+ section.title || index
228
+ )) }),
229
+ onToggleCollapse && /* @__PURE__ */ jsx2("div", { className: "p-2 border-t border-border", children: /* @__PURE__ */ jsx2(
230
+ Button,
231
+ {
232
+ variant: "ghost",
233
+ onClick: onToggleCollapse,
234
+ "aria-label": isCollapsed ? "Expand sidebar" : "Collapse sidebar",
235
+ className: cn("w-full", isCollapsed ? "justify-center" : "justify-start"),
236
+ children: isCollapsed ? /* @__PURE__ */ jsx2(PanelLeftOpenIcon, { className: "h-5 w-5" }) : /* @__PURE__ */ jsxs(Fragment, { children: [
237
+ /* @__PURE__ */ jsx2(PanelLeftCloseIcon, { className: "h-5 w-5 mr-2" }),
238
+ /* @__PURE__ */ jsx2("span", { children: "Collapse" })
239
+ ] })
240
+ }
241
+ ) })
242
+ ]
243
+ }
244
+ );
245
+ }
246
+
247
+ // src/Navbar.tsx
248
+ import { Button as Button3, MenuIcon } from "@plexusone/ui";
249
+
250
+ // src/OrgSwitcher.tsx
251
+ import { useTenantOptional as useTenantOptional2 } from "@plexusone/tenant";
252
+ import {
253
+ DropdownMenu,
254
+ DropdownMenuContent,
255
+ DropdownMenuItem,
256
+ DropdownMenuTrigger,
257
+ Avatar,
258
+ AvatarImage,
259
+ AvatarFallback,
260
+ BuildingIcon,
261
+ ChevronDownIcon,
262
+ CheckIcon,
263
+ cn as cn2
264
+ } from "@plexusone/ui";
265
+ import { Fragment as Fragment2, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
266
+ function OrgSwitcher({ compact = false }) {
267
+ const tenant = useTenantOptional2();
268
+ if (!tenant) {
269
+ return null;
270
+ }
271
+ const { currentOrg, organizations, setCurrentOrg, isLoading } = tenant;
272
+ if (isLoading) {
273
+ return /* @__PURE__ */ jsx3("div", { className: "px-3 py-2 bg-secondary rounded-md opacity-50 text-sm", children: "Loading..." });
274
+ }
275
+ if (!currentOrg) {
276
+ return null;
277
+ }
278
+ const handleSelect = (orgId) => {
279
+ setCurrentOrg(orgId);
280
+ };
281
+ return /* @__PURE__ */ jsxs2(DropdownMenu, { children: [
282
+ /* @__PURE__ */ jsx3(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxs2(
283
+ "button",
284
+ {
285
+ type: "button",
286
+ className: cn2(
287
+ "flex items-center gap-2 bg-secondary border border-border rounded-md cursor-pointer text-left",
288
+ compact ? "p-2" : "px-3 py-2 min-w-[180px]"
289
+ ),
290
+ children: [
291
+ /* @__PURE__ */ jsxs2(Avatar, { className: "h-5 w-5 rounded", children: [
292
+ currentOrg.logo_url ? /* @__PURE__ */ jsx3(AvatarImage, { src: currentOrg.logo_url, alt: "" }) : null,
293
+ /* @__PURE__ */ jsx3(AvatarFallback, { className: "rounded bg-transparent", children: /* @__PURE__ */ jsx3(BuildingIcon, { className: "h-4 w-4 text-muted-foreground" }) })
294
+ ] }),
295
+ !compact && /* @__PURE__ */ jsxs2(Fragment2, { children: [
296
+ /* @__PURE__ */ jsx3("span", { className: "flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium text-foreground", children: currentOrg.name }),
297
+ /* @__PURE__ */ jsx3(ChevronDownIcon, { className: "h-4 w-4 text-muted-foreground" })
298
+ ] })
299
+ ]
300
+ }
301
+ ) }),
302
+ /* @__PURE__ */ jsx3(DropdownMenuContent, { align: "start", className: "min-w-[200px]", children: organizations.map((org) => /* @__PURE__ */ jsxs2(
303
+ DropdownMenuItem,
304
+ {
305
+ onClick: () => handleSelect(org.id),
306
+ className: cn2("cursor-pointer", org.id === currentOrg.id && "bg-accent"),
307
+ children: [
308
+ /* @__PURE__ */ jsxs2(Avatar, { className: "h-5 w-5 mr-2 rounded", children: [
309
+ org.logo_url ? /* @__PURE__ */ jsx3(AvatarImage, { src: org.logo_url, alt: "" }) : null,
310
+ /* @__PURE__ */ jsx3(AvatarFallback, { className: "rounded bg-transparent", children: /* @__PURE__ */ jsx3(BuildingIcon, { className: "h-4 w-4 text-muted-foreground" }) })
311
+ ] }),
312
+ /* @__PURE__ */ jsx3("span", { className: "flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-sm", children: org.name }),
313
+ org.id === currentOrg.id && /* @__PURE__ */ jsx3(CheckIcon, { className: "h-4 w-4 text-primary ml-2" })
314
+ ]
315
+ },
316
+ org.id
317
+ )) })
318
+ ] });
319
+ }
320
+
321
+ // src/OrganizationSwitcher.tsx
322
+ import { organizationLabel, useSessionOptional } from "@plexusone/auth";
323
+ import {
324
+ BuildingIcon as BuildingIcon2,
325
+ CheckIcon as CheckIcon2,
326
+ ChevronDownIcon as ChevronDownIcon2,
327
+ DropdownMenu as DropdownMenu2,
328
+ DropdownMenuContent as DropdownMenuContent2,
329
+ DropdownMenuItem as DropdownMenuItem2,
330
+ DropdownMenuTrigger as DropdownMenuTrigger2,
331
+ cn as cn3
332
+ } from "@plexusone/ui";
333
+ import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
334
+ function OrganizationSwitcher({
335
+ value,
336
+ onChange,
337
+ memberships,
338
+ alwaysShow = false,
339
+ className
340
+ }) {
341
+ const ctx = useSessionOptional();
342
+ const list = memberships ?? ctx?.session.user?.memberships ?? [];
343
+ if (list.length === 0 || list.length === 1 && !alwaysShow) {
344
+ return null;
345
+ }
346
+ const current = list.find((m) => m.organizationId === value);
347
+ return /* @__PURE__ */ jsxs3(DropdownMenu2, { children: [
348
+ /* @__PURE__ */ jsx4(DropdownMenuTrigger2, { asChild: true, children: /* @__PURE__ */ jsxs3(
349
+ "button",
350
+ {
351
+ type: "button",
352
+ "aria-label": "Switch organization",
353
+ className: cn3(
354
+ "flex items-center gap-2 px-3 py-1.5 min-w-[160px] bg-secondary border border-border rounded-md text-left text-sm",
355
+ className
356
+ ),
357
+ children: [
358
+ /* @__PURE__ */ jsx4(BuildingIcon2, { className: "h-4 w-4 text-muted-foreground" }),
359
+ /* @__PURE__ */ jsx4("span", { className: "flex-1 truncate font-medium text-foreground", children: current ? organizationLabel(current) : "Choose organization" }),
360
+ /* @__PURE__ */ jsx4(ChevronDownIcon2, { className: "h-4 w-4 text-muted-foreground" })
361
+ ]
362
+ }
363
+ ) }),
364
+ /* @__PURE__ */ jsx4(DropdownMenuContent2, { align: "start", className: "min-w-[200px]", children: list.map((m) => {
365
+ const selected = m.organizationId === value;
366
+ return /* @__PURE__ */ jsxs3(
367
+ DropdownMenuItem2,
368
+ {
369
+ onSelect: () => {
370
+ if (!selected) onChange(m.organizationId);
371
+ },
372
+ "aria-current": selected ? "true" : void 0,
373
+ className: cn3("cursor-pointer", selected && "bg-accent"),
374
+ children: [
375
+ /* @__PURE__ */ jsx4("span", { className: "flex-1 truncate text-sm", children: organizationLabel(m) }),
376
+ selected && /* @__PURE__ */ jsx4(CheckIcon2, { className: "h-4 w-4 ml-2 text-primary" })
377
+ ]
378
+ },
379
+ m.organizationId
380
+ );
381
+ }) })
382
+ ] });
383
+ }
384
+
385
+ // src/ProductSwitcher.tsx
386
+ import { membershipFor, useSessionOptional as useSessionOptional2 } from "@plexusone/auth";
387
+ import { cn as cn4 } from "@plexusone/ui";
388
+
389
+ // src/switcher.ts
390
+ function switcherApps(apps, self, membership) {
391
+ const others = apps.filter((a) => a.id !== self.id && Boolean(membership?.applications[a.id]));
392
+ const fromCatalog = apps.find((a) => a.id === self.id);
393
+ return [fromCatalog ? { ...fromCatalog, ...self } : self, ...others];
394
+ }
395
+
396
+ // src/ProductSwitcher.tsx
397
+ import { jsx as jsx5 } from "react/jsx-runtime";
398
+ function ProductSwitcher({
399
+ self,
400
+ organizationId,
401
+ apps,
402
+ onNavigate,
403
+ className
404
+ }) {
405
+ const ctx = useSessionOptional2();
406
+ const session = ctx?.session;
407
+ const catalog = apps ?? session?.apps ?? [];
408
+ const membership = session ? membershipFor(session, organizationId) : void 0;
409
+ const entries = switcherApps(catalog, self, membership);
410
+ const follow = (url) => (e) => {
411
+ if (!onNavigate || e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey) {
412
+ return;
413
+ }
414
+ e.preventDefault();
415
+ onNavigate(url);
416
+ };
417
+ return /* @__PURE__ */ jsx5("nav", { "aria-label": "Products", className: cn4("flex items-center gap-1", className), children: entries.map(
418
+ (app) => app.id === self.id ? /* @__PURE__ */ jsx5(
419
+ "span",
420
+ {
421
+ "aria-current": "page",
422
+ className: "px-3 py-1.5 rounded-md text-sm font-medium bg-accent text-foreground",
423
+ children: app.name
424
+ },
425
+ app.id
426
+ ) : /* @__PURE__ */ jsx5(
427
+ "a",
428
+ {
429
+ href: app.url,
430
+ onClick: follow(app.url),
431
+ className: "px-3 py-1.5 rounded-md text-sm text-muted-foreground hover:bg-accent hover:text-foreground",
432
+ children: app.name
433
+ },
434
+ app.id
435
+ )
436
+ ) });
437
+ }
438
+
439
+ // src/Navbar.tsx
440
+ import { useSessionOptional as useSessionOptional4 } from "@plexusone/auth";
441
+
442
+ // src/ProfileMenu.tsx
443
+ import { membershipFor as membershipFor2, organizationLabel as organizationLabel2, useSessionOptional as useSessionOptional3 } from "@plexusone/auth";
444
+ import {
445
+ Avatar as Avatar2,
446
+ AvatarFallback as AvatarFallback2,
447
+ AvatarImage as AvatarImage2,
448
+ DropdownMenu as DropdownMenu3,
449
+ DropdownMenuContent as DropdownMenuContent3,
450
+ DropdownMenuGroup,
451
+ DropdownMenuItem as DropdownMenuItem3,
452
+ DropdownMenuLabel,
453
+ DropdownMenuSeparator,
454
+ DropdownMenuTrigger as DropdownMenuTrigger3,
455
+ cn as cn5
456
+ } from "@plexusone/ui";
457
+ import { Fragment as Fragment3, jsx as jsx6, jsxs as jsxs4 } from "react/jsx-runtime";
458
+ function initials(nameOrEmail) {
459
+ const s = (nameOrEmail ?? "").trim();
460
+ if (!s) return "?";
461
+ const words = s.split(/[\s@._-]+/).filter(Boolean);
462
+ const letters = words.length > 1 ? words[0][0] + words[1][0] : s[0];
463
+ return letters.toUpperCase();
464
+ }
465
+ function ProfileMenu({
466
+ appId,
467
+ organizationId,
468
+ items = [],
469
+ onNavigate,
470
+ onSignOutError = (err) => console.error("signing out failed", err)
471
+ }) {
472
+ const ctx = useSessionOptional3();
473
+ const user = ctx?.session.user;
474
+ if (!ctx || ctx.status !== "authenticated" || !user) {
475
+ return null;
476
+ }
477
+ const who = user.name || user.email || "Signed in";
478
+ const membership = membershipFor2(ctx.session, organizationId);
479
+ const role = membership ? appId && membership.applications[appId] || membership.role : void 0;
480
+ const run = (item) => {
481
+ if (item.onClick) {
482
+ item.onClick();
483
+ } else if (item.href) {
484
+ if (onNavigate) onNavigate(item.href);
485
+ else window.location.assign(item.href);
486
+ }
487
+ };
488
+ return /* @__PURE__ */ jsxs4(DropdownMenu3, { children: [
489
+ /* @__PURE__ */ jsx6(DropdownMenuTrigger3, { asChild: true, children: /* @__PURE__ */ jsx6(
490
+ "button",
491
+ {
492
+ type: "button",
493
+ "aria-label": `Account: ${who}`,
494
+ title: `Signed in as ${who}`,
495
+ className: "flex items-center p-1 bg-transparent border-none rounded-full cursor-pointer hover:bg-accent",
496
+ children: /* @__PURE__ */ jsxs4(Avatar2, { className: "h-8 w-8", children: [
497
+ user.avatarUrl ? /* @__PURE__ */ jsx6(AvatarImage2, { src: user.avatarUrl, alt: "" }) : null,
498
+ /* @__PURE__ */ jsx6(AvatarFallback2, { className: "bg-primary/10 text-primary text-xs font-medium", children: initials(user.name || user.email) })
499
+ ] })
500
+ }
501
+ ) }),
502
+ /* @__PURE__ */ jsxs4(DropdownMenuContent3, { align: "end", className: "w-64", children: [
503
+ /* @__PURE__ */ jsx6(DropdownMenuLabel, { className: "font-normal", children: /* @__PURE__ */ jsxs4("div", { className: "flex flex-col gap-1", children: [
504
+ /* @__PURE__ */ jsx6("p", { className: "text-sm font-medium leading-none", children: who }),
505
+ user.email && user.email !== who && /* @__PURE__ */ jsx6("p", { className: "text-xs leading-none text-muted-foreground", children: user.email })
506
+ ] }) }),
507
+ membership && /* @__PURE__ */ jsxs4(Fragment3, { children: [
508
+ /* @__PURE__ */ jsx6(DropdownMenuSeparator, {}),
509
+ /* @__PURE__ */ jsxs4("dl", { className: "px-2 py-1.5 grid grid-cols-[auto_1fr] gap-x-3 gap-y-1 text-xs", children: [
510
+ /* @__PURE__ */ jsx6("dt", { className: "text-muted-foreground", children: "Organization" }),
511
+ /* @__PURE__ */ jsx6("dd", { className: "truncate", children: organizationLabel2(membership) }),
512
+ role && /* @__PURE__ */ jsxs4(Fragment3, { children: [
513
+ /* @__PURE__ */ jsx6("dt", { className: "text-muted-foreground", children: "Role" }),
514
+ /* @__PURE__ */ jsx6("dd", { children: role })
515
+ ] })
516
+ ] })
517
+ ] }),
518
+ /* @__PURE__ */ jsx6(DropdownMenuSeparator, {}),
519
+ items.length > 0 && /* @__PURE__ */ jsxs4(Fragment3, { children: [
520
+ /* @__PURE__ */ jsx6(DropdownMenuGroup, { children: items.map((item) => /* @__PURE__ */ jsxs4(
521
+ DropdownMenuItem3,
522
+ {
523
+ onSelect: () => run(item),
524
+ className: cn5(
525
+ "cursor-pointer",
526
+ item.destructive && "text-destructive focus:text-destructive"
527
+ ),
528
+ children: [
529
+ item.icon && /* @__PURE__ */ jsx6("span", { className: "mr-2 text-muted-foreground", children: item.icon }),
530
+ item.label
531
+ ]
532
+ },
533
+ item.id
534
+ )) }),
535
+ /* @__PURE__ */ jsx6(DropdownMenuSeparator, {})
536
+ ] }),
537
+ /* @__PURE__ */ jsx6(
538
+ DropdownMenuItem3,
539
+ {
540
+ onSelect: () => {
541
+ ctx.signOut().catch(onSignOutError);
542
+ },
543
+ className: "cursor-pointer text-destructive focus:text-destructive",
544
+ children: "Sign out"
545
+ }
546
+ )
547
+ ] })
548
+ ] });
549
+ }
550
+
551
+ // src/ThemeToggle.tsx
552
+ import { Button as Button2, MonitorIcon, MoonIcon, SunIcon } from "@plexusone/ui";
553
+
554
+ // src/theme.ts
555
+ import { useCallback as useCallback2, useEffect as useEffect2, useState as useState2 } from "react";
556
+ var THEME_STORAGE_KEY = "systemforge:theme";
557
+ var THEME_CHANGE_EVENT = "systemforge:theme-change";
558
+ var darkQuery = "(prefers-color-scheme: dark)";
559
+ function isTheme(v) {
560
+ return v === "light" || v === "dark" || v === "system";
561
+ }
562
+ function readStoredTheme() {
563
+ try {
564
+ const v = window.localStorage.getItem(THEME_STORAGE_KEY);
565
+ return isTheme(v) ? v : "system";
566
+ } catch {
567
+ return "system";
568
+ }
569
+ }
570
+ function storeTheme(theme) {
571
+ try {
572
+ window.localStorage.setItem(THEME_STORAGE_KEY, theme);
573
+ } catch {
574
+ }
575
+ }
576
+ function systemPrefersDark() {
577
+ return typeof window.matchMedia === "function" && window.matchMedia(darkQuery).matches;
578
+ }
579
+ function resolveTheme(theme, prefersDark = systemPrefersDark()) {
580
+ if (theme === "system") return prefersDark ? "dark" : "light";
581
+ return theme;
582
+ }
583
+ function applyTheme(theme, root = document.documentElement) {
584
+ const resolved = resolveTheme(theme);
585
+ root.classList.toggle("dark", resolved === "dark");
586
+ root.style.colorScheme = resolved;
587
+ }
588
+ function applyStoredTheme() {
589
+ applyTheme(readStoredTheme());
590
+ }
591
+ function nextTheme(theme) {
592
+ return theme === "light" ? "dark" : theme === "dark" ? "system" : "light";
593
+ }
594
+ function useTheme() {
595
+ const [theme, setThemeState] = useState2(
596
+ () => typeof window === "undefined" ? "system" : readStoredTheme()
597
+ );
598
+ useEffect2(() => {
599
+ applyTheme(theme);
600
+ if (theme !== "system" || typeof window.matchMedia !== "function") return void 0;
601
+ const mq = window.matchMedia(darkQuery);
602
+ const onChange = () => applyTheme("system");
603
+ mq.addEventListener("change", onChange);
604
+ return () => mq.removeEventListener("change", onChange);
605
+ }, [theme]);
606
+ useEffect2(() => {
607
+ const onThemeChange = (e) => {
608
+ const next = e.detail;
609
+ if (isTheme(next)) setThemeState(next);
610
+ };
611
+ window.addEventListener(THEME_CHANGE_EVENT, onThemeChange);
612
+ return () => window.removeEventListener(THEME_CHANGE_EVENT, onThemeChange);
613
+ }, []);
614
+ const setTheme = useCallback2((next) => {
615
+ storeTheme(next);
616
+ setThemeState(next);
617
+ window.dispatchEvent(new CustomEvent(THEME_CHANGE_EVENT, { detail: next }));
618
+ }, []);
619
+ return [theme, setTheme];
620
+ }
621
+
622
+ // src/ThemeToggle.tsx
623
+ import { jsx as jsx7 } from "react/jsx-runtime";
624
+ var labels = {
625
+ light: "Light theme",
626
+ dark: "Dark theme",
627
+ system: "System theme"
628
+ };
629
+ function ThemeToggle() {
630
+ const [theme, setTheme] = useTheme();
631
+ const next = nextTheme(theme);
632
+ const Icon = theme === "light" ? SunIcon : theme === "dark" ? MoonIcon : MonitorIcon;
633
+ return /* @__PURE__ */ jsx7(
634
+ Button2,
635
+ {
636
+ variant: "ghost",
637
+ size: "icon",
638
+ "aria-label": `${labels[theme]}; switch to ${labels[next].toLowerCase()}`,
639
+ title: labels[theme],
640
+ onClick: () => setTheme(next),
641
+ children: /* @__PURE__ */ jsx7(Icon, { className: "h-4 w-4" })
642
+ }
643
+ );
644
+ }
645
+
646
+ // src/UserMenu.tsx
647
+ import { useAuthOptional } from "@plexusone/auth";
648
+ import {
649
+ Avatar as Avatar3,
650
+ AvatarImage as AvatarImage3,
651
+ AvatarFallback as AvatarFallback3,
652
+ DropdownMenu as DropdownMenu4,
653
+ DropdownMenuContent as DropdownMenuContent4,
654
+ DropdownMenuGroup as DropdownMenuGroup2,
655
+ DropdownMenuItem as DropdownMenuItem4,
656
+ DropdownMenuLabel as DropdownMenuLabel2,
657
+ DropdownMenuSeparator as DropdownMenuSeparator2,
658
+ DropdownMenuTrigger as DropdownMenuTrigger4,
659
+ UserIcon,
660
+ cn as cn6
661
+ } from "@plexusone/ui";
662
+ import { jsx as jsx8, jsxs as jsxs5 } from "react/jsx-runtime";
663
+ var defaultActions = [];
664
+ function UserMenu({ actions = defaultActions, onNavigate }) {
665
+ const auth = useAuthOptional();
666
+ if (!auth || auth.isLoading || !auth.user) {
667
+ return null;
668
+ }
669
+ const { user, logout } = auth;
670
+ const handleAction = (action) => {
671
+ if (action.onClick) {
672
+ action.onClick();
673
+ } else if (action.href) {
674
+ if (onNavigate) {
675
+ onNavigate(action.href);
676
+ } else {
677
+ window.location.assign(action.href);
678
+ }
679
+ }
680
+ };
681
+ const handleLogout = async () => {
682
+ await logout();
683
+ };
684
+ const allActions = [
685
+ ...actions,
686
+ { id: "logout", label: "Sign out", onClick: handleLogout, destructive: true }
687
+ ];
688
+ return /* @__PURE__ */ jsxs5(DropdownMenu4, { children: [
689
+ /* @__PURE__ */ jsx8(DropdownMenuTrigger4, { asChild: true, children: /* @__PURE__ */ jsx8(
690
+ "button",
691
+ {
692
+ type: "button",
693
+ className: "flex items-center gap-2 p-1 bg-transparent border-none rounded-full cursor-pointer hover:bg-accent",
694
+ children: /* @__PURE__ */ jsxs5(Avatar3, { className: "h-8 w-8", children: [
695
+ user.avatar_url ? /* @__PURE__ */ jsx8(AvatarImage3, { src: user.avatar_url, alt: user.name }) : null,
696
+ /* @__PURE__ */ jsx8(AvatarFallback3, { className: "bg-primary/10 text-primary", children: /* @__PURE__ */ jsx8(UserIcon, { className: "h-4 w-4" }) })
697
+ ] })
698
+ }
699
+ ) }),
700
+ /* @__PURE__ */ jsxs5(DropdownMenuContent4, { align: "end", className: "w-56", children: [
701
+ /* @__PURE__ */ jsx8(DropdownMenuLabel2, { className: "font-normal", children: /* @__PURE__ */ jsxs5("div", { className: "flex flex-col space-y-1", children: [
702
+ /* @__PURE__ */ jsx8("p", { className: "text-sm font-medium leading-none", children: user.name }),
703
+ /* @__PURE__ */ jsx8("p", { className: "text-xs leading-none text-muted-foreground", children: user.email })
704
+ ] }) }),
705
+ /* @__PURE__ */ jsx8(DropdownMenuSeparator2, {}),
706
+ /* @__PURE__ */ jsx8(DropdownMenuGroup2, { children: allActions.map((action, index) => {
707
+ const isLogout = action.id === "logout";
708
+ const showSeparator = isLogout && index > 0;
709
+ return /* @__PURE__ */ jsxs5("div", { children: [
710
+ showSeparator && /* @__PURE__ */ jsx8(DropdownMenuSeparator2, {}),
711
+ /* @__PURE__ */ jsxs5(
712
+ DropdownMenuItem4,
713
+ {
714
+ onClick: () => handleAction(action),
715
+ className: cn6(
716
+ "cursor-pointer",
717
+ action.destructive && "text-destructive focus:text-destructive"
718
+ ),
719
+ children: [
720
+ action.icon && /* @__PURE__ */ jsx8(
721
+ "span",
722
+ {
723
+ className: cn6(
724
+ "mr-2",
725
+ action.destructive ? "text-destructive" : "text-muted-foreground"
726
+ ),
727
+ children: action.icon
728
+ }
729
+ ),
730
+ action.label
731
+ ]
732
+ }
733
+ )
734
+ ] }, action.id);
735
+ }) })
736
+ ] })
737
+ ] });
738
+ }
739
+
740
+ // src/Breadcrumbs.tsx
741
+ import { ChevronRightIcon, HomeIcon, cn as cn7 } from "@plexusone/ui";
742
+ import { jsx as jsx9, jsxs as jsxs6 } from "react/jsx-runtime";
743
+ function Breadcrumbs({
744
+ items,
745
+ onNavigate,
746
+ separator,
747
+ maxItems
748
+ }) {
749
+ if (items.length === 0) {
750
+ return null;
751
+ }
752
+ const handleClick = (href, event) => {
753
+ if (!href) return;
754
+ event.preventDefault();
755
+ if (onNavigate) {
756
+ onNavigate(href);
757
+ } else {
758
+ window.location.assign(href);
759
+ }
760
+ };
761
+ let displayItems = items;
762
+ let collapsed = false;
763
+ if (maxItems && items.length > maxItems) {
764
+ collapsed = true;
765
+ displayItems = [items[0], { label: "...", href: void 0 }, ...items.slice(-(maxItems - 2))];
766
+ }
767
+ const defaultSeparator = /* @__PURE__ */ jsx9(ChevronRightIcon, { className: "h-4 w-4" });
768
+ return /* @__PURE__ */ jsx9("nav", { "aria-label": "Breadcrumb", children: /* @__PURE__ */ jsx9("ol", { className: "flex items-center gap-1 list-none m-0 p-0 text-sm", children: displayItems.map((item, index) => {
769
+ const isLast = index === displayItems.length - 1;
770
+ const isFirst = index === 0;
771
+ const isCollapsed = collapsed && index === 1;
772
+ return /* @__PURE__ */ jsxs6("li", { className: "flex items-center gap-1", children: [
773
+ index > 0 && /* @__PURE__ */ jsx9("span", { className: "text-muted-foreground flex items-center", "aria-hidden": "true", children: separator || defaultSeparator }),
774
+ isCollapsed ? /* @__PURE__ */ jsx9("span", { className: "text-muted-foreground px-1 py-0.5", children: item.label }) : item.href && !isLast ? /* @__PURE__ */ jsxs6(
775
+ "a",
776
+ {
777
+ href: item.href,
778
+ onClick: (e) => handleClick(item.href, e),
779
+ className: "flex items-center gap-1 text-muted-foreground no-underline px-1 py-0.5 rounded hover:text-foreground hover:bg-accent",
780
+ children: [
781
+ isFirst && !item.icon && /* @__PURE__ */ jsx9(HomeIcon, { className: "h-4 w-4" }),
782
+ item.icon,
783
+ /* @__PURE__ */ jsx9("span", { children: item.label })
784
+ ]
785
+ }
786
+ ) : /* @__PURE__ */ jsxs6(
787
+ "span",
788
+ {
789
+ className: cn7(
790
+ "flex items-center gap-1 px-1 py-0.5",
791
+ isLast ? "text-foreground font-medium" : "text-muted-foreground"
792
+ ),
793
+ "aria-current": isLast ? "page" : void 0,
794
+ children: [
795
+ isFirst && !item.icon && /* @__PURE__ */ jsx9(HomeIcon, { className: "h-4 w-4" }),
796
+ item.icon,
797
+ /* @__PURE__ */ jsx9("span", { children: item.label })
798
+ ]
799
+ }
800
+ )
801
+ ] }, `${item.label}-${index}`);
802
+ }) }) });
803
+ }
804
+
805
+ // src/Navbar.tsx
806
+ import { jsx as jsx10, jsxs as jsxs7 } from "react/jsx-runtime";
807
+ function Navbar({
808
+ showOrgSwitcher = true,
809
+ userActions,
810
+ breadcrumbs,
811
+ onToggleSidebar,
812
+ onNavigate,
813
+ brand,
814
+ app,
815
+ organizationId,
816
+ onOrganizationChange,
817
+ onNavigateApp,
818
+ command,
819
+ actions,
820
+ profileItems,
821
+ showThemeToggle = false
822
+ }) {
823
+ const hasSession = useSessionOptional4() !== void 0;
824
+ return /* @__PURE__ */ jsxs7("header", { className: "h-16 bg-background border-b border-border flex items-center px-4 gap-4", children: [
825
+ onToggleSidebar && /* @__PURE__ */ jsx10(
826
+ Button3,
827
+ {
828
+ variant: "ghost",
829
+ size: "icon",
830
+ onClick: onToggleSidebar,
831
+ "aria-label": "Toggle sidebar",
832
+ className: "md:hidden",
833
+ children: /* @__PURE__ */ jsx10(MenuIcon, { className: "h-5 w-5" })
834
+ }
835
+ ),
836
+ brand && /* @__PURE__ */ jsx10("div", { className: "flex items-center gap-2 shrink-0", "data-slot": "brand", children: brand }),
837
+ app && /* @__PURE__ */ jsx10(ProductSwitcher, { self: app, organizationId, onNavigate: onNavigateApp }),
838
+ onOrganizationChange ? /* @__PURE__ */ jsx10(OrganizationSwitcher, { value: organizationId, onChange: onOrganizationChange }) : showOrgSwitcher && /* @__PURE__ */ jsx10(OrgSwitcher, { onNavigate }),
839
+ command && /* @__PURE__ */ jsx10("div", { className: "flex items-center min-w-0", "data-slot": "command", children: command }),
840
+ /* @__PURE__ */ jsx10("div", { className: "flex-1 min-w-0", children: breadcrumbs && breadcrumbs.length > 0 && /* @__PURE__ */ jsx10(Breadcrumbs, { items: breadcrumbs, onNavigate }) }),
841
+ actions && /* @__PURE__ */ jsx10("div", { className: "flex items-center gap-1 shrink-0", "data-slot": "actions", children: actions }),
842
+ showThemeToggle && /* @__PURE__ */ jsx10(ThemeToggle, {}),
843
+ hasSession ? /* @__PURE__ */ jsx10(
844
+ ProfileMenu,
845
+ {
846
+ appId: app?.id,
847
+ organizationId,
848
+ items: profileItems ?? userActions,
849
+ onNavigate
850
+ }
851
+ ) : /* @__PURE__ */ jsx10(UserMenu, { actions: profileItems ?? userActions, onNavigate })
852
+ ] });
853
+ }
854
+
855
+ // src/AppShell.tsx
856
+ import { jsx as jsx11, jsxs as jsxs8 } from "react/jsx-runtime";
857
+ function AppShellInner({
858
+ children,
859
+ appName,
860
+ logo,
861
+ logoCollapsed,
862
+ navigation = [],
863
+ userActions,
864
+ sidebarCollapsible = true,
865
+ showOrgSwitcher = true,
866
+ breadcrumbs,
867
+ brand,
868
+ app,
869
+ organizationId,
870
+ onOrganizationChange,
871
+ onNavigateApp,
872
+ command,
873
+ actions,
874
+ profileItems,
875
+ showThemeToggle
876
+ }) {
877
+ const {
878
+ sidebarCollapsed,
879
+ toggleSidebar,
880
+ sidebarVisible,
881
+ toggleSidebarVisible,
882
+ currentPath,
883
+ navigate
884
+ } = useShell();
885
+ const defaultLogo = /* @__PURE__ */ jsx11("div", { className: "font-semibold text-lg text-foreground", children: appName });
886
+ const defaultLogoCollapsed = /* @__PURE__ */ jsx11("div", { className: "font-bold text-lg text-primary", children: appName.charAt(0) });
887
+ return /* @__PURE__ */ jsxs8("div", { className: "flex min-h-screen bg-secondary", children: [
888
+ /* @__PURE__ */ jsx11(
889
+ Sidebar,
890
+ {
891
+ navigation,
892
+ isCollapsed: sidebarCollapsed,
893
+ onToggleCollapse: sidebarCollapsible ? toggleSidebar : void 0,
894
+ logo: logo || defaultLogo,
895
+ logoCollapsed: logoCollapsed || defaultLogoCollapsed,
896
+ onNavigate: navigate,
897
+ currentPath
898
+ }
899
+ ),
900
+ sidebarVisible && /* @__PURE__ */ jsx11(
901
+ "div",
902
+ {
903
+ className: "fixed inset-0 bg-black/50 z-[1300] md:hidden",
904
+ onClick: toggleSidebarVisible
905
+ }
906
+ ),
907
+ /* @__PURE__ */ jsxs8("div", { className: "flex-1 flex flex-col min-w-0", children: [
908
+ /* @__PURE__ */ jsx11(
909
+ Navbar,
910
+ {
911
+ showOrgSwitcher,
912
+ userActions,
913
+ onToggleSidebar: toggleSidebarVisible,
914
+ onNavigate: navigate,
915
+ breadcrumbs,
916
+ brand,
917
+ app,
918
+ organizationId,
919
+ onOrganizationChange,
920
+ onNavigateApp,
921
+ command,
922
+ actions,
923
+ profileItems,
924
+ showThemeToggle
925
+ }
926
+ ),
927
+ /* @__PURE__ */ jsx11("main", { className: "flex-1 p-6 overflow-y-auto", children })
928
+ ] })
929
+ ] });
930
+ }
931
+ function AppShell(props) {
932
+ return /* @__PURE__ */ jsx11(
933
+ ShellProvider,
934
+ {
935
+ defaultCollapsed: props.defaultCollapsed,
936
+ onNavigate: props.onNavigate,
937
+ onCollapseChange: props.onCollapseChange,
938
+ children: /* @__PURE__ */ jsx11(AppShellInner, { ...props })
939
+ }
940
+ );
941
+ }
942
+ export {
943
+ AppShell,
944
+ Breadcrumbs,
945
+ Navbar,
946
+ OrgSwitcher,
947
+ OrganizationSwitcher,
948
+ ProductSwitcher,
949
+ ProfileMenu,
950
+ ShellProvider,
951
+ Sidebar,
952
+ THEME_CHANGE_EVENT,
953
+ THEME_STORAGE_KEY,
954
+ ThemeToggle,
955
+ UserMenu,
956
+ applyStoredTheme,
957
+ applyTheme,
958
+ initials,
959
+ nextTheme,
960
+ readStoredTheme,
961
+ resolveTheme,
962
+ switcherApps,
963
+ useShell,
964
+ useShellOptional,
965
+ useTheme
966
+ };
967
+ //# sourceMappingURL=index.mjs.map