@umituz/web-dashboard 7.4.4 → 7.6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-dashboard",
3
- "version": "7.4.4",
3
+ "version": "7.6.0",
4
4
  "description": "Installable app skeleton for the fleet — layouts, analytics (dependency-free SVG charts), calendar, onboarding flow permissions/module registry; udb-* stylesheet on design-system tokens",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -80,7 +80,7 @@
80
80
  "@types/react-dom": "^19.2.3",
81
81
  "@typescript-eslint/eslint-plugin": "^8.57.2",
82
82
  "@typescript-eslint/parser": "^8.57.2",
83
- "@umituz/web-design-system": "^4.21.0",
83
+ "@umituz/web-design-system": "^4.29.4",
84
84
  "eslint": "^10.0.2",
85
85
  "framer-motion": "^12.23.12",
86
86
  "jsdom": "^29.1.1",
@@ -174,6 +174,8 @@ export interface BarSeries {
174
174
  data?: readonly number[];
175
175
  tone?: string;
176
176
  color?: string;
177
+ /** Per-bar color override (index-aligned with labels); undefined falls back to the series tone. */
178
+ accents?: readonly (string | undefined)[];
177
179
  }
178
180
 
179
181
  export interface BarChartGoalLine {
@@ -201,6 +203,7 @@ export function BarChart({
201
203
  const vals = (s: BarSeries) => s.values ?? s.data ?? [];
202
204
  const max = Math.max(1, ...series.flatMap((s) => vals(s)), goalLine?.value ?? 0);
203
205
  const toneOf = (s: BarSeries, i: number) => s.tone ?? s.color ?? PALETTE[i % PALETTE.length]!;
206
+ const accentOf = (s: BarSeries, si: number, i: number) => s.accents?.[i] ?? toneOf(s, si);
204
207
  const goalPct = goalLine !== undefined ? Math.min(100, (goalLine.value / max) * 100) : null;
205
208
  return (
206
209
  <div className="udb-barchart" style={{ height }}>
@@ -244,7 +247,7 @@ export function BarChart({
244
247
  className="udb-barchart__bar"
245
248
  style={{
246
249
  height: `${Math.max(2, ((vals(s)[i] ?? 0) / max) * 100)}%`,
247
- background: toneOf(s, si),
250
+ background: accentOf(s, si, i),
248
251
  opacity: hover === null || hover === i ? 1 : 0.4,
249
252
  }}
250
253
  />
@@ -258,7 +261,7 @@ export function BarChart({
258
261
  {series.map((s, si) => (
259
262
  <div key={s.key ?? s.label} className="udb-barchart__tiprow">
260
263
  <span className="udb-barchart__tiplabel">
261
- <span className="udb-chartlegend__dot" style={{ background: toneOf(s, si) }} />
264
+ <span className="udb-chartlegend__dot" style={{ background: accentOf(s, si, i) }} />
262
265
  {s.label}
263
266
  </span>
264
267
 
@@ -0,0 +1,97 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { renderToString } from "react-dom/server";
3
+ import {
4
+ FilterPanel,
5
+ FilterSection,
6
+ CheckFilterList,
7
+ SingleSelectFilterList,
8
+ } from "./FilterPanel";
9
+
10
+ describe("FilterPanel", () => {
11
+ it("renders the header, clear action and sections", () => {
12
+ const html = renderToString(
13
+ <FilterPanel title="Program filters" onClear={() => undefined}>
14
+ <FilterSection title="Status">
15
+ <span>body</span>
16
+ </FilterSection>
17
+ </FilterPanel>,
18
+ );
19
+ expect(html).toContain('aria-label="Program filters"');
20
+ expect(html).toContain("Program filters");
21
+ expect(html).toContain("Clear all");
22
+ expect(html).toContain("Status");
23
+ });
24
+
25
+ it("can disable and label the clear action", () => {
26
+ const html = renderToString(
27
+ <FilterPanel onClear={() => undefined} clearDisabled clearLabel="Temizle">
28
+ <span>body</span>
29
+ </FilterPanel>,
30
+ );
31
+ expect(html).toContain("Temizle");
32
+ expect(html).toContain("disabled");
33
+ });
34
+ });
35
+
36
+ describe("CheckFilterList", () => {
37
+ const options = Array.from({ length: 10 }, (_, i) => ({
38
+ value: `v${i}`,
39
+ label: `Option ${i}`,
40
+ }));
41
+
42
+ it("marks selected options checked", () => {
43
+ const html = renderToString(
44
+ <CheckFilterList options={options.slice(0, 3)} selected={["v1"]} onToggle={() => undefined} />,
45
+ );
46
+ expect(html).toContain('aria-checked="true"');
47
+ expect(html).toContain("Option 1");
48
+ });
49
+
50
+ it("caps visible options, keeps hidden selected ones and notes the rest", () => {
51
+ const html = renderToString(
52
+ <CheckFilterList options={options} selected={["v9"]} onToggle={() => undefined} maxVisible={4} />,
53
+ );
54
+ expect(html).toContain("Option 3");
55
+ expect(html).toContain("Option 9");
56
+ expect(html).not.toContain("Option 5<");
57
+ expect(html).toContain("+5 more");
58
+ });
59
+ });
60
+
61
+ describe("SingleSelectFilterList", () => {
62
+ const options = [
63
+ { value: "all", label: "All", count: 42 },
64
+ { value: "open", label: "Open", count: 7 },
65
+ ];
66
+
67
+ it("marks the active option as the checked radio", () => {
68
+ const html = renderToString(
69
+ <SingleSelectFilterList options={options} value="open" onChange={() => undefined} />,
70
+ );
71
+ expect(html).toContain('role="radio"');
72
+ expect(html).toContain('aria-checked="true"');
73
+ expect(html).toContain(">7</span>");
74
+ });
75
+ });
76
+
77
+ import { BarChart } from "../../charts/components";
78
+
79
+ describe("BarChart per-bar accents", () => {
80
+ it("paints bars with accent overrides falling back to the series tone", () => {
81
+ const html = renderToString(
82
+ <BarChart
83
+ labels={["Mon", "Tue"]}
84
+ series={[
85
+ {
86
+ label: "load",
87
+ values: [3, 5],
88
+ tone: "#111111",
89
+ accents: ["#ff0000", undefined],
90
+ },
91
+ ]}
92
+ />,
93
+ );
94
+ expect(html).toContain("#ff0000");
95
+ expect(html).toContain("#111111");
96
+ });
97
+ });
@@ -0,0 +1,132 @@
1
+ import type { ReactNode } from 'react';
2
+ import { Check } from 'lucide-react';
3
+
4
+ export interface FilterPanelProps {
5
+ title?: string;
6
+ onClear?: () => void;
7
+ clearDisabled?: boolean;
8
+ clearLabel?: string;
9
+ children: ReactNode;
10
+ className?: string;
11
+ }
12
+
13
+ /** Sidebar container for a set of filter sections. */
14
+ export function FilterPanel({
15
+ title = 'Filters',
16
+ onClear,
17
+ clearDisabled,
18
+ clearLabel = 'Clear all',
19
+ children,
20
+ className,
21
+ }: FilterPanelProps) {
22
+ return (
23
+ <aside className={className !== undefined ? `udb-filter-panel ${className}` : 'udb-filter-panel'} aria-label={title}>
24
+ <div className="udb-filter-panel__header">
25
+ <span className="udb-filter-panel__title">{title}</span>
26
+ {onClear !== undefined && (
27
+ <button type="button" className="udb-filter-panel__clear" onClick={onClear} disabled={clearDisabled}>
28
+ {clearLabel}
29
+ </button>
30
+ )}
31
+ </div>
32
+ <div className="udb-filter-panel__sections">{children}</div>
33
+ </aside>
34
+ );
35
+ }
36
+
37
+ export interface FilterSectionProps {
38
+ title: string;
39
+ children: ReactNode;
40
+ }
41
+
42
+ export function FilterSection({ title, children }: FilterSectionProps) {
43
+ return (
44
+ <section className="udb-filter-section">
45
+ <h4 className="udb-filter-section__title">{title}</h4>
46
+ <div className="udb-filter-section__body">{children}</div>
47
+ </section>
48
+ );
49
+ }
50
+
51
+ export interface FilterListOption {
52
+ value: string;
53
+ label: string;
54
+ count?: number;
55
+ }
56
+
57
+ export interface CheckFilterListProps {
58
+ options: readonly FilterListOption[];
59
+ selected: readonly string[];
60
+ onToggle: (value: string) => void;
61
+ /** Limit visible options; hidden-but-selected ones stay visible with a "+N more" note. */
62
+ maxVisible?: number;
63
+ }
64
+
65
+ export function CheckFilterList({ options, selected, onToggle, maxVisible = 8 }: CheckFilterListProps) {
66
+ const visible = options.slice(0, maxVisible);
67
+ const hasMore = options.length > maxVisible;
68
+ const hiddenSelected = options.slice(maxVisible).filter((option) => selected.includes(option.value));
69
+ const shown = hasMore ? [...visible, ...hiddenSelected] : visible;
70
+
71
+ return (
72
+ <ul className="udb-check-filter" role="group">
73
+ {shown.map((option) => {
74
+ const isSelected = selected.includes(option.value);
75
+ return (
76
+ <li key={option.value}>
77
+ <button
78
+ type="button"
79
+ role="checkbox"
80
+ aria-checked={isSelected}
81
+ className={isSelected ? 'udb-check-filter__item udb-check-filter__item--checked' : 'udb-check-filter__item'}
82
+ onClick={() => onToggle(option.value)}
83
+ >
84
+ <span className="udb-check-filter__box" aria-hidden="true">
85
+ {isSelected && <Check size={12} strokeWidth={3} />}
86
+ </span>
87
+ <span className="udb-check-filter__label">{option.label}</span>
88
+ {option.count !== undefined && <span className="udb-check-filter__count">{option.count}</span>}
89
+ </button>
90
+ </li>
91
+ );
92
+ })}
93
+ {hasMore && (
94
+ <li className="udb-check-filter__more">{`+${options.length - shown.length} more`}</li>
95
+ )}
96
+ </ul>
97
+ );
98
+ }
99
+
100
+ export interface SingleSelectFilterListProps {
101
+ options: readonly FilterListOption[];
102
+ value: string | null;
103
+ onChange: (value: string | null) => void;
104
+ }
105
+
106
+ /** Radio-style list where one option (or none) is active. */
107
+ export function SingleSelectFilterList({ options, value, onChange }: SingleSelectFilterListProps) {
108
+ return (
109
+ <ul className="udb-check-filter" role="group">
110
+ {options.map((option) => {
111
+ const isActive = value === option.value;
112
+ return (
113
+ <li key={option.value}>
114
+ <button
115
+ type="button"
116
+ role="radio"
117
+ aria-checked={isActive}
118
+ className={isActive ? 'udb-check-filter__item udb-check-filter__item--checked' : 'udb-check-filter__item'}
119
+ onClick={() => onChange(isActive ? null : option.value)}
120
+ >
121
+ <span className="udb-check-filter__box udb-check-filter__box--radio" aria-hidden="true">
122
+ {isActive && <span className="udb-check-filter__radio-dot" />}
123
+ </span>
124
+ <span className="udb-check-filter__label">{option.label}</span>
125
+ {option.count !== undefined && <span className="udb-check-filter__count">{option.count}</span>}
126
+ </button>
127
+ </li>
128
+ );
129
+ })}
130
+ </ul>
131
+ );
132
+ }
@@ -0,0 +1,33 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { renderToString } from "react-dom/server";
3
+ import { MemoryRouter } from "react-router";
4
+ import { TabNav } from "./TabNav";
5
+
6
+ describe("TabNav", () => {
7
+ const items = [
8
+ { to: "/events", label: "Events", end: true },
9
+ { to: "/events/program", label: "Program" },
10
+ ];
11
+
12
+ it("renders nav links with the accessible label", () => {
13
+ const html = renderToString(
14
+ <MemoryRouter>
15
+ <TabNav items={items} />
16
+ </MemoryRouter>,
17
+ );
18
+ expect(html).toContain('aria-label="Section tabs"');
19
+ expect(html).toContain("Events");
20
+ expect(html).toContain("Program");
21
+ expect(html).toContain('href="/events"');
22
+ expect(html).toContain('href="/events/program"');
23
+ });
24
+
25
+ it("marks the matching route active", () => {
26
+ const html = renderToString(
27
+ <MemoryRouter initialEntries={["/events/program"]}>
28
+ <TabNav items={items} />
29
+ </MemoryRouter>,
30
+ );
31
+ expect(html).toContain("udb-tabnav__tab--active");
32
+ });
33
+ });
@@ -0,0 +1,33 @@
1
+ import { NavLink } from 'react-router';
2
+
3
+ export interface TabNavItem {
4
+ to: string;
5
+ label: string;
6
+ /** Should the route match only exactly (e.g. the index tab). */
7
+ end?: boolean;
8
+ }
9
+
10
+ export interface TabNavProps {
11
+ items: readonly TabNavItem[];
12
+ className?: string;
13
+ }
14
+
15
+ /** Route-connected section tabs with an active underline. */
16
+ export function TabNav({ items, className }: TabNavProps) {
17
+ return (
18
+ <nav className={className !== undefined ? `udb-tabnav ${className}` : 'udb-tabnav'} aria-label="Section tabs">
19
+ {items.map((item) => (
20
+ <NavLink
21
+ key={item.to}
22
+ to={item.to}
23
+ end={item.end}
24
+ className={({ isActive }) =>
25
+ isActive ? 'udb-tabnav__tab udb-tabnav__tab--active' : 'udb-tabnav__tab'
26
+ }
27
+ >
28
+ {item.label}
29
+ </NavLink>
30
+ ))}
31
+ </nav>
32
+ );
33
+ }
@@ -11,3 +11,13 @@ export { PageHeader } from './PageHeader';
11
11
  export type { PageHeaderProps, PageHeaderCrumb } from './PageHeader';
12
12
  export { SectionHeader } from './SectionHeader';
13
13
  export type { SectionHeaderProps } from './SectionHeader';
14
+ export { TabNav } from './TabNav';
15
+ export type { TabNavItem, TabNavProps } from './TabNav';
16
+ export { FilterPanel, FilterSection, CheckFilterList, SingleSelectFilterList } from './FilterPanel';
17
+ export type {
18
+ FilterPanelProps,
19
+ FilterSectionProps,
20
+ FilterListOption,
21
+ CheckFilterListProps,
22
+ SingleSelectFilterListProps,
23
+ } from './FilterPanel';
@@ -0,0 +1,144 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { render, screen } from "@testing-library/react";
3
+ import { MemoryRouter, Route, Routes } from "react-router";
4
+ import {
5
+ Can,
6
+ PermissionsProvider,
7
+ RequirePermission,
8
+ usePermissions,
9
+ } from "./PermissionsContext";
10
+ import type { PermissionMatrix } from "./types";
11
+
12
+ const MATRIX: PermissionMatrix = {
13
+ owner: "*",
14
+ editor: ["orders.view", "orders.edit"],
15
+ viewer: ["orders.view"],
16
+ };
17
+
18
+ const mount = (role: string | null, node: React.ReactNode) =>
19
+ render(
20
+ <PermissionsProvider matrix={MATRIX} role={role}>
21
+ {node}
22
+ </PermissionsProvider>,
23
+ );
24
+
25
+ describe("usePermissions", () => {
26
+ it("exposes role, can and canAny from the provider", () => {
27
+ const captured: ReturnType<typeof usePermissions>[] = [];
28
+ const Probe = () => {
29
+ captured.push(usePermissions());
30
+ return null;
31
+ };
32
+ mount("editor", <Probe />);
33
+ const value = captured[0]!;
34
+ expect(value.role).toBe("editor");
35
+ expect(value.can("orders.edit")).toBe(true);
36
+ expect(value.can("settings.manage")).toBe(false);
37
+ expect(value.canAny(["settings.manage", "orders.view"])).toBe(true);
38
+ expect(value.canAny(["settings.manage", "x.y"])).toBe(false);
39
+ });
40
+
41
+ it("denies everything without a role", () => {
42
+ const captured: ReturnType<typeof usePermissions>[] = [];
43
+ const Probe = () => {
44
+ captured.push(usePermissions());
45
+ return null;
46
+ };
47
+ mount(null, <Probe />);
48
+ const value = captured[0]!;
49
+ expect(value.can("orders.view")).toBe(false);
50
+ expect(value.canAny(["orders.view"])).toBe(false);
51
+ });
52
+ });
53
+
54
+ describe("RequirePermission", () => {
55
+ it("renders children when the permission is granted", () => {
56
+ mount("editor", (
57
+ <RequirePermission permission="orders.edit">
58
+ <span>Edit tools</span>
59
+ </RequirePermission>
60
+ ));
61
+ expect(screen.getByText("Edit tools")).not.toBeNull();
62
+ });
63
+
64
+ it("renders the fallback when denied", () => {
65
+ mount("viewer", (
66
+ <RequirePermission permission="orders.edit" fallback={<span>No access</span>}>
67
+ <span>Edit tools</span>
68
+ </RequirePermission>
69
+ ));
70
+ expect(screen.queryByText("Edit tools")).toBeNull();
71
+ expect(screen.getByText("No access")).not.toBeNull();
72
+ });
73
+
74
+ it("accepts anyOf for alternative permissions", () => {
75
+ mount("viewer", (
76
+ <RequirePermission anyOf={["orders.edit", "orders.view"]}>
77
+ <span>Visible</span>
78
+ </RequirePermission>
79
+ ));
80
+ expect(screen.getByText("Visible")).not.toBeNull();
81
+ });
82
+
83
+ it("denies when neither permission nor anyOf is given", () => {
84
+ mount("owner", (
85
+ <RequirePermission fallback={<span>Denied</span>}>
86
+ <span>Visible</span>
87
+ </RequirePermission>
88
+ ));
89
+ expect(screen.queryByText("Visible")).toBeNull();
90
+ expect(screen.getByText("Denied")).not.toBeNull();
91
+ });
92
+
93
+ it("redirects to redirectTo when denied", () => {
94
+ render(
95
+ <MemoryRouter initialEntries={["/dashboard"]}>
96
+ <Routes>
97
+ <Route
98
+ path="/dashboard"
99
+ element={
100
+ <PermissionsProvider matrix={MATRIX} role="viewer">
101
+ <RequirePermission permission="orders.edit" redirectTo="/signin">
102
+ <span>Dashboard</span>
103
+ </RequirePermission>
104
+ </PermissionsProvider>
105
+ }
106
+ />
107
+ <Route path="/signin" element={<span>Sign in</span>} />
108
+ </Routes>
109
+ </MemoryRouter>,
110
+ );
111
+ expect(screen.queryByText("Dashboard")).toBeNull();
112
+ expect(screen.getByText("Sign in")).not.toBeNull();
113
+ });
114
+ });
115
+
116
+ describe("Can", () => {
117
+ it("renders children while allowed and the fallback otherwise", () => {
118
+ const { rerender } = mount("editor", (
119
+ <Can permission="orders.view" fallback={<span>Hidden</span>}>
120
+ <span>Orders</span>
121
+ </Can>
122
+ ));
123
+ expect(screen.getByText("Orders")).not.toBeNull();
124
+
125
+ rerender(
126
+ <PermissionsProvider matrix={MATRIX} role="viewer">
127
+ <Can permission="orders.edit" fallback={<span>Hidden</span>}>
128
+ <span>Orders</span>
129
+ </Can>
130
+ </PermissionsProvider>,
131
+ );
132
+ expect(screen.queryByText("Orders")).toBeNull();
133
+ expect(screen.getByText("Hidden")).not.toBeNull();
134
+ });
135
+
136
+ it("supports anyOf", () => {
137
+ mount("viewer", (
138
+ <Can anyOf={["settings.manage", "orders.view"]}>
139
+ <span>Orders</span>
140
+ </Can>
141
+ ));
142
+ expect(screen.getByText("Orders")).not.toBeNull();
143
+ });
144
+ });
@@ -1,5 +1,6 @@
1
1
  import { createContext, useCallback, useContext, useMemo, type ReactNode } from "react";
2
- import { hasPermission } from "./hasPermission";
2
+ import { Navigate } from "react-router";
3
+ import { hasAnyPermission, hasPermission } from "./hasPermission";
3
4
  import type { Permission, PermissionMatrix, RoleKey } from "./types";
4
5
 
5
6
  export interface PermissionsProviderProps {
@@ -12,6 +13,7 @@ interface PermissionsContextValue {
12
13
  matrix: PermissionMatrix;
13
14
  role: RoleKey | null;
14
15
  can: (permission: Permission) => boolean;
16
+ canAny: (permissions: readonly Permission[]) => boolean;
15
17
  }
16
18
 
17
19
  const PermissionsContext = createContext<PermissionsContextValue | null>(null);
@@ -26,16 +28,21 @@ export const PermissionsProvider = ({
26
28
  [matrix, role],
27
29
  );
28
30
 
31
+ const canAny = useCallback(
32
+ (permissions: readonly Permission[]) =>
33
+ hasAnyPermission(matrix, role, permissions),
34
+ [matrix, role],
35
+ );
36
+
29
37
  const value = useMemo<PermissionsContextValue>(
30
- () => ({ matrix, role: role ?? null, can }),
31
- [matrix, role, can],
38
+ () => ({ matrix, role: role ?? null, can, canAny }),
39
+ [matrix, role, can, canAny],
32
40
  );
33
41
 
34
42
  return (
35
43
  <PermissionsContext.Provider value={value}>
36
44
  {children}
37
45
  </PermissionsContext.Provider>
38
-
39
46
  );
40
47
  };
41
48
 
@@ -47,29 +54,55 @@ export const usePermissions = (): PermissionsContextValue => {
47
54
  return context;
48
55
  };
49
56
 
57
+ const resolveAllowed = (
58
+ can: (permission: Permission) => boolean,
59
+ canAny: (permissions: readonly Permission[]) => boolean,
60
+ permission?: Permission,
61
+ anyOf?: readonly Permission[],
62
+ ): boolean =>
63
+ permission
64
+ ? anyOf
65
+ ? can(permission) || canAny(anyOf)
66
+ : can(permission)
67
+ : anyOf
68
+ ? canAny(anyOf)
69
+ : false;
70
+
50
71
  export interface RequirePermissionProps {
51
- permission: Permission;
52
- children: ReactNode;
72
+ permission?: Permission;
73
+ anyOf?: readonly Permission[];
74
+ children?: ReactNode;
53
75
  fallback?: ReactNode;
76
+ redirectTo?: string;
54
77
  }
55
78
 
56
79
  export const RequirePermission = ({
57
80
  permission,
81
+ anyOf,
58
82
  children,
59
83
  fallback = null,
84
+ redirectTo,
60
85
  }: RequirePermissionProps) => {
61
- const { can } = usePermissions();
62
- if (can(permission)) return <>{children}</>;
63
-
86
+ const { can, canAny } = usePermissions();
87
+ if (resolveAllowed(can, canAny, permission, anyOf)) return <>{children}</>;
88
+ if (redirectTo !== undefined) return <Navigate to={redirectTo} replace />;
64
89
  return <>{fallback}</>;
65
-
66
90
  };
67
91
 
68
- export interface CanProps extends RequirePermissionProps {}
69
-
70
- export const Can = ({ permission, children, fallback }: CanProps) => (
71
- <RequirePermission permission={permission} fallback={fallback}>
72
- {children}
73
- </RequirePermission>
92
+ export interface CanProps {
93
+ permission?: Permission;
94
+ anyOf?: readonly Permission[];
95
+ children?: ReactNode;
96
+ fallback?: ReactNode;
97
+ }
74
98
 
75
- );
99
+ export const Can = ({
100
+ permission,
101
+ anyOf,
102
+ children,
103
+ fallback = null,
104
+ }: CanProps) => {
105
+ const { can, canAny } = usePermissions();
106
+ const allowed = resolveAllowed(can, canAny, permission, anyOf);
107
+ return allowed ? <>{children}</> : <>{fallback}</>;
108
+ };
@@ -1,10 +1,15 @@
1
1
  import { describe, expect, it } from "vitest";
2
- import { hasPermission } from "./hasPermission";
2
+ import {
3
+ effectivePermissions,
4
+ hasAnyPermission,
5
+ hasPermission,
6
+ } from "./hasPermission";
3
7
  import type { PermissionMatrix } from "./types";
4
8
 
5
9
  const MATRIX: PermissionMatrix = {
6
10
  owner: "*",
7
11
  admin: ["dashboard.view", "sales.*", "reports.view"],
12
+ colonAdmin: ["orders:*"],
8
13
  viewer: ["dashboard.view"],
9
14
  nobody: [],
10
15
  };
@@ -30,6 +35,12 @@ describe("hasPermission", () => {
30
35
  expect(hasPermission(MATRIX, "admin", "salesforce.view")).toBe(false);
31
36
  });
32
37
 
38
+ it("honors colon-separated module wildcards", () => {
39
+ expect(hasPermission(MATRIX, "colonAdmin", "orders:create")).toBe(true);
40
+ expect(hasPermission(MATRIX, "colonAdmin", "orders:delete")).toBe(true);
41
+ expect(hasPermission(MATRIX, "colonAdmin", "ordersArchive:create")).toBe(false);
42
+ });
43
+
33
44
  it("denies unknown roles", () => {
34
45
  expect(hasPermission(MATRIX, "intruder", "dashboard.view")).toBe(false);
35
46
  });
@@ -47,4 +58,43 @@ describe("hasPermission", () => {
47
58
  expect(hasPermission(MATRIX, null, "dashboard.view")).toBe(false);
48
59
  expect(hasPermission(undefined, undefined, "x")).toBe(false);
49
60
  });
61
+
62
+ it("hasAnyPermission requires at least one grant", () => {
63
+ expect(
64
+ hasAnyPermission(MATRIX, "admin", ["sales.view", "settings.manage"]),
65
+ ).toBe(true);
66
+ expect(
67
+ hasAnyPermission(MATRIX, "viewer", ["sales.view", "settings.manage"]),
68
+ ).toBe(false);
69
+ expect(hasAnyPermission(MATRIX, "viewer", [])).toBe(false);
70
+ expect(hasAnyPermission(MATRIX, null, ["dashboard.view"])).toBe(false);
71
+ });
72
+ });
73
+
74
+ describe("effectivePermissions", () => {
75
+ it("returns the base set without overrides", () => {
76
+ const result = effectivePermissions(["a.b", "c.d"]);
77
+ expect([...result].sort()).toEqual(["a.b", "c.d"]);
78
+ });
79
+
80
+ it("unions grants and subtracts revokes", () => {
81
+ const result = effectivePermissions(["a.b", "c.d"], {
82
+ grant: ["e.f"],
83
+ revoke: ["a.b"],
84
+ });
85
+ expect([...result].sort()).toEqual(["c.d", "e.f"]);
86
+ });
87
+
88
+ it("lets revokes win over grants", () => {
89
+ const result = effectivePermissions(["a.b"], {
90
+ grant: ["c.d"],
91
+ revoke: ["c.d"],
92
+ });
93
+ expect([...result]).toEqual(["a.b"]);
94
+ });
95
+
96
+ it("deduplicates repeated permissions", () => {
97
+ const result = effectivePermissions(["a.b"], { grant: ["a.b"] });
98
+ expect(result.size).toBe(1);
99
+ });
50
100
  });
@@ -2,19 +2,27 @@ import {
2
2
  PERMISSION_WILDCARD,
3
3
  type Permission,
4
4
  type PermissionMatrix,
5
+ type PermissionOverrides,
5
6
  type RoleGrant,
6
7
  type RoleKey,
7
8
  } from "./types";
8
9
 
9
- const MODULE_WILDCARD_SUFFIX = ".*";
10
+ const MODULE_WILDCARD_SUFFIXES = [".*", ":*"] as const;
11
+
12
+ const matchesModuleWildcard = (
13
+ entry: Permission,
14
+ permission: Permission,
15
+ ): boolean =>
16
+ MODULE_WILDCARD_SUFFIXES.some((suffix) => {
17
+ if (!entry.endsWith(suffix)) return false;
18
+ return permission.startsWith(entry.slice(0, -suffix.length + 1));
19
+ });
10
20
 
11
21
  const grantIncludes = (grant: RoleGrant, permission: Permission): boolean => {
12
22
  if (grant === PERMISSION_WILDCARD) return true;
13
23
  return grant.some(
14
24
  (entry) =>
15
- entry === permission ||
16
- (entry.endsWith(MODULE_WILDCARD_SUFFIX) &&
17
- permission.startsWith(entry.slice(0, -1))),
25
+ entry === permission || matchesModuleWildcard(entry, permission),
18
26
  );
19
27
  };
20
28
 
@@ -28,3 +36,18 @@ export const hasPermission = (
28
36
  if (grant === undefined) return false;
29
37
  return grantIncludes(grant, permission);
30
38
  };
39
+
40
+ export const hasAnyPermission = (
41
+ matrix: PermissionMatrix | null | undefined,
42
+ role: RoleKey | null | undefined,
43
+ permissions: readonly Permission[],
44
+ ): boolean => permissions.some((p) => hasPermission(matrix, role, p));
45
+
46
+ export const effectivePermissions = (
47
+ base: readonly Permission[],
48
+ overrides?: PermissionOverrides,
49
+ ): Set<Permission> => {
50
+ const grants = overrides?.grant ?? [];
51
+ const revokes = overrides?.revoke ?? [];
52
+ return new Set([...base, ...grants].filter((p) => !revokes.includes(p)));
53
+ };
@@ -7,11 +7,18 @@ export {
7
7
  type RequirePermissionProps,
8
8
  type CanProps,
9
9
  } from "./PermissionsContext";
10
- export { hasPermission } from "./hasPermission";
10
+ export {
11
+ hasPermission,
12
+ hasAnyPermission,
13
+ effectivePermissions,
14
+ } from "./hasPermission";
11
15
  export {
12
16
  PERMISSION_WILDCARD,
13
17
  type Permission,
14
18
  type PermissionMatrix,
19
+ type PermissionOverrides,
20
+ type PermissionEntry,
21
+ type PermissionGroup,
15
22
  type RoleGrant,
16
23
  type RoleKey,
17
24
  type RoleInfo,
@@ -13,3 +13,20 @@ export interface RoleInfo {
13
13
  label: string;
14
14
  description?: string;
15
15
  }
16
+
17
+ export interface PermissionOverrides {
18
+ grant?: readonly Permission[];
19
+ revoke?: readonly Permission[];
20
+ }
21
+
22
+ export interface PermissionEntry {
23
+ key: Permission;
24
+ label?: string;
25
+ }
26
+
27
+ export interface PermissionGroup {
28
+ key?: string;
29
+ label?: string;
30
+ permissions: readonly (Permission | PermissionEntry)[];
31
+ }
32
+
@@ -0,0 +1,90 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { renderToString } from "react-dom/server";
3
+ import { PermissionMatrixScreen } from "./PermissionMatrixScreen";
4
+ import type { PermissionMatrix } from "../../permissions/types";
5
+
6
+ const MATRIX: PermissionMatrix = {
7
+ owner: "*",
8
+ editor: ["orders.view", "orders.edit"],
9
+ viewer: ["orders.view"],
10
+ };
11
+
12
+ const ROLES = [
13
+ { key: "owner", label: "Owner" },
14
+ { key: "editor", label: "Editor" },
15
+ { key: "viewer", label: "Viewer" },
16
+ ];
17
+
18
+ describe("PermissionMatrixScreen", () => {
19
+ it("renders grouped columns with labels and colSpan headers", () => {
20
+ const html = renderToString(
21
+ <PermissionMatrixScreen
22
+ matrix={MATRIX}
23
+ roles={ROLES}
24
+ groups={[
25
+ {
26
+ key: "orders",
27
+ label: "Orders",
28
+ permissions: [
29
+ { key: "orders.view", label: "View orders" },
30
+ { key: "orders.edit", label: "Edit orders" },
31
+ ],
32
+ },
33
+ {
34
+ key: "settings",
35
+ label: "Settings",
36
+ permissions: ["settings.manage"],
37
+ },
38
+ ]}
39
+ />,
40
+ );
41
+ expect(html).toContain('colSpan="2"');
42
+ expect(html).toContain("Orders");
43
+ expect(html).toContain("Settings");
44
+ expect(html).toContain("View orders");
45
+ expect(html).toContain("Edit orders");
46
+ expect(html).toContain("settings.manage");
47
+ expect(html).toContain("udb-matrix__check");
48
+ expect(html).toContain("udb-matrix__deny");
49
+ });
50
+
51
+ it("renders flat string columns when groups are omitted", () => {
52
+ const html = renderToString(
53
+ <PermissionMatrixScreen
54
+ matrix={MATRIX}
55
+ roles={ROLES}
56
+ permissions={["orders.view", "orders.edit"]}
57
+ />,
58
+ );
59
+ expect(html).not.toContain("udb-matrix__group");
60
+ expect(html).toContain("orders.view");
61
+ expect(html).toContain("orders.edit");
62
+ });
63
+
64
+ it("renders toggle buttons with aria-pressed in editable mode", () => {
65
+ const html = renderToString(
66
+ <PermissionMatrixScreen
67
+ matrix={MATRIX}
68
+ roles={ROLES}
69
+ permissions={["orders.view", "orders.edit"]}
70
+ onToggle={() => undefined}
71
+ />,
72
+ );
73
+ expect(html).toContain('aria-pressed="true"');
74
+ expect(html).toContain('aria-pressed="false"');
75
+ expect(html).toContain('aria-label="Editor — orders.view"');
76
+ expect(html).toContain("udb-matrix__cell--on");
77
+ });
78
+
79
+ it("stays read-only without onToggle", () => {
80
+ const html = renderToString(
81
+ <PermissionMatrixScreen
82
+ matrix={MATRIX}
83
+ roles={ROLES}
84
+ permissions={["orders.view"]}
85
+ />,
86
+ );
87
+ expect(html).not.toContain("<button");
88
+ expect(html).not.toContain("aria-pressed");
89
+ });
90
+ });
@@ -1,8 +1,18 @@
1
1
  import { Check, Minus } from "lucide-react";
2
+ import {
3
+ Table,
4
+ TableBody,
5
+ TableCell,
6
+ TableHead,
7
+ TableHeader,
8
+ TableRow,
9
+ } from "@umituz/web-design-system/organisms";
2
10
  import { cn } from "@umituz/web-design-system/utils/cn";
3
11
  import { hasPermission } from "../../permissions/hasPermission";
4
12
  import type {
5
13
  Permission,
14
+ PermissionEntry,
15
+ PermissionGroup,
6
16
  PermissionMatrix,
7
17
  RoleInfo,
8
18
  } from "../../permissions/types";
@@ -10,45 +20,72 @@ import type {
10
20
  export interface PermissionMatrixScreenProps {
11
21
  matrix: PermissionMatrix;
12
22
  roles: RoleInfo[];
13
- permissions: Permission[];
23
+ groups?: PermissionGroup[];
24
+ permissions?: (Permission | PermissionEntry)[];
14
25
  onToggle?: (role: string, permission: Permission, granted: boolean) => void;
15
26
  }
16
27
 
28
+ const toEntry = (
29
+ column: Permission | PermissionEntry,
30
+ ): { key: Permission; label: string } =>
31
+ typeof column === "string"
32
+ ? { key: column, label: column }
33
+ : { key: column.key, label: column.label ?? column.key };
34
+
17
35
  export const PermissionMatrixScreen = ({
18
36
  matrix,
19
37
  roles,
38
+ groups,
20
39
  permissions,
21
40
  onToggle,
22
- }: PermissionMatrixScreenProps) => (
23
- <div className="udb-table__wrap">
24
- <table className="udb-table">
25
- <thead>
26
- <tr>
27
- <th className="udb-table__th" />
28
- {permissions.map((permission) => (
29
- <th key={permission} className="udb-table__th udb-table__th--code">
30
- {permission}
31
- </th>
41
+ }: PermissionMatrixScreenProps) => {
42
+ const columns = groups
43
+ ? groups.flatMap((group) => group.permissions.map(toEntry))
44
+ : (permissions ?? []).map(toEntry);
45
+ const hasGroups = Boolean(groups && groups.length > 0);
32
46
 
47
+ return (
48
+ <Table className="udb-matrix">
49
+ <TableHeader>
50
+ {hasGroups && (
51
+ <TableRow className="udb-matrix__group-row">
52
+ <TableHead className="udb-matrix__corner" />
53
+ {groups!.map((group, index) => (
54
+ <TableHead
55
+ key={group.key ?? group.label ?? index}
56
+ colSpan={group.permissions.length}
57
+ scope="colgroup"
58
+ className="udb-matrix__group"
59
+ >
60
+ {group.label ?? ""}
61
+ </TableHead>
62
+ ))}
63
+ </TableRow>
64
+ )}
65
+ <TableRow>
66
+ <TableHead />
67
+ {columns.map((column) => (
68
+ <TableHead key={column.key} className="udb-matrix__col">
69
+ {column.label}
70
+ </TableHead>
33
71
  ))}
34
- </tr>
35
-
36
- </thead>
72
+ </TableRow>
73
+ </TableHeader>
37
74
 
38
- <tbody>
75
+ <TableBody>
39
76
  {roles.map((role) => (
40
- <tr key={role.key} className="udb-table__row">
41
- <td className="udb-table__td udb-table__td--strong">{role.label}</td>
77
+ <TableRow key={role.key}>
78
+ <TableCell className="udb-matrix__role">{role.label}</TableCell>
42
79
 
43
- {permissions.map((permission) => {
44
- const granted = hasPermission(matrix, role.key, permission);
80
+ {columns.map((column) => {
81
+ const granted = hasPermission(matrix, role.key, column.key);
45
82
  const cell = granted ? (
46
83
  <Check className="udb-matrix__check" aria-hidden="true" />
47
84
  ) : (
48
85
  <Minus className="udb-matrix__deny" aria-hidden="true" />
49
86
  );
50
87
  return (
51
- <td key={permission} className="udb-table__td udb-table__td--center">
88
+ <TableCell key={column.key} className="udb-matrix__cell-td">
52
89
  {onToggle ? (
53
90
  <button
54
91
  type="button"
@@ -57,27 +94,22 @@ export const PermissionMatrixScreen = ({
57
94
  granted && "udb-matrix__cell--on",
58
95
  )}
59
96
  aria-pressed={granted}
60
- onClick={() => onToggle(role.key, permission, granted)}
97
+ aria-label={`${role.label} — ${column.label}`}
98
+ onClick={() => onToggle(role.key, column.key, granted)}
61
99
  >
62
100
  {cell}
63
101
  </button>
64
-
65
102
  ) : (
66
103
  cell
67
104
  )}
68
- </td>
69
-
105
+ </TableCell>
70
106
  );
71
107
  })}
72
- </tr>
73
-
108
+ </TableRow>
74
109
  ))}
75
- </tbody>
76
-
77
- </table>
78
-
79
- </div>
80
-
81
- );
110
+ </TableBody>
111
+ </Table>
112
+ );
113
+ };
82
114
 
83
115
  export default PermissionMatrixScreen;
@@ -1275,50 +1275,33 @@
1275
1275
  font-size: var(--udb-font-size-sm);
1276
1276
  color: var(--color-muted-foreground);
1277
1277
  }
1278
- .udb-table__wrap {
1279
- width: 100%;
1280
- overflow-x: auto;
1281
- border: 1px solid var(--color-border);
1282
- border-radius: var(--radius-lg);
1283
- background: var(--color-card);
1284
- }
1285
- .udb-table {
1286
- width: 100%;
1287
- border-collapse: collapse;
1288
- font-size: var(--udb-font-size-md);
1289
- }
1290
- .udb-table__th {
1291
- padding: var(--udb-space-2) var(--udb-space-3);
1292
- border-bottom: 1px solid var(--udb-border-strong);
1293
- text-align: left;
1278
+ .udb-matrix__group-row th {
1294
1279
  font-size: var(--udb-font-size-xs);
1295
1280
  font-weight: var(--font-weight-bold);
1296
1281
  text-transform: uppercase;
1297
1282
  letter-spacing: 0.06em;
1298
1283
  color: var(--color-muted-foreground);
1299
- white-space: nowrap;
1284
+ text-align: center;
1285
+ border-bottom: none;
1286
+ padding-bottom: 0;
1300
1287
  }
1301
- .udb-table__th--code {
1288
+ .udb-matrix__corner {
1289
+ border-bottom: none;
1290
+ padding-bottom: 0;
1291
+ }
1292
+ .udb-matrix__col {
1302
1293
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
1294
+ font-size: var(--udb-font-size-xs);
1303
1295
  text-transform: none;
1304
1296
  letter-spacing: 0;
1297
+ text-align: center;
1298
+ white-space: nowrap;
1305
1299
  }
1306
- .udb-table__td {
1307
- padding: var(--udb-space-3);
1308
- border-bottom: 1px solid var(--color-border);
1309
- color: var(--color-foreground);
1310
- }
1311
- .udb-table__row:last-child .udb-table__td {
1312
- border-bottom: none;
1313
- }
1314
- .udb-table__row:hover .udb-table__td {
1315
- background: var(--color-muted);
1316
- }
1317
- .udb-table__td--strong {
1300
+ .udb-matrix__role {
1318
1301
  font-weight: var(--font-weight-medium);
1319
1302
  white-space: nowrap;
1320
1303
  }
1321
- .udb-table__td--center {
1304
+ .udb-matrix__cell-td {
1322
1305
  text-align: center;
1323
1306
  }
1324
1307
  .udb-matrix__cell {
@@ -2709,3 +2692,174 @@
2709
2692
  margin-top: var(--udb-space-3);
2710
2693
  font-size: var(--udb-font-size-sm);
2711
2694
  }
2695
+
2696
+ /* ------------------------------------------------------------- tab nav */
2697
+
2698
+ .udb-tabnav {
2699
+ display: flex;
2700
+ gap: var(--udb-space-1);
2701
+ border-bottom: 1px solid var(--color-border);
2702
+ overflow-x: auto;
2703
+ scrollbar-width: none;
2704
+ }
2705
+ .udb-tabnav::-webkit-scrollbar {
2706
+ display: none;
2707
+ }
2708
+ .udb-tabnav__tab {
2709
+ position: relative;
2710
+ padding: var(--udb-space-3) var(--udb-space-4);
2711
+ font-size: var(--udb-font-size-md);
2712
+ font-weight: var(--font-weight-medium);
2713
+ color: var(--color-muted-foreground);
2714
+ white-space: nowrap;
2715
+ transition: color 0.15s ease-out;
2716
+ }
2717
+ .udb-tabnav__tab:hover {
2718
+ color: var(--color-foreground);
2719
+ }
2720
+ .udb-tabnav__tab--active {
2721
+ color: var(--color-primary);
2722
+ }
2723
+ .udb-tabnav__tab--active::after {
2724
+ content: '';
2725
+ position: absolute;
2726
+ left: var(--udb-space-3);
2727
+ right: var(--udb-space-3);
2728
+ bottom: -1px;
2729
+ height: 2px;
2730
+ border-radius: var(--udb-radius-pill);
2731
+ background: var(--color-primary);
2732
+ }
2733
+
2734
+ /* --------------------------------------------------------- filter panel */
2735
+
2736
+ .udb-filter-panel {
2737
+ background: var(--color-card);
2738
+ border: 1px solid var(--color-border);
2739
+ border-radius: var(--radius-lg);
2740
+ padding: var(--udb-space-4);
2741
+ }
2742
+ .udb-filter-panel__header {
2743
+ display: flex;
2744
+ align-items: center;
2745
+ justify-content: space-between;
2746
+ margin-bottom: var(--udb-space-3);
2747
+ }
2748
+ .udb-filter-panel__title {
2749
+ font-size: var(--udb-font-size-sm);
2750
+ font-weight: var(--font-weight-bold);
2751
+ }
2752
+ .udb-filter-panel__clear {
2753
+ border: none;
2754
+ background: transparent;
2755
+ padding: 0;
2756
+ font-family: inherit;
2757
+ font-size: var(--udb-font-size-xs);
2758
+ font-weight: var(--font-weight-medium);
2759
+ color: var(--color-primary);
2760
+ cursor: pointer;
2761
+ }
2762
+ .udb-filter-panel__clear:hover:not(:disabled) {
2763
+ text-decoration: underline;
2764
+ }
2765
+ .udb-filter-panel__clear:disabled {
2766
+ opacity: 0.45;
2767
+ cursor: default;
2768
+ }
2769
+ .udb-filter-panel__sections {
2770
+ display: flex;
2771
+ flex-direction: column;
2772
+ gap: var(--udb-space-4);
2773
+ }
2774
+ .udb-filter-section__title {
2775
+ margin: 0 0 var(--udb-space-2);
2776
+ font-size: var(--udb-font-size-xs);
2777
+ font-weight: var(--font-weight-bold);
2778
+ text-transform: uppercase;
2779
+ letter-spacing: 0.08em;
2780
+ color: var(--color-muted-foreground);
2781
+ }
2782
+ .udb-filter-section__body {
2783
+ display: flex;
2784
+ flex-direction: column;
2785
+ gap: var(--udb-space-2);
2786
+ }
2787
+
2788
+ /* --------------------------------------------------------- check list */
2789
+
2790
+ .udb-check-filter {
2791
+ list-style: none;
2792
+ padding: 0;
2793
+ margin: 0;
2794
+ display: flex;
2795
+ flex-direction: column;
2796
+ gap: 2px;
2797
+ }
2798
+ .udb-check-filter__item {
2799
+ display: flex;
2800
+ align-items: center;
2801
+ gap: var(--udb-space-2);
2802
+ width: 100%;
2803
+ padding: 6px var(--udb-space-2);
2804
+ margin-left: calc(var(--udb-space-2) * -1);
2805
+ border: none;
2806
+ border-radius: var(--radius-sm);
2807
+ background: transparent;
2808
+ font-family: inherit;
2809
+ font-size: var(--udb-font-size-md);
2810
+ color: var(--color-muted-foreground);
2811
+ text-align: left;
2812
+ cursor: pointer;
2813
+ transition: background 0.15s ease-out, color 0.15s ease-out;
2814
+ }
2815
+ .udb-check-filter__item:hover {
2816
+ background: var(--color-muted);
2817
+ color: var(--color-foreground);
2818
+ }
2819
+ .udb-check-filter__item--checked {
2820
+ color: var(--color-foreground);
2821
+ font-weight: var(--font-weight-medium);
2822
+ }
2823
+ .udb-check-filter__box {
2824
+ display: inline-flex;
2825
+ align-items: center;
2826
+ justify-content: center;
2827
+ width: 16px;
2828
+ height: 16px;
2829
+ border: 1.5px solid var(--color-border);
2830
+ border-radius: 4px;
2831
+ background: var(--color-card);
2832
+ color: var(--color-primary-foreground, #fff);
2833
+ flex: none;
2834
+ transition: background 0.15s ease-out, border-color 0.15s ease-out;
2835
+ }
2836
+ .udb-check-filter__item--checked .udb-check-filter__box {
2837
+ background: var(--color-primary);
2838
+ border-color: var(--color-primary);
2839
+ }
2840
+ .udb-check-filter__box--radio {
2841
+ border-radius: 50%;
2842
+ }
2843
+ .udb-check-filter__radio-dot {
2844
+ width: 6px;
2845
+ height: 6px;
2846
+ border-radius: 50%;
2847
+ background: var(--color-primary-foreground, #fff);
2848
+ }
2849
+ .udb-check-filter__label {
2850
+ flex: 1;
2851
+ min-width: 0;
2852
+ overflow: hidden;
2853
+ text-overflow: ellipsis;
2854
+ white-space: nowrap;
2855
+ }
2856
+ .udb-check-filter__count {
2857
+ font-size: var(--udb-font-size-xs);
2858
+ color: var(--color-muted-foreground);
2859
+ font-variant-numeric: tabular-nums;
2860
+ }
2861
+ .udb-check-filter__more {
2862
+ padding: 2px var(--udb-space-2);
2863
+ font-size: var(--udb-font-size-xs);
2864
+ color: var(--color-muted-foreground);
2865
+ }