@plexusone/shell 0.2.1 → 0.2.4

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 CHANGED
@@ -1,36 +1,151 @@
1
+ // src/theme.ts
2
+ import { useCallback, useEffect, useState } from "react";
3
+ var THEME_STORAGE_KEY = "systemforge:theme";
4
+ var THEME_CHANGE_EVENT = "systemforge:theme-change";
5
+ var darkQuery = "(prefers-color-scheme: dark)";
6
+ function isTheme(v) {
7
+ return v === "light" || v === "dark" || v === "system";
8
+ }
9
+ function readStoredTheme() {
10
+ try {
11
+ const v = window.localStorage.getItem(THEME_STORAGE_KEY);
12
+ return isTheme(v) ? v : "system";
13
+ } catch {
14
+ return "system";
15
+ }
16
+ }
17
+ function storeTheme(theme) {
18
+ try {
19
+ window.localStorage.setItem(THEME_STORAGE_KEY, theme);
20
+ } catch {
21
+ }
22
+ }
23
+ function systemPrefersDark() {
24
+ return typeof window.matchMedia === "function" && window.matchMedia(darkQuery).matches;
25
+ }
26
+ function resolveTheme(theme, prefersDark = systemPrefersDark()) {
27
+ if (theme === "system") return prefersDark ? "dark" : "light";
28
+ return theme;
29
+ }
30
+ function applyTheme(theme, root = document.documentElement) {
31
+ const resolved = resolveTheme(theme);
32
+ root.classList.toggle("dark", resolved === "dark");
33
+ root.style.colorScheme = resolved;
34
+ }
35
+ function applyStoredTheme() {
36
+ adoptCarriedPreferences();
37
+ applyTheme(readStoredTheme());
38
+ }
39
+ function nextTheme(theme) {
40
+ return theme === "light" ? "dark" : theme === "dark" ? "system" : "light";
41
+ }
42
+ function useTheme() {
43
+ const [theme, setThemeState] = useState(
44
+ () => typeof window === "undefined" ? "system" : readStoredTheme()
45
+ );
46
+ useEffect(() => {
47
+ applyTheme(theme);
48
+ if (theme !== "system" || typeof window.matchMedia !== "function") return void 0;
49
+ const mq = window.matchMedia(darkQuery);
50
+ const onChange = () => applyTheme("system");
51
+ mq.addEventListener("change", onChange);
52
+ return () => mq.removeEventListener("change", onChange);
53
+ }, [theme]);
54
+ useEffect(() => {
55
+ const onThemeChange = (e) => {
56
+ const next = e.detail;
57
+ if (isTheme(next)) setThemeState(next);
58
+ };
59
+ window.addEventListener(THEME_CHANGE_EVENT, onThemeChange);
60
+ return () => window.removeEventListener(THEME_CHANGE_EVENT, onThemeChange);
61
+ }, []);
62
+ const setTheme = useCallback((next) => {
63
+ storeTheme(next);
64
+ setThemeState(next);
65
+ window.dispatchEvent(new CustomEvent(THEME_CHANGE_EVENT, { detail: next }));
66
+ }, []);
67
+ return [theme, setTheme];
68
+ }
69
+
70
+ // src/preferences.ts
71
+ var COLLAPSED_KEY = "systemforge:sidebar-collapsed";
72
+ var SIDEBAR_PARAM = "sf-sidebar";
73
+ var THEME_PARAM = "sf-theme";
74
+ function read(key) {
75
+ try {
76
+ return window.localStorage.getItem(key);
77
+ } catch {
78
+ return null;
79
+ }
80
+ }
81
+ function write(key, value) {
82
+ try {
83
+ window.localStorage.setItem(key, value);
84
+ } catch {
85
+ }
86
+ }
87
+ function carryPreferences(url, prefs = {}) {
88
+ if (typeof window === "undefined" || url.includes("#")) return url;
89
+ const p = new URLSearchParams();
90
+ const collapsed = prefs.sidebarCollapsed ?? read(COLLAPSED_KEY) === "true";
91
+ p.set(SIDEBAR_PARAM, collapsed ? "collapsed" : "expanded");
92
+ const theme = prefs.theme ?? read(THEME_STORAGE_KEY);
93
+ if (theme === "light" || theme === "dark" || theme === "system") p.set(THEME_PARAM, theme);
94
+ return `${url}#${p.toString()}`;
95
+ }
96
+ function adoptCarriedPreferences() {
97
+ if (typeof window === "undefined" || !window.location.hash) return;
98
+ const p = new URLSearchParams(window.location.hash.slice(1));
99
+ const sidebar = p.get(SIDEBAR_PARAM);
100
+ const theme = p.get(THEME_PARAM);
101
+ if (sidebar === null && theme === null) return;
102
+ if (sidebar === "collapsed" || sidebar === "expanded")
103
+ write(COLLAPSED_KEY, String(sidebar === "collapsed"));
104
+ if (theme === "light" || theme === "dark" || theme === "system") write(THEME_STORAGE_KEY, theme);
105
+ p.delete(SIDEBAR_PARAM);
106
+ p.delete(THEME_PARAM);
107
+ const rest = p.toString();
108
+ const { pathname, search } = window.location;
109
+ window.history.replaceState(
110
+ window.history.state,
111
+ "",
112
+ `${pathname}${search}${rest ? `#${rest}` : ""}`
113
+ );
114
+ }
115
+
1
116
  // src/ShellContext.tsx
