@plexusone/shell 0.2.3 → 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/index.mjs CHANGED
@@ -1,36 +1,152 @@
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";
125
+ import { navigateTo } from "@plexusone/auth";
10
126
  import { jsx } from "react/jsx-runtime";
11
127
  var ShellContext = createContext(null);
12
- var COLLAPSED_KEY = "systemforge:sidebar-collapsed";
13
128
  function ShellProvider({
14
129
  children,
15
130
  defaultCollapsed = false,
16
131
  onNavigate,
17
132
  onCollapseChange
18
133
  }) {
19
- const [sidebarCollapsed, setSidebarCollapsedState] = useState(() => {
134
+ const [sidebarCollapsed, setSidebarCollapsedState] = useState2(() => {
20
135
  if (typeof window !== "undefined") {
136
+ adoptCarriedPreferences();
21
137
  const stored = localStorage.getItem(COLLAPSED_KEY);
22
138
  return stored ? stored === "true" : defaultCollapsed;
23
139
  }
24
140
  return defaultCollapsed;
25
141
  });
26
- const [sidebarVisible, setSidebarVisible] = useState(false);
27
- const [currentPath, setCurrentPath] = useState(() => {
142
+ const [sidebarVisible, setSidebarVisible] = useState2(false);
143
+ const [currentPath, setCurrentPath] = useState2(() => {
28
144
  if (typeof window !== "undefined") {
29
145
  return window.location.pathname;
30
146
  }
31
147
  return "/";
32
148
  });
33
- const toggleSidebar = useCallback(() => {
149
+ const toggleSidebar = useCallback2(() => {
34
150
  setSidebarCollapsedState((prev) => {
35
151
  const next = !prev;
36
152
  if (typeof window !== "undefined") {
@@ -40,7 +156,7 @@ function ShellProvider({
40
156
  return next;
41
157
  });
42
158
  }, [onCollapseChange]);
43
- const setSidebarCollapsed = useCallback(
159
+ const setSidebarCollapsed = useCallback2(
44
160
  (collapsed) => {
45
161
  setSidebarCollapsedState(collapsed);
46
162
  if (typeof window !== "undefined") {
@@ -50,22 +166,22 @@ function ShellProvider({
50
166
  },
51
167
  [onCollapseChange]
52
168
  );
53
- const toggleSidebarVisible = useCallback(() => {
169
+ const toggleSidebarVisible = useCallback2(() => {
54
170
  setSidebarVisible((prev) => !prev);
55
171
  }, []);
56
- const navigate = useCallback(
172
+ const navigate = useCallback2(
57
173
  (href) => {
58
174
  if (onNavigate) {
59
175
  onNavigate(href);
60
176
  } else {
61
- window.location.assign(href);
177
+ navigateTo(href);
62
178
  }
63
179
  setCurrentPath(href);
64
180
  setSidebarVisible(false);
65
181
  },
66
182
  [onNavigate]
67
183
  );
68
- useEffect(() => {
184
+ useEffect2(() => {
69
185
  const handlePopState = () => {
70
186
  setCurrentPath(window.location.pathname);
71
187
  };
@@ -107,7 +223,7 @@ function useShellOptional() {
107
223
  }
108
224
 
109
225
  // src/Sidebar.tsx
110
- import { ROLE_LEVELS } from "@plexusone/auth";
226
+ import { navigateTo as navigateTo2, ROLE_LEVELS, safeHref } from "@plexusone/auth";
111
227
  import { useTenantOptional } from "@plexusone/tenant";
112
228
  import {
113
229
  Badge,
@@ -142,13 +258,13 @@ function NavItemComponent({
142
258
  if (onNavigate) {
143
259
  onNavigate(item.href);
144
260
  } else {
145
- window.location.assign(item.href);
261
+ navigateTo2(item.href);
146
262
  }
147
263
  };
148
264
  return /* @__PURE__ */ jsxs(
149
265
  "a",
150
266
  {
151
- href: item.href,
267
+ href: safeHref(item.href),
152
268
  onClick: handleClick,
153
269
  target: item.external ? "_blank" : void 0,
154
270
  rel: item.external ? "noopener noreferrer" : void 0,
@@ -383,14 +499,13 @@ function OrganizationSwitcher({
383
499
  }
384
500
 
385
501
  // src/ProductSwitcher.tsx
386
- import { membershipFor, useSessionOptional as useSessionOptional2 } from "@plexusone/auth";
502
+ import { membershipFor, safeHref as safeHref2, useSessionOptional as useSessionOptional2 } from "@plexusone/auth";
387
503
  import { cn as cn4 } from "@plexusone/ui";
388
504
 
389
505
  // src/switcher.ts
390
506
  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];
507
+ const listed = apps.filter((a) => a.id === self.id || Boolean(membership?.applications[a.id])).map((a) => a.id === self.id ? { ...a, ...self } : a);
508
+ return listed.some((a) => a.id === self.id) ? listed : [self, ...listed];
394
509
  }
395
510
 
396
511
  // src/ProductSwitcher.tsx
@@ -406,8 +521,18 @@ function ProductSwitcher({
406
521
  const session = ctx?.session;
407
522
  const catalog = apps ?? session?.apps ?? [];
408
523
  const membership = session ? membershipFor(session, organizationId) : void 0;
409
- const entries = switcherApps(catalog, self, membership);
524
+ const entries = switcherApps(catalog, self, membership).filter(
525
+ (app) => app.id === self.id || safeHref2(app.url) !== void 0
526
+ );
410
527
  const follow = (url) => (e) => {
528
+ if (!onNavigate) {
529
+ const href = safeHref2(carryPreferences(url));
530
+ if (href === void 0) {
531
+ e.preventDefault();
532
+ return;
533
+ }
534
+ e.currentTarget.href = href;
535
+ }
411
536
  if (!onNavigate || e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey) {
412
537
  return;
413
538
  }
@@ -428,6 +553,7 @@ function ProductSwitcher({
428
553
  {
429
554
  href: app.url,
430
555
  onClick: follow(app.url),
556
+ onAuxClick: follow(app.url),
431
557
  className: "px-3 py-1.5 rounded-md text-sm text-muted-foreground hover:bg-accent hover:text-foreground",
432
558
  children: app.name
433
559
  },
@@ -440,7 +566,7 @@ function ProductSwitcher({
440
566
  import { useSessionOptional as useSessionOptional4 } from "@plexusone/auth";
441
567
 
442
568
  // src/ProfileMenu.tsx
443
- import { membershipFor as membershipFor2, organizationLabel as organizationLabel2, useSessionOptional as useSessionOptional3 } from "@plexusone/auth";
569
+ import { membershipFor as membershipFor2, navigateTo as navigateTo3, organizationLabel as organizationLabel2, useSessionOptional as useSessionOptional3 } from "@plexusone/auth";
444
570
  import {
445
571
  Avatar as Avatar2,
446
572
  AvatarFallback as AvatarFallback2,
@@ -482,7 +608,7 @@ function ProfileMenu({
482
608
  item.onClick();
483
609
  } else if (item.href) {
484
610
  if (onNavigate) onNavigate(item.href);
485
- else window.location.assign(item.href);
611
+ else navigateTo3(item.href);
486
612
  }
487
613
  };
488
614
  return /* @__PURE__ */ jsxs4(DropdownMenu3, { children: [
@@ -550,76 +676,6 @@ function ProfileMenu({
550
676
 
551
677
  // src/ThemeToggle.tsx
552
678
  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
679
  import { jsx as jsx7 } from "react/jsx-runtime";
624
680
  var labels = {
625
681
  light: "Light theme",
@@ -644,7 +700,7 @@ function ThemeToggle() {
644
700
  }
645
701
 
646
702
  // src/UserMenu.tsx
647
- import { useAuthOptional } from "@plexusone/auth";
703
+ import { navigateTo as navigateTo4, useAuthOptional } from "@plexusone/auth";
648
704
  import {
649
705
  Avatar as Avatar3,
650
706
  AvatarImage as AvatarImage3,
@@ -674,7 +730,7 @@ function UserMenu({ actions = defaultActions, onNavigate }) {
674
730
  if (onNavigate) {
675
731
  onNavigate(action.href);
676
732
  } else {
677
- window.location.assign(action.href);
733
+ navigateTo4(action.href);
678
734
  }
679
735
  }
680
736
  };
@@ -739,6 +795,7 @@ function UserMenu({ actions = defaultActions, onNavigate }) {
739
795
 
740
796
  // src/Breadcrumbs.tsx
741
797
  import { ChevronRightIcon, HomeIcon, cn as cn7 } from "@plexusone/ui";
798
+ import { navigateTo as navigateTo5, safeHref as safeHref3 } from "@plexusone/auth";
742
799
  import { jsx as jsx9, jsxs as jsxs6 } from "react/jsx-runtime";
743
800
  function Breadcrumbs({
744
801
  items,
@@ -755,7 +812,7 @@ function Breadcrumbs({
755
812
  if (onNavigate) {
756
813
  onNavigate(href);
757
814
  } else {
758
- window.location.assign(href);
815
+ navigateTo5(href);
759
816
  }
760
817
  };
761
818
  let displayItems = items;
@@ -774,7 +831,7 @@ function Breadcrumbs({
774
831
  isCollapsed ? /* @__PURE__ */ jsx9("span", { className: "text-muted-foreground px-1 py-0.5", children: item.label }) : item.href && !isLast ? /* @__PURE__ */ jsxs6(
775
832
  "a",
776
833
  {
777
- href: item.href,
834
+ href: safeHref3(item.href),
778
835
  onClick: (e) => handleClick(item.href, e),
779
836
  className: "flex items-center gap-1 text-muted-foreground no-underline px-1 py-0.5 rounded hover:text-foreground hover:bg-accent",
780
837
  children: [
@@ -942,6 +999,7 @@ function AppShell(props) {
942
999
  export {
943
1000
  AppShell,
944
1001
  Breadcrumbs,
1002
+ COLLAPSED_KEY,
945
1003
  Navbar,
946
1004
  OrgSwitcher,
947
1005
  OrganizationSwitcher,
@@ -953,8 +1011,10 @@ export {
953
1011
  THEME_STORAGE_KEY,
954
1012
  ThemeToggle,
955
1013
  UserMenu,
1014
+ adoptCarriedPreferences,
956
1015
  applyStoredTheme,
957
1016
  applyTheme,
1017
+ carryPreferences,
958
1018
  initials,
959
1019
  nextTheme,
960
1020
  readStoredTheme,