@classic-homes/notifications 1.0.0 → 1.22.2
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 +1 -1
- package/dist/core/index.d.ts +17 -4
- package/dist/core/index.js +37 -14
- package/dist/index.d.ts +2 -2
- package/dist/index.js +61 -17
- package/dist/svelte/index.d.ts +9 -3
- package/dist/svelte/index.js +53 -9
- package/dist/{types-VWF2niWE.d.ts → types-C3dITqDG.d.ts} +18 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -296,7 +296,7 @@ import {
|
|
|
296
296
|
|
|
297
297
|
// Get icon for notification type
|
|
298
298
|
getNotificationIconName('security'); // 'shield'
|
|
299
|
-
getNotificationIconColor('security'); // 'text-
|
|
299
|
+
getNotificationIconColor('security'); // 'text-destructive'
|
|
300
300
|
|
|
301
301
|
// Get badge variants
|
|
302
302
|
getPriorityBadgeVariant('urgent'); // 'destructive'
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { C as ChannelType,
|
|
1
|
+
import { l as NotificationsListResponse, c as ChapiNotificationsListResponse, g as NotificationListParams, N as Notification, M as MarkReadResponse, b as ChannelsResponse, k as NotificationType, P as PreferencesResponse, U as UpdatePreferenceData, n as UpdatePreferenceResponse, B as BatchPreferenceUpdate, a as BulkPreferenceResponse, D as DeletePreferenceResponse, T as TestNotificationParams, m as TestNotificationResponse, d as NotificationChannel, h as NotificationPreference, j as NotificationPriority } from '../types-C3dITqDG.js';
|
|
2
|
+
export { C as ChannelType, e as NotificationError, f as NotificationFrequency, i as NotificationPreferenceResult } from '../types-C3dITqDG.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Notifications API
|
|
@@ -8,6 +8,18 @@ export { C as ChannelType, d as NotificationError, e as NotificationFrequency, h
|
|
|
8
8
|
* Uses the auth package's HTTP client for making requests.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
type RawNotificationsListResponse = NotificationsListResponse | ChapiNotificationsListResponse;
|
|
12
|
+
/**
|
|
13
|
+
* Normalize a list response into NotificationsListResponse.
|
|
14
|
+
*
|
|
15
|
+
* CHAPI returns `{ data, pagination: { page, pageSize, total, pages },
|
|
16
|
+
* meta: { unreadCount } }`; older backends return the package shape
|
|
17
|
+
* directly. Both are accepted so the store works against either.
|
|
18
|
+
*/
|
|
19
|
+
declare function normalizeListResponse(raw: RawNotificationsListResponse, requested: {
|
|
20
|
+
page: number;
|
|
21
|
+
limit: number;
|
|
22
|
+
}): NotificationsListResponse;
|
|
11
23
|
declare const notificationsApi: {
|
|
12
24
|
/**
|
|
13
25
|
* List notifications with optional filters.
|
|
@@ -180,7 +192,8 @@ type BadgeVariant = 'default' | 'secondary' | 'destructive' | 'outline' | 'succe
|
|
|
180
192
|
declare function getNotificationIconName(type: NotificationType): string;
|
|
181
193
|
/**
|
|
182
194
|
* Get the icon color class for a notification type.
|
|
183
|
-
* Returns Tailwind
|
|
195
|
+
* Returns semantic Tailwind color classes from the shared preset, so icons
|
|
196
|
+
* follow the theme (including dark mode) instead of fixed palette shades.
|
|
184
197
|
*/
|
|
185
198
|
declare function getNotificationIconColor(type: NotificationType): string;
|
|
186
199
|
/**
|
|
@@ -229,4 +242,4 @@ interface NotificationCounts {
|
|
|
229
242
|
read: number;
|
|
230
243
|
}
|
|
231
244
|
|
|
232
|
-
export { type BadgeVariant, BatchPreferenceUpdate, BulkPreferenceResponse, ChannelsResponse, DeletePreferenceResponse, Notification, NotificationChannel, type NotificationCounts, type NotificationFilter, NotificationListParams, NotificationPreference, NotificationPriority, NotificationService, NotificationType, NotificationsListResponse, PreferencesResponse, TestNotificationParams, TestNotificationResponse, UpdatePreferenceData, UpdatePreferenceResponse, formatFullTimestamp, formatNotificationTime, formatRelativeTime, getNotificationIconColor, getNotificationIconName, getPriorityBadgeVariant, getPriorityLabel, getPrioritySortValue, getTypeBadgeVariant, getTypeLabel, notificationService, notificationsApi };
|
|
245
|
+
export { type BadgeVariant, BatchPreferenceUpdate, BulkPreferenceResponse, ChannelsResponse, ChapiNotificationsListResponse, DeletePreferenceResponse, Notification, NotificationChannel, type NotificationCounts, type NotificationFilter, NotificationListParams, NotificationPreference, NotificationPriority, NotificationService, NotificationType, NotificationsListResponse, PreferencesResponse, TestNotificationParams, TestNotificationResponse, UpdatePreferenceData, UpdatePreferenceResponse, formatFullTimestamp, formatNotificationTime, formatRelativeTime, getNotificationIconColor, getNotificationIconName, getPriorityBadgeVariant, getPriorityLabel, getPrioritySortValue, getTypeBadgeVariant, getTypeLabel, normalizeListResponse, notificationService, notificationsApi };
|
package/dist/core/index.js
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import { api } from '@classic-homes/auth/core';
|
|
2
2
|
|
|
3
3
|
// src/core/api.ts
|
|
4
|
+
function normalizeListResponse(raw, requested) {
|
|
5
|
+
if ("notifications" in raw && Array.isArray(raw.notifications)) {
|
|
6
|
+
return raw;
|
|
7
|
+
}
|
|
8
|
+
const chapi = raw;
|
|
9
|
+
const notifications = Array.isArray(chapi.data) ? chapi.data : [];
|
|
10
|
+
const pagination = chapi.pagination;
|
|
11
|
+
const limit = pagination?.pageSize ?? requested.limit;
|
|
12
|
+
const total = pagination?.total ?? notifications.length;
|
|
13
|
+
return {
|
|
14
|
+
notifications,
|
|
15
|
+
unreadCount: chapi.meta?.unreadCount ?? notifications.filter((n) => !n.read).length,
|
|
16
|
+
pagination: {
|
|
17
|
+
page: pagination?.page ?? requested.page,
|
|
18
|
+
limit,
|
|
19
|
+
total,
|
|
20
|
+
totalPages: pagination?.pages ?? Math.max(1, Math.ceil(total / limit))
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
4
24
|
var notificationsApi = {
|
|
5
25
|
// ============================================================================
|
|
6
26
|
// Notifications CRUD
|
|
@@ -13,6 +33,8 @@ var notificationsApi = {
|
|
|
13
33
|
const page = params?.page || 1;
|
|
14
34
|
const limit = params?.limit || 20;
|
|
15
35
|
const offset = (page - 1) * limit;
|
|
36
|
+
queryParams.append("page", page.toString());
|
|
37
|
+
queryParams.append("pageSize", limit.toString());
|
|
16
38
|
queryParams.append("limit", limit.toString());
|
|
17
39
|
queryParams.append("offset", offset.toString());
|
|
18
40
|
if (params?.unreadOnly) {
|
|
@@ -28,24 +50,25 @@ var notificationsApi = {
|
|
|
28
50
|
true,
|
|
29
51
|
customFetch
|
|
30
52
|
);
|
|
31
|
-
return response;
|
|
53
|
+
return normalizeListResponse(response, { page, limit });
|
|
32
54
|
},
|
|
33
55
|
/**
|
|
34
56
|
* Get a single notification by ID.
|
|
35
57
|
*/
|
|
36
58
|
async get(id, customFetch) {
|
|
37
59
|
const response = await api.get(
|
|
38
|
-
`/notifications/${id}`,
|
|
60
|
+
`/notifications/${encodeURIComponent(id)}`,
|
|
39
61
|
true,
|
|
40
62
|
customFetch
|
|
41
63
|
);
|
|
42
|
-
|
|
64
|
+
const body = response;
|
|
65
|
+
return body.notification ?? body.data;
|
|
43
66
|
},
|
|
44
67
|
/**
|
|
45
68
|
* Mark a notification as read.
|
|
46
69
|
*/
|
|
47
70
|
async markRead(id) {
|
|
48
|
-
await api.patch(`/notifications/${id}/read`, {}, true);
|
|
71
|
+
await api.patch(`/notifications/${encodeURIComponent(id)}/read`, {}, true);
|
|
49
72
|
},
|
|
50
73
|
/**
|
|
51
74
|
* Mark multiple notifications as read.
|
|
@@ -73,7 +96,7 @@ var notificationsApi = {
|
|
|
73
96
|
* Dismiss (delete) a notification.
|
|
74
97
|
*/
|
|
75
98
|
async dismiss(id) {
|
|
76
|
-
await api.delete(`/notifications/${id}`, true);
|
|
99
|
+
await api.delete(`/notifications/${encodeURIComponent(id)}`, true);
|
|
77
100
|
},
|
|
78
101
|
/**
|
|
79
102
|
* Delete multiple notifications in bulk.
|
|
@@ -142,7 +165,7 @@ var notificationsApi = {
|
|
|
142
165
|
*/
|
|
143
166
|
async deletePreference(subscriptionId) {
|
|
144
167
|
const response = await api.delete(
|
|
145
|
-
`/notifications/preferences/${subscriptionId}`,
|
|
168
|
+
`/notifications/preferences/${encodeURIComponent(subscriptionId)}`,
|
|
146
169
|
true
|
|
147
170
|
);
|
|
148
171
|
return response;
|
|
@@ -279,14 +302,14 @@ function getNotificationIconName(type) {
|
|
|
279
302
|
}
|
|
280
303
|
function getNotificationIconColor(type) {
|
|
281
304
|
const colorMap = {
|
|
282
|
-
security: "text-
|
|
283
|
-
system: "text-
|
|
284
|
-
feature: "text-
|
|
285
|
-
update: "text-
|
|
286
|
-
alert: "text-
|
|
287
|
-
info: "text-
|
|
305
|
+
security: "text-destructive",
|
|
306
|
+
system: "text-info",
|
|
307
|
+
feature: "text-primary",
|
|
308
|
+
update: "text-success",
|
|
309
|
+
alert: "text-warning",
|
|
310
|
+
info: "text-muted-foreground"
|
|
288
311
|
};
|
|
289
|
-
return colorMap[type] ?? "text-
|
|
312
|
+
return colorMap[type] ?? "text-muted-foreground";
|
|
290
313
|
}
|
|
291
314
|
function getPriorityBadgeVariant(priority) {
|
|
292
315
|
const variantMap = {
|
|
@@ -392,4 +415,4 @@ function getPrioritySortValue(priority) {
|
|
|
392
415
|
return sortMap[priority] ?? 2;
|
|
393
416
|
}
|
|
394
417
|
|
|
395
|
-
export { NotificationService, formatFullTimestamp, formatNotificationTime, formatRelativeTime, getNotificationIconColor, getNotificationIconName, getPriorityBadgeVariant, getPriorityLabel, getPrioritySortValue, getTypeBadgeVariant, getTypeLabel, notificationService, notificationsApi };
|
|
418
|
+
export { NotificationService, formatFullTimestamp, formatNotificationTime, formatRelativeTime, getNotificationIconColor, getNotificationIconName, getPriorityBadgeVariant, getPriorityLabel, getPrioritySortValue, getTypeBadgeVariant, getTypeLabel, normalizeListResponse, notificationService, notificationsApi };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { BadgeVariant, NotificationCounts, NotificationFilter, NotificationService, formatFullTimestamp, formatNotificationTime, formatRelativeTime, getNotificationIconColor, getNotificationIconName, getPriorityBadgeVariant, getPriorityLabel, getPrioritySortValue, getTypeBadgeVariant, getTypeLabel, notificationService, notificationsApi } from './core/index.js';
|
|
2
|
-
export { B as BatchPreferenceUpdate, a as BulkPreferenceResponse, C as ChannelType, b as ChannelsResponse, D as DeletePreferenceResponse, N as Notification,
|
|
1
|
+
export { BadgeVariant, NotificationCounts, NotificationFilter, NotificationService, formatFullTimestamp, formatNotificationTime, formatRelativeTime, getNotificationIconColor, getNotificationIconName, getPriorityBadgeVariant, getPriorityLabel, getPrioritySortValue, getTypeBadgeVariant, getTypeLabel, normalizeListResponse, notificationService, notificationsApi } from './core/index.js';
|
|
2
|
+
export { B as BatchPreferenceUpdate, a as BulkPreferenceResponse, C as ChannelType, b as ChannelsResponse, c as ChapiNotificationsListResponse, D as DeletePreferenceResponse, N as Notification, d as NotificationChannel, e as NotificationError, f as NotificationFrequency, g as NotificationListParams, h as NotificationPreference, i as NotificationPreferenceResult, j as NotificationPriority, k as NotificationType, l as NotificationsListResponse, P as PreferencesResponse, T as TestNotificationParams, m as TestNotificationResponse, U as UpdatePreferenceData, n as UpdatePreferenceResponse } from './types-C3dITqDG.js';
|
|
3
3
|
export { isLoadingNotifications, notificationStore, notifications, unreadCount } from './svelte/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import { api } from '@classic-homes/auth/core';
|
|
2
2
|
|
|
3
3
|
// src/core/api.ts
|
|
4
|
+
function normalizeListResponse(raw, requested) {
|
|
5
|
+
if ("notifications" in raw && Array.isArray(raw.notifications)) {
|
|
6
|
+
return raw;
|
|
7
|
+
}
|
|
8
|
+
const chapi = raw;
|
|
9
|
+
const notifications2 = Array.isArray(chapi.data) ? chapi.data : [];
|
|
10
|
+
const pagination = chapi.pagination;
|
|
11
|
+
const limit = pagination?.pageSize ?? requested.limit;
|
|
12
|
+
const total = pagination?.total ?? notifications2.length;
|
|
13
|
+
return {
|
|
14
|
+
notifications: notifications2,
|
|
15
|
+
unreadCount: chapi.meta?.unreadCount ?? notifications2.filter((n) => !n.read).length,
|
|
16
|
+
pagination: {
|
|
17
|
+
page: pagination?.page ?? requested.page,
|
|
18
|
+
limit,
|
|
19
|
+
total,
|
|
20
|
+
totalPages: pagination?.pages ?? Math.max(1, Math.ceil(total / limit))
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
4
24
|
var notificationsApi = {
|
|
5
25
|
// ============================================================================
|
|
6
26
|
// Notifications CRUD
|
|
@@ -13,6 +33,8 @@ var notificationsApi = {
|
|
|
13
33
|
const page = params?.page || 1;
|
|
14
34
|
const limit = params?.limit || 20;
|
|
15
35
|
const offset = (page - 1) * limit;
|
|
36
|
+
queryParams.append("page", page.toString());
|
|
37
|
+
queryParams.append("pageSize", limit.toString());
|
|
16
38
|
queryParams.append("limit", limit.toString());
|
|
17
39
|
queryParams.append("offset", offset.toString());
|
|
18
40
|
if (params?.unreadOnly) {
|
|
@@ -28,24 +50,25 @@ var notificationsApi = {
|
|
|
28
50
|
true,
|
|
29
51
|
customFetch
|
|
30
52
|
);
|
|
31
|
-
return response;
|
|
53
|
+
return normalizeListResponse(response, { page, limit });
|
|
32
54
|
},
|
|
33
55
|
/**
|
|
34
56
|
* Get a single notification by ID.
|
|
35
57
|
*/
|
|
36
58
|
async get(id, customFetch) {
|
|
37
59
|
const response = await api.get(
|
|
38
|
-
`/notifications/${id}`,
|
|
60
|
+
`/notifications/${encodeURIComponent(id)}`,
|
|
39
61
|
true,
|
|
40
62
|
customFetch
|
|
41
63
|
);
|
|
42
|
-
|
|
64
|
+
const body = response;
|
|
65
|
+
return body.notification ?? body.data;
|
|
43
66
|
},
|
|
44
67
|
/**
|
|
45
68
|
* Mark a notification as read.
|
|
46
69
|
*/
|
|
47
70
|
async markRead(id) {
|
|
48
|
-
await api.patch(`/notifications/${id}/read`, {}, true);
|
|
71
|
+
await api.patch(`/notifications/${encodeURIComponent(id)}/read`, {}, true);
|
|
49
72
|
},
|
|
50
73
|
/**
|
|
51
74
|
* Mark multiple notifications as read.
|
|
@@ -73,7 +96,7 @@ var notificationsApi = {
|
|
|
73
96
|
* Dismiss (delete) a notification.
|
|
74
97
|
*/
|
|
75
98
|
async dismiss(id) {
|
|
76
|
-
await api.delete(`/notifications/${id}`, true);
|
|
99
|
+
await api.delete(`/notifications/${encodeURIComponent(id)}`, true);
|
|
77
100
|
},
|
|
78
101
|
/**
|
|
79
102
|
* Delete multiple notifications in bulk.
|
|
@@ -142,7 +165,7 @@ var notificationsApi = {
|
|
|
142
165
|
*/
|
|
143
166
|
async deletePreference(subscriptionId) {
|
|
144
167
|
const response = await api.delete(
|
|
145
|
-
`/notifications/preferences/${subscriptionId}`,
|
|
168
|
+
`/notifications/preferences/${encodeURIComponent(subscriptionId)}`,
|
|
146
169
|
true
|
|
147
170
|
);
|
|
148
171
|
return response;
|
|
@@ -279,14 +302,14 @@ function getNotificationIconName(type) {
|
|
|
279
302
|
}
|
|
280
303
|
function getNotificationIconColor(type) {
|
|
281
304
|
const colorMap = {
|
|
282
|
-
security: "text-
|
|
283
|
-
system: "text-
|
|
284
|
-
feature: "text-
|
|
285
|
-
update: "text-
|
|
286
|
-
alert: "text-
|
|
287
|
-
info: "text-
|
|
305
|
+
security: "text-destructive",
|
|
306
|
+
system: "text-info",
|
|
307
|
+
feature: "text-primary",
|
|
308
|
+
update: "text-success",
|
|
309
|
+
alert: "text-warning",
|
|
310
|
+
info: "text-muted-foreground"
|
|
288
311
|
};
|
|
289
|
-
return colorMap[type] ?? "text-
|
|
312
|
+
return colorMap[type] ?? "text-muted-foreground";
|
|
290
313
|
}
|
|
291
314
|
function getPriorityBadgeVariant(priority) {
|
|
292
315
|
const variantMap = {
|
|
@@ -396,6 +419,10 @@ function getPrioritySortValue(priority) {
|
|
|
396
419
|
var NotificationStore = class {
|
|
397
420
|
constructor() {
|
|
398
421
|
this.subscribers = /* @__PURE__ */ new Set();
|
|
422
|
+
/** Filters from the last fetch(), reapplied by refresh() and loadMore(). */
|
|
423
|
+
this.filters = {};
|
|
424
|
+
/** Bumped by every list request; a response from an older one is dropped. */
|
|
425
|
+
this.requestSeq = 0;
|
|
399
426
|
this.state = {
|
|
400
427
|
notifications: [],
|
|
401
428
|
unreadCount: 0,
|
|
@@ -447,9 +474,15 @@ var NotificationStore = class {
|
|
|
447
474
|
* Fetch notifications from the API.
|
|
448
475
|
*/
|
|
449
476
|
async fetch(params) {
|
|
477
|
+
const filters = { ...params };
|
|
478
|
+
delete filters.page;
|
|
479
|
+
delete filters.limit;
|
|
480
|
+
this.filters = filters;
|
|
481
|
+
const seq = ++this.requestSeq;
|
|
450
482
|
this.setState({ isLoading: true, error: null });
|
|
451
483
|
try {
|
|
452
484
|
const response = await notificationsApi.list(params);
|
|
485
|
+
if (seq !== this.requestSeq) return;
|
|
453
486
|
this.setState({
|
|
454
487
|
notifications: response.notifications,
|
|
455
488
|
unreadCount: response.unreadCount,
|
|
@@ -457,6 +490,7 @@ var NotificationStore = class {
|
|
|
457
490
|
isLoading: false
|
|
458
491
|
});
|
|
459
492
|
} catch (error) {
|
|
493
|
+
if (seq !== this.requestSeq) return;
|
|
460
494
|
this.setState({
|
|
461
495
|
error: error instanceof Error ? error.message : "Failed to fetch notifications",
|
|
462
496
|
isLoading: false
|
|
@@ -464,11 +498,14 @@ var NotificationStore = class {
|
|
|
464
498
|
}
|
|
465
499
|
}
|
|
466
500
|
/**
|
|
467
|
-
* Refresh notifications
|
|
501
|
+
* Refresh notifications: re-fetch from the first page with the current
|
|
502
|
+
* filters and page size. Re-fetching only the current page would replace
|
|
503
|
+
* the list with that page alone after loadMore().
|
|
468
504
|
*/
|
|
469
505
|
async refresh() {
|
|
470
506
|
await this.fetch({
|
|
471
|
-
|
|
507
|
+
...this.filters,
|
|
508
|
+
page: 1,
|
|
472
509
|
limit: this.state.pagination.limit
|
|
473
510
|
});
|
|
474
511
|
}
|
|
@@ -482,12 +519,15 @@ var NotificationStore = class {
|
|
|
482
519
|
if (this.state.pagination.page >= this.state.pagination.totalPages) {
|
|
483
520
|
return;
|
|
484
521
|
}
|
|
522
|
+
const seq = ++this.requestSeq;
|
|
485
523
|
this.setState({ isLoading: true, error: null });
|
|
486
524
|
try {
|
|
487
525
|
const response = await notificationsApi.list({
|
|
526
|
+
...this.filters,
|
|
488
527
|
page: this.state.pagination.page + 1,
|
|
489
528
|
limit: this.state.pagination.limit
|
|
490
529
|
});
|
|
530
|
+
if (seq !== this.requestSeq) return;
|
|
491
531
|
const existingIds = new Set(this.state.notifications.map((n) => n.id));
|
|
492
532
|
const newNotifications = response.notifications.filter((n) => !existingIds.has(n.id));
|
|
493
533
|
this.setState({
|
|
@@ -497,6 +537,7 @@ var NotificationStore = class {
|
|
|
497
537
|
isLoading: false
|
|
498
538
|
});
|
|
499
539
|
} catch (error) {
|
|
540
|
+
if (seq !== this.requestSeq) return;
|
|
500
541
|
this.setState({
|
|
501
542
|
error: error instanceof Error ? error.message : "Failed to load more notifications",
|
|
502
543
|
isLoading: false
|
|
@@ -509,10 +550,11 @@ var NotificationStore = class {
|
|
|
509
550
|
async markRead(id) {
|
|
510
551
|
try {
|
|
511
552
|
await notificationsApi.markRead(id);
|
|
553
|
+
const target = this.state.notifications.find((n) => n.id === id);
|
|
512
554
|
const notifications2 = this.state.notifications.map(
|
|
513
555
|
(n) => n.id === id ? { ...n, read: true, readAt: (/* @__PURE__ */ new Date()).toISOString() } : n
|
|
514
556
|
);
|
|
515
|
-
const unreadCount2 = Math.max(0, this.state.unreadCount - 1);
|
|
557
|
+
const unreadCount2 = target && !target.read ? Math.max(0, this.state.unreadCount - 1) : this.state.unreadCount;
|
|
516
558
|
this.setState({ notifications: notifications2, unreadCount: unreadCount2 });
|
|
517
559
|
} catch (error) {
|
|
518
560
|
this.setState({
|
|
@@ -609,6 +651,8 @@ var NotificationStore = class {
|
|
|
609
651
|
* Reset the store to initial state.
|
|
610
652
|
*/
|
|
611
653
|
reset() {
|
|
654
|
+
this.filters = {};
|
|
655
|
+
this.requestSeq++;
|
|
612
656
|
this.setState({
|
|
613
657
|
notifications: [],
|
|
614
658
|
unreadCount: 0,
|
|
@@ -640,4 +684,4 @@ var isLoadingNotifications = {
|
|
|
640
684
|
}
|
|
641
685
|
};
|
|
642
686
|
|
|
643
|
-
export { NotificationService, formatFullTimestamp, formatNotificationTime, formatRelativeTime, getNotificationIconColor, getNotificationIconName, getPriorityBadgeVariant, getPriorityLabel, getPrioritySortValue, getTypeBadgeVariant, getTypeLabel, isLoadingNotifications, notificationService, notificationStore, notifications, notificationsApi, unreadCount };
|
|
687
|
+
export { NotificationService, formatFullTimestamp, formatNotificationTime, formatRelativeTime, getNotificationIconColor, getNotificationIconName, getPriorityBadgeVariant, getPriorityLabel, getPrioritySortValue, getTypeBadgeVariant, getTypeLabel, isLoadingNotifications, normalizeListResponse, notificationService, notificationStore, notifications, notificationsApi, unreadCount };
|
package/dist/svelte/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { N as Notification,
|
|
2
|
-
export {
|
|
1
|
+
import { N as Notification, g as NotificationListParams } from '../types-C3dITqDG.js';
|
|
2
|
+
export { j as NotificationPriority, k as NotificationType } from '../types-C3dITqDG.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Notifications Store
|
|
@@ -25,6 +25,10 @@ interface NotificationState {
|
|
|
25
25
|
declare class NotificationStore {
|
|
26
26
|
private state;
|
|
27
27
|
private subscribers;
|
|
28
|
+
/** Filters from the last fetch(), reapplied by refresh() and loadMore(). */
|
|
29
|
+
private filters;
|
|
30
|
+
/** Bumped by every list request; a response from an older one is dropped. */
|
|
31
|
+
private requestSeq;
|
|
28
32
|
constructor();
|
|
29
33
|
/**
|
|
30
34
|
* Subscribe to state changes (Svelte store contract).
|
|
@@ -41,7 +45,9 @@ declare class NotificationStore {
|
|
|
41
45
|
*/
|
|
42
46
|
fetch(params?: NotificationListParams): Promise<void>;
|
|
43
47
|
/**
|
|
44
|
-
* Refresh notifications
|
|
48
|
+
* Refresh notifications: re-fetch from the first page with the current
|
|
49
|
+
* filters and page size. Re-fetching only the current page would replace
|
|
50
|
+
* the list with that page alone after loadMore().
|
|
45
51
|
*/
|
|
46
52
|
refresh(): Promise<void>;
|
|
47
53
|
/**
|
package/dist/svelte/index.js
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
import { api } from '@classic-homes/auth/core';
|
|
2
2
|
|
|
3
3
|
// src/core/api.ts
|
|
4
|
+
function normalizeListResponse(raw, requested) {
|
|
5
|
+
if ("notifications" in raw && Array.isArray(raw.notifications)) {
|
|
6
|
+
return raw;
|
|
7
|
+
}
|
|
8
|
+
const chapi = raw;
|
|
9
|
+
const notifications2 = Array.isArray(chapi.data) ? chapi.data : [];
|
|
10
|
+
const pagination = chapi.pagination;
|
|
11
|
+
const limit = pagination?.pageSize ?? requested.limit;
|
|
12
|
+
const total = pagination?.total ?? notifications2.length;
|
|
13
|
+
return {
|
|
14
|
+
notifications: notifications2,
|
|
15
|
+
unreadCount: chapi.meta?.unreadCount ?? notifications2.filter((n) => !n.read).length,
|
|
16
|
+
pagination: {
|
|
17
|
+
page: pagination?.page ?? requested.page,
|
|
18
|
+
limit,
|
|
19
|
+
total,
|
|
20
|
+
totalPages: pagination?.pages ?? Math.max(1, Math.ceil(total / limit))
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
4
24
|
var notificationsApi = {
|
|
5
25
|
// ============================================================================
|
|
6
26
|
// Notifications CRUD
|
|
@@ -13,6 +33,8 @@ var notificationsApi = {
|
|
|
13
33
|
const page = params?.page || 1;
|
|
14
34
|
const limit = params?.limit || 20;
|
|
15
35
|
const offset = (page - 1) * limit;
|
|
36
|
+
queryParams.append("page", page.toString());
|
|
37
|
+
queryParams.append("pageSize", limit.toString());
|
|
16
38
|
queryParams.append("limit", limit.toString());
|
|
17
39
|
queryParams.append("offset", offset.toString());
|
|
18
40
|
if (params?.unreadOnly) {
|
|
@@ -28,24 +50,25 @@ var notificationsApi = {
|
|
|
28
50
|
true,
|
|
29
51
|
customFetch
|
|
30
52
|
);
|
|
31
|
-
return response;
|
|
53
|
+
return normalizeListResponse(response, { page, limit });
|
|
32
54
|
},
|
|
33
55
|
/**
|
|
34
56
|
* Get a single notification by ID.
|
|
35
57
|
*/
|
|
36
58
|
async get(id, customFetch) {
|
|
37
59
|
const response = await api.get(
|
|
38
|
-
`/notifications/${id}`,
|
|
60
|
+
`/notifications/${encodeURIComponent(id)}`,
|
|
39
61
|
true,
|
|
40
62
|
customFetch
|
|
41
63
|
);
|
|
42
|
-
|
|
64
|
+
const body = response;
|
|
65
|
+
return body.notification ?? body.data;
|
|
43
66
|
},
|
|
44
67
|
/**
|
|
45
68
|
* Mark a notification as read.
|
|
46
69
|
*/
|
|
47
70
|
async markRead(id) {
|
|
48
|
-
await api.patch(`/notifications/${id}/read`, {}, true);
|
|
71
|
+
await api.patch(`/notifications/${encodeURIComponent(id)}/read`, {}, true);
|
|
49
72
|
},
|
|
50
73
|
/**
|
|
51
74
|
* Mark multiple notifications as read.
|
|
@@ -73,7 +96,7 @@ var notificationsApi = {
|
|
|
73
96
|
* Dismiss (delete) a notification.
|
|
74
97
|
*/
|
|
75
98
|
async dismiss(id) {
|
|
76
|
-
await api.delete(`/notifications/${id}`, true);
|
|
99
|
+
await api.delete(`/notifications/${encodeURIComponent(id)}`, true);
|
|
77
100
|
},
|
|
78
101
|
/**
|
|
79
102
|
* Delete multiple notifications in bulk.
|
|
@@ -142,7 +165,7 @@ var notificationsApi = {
|
|
|
142
165
|
*/
|
|
143
166
|
async deletePreference(subscriptionId) {
|
|
144
167
|
const response = await api.delete(
|
|
145
|
-
`/notifications/preferences/${subscriptionId}`,
|
|
168
|
+
`/notifications/preferences/${encodeURIComponent(subscriptionId)}`,
|
|
146
169
|
true
|
|
147
170
|
);
|
|
148
171
|
return response;
|
|
@@ -168,6 +191,10 @@ var notificationsApi = {
|
|
|
168
191
|
var NotificationStore = class {
|
|
169
192
|
constructor() {
|
|
170
193
|
this.subscribers = /* @__PURE__ */ new Set();
|
|
194
|
+
/** Filters from the last fetch(), reapplied by refresh() and loadMore(). */
|
|
195
|
+
this.filters = {};
|
|
196
|
+
/** Bumped by every list request; a response from an older one is dropped. */
|
|
197
|
+
this.requestSeq = 0;
|
|
171
198
|
this.state = {
|
|
172
199
|
notifications: [],
|
|
173
200
|
unreadCount: 0,
|
|
@@ -219,9 +246,15 @@ var NotificationStore = class {
|
|
|
219
246
|
* Fetch notifications from the API.
|
|
220
247
|
*/
|
|
221
248
|
async fetch(params) {
|
|
249
|
+
const filters = { ...params };
|
|
250
|
+
delete filters.page;
|
|
251
|
+
delete filters.limit;
|
|
252
|
+
this.filters = filters;
|
|
253
|
+
const seq = ++this.requestSeq;
|
|
222
254
|
this.setState({ isLoading: true, error: null });
|
|
223
255
|
try {
|
|
224
256
|
const response = await notificationsApi.list(params);
|
|
257
|
+
if (seq !== this.requestSeq) return;
|
|
225
258
|
this.setState({
|
|
226
259
|
notifications: response.notifications,
|
|
227
260
|
unreadCount: response.unreadCount,
|
|
@@ -229,6 +262,7 @@ var NotificationStore = class {
|
|
|
229
262
|
isLoading: false
|
|
230
263
|
});
|
|
231
264
|
} catch (error) {
|
|
265
|
+
if (seq !== this.requestSeq) return;
|
|
232
266
|
this.setState({
|
|
233
267
|
error: error instanceof Error ? error.message : "Failed to fetch notifications",
|
|
234
268
|
isLoading: false
|
|
@@ -236,11 +270,14 @@ var NotificationStore = class {
|
|
|
236
270
|
}
|
|
237
271
|
}
|
|
238
272
|
/**
|
|
239
|
-
* Refresh notifications
|
|
273
|
+
* Refresh notifications: re-fetch from the first page with the current
|
|
274
|
+
* filters and page size. Re-fetching only the current page would replace
|
|
275
|
+
* the list with that page alone after loadMore().
|
|
240
276
|
*/
|
|
241
277
|
async refresh() {
|
|
242
278
|
await this.fetch({
|
|
243
|
-
|
|
279
|
+
...this.filters,
|
|
280
|
+
page: 1,
|
|
244
281
|
limit: this.state.pagination.limit
|
|
245
282
|
});
|
|
246
283
|
}
|
|
@@ -254,12 +291,15 @@ var NotificationStore = class {
|
|
|
254
291
|
if (this.state.pagination.page >= this.state.pagination.totalPages) {
|
|
255
292
|
return;
|
|
256
293
|
}
|
|
294
|
+
const seq = ++this.requestSeq;
|
|
257
295
|
this.setState({ isLoading: true, error: null });
|
|
258
296
|
try {
|
|
259
297
|
const response = await notificationsApi.list({
|
|
298
|
+
...this.filters,
|
|
260
299
|
page: this.state.pagination.page + 1,
|
|
261
300
|
limit: this.state.pagination.limit
|
|
262
301
|
});
|
|
302
|
+
if (seq !== this.requestSeq) return;
|
|
263
303
|
const existingIds = new Set(this.state.notifications.map((n) => n.id));
|
|
264
304
|
const newNotifications = response.notifications.filter((n) => !existingIds.has(n.id));
|
|
265
305
|
this.setState({
|
|
@@ -269,6 +309,7 @@ var NotificationStore = class {
|
|
|
269
309
|
isLoading: false
|
|
270
310
|
});
|
|
271
311
|
} catch (error) {
|
|
312
|
+
if (seq !== this.requestSeq) return;
|
|
272
313
|
this.setState({
|
|
273
314
|
error: error instanceof Error ? error.message : "Failed to load more notifications",
|
|
274
315
|
isLoading: false
|
|
@@ -281,10 +322,11 @@ var NotificationStore = class {
|
|
|
281
322
|
async markRead(id) {
|
|
282
323
|
try {
|
|
283
324
|
await notificationsApi.markRead(id);
|
|
325
|
+
const target = this.state.notifications.find((n) => n.id === id);
|
|
284
326
|
const notifications2 = this.state.notifications.map(
|
|
285
327
|
(n) => n.id === id ? { ...n, read: true, readAt: (/* @__PURE__ */ new Date()).toISOString() } : n
|
|
286
328
|
);
|
|
287
|
-
const unreadCount2 = Math.max(0, this.state.unreadCount - 1);
|
|
329
|
+
const unreadCount2 = target && !target.read ? Math.max(0, this.state.unreadCount - 1) : this.state.unreadCount;
|
|
288
330
|
this.setState({ notifications: notifications2, unreadCount: unreadCount2 });
|
|
289
331
|
} catch (error) {
|
|
290
332
|
this.setState({
|
|
@@ -381,6 +423,8 @@ var NotificationStore = class {
|
|
|
381
423
|
* Reset the store to initial state.
|
|
382
424
|
*/
|
|
383
425
|
reset() {
|
|
426
|
+
this.filters = {};
|
|
427
|
+
this.requestSeq++;
|
|
384
428
|
this.setState({
|
|
385
429
|
notifications: [],
|
|
386
430
|
unreadCount: 0,
|
|
@@ -34,6 +34,23 @@ interface NotificationsListResponse {
|
|
|
34
34
|
totalPages: number;
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Raw list response as returned by CHAPI `GET /v1/notifications`.
|
|
39
|
+
* Normalized into NotificationsListResponse by notificationsApi.list().
|
|
40
|
+
*/
|
|
41
|
+
interface ChapiNotificationsListResponse {
|
|
42
|
+
data: Notification[];
|
|
43
|
+
pagination?: {
|
|
44
|
+
page: number;
|
|
45
|
+
pageSize: number;
|
|
46
|
+
total: number;
|
|
47
|
+
pages: number;
|
|
48
|
+
};
|
|
49
|
+
meta?: {
|
|
50
|
+
unreadCount?: number;
|
|
51
|
+
timestamp?: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
37
54
|
interface NotificationListParams {
|
|
38
55
|
page?: number;
|
|
39
56
|
limit?: number;
|
|
@@ -152,4 +169,4 @@ interface TestNotificationResponse {
|
|
|
152
169
|
};
|
|
153
170
|
}
|
|
154
171
|
|
|
155
|
-
export type { BatchPreferenceUpdate as B, ChannelType as C, DeletePreferenceResponse as D, MarkReadResponse as M, Notification as N, PreferencesResponse as P, TestNotificationParams as T, UpdatePreferenceData as U, BulkPreferenceResponse as a, ChannelsResponse as b,
|
|
172
|
+
export type { BatchPreferenceUpdate as B, ChannelType as C, DeletePreferenceResponse as D, MarkReadResponse as M, Notification as N, PreferencesResponse as P, TestNotificationParams as T, UpdatePreferenceData as U, BulkPreferenceResponse as a, ChannelsResponse as b, ChapiNotificationsListResponse as c, NotificationChannel as d, NotificationError as e, NotificationFrequency as f, NotificationListParams as g, NotificationPreference as h, NotificationPreferenceResult as i, NotificationPriority as j, NotificationType as k, NotificationsListResponse as l, TestNotificationResponse as m, UpdatePreferenceResponse as n };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classic-homes/notifications",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.2",
|
|
4
4
|
"description": "Notification services and Svelte bindings for Classic Theme apps",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"access": "public"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@classic-homes/auth": "^1.
|
|
50
|
+
"@classic-homes/auth": "^1.22.2"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
53
|
"svelte": "^5.0.0"
|