2
117
  import {
3
118
  createContext,
4
119
  useContext,
5
- useState,
6
- useCallback,
120
+ useState as useState2,
121
+ useCallback as useCallback2,
7
122
  useMemo,
8
- useEffect
123
+ useEffect as useEffect2
9
124
  } from "react";
10
125
  import { jsx } from "react/jsx-runtime";
11
126
  var ShellContext = createContext(null);
12
- var COLLAPSED_KEY = "systemforge:sidebar-collapsed";
13
127
  function ShellProvider({
14
128
  children,
15
129
  defaultCollapsed = false,
16
130
  onNavigate,
17
131
  onCollapseChange
18
132
  }) {
19
- const [sidebarCollapsed, setSidebarCollapsedState] = useState(() => {
133
+ const [sidebarCollapsed, setSidebarCollapsedState] = useState2(() => {
20
134
  if (typeof window !== "undefined") {
135
+ adoptCarriedPreferences();
21
136
  const stored = localStorage.getItem(COLLAPSED_KEY);
22
137
  return stored ? stored === "true" : defaultCollapsed;
23
138
  }
24
139
  return defaultCollapsed;
25
140
  });
26
- const [sidebarVisible, setSidebarVisible] = useState(false);
27
- const [currentPath, setCurrentPath] = useState(() => {
141
+ const [sidebarVisible, setSidebarVisible] = useState2(false);
142
+ const [currentPath, setCurrentPath] = useState2(() => {
28
143
  if (typeof window !== "undefined") {
29
144
  return window.location.pathname;
30
145
  }
31
146
  return "/";
32
147
  });
33
- const toggleSidebar = useCallback(() => {
148
+ const toggleSidebar = useCallback2(() => {
34
149
  setSidebarCollapsedState((prev) => {
35
150
  const next = !prev;
36
151
  if (typeof window !== "undefined") {
@@ -40,7 +155,7 @@ function ShellProvider({
40
155
  return next;
41
156
  });
42
157
  }, [onCollapseChange]);
43
- const setSidebarCollapsed = useCallback(
158
+ const setSidebarCollapsed = useCallback2(
44
159
  (collapsed) => {
45
160
  setSidebarCollapsedState(collapsed);
46
161
  if (typeof window !== "undefined") {
@@ -50,10 +165,10 @@ function ShellProvider({
50
165
  },
51
166
  [onCollapseChange]
52
167
  );
53
- const toggleSidebarVisible = useCallback(() => {
168
+ const toggleSidebarVisible = useCallback2(() => {
54
169
  setSidebarVisible((prev) => !prev);
55
170
  }, []);
56
- const navigate = useCallback(
171
+ const navigate = useCallback2(
57
172
  (href) => {
58
173
  if (onNavigate) {
59
174
  onNavigate(href);
@@ -65,7 +180,7 @@ function ShellProvider({
65
180
  },
66
181
  [onNavigate]
67
182
  );
68
- useEffect(() => {
183
+ useEffect2(() => {
69
184
  const handlePopState = () => {
70
185
  setCurrentPath(window.location.pathname);
71
186
  };
@@ -107,7 +222,8 @@ function useShellOptional() {
107
222
  }
108
223
 
109
224
  // src/Sidebar.tsx
110
- import { useRoleCheck } from "@plexusone/tenant";
225
+ import { ROLE_LEVELS } from "@plexusone/auth";
226
+ import { useTenantOptional } from "@plexusone/tenant";
111
227
  import {
112
228
  Badge,
113
229
  Button,
@@ -117,14 +233,21 @@ import {
117
233
  cn
118
234
  } from "@plexusone/ui";
119
235
  import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
236
+ function mayView(tenant, roles) {
237
+ if (!tenant) return false;
238
+ if (tenant.isPlatformAdmin) return true;
239
+ if (!tenant.membership) return false;
240
+ const required = Math.min(...roles.map((r) => ROLE_LEVELS[r]));
241
+ return ROLE_LEVELS[tenant.membership.role] >= required;
242
+ }
120
243
  function NavItemComponent({
121
244
  item,
122
245
  isCollapsed,
123
246
  onNavigate,
124
247
  currentPath
125
248
  }) {
126
- const { hasRole } = useRoleCheck();
127
- if (item.roles && !hasRole(item.roles)) {
249
+ const tenant = useTenantOptional();
250
+ if (item.roles && !mayView(tenant, item.roles)) {
128
251
  return null;
129
252
  }
130
253
  const isActive = item.isActive ?? currentPath === item.href;
@@ -240,7 +363,7 @@ function Sidebar({
240
363
  import { Button as Button3, MenuIcon } from "@plexusone/ui";
241
364
 
242
365
  // src/OrgSwitcher.tsx
243
- import { useTenantOptional } from "@plexusone/tenant";
366
+ import { useTenantOptional as useTenantOptional2 } from "@plexusone/tenant";
244
367
  import {
245
368
  DropdownMenu,
246
369
  DropdownMenuContent,
@@ -256,7 +379,7 @@ import {
256
379
  } from "@plexusone/ui";
257
380
  import { Fragment as Fragment2, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
258
381
  function OrgSwitcher({ compact = false }) {
259
- const tenant = useTenantOptional();
382
+ const tenant = useTenantOptional2();
260
383
  if (!tenant) {
261
384
  return null;
262
385
  }
@@ -380,9 +503,8 @@ import { cn as cn4 } from "@plexusone/ui";
380
503
 
381
504
  // src/switcher.ts
382
505
  function switcherApps(apps, self, membership) {
383
- const others = apps.filter((a) => a.id !== self.id && Boolean(membership?.applications[a.id]));
384
- const fromCatalog = apps.find((a) => a.id === self.id);
385
- return [fromCatalog ? { ...fromCatalog, ...self } : self, ...others];
506
+ const listed = apps.filter((a) => a.id === self.id || Boolean(membership?.applications[a.id])).map((a) => a.id === self.id ? { ...a, ...self } : a);
507
+ return listed.some((a) => a.id === self.id) ? listed : [self, ...listed];
386
508
  }
387
509
 
388
510
  // src/ProductSwitcher.tsx
@@ -400,6 +522,7 @@ function ProductSwitcher({
400
522
  const membership = session ? membershipFor(session, organizationId) : void 0;
401
523
  const entries = switcherApps(catalog, self, membership);
402
524
  const follow = (url) => (e) => {
525
+ if (!onNavigate) e.currentTarget.href = carryPreferences(url);
403
526
  if (!onNavigate || e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey) {
404
527
  return;
405
528
  }
@@ -420,6 +543,7 @@ function ProductSwitcher({
420
543
  {
421
544
  href: app.url,
422
545
  onClick: follow(app.url),
546
+ onAuxClick: follow(app.url),
423
547
  className: "px-3 py-1.5 rounded-md text-sm text-muted-foreground hover:bg-accent hover:text-foreground",
424
548
  children: app.name
425
549
  },
@@ -542,76 +666,6 @@ function ProfileMenu({
542
666
 
543
667
  // src/ThemeToggle.tsx
544
668
  import { Button as Button2, MonitorIcon, MoonIcon, SunIcon } from "@plexusone/ui";
545
-
546
- // src/theme.ts
547
- import { useCallback as useCallback2, useEffect as useEffect2, useState as useState2 } from "react";
548
- var THEME_STORAGE_KEY = "systemforge:theme";
549
- var THEME_CHANGE_EVENT = "systemforge:theme-change";
550
- var darkQuery = "(prefers-color-scheme: dark)";
551
- function isTheme(v) {
552
- return v === "light" || v === "dark" || v === "system";
553
- }
554
- function readStoredTheme() {
555
- try {
556
- const v = window.localStorage.getItem(THEME_STORAGE_KEY);
557
- return isTheme(v) ? v : "system";
558
- } catch {
559
- return "system";
560
- }
561
- }
562
- function storeTheme(theme) {
563
- try {
564
- window.localStorage.setItem(THEME_STORAGE_KEY, theme);
565
- } catch {
566
- }
567
- }
568
- function systemPrefersDark() {
569
- return typeof window.matchMedia === "function" && window.matchMedia(darkQuery).matches;
570
- }
571
- function resolveTheme(theme, prefersDark = systemPrefersDark()) {
572
- if (theme === "system") return prefersDark ? "dark" : "light";
573
- return theme;
574
- }
575
- function applyTheme(theme, root = document.documentElement) {
576
- const resolved = resolveTheme(theme);
577
- root.classList.toggle("dark", resolved === "dark");
578
- root.style.colorScheme = resolved;
579
- }
580
- function applyStoredTheme() {
581
- applyTheme(readStoredTheme());
582
- }
583
- function nextTheme(theme) {
584
- return theme === "light" ? "dark" : theme === "dark" ? "system" : "light";
585
- }
586
- function useTheme() {
587
- const [theme, setThemeState] = useState2(
588
- () => typeof window === "undefined" ? "system" : readStoredTheme()
589
- );
590
- useEffect2(() => {
591
- applyTheme(theme);
592
- if (theme !== "system" || typeof window.matchMedia !== "function") return void 0;
593
- const mq = window.matchMedia(darkQuery);
594
- const onChange = () => applyTheme("system");
595
- mq.addEventListener("change", onChange);
596
- return () => mq.removeEventListener("change", onChange);
597
- }, [theme]);
598
- useEffect2(() => {
599
- const onThemeChange = (e) => {
600
- const next = e.detail;
601
- if (isTheme(next)) setThemeState(next);
602
- };
603
- window.addEventListener(THEME_CHANGE_EVENT, onThemeChange);
604
- return () => window.removeEventListener(THEME_CHANGE_EVENT, onThemeChange);
605
- }, []);
606
- const setTheme = useCallback2((next) => {
607
- storeTheme(next);
608
- setThemeState(next);
609
- window.dispatchEvent(new CustomEvent(THEME_CHANGE_EVENT, { detail: next }));
610
- }, []);
611
- return [theme, setTheme];
612
- }
613
-
614
- // src/ThemeToggle.tsx
615
669
  import { jsx as jsx7 } from "react/jsx-runtime";
616
670
  var labels = {
617
671
  light: "Light theme",
@@ -934,6 +988,7 @@ function AppShell(props) {
934
988
  export {
935
989
  AppShell,
936
990
  Breadcrumbs,
991
+ COLLAPSED_KEY,
937
992
  Navbar,
938
993
  OrgSwitcher,
939
994
  OrganizationSwitcher,
@@ -945,8 +1000,10 @@ export {
945
1000
  THEME_STORAGE_KEY,
946
1001
  ThemeToggle,
947
1002
  UserMenu,
1003
+ adoptCarriedPreferences,
948
1004
  applyStoredTheme,
949
1005
  applyTheme,
1006
+ carryPreferences,
950
1007
  initials,
951
1008
  nextTheme,
952
1009
  readStoredTheme,