@umituz/web-dashboard 7.9.0 → 8.0.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/README.md +27 -2
- package/package.json +3 -1
- package/src/domains/layouts/components/DashboardLayout.test.tsx +22 -3
- package/src/domains/layouts/components/DashboardLayout.tsx +10 -20
- package/src/domains/notifications/NotificationsContext.test.tsx +90 -0
- package/src/domains/notifications/NotificationsContext.tsx +92 -0
- package/src/domains/notifications/components/NotificationCenter.test.tsx +81 -0
- package/src/domains/notifications/components/NotificationCenter.tsx +154 -0
- package/src/domains/notifications/components/NotificationItem.test.tsx +108 -0
- package/src/domains/notifications/components/NotificationItem.tsx +100 -0
- package/src/domains/notifications/components/NotificationList.tsx +75 -0
- package/src/domains/notifications/components/NotificationsPage.test.tsx +100 -0
- package/src/domains/notifications/components/NotificationsPage.tsx +126 -0
- package/src/domains/notifications/components/index.ts +8 -0
- package/src/domains/notifications/index.ts +16 -0
- package/src/domains/notifications/time.test.ts +42 -0
- package/src/domains/notifications/time.ts +55 -0
- package/src/domains/notifications/types.ts +42 -0
- package/src/styles/chrome.css +226 -0
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @umituz/web-dashboard
|
|
2
2
|
|
|
3
3
|
Installable app skeleton for SaaS dashboards: layouts, analytics, a content
|
|
4
|
-
calendar, a product onboarding flow, a permissions/module registry,
|
|
5
|
-
organization/membership module. Every
|
|
4
|
+
calendar, a product onboarding flow, a permissions/module registry, an
|
|
5
|
+
organization/membership module, and a notifications module. Every
|
|
6
6
|
domain is an independent entry point — import only what your app uses. UI
|
|
7
7
|
atoms and design tokens come from [@umituz/web-design-system](https://github.com/umituz/web-design-system);
|
|
8
8
|
translations from [@umituz/web-localization](https://github.com/umituz/web-localization).
|
|
@@ -46,6 +46,7 @@ hosts set dark values on the DS variables under `[data-theme="dark"]` /
|
|
|
46
46
|
@umituz/web-dashboard/styles.css # component stylesheet (udb-* BEM, incl. udb-onb-*)
|
|
47
47
|
@umituz/web-dashboard/permissions # PermissionMatrix, hasPermission, guards
|
|
48
48
|
@umituz/web-dashboard/organization # OrganizationProvider, members + invitations, seats, switcher
|
|
49
|
+
@umituz/web-dashboard/notifications # NotificationsProvider, bell + center, page skeleton
|
|
49
50
|
@umituz/web-dashboard/modules # DashboardModule, buildRoutes, buildNav
|
|
50
51
|
@umituz/web-dashboard/layouts # DashboardLayout, DashboardSidebar, DashboardHeader
|
|
51
52
|
@umituz/web-dashboard/settings # SettingsLayout, admin skeletons (users, matrix, audit)
|
|
@@ -148,6 +149,30 @@ const service = new CalendarService(new MyDatabase());
|
|
|
148
149
|
const demo = new CalendarService(createInMemoryCalendarDatabase());
|
|
149
150
|
```
|
|
150
151
|
|
|
152
|
+
### Notifications
|
|
153
|
+
|
|
154
|
+
Feed state is provider-owned; the host owns persistence. Records carry
|
|
155
|
+
`{ id, type, title?, body?, read?, at | createdAt, severity?, link? }` —
|
|
156
|
+
either timestamp column is accepted and `critical` severity normalizes to
|
|
157
|
+
`danger`. Reads and dismisses apply optimistically while the callbacks persist.
|
|
158
|
+
|
|
159
|
+
```tsx
|
|
160
|
+
import { NotificationsProvider } from "@umituz/web-dashboard/notifications";
|
|
161
|
+
import { NotificationCenter, NotificationsPage } from "@umituz/web-dashboard/notifications/components";
|
|
162
|
+
|
|
163
|
+
<NotificationsProvider
|
|
164
|
+
notifications={feed} // fresh array re-seeds the copy
|
|
165
|
+
loaded={!loading}
|
|
166
|
+
onMarkAllRead={() => api.post("/api/notifications/mark-read")}
|
|
167
|
+
onMarkRead={(id) => api.post(`/api/notifications/${id}/read`)}
|
|
168
|
+
>
|
|
169
|
+
<NotificationCenter viewAllTo="/notifications" /> {/* bell + badge, popover or drawer */}
|
|
170
|
+
</NotificationsProvider>
|
|
171
|
+
|
|
172
|
+
// Route body — segments derive from typeMeta counts; "Unread (n)" is built in:
|
|
173
|
+
<NotificationsPage typeMeta={TYPE_META} attentionLabel="Needs attention" />
|
|
174
|
+
```
|
|
175
|
+
|
|
151
176
|
### Analytics
|
|
152
177
|
|
|
153
178
|
`AnalyticsChart` consumes `ChartConfig` for line/area/bar/pie/donut. Tooltips
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-dashboard",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.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",
|
|
@@ -31,6 +31,8 @@
|
|
|
31
31
|
"./layouts/types": "./src/domains/layouts/types/index.ts",
|
|
32
32
|
"./layouts/utils": "./src/domains/layouts/utils/index.ts",
|
|
33
33
|
"./modules": "./src/domains/modules/index.ts",
|
|
34
|
+
"./notifications": "./src/domains/notifications/index.ts",
|
|
35
|
+
"./notifications/components": "./src/domains/notifications/components/index.ts",
|
|
34
36
|
"./onboarding": "./src/domains/onboarding/index.ts",
|
|
35
37
|
"./organization": "./src/domains/organization/index.ts",
|
|
36
38
|
"./organization/components": "./src/domains/organization/components/index.ts",
|
|
@@ -157,14 +157,13 @@ describe("DashboardSidebar (secondaryLabel)", () => {
|
|
|
157
157
|
});
|
|
158
158
|
|
|
159
159
|
it("hides the secondary label when the sidebar is collapsed", () => {
|
|
160
|
-
window.localStorage.setItem("udb-collapsed-test", "1");
|
|
161
160
|
render(
|
|
162
161
|
<MemoryRouter initialEntries={["/"]}>
|
|
163
162
|
<Routes>
|
|
164
163
|
<Route
|
|
165
164
|
path="*"
|
|
166
165
|
element={
|
|
167
|
-
<DashboardLayout config={secondaryConfig} labels={labels}
|
|
166
|
+
<DashboardLayout config={secondaryConfig} labels={labels} collapsed />
|
|
168
167
|
}
|
|
169
168
|
>
|
|
170
169
|
<Route path="*" element={<div>outlet-content</div>} />
|
|
@@ -174,7 +173,27 @@ describe("DashboardSidebar (secondaryLabel)", () => {
|
|
|
174
173
|
);
|
|
175
174
|
const link = screen.getByRole("link", { name: /home/i });
|
|
176
175
|
expect(link.textContent).not.toContain("오늘");
|
|
177
|
-
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("reports toggle events through onCollapsedChange", () => {
|
|
179
|
+
const onCollapsedChange = vi.fn();
|
|
180
|
+
render(
|
|
181
|
+
<MemoryRouter initialEntries={["/"]}>
|
|
182
|
+
<Routes>
|
|
183
|
+
<Route
|
|
184
|
+
path="*"
|
|
185
|
+
element={
|
|
186
|
+
<DashboardLayout config={secondaryConfig} labels={labels} onCollapsedChange={onCollapsedChange} />
|
|
187
|
+
}
|
|
188
|
+
>
|
|
189
|
+
<Route path="*" element={<div>outlet-content</div>} />
|
|
190
|
+
</Route>
|
|
191
|
+
</Routes>
|
|
192
|
+
</MemoryRouter>
|
|
193
|
+
);
|
|
194
|
+
const toggle = document.querySelector("button.udb-sidebar__footer-btn");
|
|
195
|
+
toggle?.click();
|
|
196
|
+
expect(onCollapsedChange).toHaveBeenCalledWith(true);
|
|
178
197
|
});
|
|
179
198
|
});
|
|
180
199
|
|
|
@@ -23,22 +23,14 @@ interface DashboardLayoutProps {
|
|
|
23
23
|
theme?: "light" | "dark";
|
|
24
24
|
onToggleTheme?: () => void;
|
|
25
25
|
routeTransitionMs?: number;
|
|
26
|
-
|
|
26
|
+
collapsed?: boolean;
|
|
27
|
+
onCollapsedChange?: (collapsed: boolean) => void;
|
|
27
28
|
footer?: ReactNode;
|
|
28
29
|
mainId?: string;
|
|
29
30
|
contentFallback?: ReactNode;
|
|
30
31
|
formatNotificationTime?: (createdAt: Date | string | number) => string;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
const readCollapsed = (key?: string): boolean => {
|
|
34
|
-
if (!key || typeof window === "undefined") return false;
|
|
35
|
-
try {
|
|
36
|
-
return window.localStorage.getItem(key) === "1";
|
|
37
|
-
} catch {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
34
|
export const DashboardLayout = ({
|
|
43
35
|
config,
|
|
44
36
|
labels,
|
|
@@ -53,7 +45,8 @@ export const DashboardLayout = ({
|
|
|
53
45
|
theme,
|
|
54
46
|
onToggleTheme,
|
|
55
47
|
routeTransitionMs = 0,
|
|
56
|
-
|
|
48
|
+
collapsed: collapsedProp,
|
|
49
|
+
onCollapsedChange,
|
|
57
50
|
footer,
|
|
58
51
|
mainId = "udb-main",
|
|
59
52
|
contentFallback,
|
|
@@ -61,7 +54,12 @@ export const DashboardLayout = ({
|
|
|
61
54
|
}: DashboardLayoutProps) => {
|
|
62
55
|
const location = useLocation();
|
|
63
56
|
const navigate = useNavigate();
|
|
64
|
-
const [
|
|
57
|
+
const [internalCollapsed, setInternalCollapsed] = useState(false);
|
|
58
|
+
const collapsed = collapsedProp ?? internalCollapsed;
|
|
59
|
+
const setCollapsed = (next: boolean) => {
|
|
60
|
+
setInternalCollapsed(next);
|
|
61
|
+
onCollapsedChange?.(next);
|
|
62
|
+
};
|
|
65
63
|
const [mobileOpen, setMobileOpen] = useState(false);
|
|
66
64
|
const [loading, setLoading] = useState(routeTransitionMs > 0);
|
|
67
65
|
const [paletteOpen, setPaletteOpen] = useState(false);
|
|
@@ -71,14 +69,6 @@ export const DashboardLayout = ({
|
|
|
71
69
|
const palette = config.commandPalette;
|
|
72
70
|
const paletteSearch = palette?.search;
|
|
73
71
|
|
|
74
|
-
useEffect(() => {
|
|
75
|
-
if (!collapsedStorageKey || typeof window === "undefined") return;
|
|
76
|
-
try {
|
|
77
|
-
window.localStorage.setItem(collapsedStorageKey, collapsed ? "1" : "0");
|
|
78
|
-
} catch {
|
|
79
|
-
}
|
|
80
|
-
}, [collapsed, collapsedStorageKey]);
|
|
81
|
-
|
|
82
72
|
useEffect(() => {
|
|
83
73
|
if (routeTransitionMs <= 0) {
|
|
84
74
|
setLoading(false);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { act, render } from "@testing-library/react";
|
|
3
|
+
import { NotificationsProvider, useNotifications } from "./NotificationsContext";
|
|
4
|
+
import type { NotificationRecord } from "./types";
|
|
5
|
+
|
|
6
|
+
const FEED: NotificationRecord[] = [
|
|
7
|
+
{ id: "n1", type: "order", title: "Order shipped", body: "Out for delivery", read: false, at: "2026-10-23T10:00:00Z" },
|
|
8
|
+
{ id: "n2", type: "system", title: "Welcome", read: true, createdAt: "2026-10-22T10:00:00Z" },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
interface ProbeValue {
|
|
12
|
+
notifications: NotificationRecord[];
|
|
13
|
+
unreadCount: number;
|
|
14
|
+
loaded: boolean;
|
|
15
|
+
canDismiss: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function Probe({ into }: { into: ProbeValue[] }) {
|
|
19
|
+
const { notifications, unreadCount, loaded, canDismiss } = useNotifications();
|
|
20
|
+
into.push({ notifications: [...notifications], unreadCount, loaded, canDismiss });
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("NotificationsProvider", () => {
|
|
25
|
+
it("counts unread and passes loaded through", () => {
|
|
26
|
+
const seen: ProbeValue[] = [];
|
|
27
|
+
render(
|
|
28
|
+
<NotificationsProvider notifications={FEED} loaded={false}>
|
|
29
|
+
<Probe into={seen} />
|
|
30
|
+
</NotificationsProvider>,
|
|
31
|
+
);
|
|
32
|
+
expect(seen.at(-1)?.unreadCount).toBe(1);
|
|
33
|
+
expect(seen.at(-1)?.loaded).toBe(false);
|
|
34
|
+
expect(seen.at(-1)?.canDismiss).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("marks all read optimistically and notifies the host", () => {
|
|
38
|
+
const onMarkAllRead = vi.fn();
|
|
39
|
+
const seen: ProbeValue[] = [];
|
|
40
|
+
let api: ReturnType<typeof useNotifications> | null = null;
|
|
41
|
+
function Caller() {
|
|
42
|
+
api = useNotifications();
|
|
43
|
+
return <Probe into={seen} />;
|
|
44
|
+
}
|
|
45
|
+
render(
|
|
46
|
+
<NotificationsProvider notifications={FEED} onMarkAllRead={onMarkAllRead}>
|
|
47
|
+
<Caller />
|
|
48
|
+
</NotificationsProvider>,
|
|
49
|
+
);
|
|
50
|
+
act(() => api!.markAllAsRead());
|
|
51
|
+
expect(seen.at(-1)?.unreadCount).toBe(0);
|
|
52
|
+
expect(onMarkAllRead).toHaveBeenCalledTimes(1);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("marks one read once and dismisses through the host callback", () => {
|
|
56
|
+
const onMarkRead = vi.fn();
|
|
57
|
+
const onDismiss = vi.fn();
|
|
58
|
+
let api: ReturnType<typeof useNotifications> | null = null;
|
|
59
|
+
function Caller() {
|
|
60
|
+
api = useNotifications();
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
render(
|
|
64
|
+
<NotificationsProvider notifications={FEED} onMarkRead={onMarkRead} onDismiss={onDismiss}>
|
|
65
|
+
<Caller />
|
|
66
|
+
</NotificationsProvider>,
|
|
67
|
+
);
|
|
68
|
+
act(() => api!.markRead("n1"));
|
|
69
|
+
act(() => api!.markRead("n1"));
|
|
70
|
+
expect(onMarkRead).toHaveBeenCalledTimes(2);
|
|
71
|
+
act(() => api!.dismiss("n2"));
|
|
72
|
+
expect(onDismiss).toHaveBeenCalledWith("n2");
|
|
73
|
+
expect(api!.notifications.map((n) => n.id)).toEqual(["n1"]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("re-seeds when the host passes a fresh array reference", () => {
|
|
77
|
+
const seen: ProbeValue[] = [];
|
|
78
|
+
const { rerender } = render(
|
|
79
|
+
<NotificationsProvider notifications={FEED}>
|
|
80
|
+
<Probe into={seen} />
|
|
81
|
+
</NotificationsProvider>,
|
|
82
|
+
);
|
|
83
|
+
rerender(
|
|
84
|
+
<NotificationsProvider notifications={[{ id: "n9", type: "x", title: "From host" }]}>
|
|
85
|
+
<Probe into={seen} />
|
|
86
|
+
</NotificationsProvider>,
|
|
87
|
+
);
|
|
88
|
+
expect(seen.at(-1)?.notifications.map((n) => n.id)).toEqual(["n9"]);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import type { NotificationRecord } from "./types";
|
|
4
|
+
|
|
5
|
+
export interface NotificationsContextValue {
|
|
6
|
+
notifications: NotificationRecord[];
|
|
7
|
+
unreadCount: number;
|
|
8
|
+
loaded: boolean;
|
|
9
|
+
markAllAsRead: () => void;
|
|
10
|
+
markRead: (id: string) => void;
|
|
11
|
+
dismiss: (id: string) => void;
|
|
12
|
+
canDismiss: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface NotificationsProviderProps {
|
|
16
|
+
/** The host's current feed — a new array reference re-seeds the local copy. */
|
|
17
|
+
notifications: readonly NotificationRecord[];
|
|
18
|
+
/** False while the host is still fetching; pages can render their own loading row. */
|
|
19
|
+
loaded?: boolean;
|
|
20
|
+
onMarkAllRead?: () => void;
|
|
21
|
+
onMarkRead?: (id: string) => void;
|
|
22
|
+
onDismiss?: (id: string) => void;
|
|
23
|
+
children: ReactNode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const NotificationsContext = createContext<NotificationsContextValue | null>(null);
|
|
27
|
+
|
|
28
|
+
/** Shared feed state so the header bell and the page stay in sync. */
|
|
29
|
+
export function NotificationsProvider({
|
|
30
|
+
notifications,
|
|
31
|
+
loaded = true,
|
|
32
|
+
onMarkAllRead,
|
|
33
|
+
onMarkRead,
|
|
34
|
+
onDismiss,
|
|
35
|
+
children,
|
|
36
|
+
}: NotificationsProviderProps) {
|
|
37
|
+
// Optimistic local copy: reads/dismisses land instantly while the host
|
|
38
|
+
// persists through the callbacks; a fresh array from the host re-seeds it.
|
|
39
|
+
const [items, setItems] = useState<NotificationRecord[]>(() => [...notifications]);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
setItems([...notifications]);
|
|
43
|
+
}, [notifications]);
|
|
44
|
+
|
|
45
|
+
const markAllAsRead = useCallback(() => {
|
|
46
|
+
setItems((prev) => prev.map((n) => (n.read ? n : { ...n, read: true })));
|
|
47
|
+
onMarkAllRead?.();
|
|
48
|
+
}, [onMarkAllRead]);
|
|
49
|
+
|
|
50
|
+
const markRead = useCallback(
|
|
51
|
+
(id: string) => {
|
|
52
|
+
setItems((prev) => {
|
|
53
|
+
const target = prev.find((n) => n.id === id);
|
|
54
|
+
if (!target || target.read) return prev;
|
|
55
|
+
return prev.map((n) => (n.id === id ? { ...n, read: true } : n));
|
|
56
|
+
});
|
|
57
|
+
onMarkRead?.(id);
|
|
58
|
+
},
|
|
59
|
+
[onMarkRead],
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const dismiss = useCallback(
|
|
63
|
+
(id: string) => {
|
|
64
|
+
setItems((prev) => prev.filter((n) => n.id !== id));
|
|
65
|
+
onDismiss?.(id);
|
|
66
|
+
},
|
|
67
|
+
[onDismiss],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const unreadCount = useMemo(() => items.filter((n) => !n.read).length, [items]);
|
|
71
|
+
|
|
72
|
+
const value = useMemo<NotificationsContextValue>(
|
|
73
|
+
() => ({
|
|
74
|
+
notifications: items,
|
|
75
|
+
unreadCount,
|
|
76
|
+
loaded,
|
|
77
|
+
markAllAsRead,
|
|
78
|
+
markRead,
|
|
79
|
+
dismiss,
|
|
80
|
+
canDismiss: Boolean(onDismiss),
|
|
81
|
+
}),
|
|
82
|
+
[items, unreadCount, loaded, markAllAsRead, markRead, dismiss, onDismiss],
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
return <NotificationsContext.Provider value={value}>{children}</NotificationsContext.Provider>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function useNotifications(): NotificationsContextValue {
|
|
89
|
+
const ctx = useContext(NotificationsContext);
|
|
90
|
+
if (!ctx) throw new Error("useNotifications must be used within NotificationsProvider");
|
|
91
|
+
return ctx;
|
|
92
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { MemoryRouter } from "react-router";
|
|
4
|
+
import { NotificationCenter } from "./NotificationCenter";
|
|
5
|
+
import { NotificationsProvider } from "../NotificationsContext";
|
|
6
|
+
import type { NotificationRecord } from "../types";
|
|
7
|
+
|
|
8
|
+
const FEED: NotificationRecord[] = Array.from({ length: 10 }, (_, i) => ({
|
|
9
|
+
id: `n${i}`,
|
|
10
|
+
type: "order",
|
|
11
|
+
title: `Alert ${i}`,
|
|
12
|
+
read: i < 8,
|
|
13
|
+
at: "2026-10-23T10:00:00Z",
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
const mount = (feed: readonly NotificationRecord[] = FEED, props: Record<string, unknown> = {}) =>
|
|
17
|
+
render(
|
|
18
|
+
<MemoryRouter>
|
|
19
|
+
<NotificationsProvider notifications={feed}>
|
|
20
|
+
<NotificationCenter timeFormatter={() => "T"} {...props} />
|
|
21
|
+
</NotificationsProvider>
|
|
22
|
+
</MemoryRouter>,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
describe("NotificationCenter", () => {
|
|
26
|
+
it("shows the unread badge count on the bell", () => {
|
|
27
|
+
mount();
|
|
28
|
+
expect(screen.getByRole("button", { name: "Notifications, 2 unread" })).not.toBeNull();
|
|
29
|
+
expect(screen.getByText("2")).not.toBeNull();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("opens the popover with the truncated list and the more hint", () => {
|
|
33
|
+
mount();
|
|
34
|
+
fireEvent.click(screen.getByRole("button", { name: /Notifications/ }));
|
|
35
|
+
expect(screen.getByRole("dialog", { name: "Notifications" })).not.toBeNull();
|
|
36
|
+
expect(screen.getByText("Alert 0")).not.toBeNull();
|
|
37
|
+
expect(screen.getByText("+2 more")).not.toBeNull();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("closes on Escape", () => {
|
|
41
|
+
mount();
|
|
42
|
+
fireEvent.click(screen.getByRole("button", { name: /Notifications/ }));
|
|
43
|
+
fireEvent.keyDown(document, { key: "Escape" });
|
|
44
|
+
expect(screen.queryByRole("dialog")).toBeNull();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("marks all read from the panel header", () => {
|
|
48
|
+
const onMarkAllRead = vi.fn();
|
|
49
|
+
render(
|
|
50
|
+
<MemoryRouter>
|
|
51
|
+
<NotificationsProvider notifications={FEED} onMarkAllRead={onMarkAllRead}>
|
|
52
|
+
<NotificationCenter timeFormatter={() => "T"} />
|
|
53
|
+
</NotificationsProvider>
|
|
54
|
+
</MemoryRouter>,
|
|
55
|
+
);
|
|
56
|
+
fireEvent.click(screen.getByRole("button", { name: /Notifications/ }));
|
|
57
|
+
fireEvent.click(screen.getByText("Mark all read"));
|
|
58
|
+
expect(onMarkAllRead).toHaveBeenCalledTimes(1);
|
|
59
|
+
expect(screen.queryByText("Mark all read")).toBeNull();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("renders the caught-up empty state instead of rows", () => {
|
|
63
|
+
mount([], { itemLimit: 8 });
|
|
64
|
+
fireEvent.click(screen.getByRole("button", { name: "Notifications" }));
|
|
65
|
+
expect(screen.getByText("You're all caught up")).not.toBeNull();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("links to the notifications page from the footer", () => {
|
|
69
|
+
mount(FEED, { viewAllTo: "/notifications" });
|
|
70
|
+
fireEvent.click(screen.getByRole("button", { name: /Notifications/ }));
|
|
71
|
+
expect(screen.getByRole("link", { name: "View all" }).getAttribute("href")).toBe(
|
|
72
|
+
"/notifications",
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("drawer variant opens the design-system drawer", () => {
|
|
77
|
+
mount(FEED, { variant: "drawer" });
|
|
78
|
+
fireEvent.click(screen.getByRole("button", { name: /Notifications/ }));
|
|
79
|
+
expect(screen.getByText("Alert 0")).not.toBeNull();
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
import { Link } from "react-router";
|
|
3
|
+
import { Bell, CheckCheck } from "lucide-react";
|
|
4
|
+
import { Button } from "@umituz/web-design-system/atoms";
|
|
5
|
+
import { Drawer } from "@umituz/web-design-system/organisms";
|
|
6
|
+
import { useNotifications } from "../NotificationsContext";
|
|
7
|
+
import type {
|
|
8
|
+
NotificationListLabels,
|
|
9
|
+
NotificationTypeMeta,
|
|
10
|
+
} from "../types";
|
|
11
|
+
import { NotificationList } from "./NotificationList";
|
|
12
|
+
|
|
13
|
+
export interface NotificationCenterLabels extends NotificationListLabels {
|
|
14
|
+
title: string;
|
|
15
|
+
markAllLabel: string;
|
|
16
|
+
viewAllLabel: string;
|
|
17
|
+
/** "{n} more" hint under a truncated popover list. */
|
|
18
|
+
morePattern: string;
|
|
19
|
+
/** "{n} unread" — the bell's accessible name when the badge has a count. */
|
|
20
|
+
bellPattern: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const DEFAULT_LABELS: NotificationCenterLabels = {
|
|
24
|
+
title: "Notifications",
|
|
25
|
+
markAllLabel: "Mark all read",
|
|
26
|
+
viewAllLabel: "View all",
|
|
27
|
+
morePattern: "+{n} more",
|
|
28
|
+
bellPattern: "Notifications, {n} unread",
|
|
29
|
+
markReadLabel: "Mark read",
|
|
30
|
+
dismissLabel: "Dismiss",
|
|
31
|
+
viewLabel: "View",
|
|
32
|
+
unreadLabel: "Unread",
|
|
33
|
+
emptyTitle: "You're all caught up",
|
|
34
|
+
emptyDescription: "New alerts will land here.",
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export interface NotificationCenterProps {
|
|
38
|
+
/** `popover` anchors under the bell; `drawer` opens the design-system Drawer. */
|
|
39
|
+
variant?: "popover" | "drawer";
|
|
40
|
+
/** Rows shown before the popover truncates into the "more" hint. */
|
|
41
|
+
itemLimit?: number;
|
|
42
|
+
/** Page path for the "View all" link; omit to hide the footer link. */
|
|
43
|
+
viewAllTo?: string;
|
|
44
|
+
typeMeta?: NotificationTypeMeta;
|
|
45
|
+
timeFormatter?: (iso: string) => string;
|
|
46
|
+
labels?: Partial<NotificationCenterLabels>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Header bell + unread badge + feed surface; reads the NotificationsProvider. */
|
|
50
|
+
export const NotificationCenter = ({
|
|
51
|
+
variant = "popover",
|
|
52
|
+
itemLimit = 8,
|
|
53
|
+
viewAllTo,
|
|
54
|
+
typeMeta,
|
|
55
|
+
timeFormatter,
|
|
56
|
+
labels,
|
|
57
|
+
}: NotificationCenterProps) => {
|
|
58
|
+
const text = { ...DEFAULT_LABELS, ...labels };
|
|
59
|
+
const { notifications, unreadCount, markAllAsRead, markRead, dismiss, canDismiss } =
|
|
60
|
+
useNotifications();
|
|
61
|
+
const [open, setOpen] = useState(false);
|
|
62
|
+
const wrapRef = useRef<HTMLDivElement>(null);
|
|
63
|
+
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
if (!open) return;
|
|
66
|
+
const onPointerDown = (event: MouseEvent) => {
|
|
67
|
+
if (!wrapRef.current?.contains(event.target as Node)) setOpen(false);
|
|
68
|
+
};
|
|
69
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
70
|
+
if (event.key === "Escape") setOpen(false);
|
|
71
|
+
};
|
|
72
|
+
document.addEventListener("mousedown", onPointerDown);
|
|
73
|
+
document.addEventListener("keydown", onKeyDown);
|
|
74
|
+
return () => {
|
|
75
|
+
document.removeEventListener("mousedown", onPointerDown);
|
|
76
|
+
document.removeEventListener("keydown", onKeyDown);
|
|
77
|
+
};
|
|
78
|
+
}, [open]);
|
|
79
|
+
|
|
80
|
+
const shown = itemLimit > 0 ? notifications.slice(0, itemLimit) : notifications;
|
|
81
|
+
const hidden = notifications.length - shown.length;
|
|
82
|
+
const list = (
|
|
83
|
+
<>
|
|
84
|
+
<NotificationList
|
|
85
|
+
notifications={shown}
|
|
86
|
+
typeMeta={typeMeta}
|
|
87
|
+
onMarkRead={markRead}
|
|
88
|
+
onDismiss={canDismiss ? dismiss : undefined}
|
|
89
|
+
timeFormatter={timeFormatter}
|
|
90
|
+
labels={text}
|
|
91
|
+
/>
|
|
92
|
+
{hidden > 0 && (
|
|
93
|
+
<p className="udb-notif-center__more">{text.morePattern.replace("{n}", String(hidden))}</p>
|
|
94
|
+
)}
|
|
95
|
+
</>
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const markAllButton = unreadCount > 0 && (
|
|
99
|
+
<Button variant="ghost" size="sm" icon={<CheckCheck size={14} />} onClick={markAllAsRead}>
|
|
100
|
+
{text.markAllLabel}
|
|
101
|
+
</Button>
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
const bellLabel = unreadCount > 0 ? text.bellPattern.replace("{n}", String(unreadCount)) : text.title;
|
|
105
|
+
|
|
106
|
+
const bell = (
|
|
107
|
+
<button
|
|
108
|
+
type="button"
|
|
109
|
+
className="udb-notif-bell"
|
|
110
|
+
aria-label={bellLabel}
|
|
111
|
+
aria-expanded={open}
|
|
112
|
+
aria-haspopup="dialog"
|
|
113
|
+
onClick={() => setOpen((value) => !value)}
|
|
114
|
+
>
|
|
115
|
+
<Bell size={17} aria-hidden="true" />
|
|
116
|
+
{unreadCount > 0 && (
|
|
117
|
+
<span className="udb-notif-bell__badge">{unreadCount > 99 ? "99+" : unreadCount}</span>
|
|
118
|
+
)}
|
|
119
|
+
</button>
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
if (variant === "drawer") {
|
|
123
|
+
return (
|
|
124
|
+
<div ref={wrapRef}>
|
|
125
|
+
{bell}
|
|
126
|
+
<Drawer open={open} onClose={() => setOpen(false)} title={text.title} footer={markAllButton}>
|
|
127
|
+
{list}
|
|
128
|
+
</Drawer>
|
|
129
|
+
</div>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<div className="udb-notif-center" ref={wrapRef}>
|
|
135
|
+
{bell}
|
|
136
|
+
{open && (
|
|
137
|
+
<div className="udb-notif-center__panel" role="dialog" aria-label={text.title}>
|
|
138
|
+
<header className="udb-notif-center__head">
|
|
139
|
+
<h3 className="udb-notif-center__title">{text.title}</h3>
|
|
140
|
+
{markAllButton}
|
|
141
|
+
</header>
|
|
142
|
+
<div className="udb-notif-center__list">{list}</div>
|
|
143
|
+
{viewAllTo && (
|
|
144
|
+
<footer className="udb-notif-center__foot">
|
|
145
|
+
<Link to={viewAllTo} onClick={() => setOpen(false)}>
|
|
146
|
+
{text.viewAllLabel}
|
|
147
|
+
</Link>
|
|
148
|
+
</footer>
|
|
149
|
+
)}
|
|
150
|
+
</div>
|
|
151
|
+
)}
|
|
152
|
+
</div>
|
|
153
|
+
);
|
|
154
|
+
};
|