Skip to content

Commit b5c6d3f

Browse files
committed
refactor: add NotificationManager state getters and use single object arguments
1 parent b860910 commit b5c6d3f

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

src/notifications/NotificationManager.ts

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import { v4 as uuidv4 } from 'uuid';
12
import { StateStore } from '../store';
23
import type {
4+
AddNotificationPayload,
35
Notification,
46
NotificationManagerConfig,
5-
NotificationOptions,
6-
NotificationSeverity,
77
NotificationState,
88
} from './types';
9-
import { v4 as uuidv4 } from 'uuid';
109

1110
const DURATIONS: NotificationManagerConfig['durations'] = {
1211
error: 10000,
@@ -28,11 +27,27 @@ export class NotificationManager {
2827
};
2928
}
3029

31-
private add(
32-
message: string,
33-
origin: string,
34-
options: NotificationOptions = {},
35-
): string {
30+
get notifications() {
31+
return this.store.getLatestValue().notifications;
32+
}
33+
34+
get warning() {
35+
return this.notifications.filter((n) => n.severity === 'warning');
36+
}
37+
38+
get error() {
39+
return this.notifications.filter((n) => n.severity === 'error');
40+
}
41+
42+
get info() {
43+
return this.notifications.filter((n) => n.severity === 'info');
44+
}
45+
46+
get success() {
47+
return this.notifications.filter((n) => n.severity === 'success');
48+
}
49+
50+
add({ message, origin, options }: AddNotificationPayload): string {
3651
const id = uuidv4();
3752
const now = Date.now();
3853

@@ -63,20 +78,20 @@ export class NotificationManager {
6378
return id;
6479
}
6580

66-
error(message: string, origin: string, options = {}) {
67-
return this.add(message, origin, { severity: 'error', ...options });
81+
addError({ message, origin, options }: AddNotificationPayload) {
82+
return this.add({ message, origin, options: { ...options, severity: 'error' } });
6883
}
6984

70-
warning(message: string, origin: string, options = {}) {
71-
return this.add(message, origin, { severity: 'warning', ...options });
85+
addWarning({ message, origin, options }: AddNotificationPayload) {
86+
return this.add({ message, origin, options: { ...options, severity: 'warning' } });
7287
}
7388

74-
info(message: string, origin: string, options = {}) {
75-
return this.add(message, origin, { severity: 'info', ...options });
89+
addInfo({ message, origin, options }: AddNotificationPayload) {
90+
return this.add({ message, origin, options: { ...options, severity: 'info' } });
7691
}
7792

78-
success(message: string, origin: string, options = {}) {
79-
return this.add(message, origin, { severity: 'success', ...options });
93+
addSuccess({ message, origin, options }: AddNotificationPayload) {
94+
return this.add({ message, origin, options: { ...options, severity: 'success' } });
8095
}
8196

8297
remove(id: string): void {
@@ -97,18 +112,4 @@ export class NotificationManager {
97112

98113
this.store.partialNext({ notifications: [] });
99114
}
100-
101-
getBySeverity(severity: NotificationSeverity): Notification[] {
102-
return this.store
103-
.getLatestValue()
104-
.notifications.filter((n) => n.severity === severity);
105-
}
106-
107-
subscribe(callback: (state: NotificationState) => void) {
108-
return this.store.subscribe(callback);
109-
}
110-
111-
getState(): NotificationState {
112-
return this.store.getLatestValue();
113-
}
114115
}

src/notifications/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ export type NotificationState = {
6464
export type NotificationManagerConfig = {
6565
durations: Record<NotificationSeverity, number>;
6666
};
67+
68+
export type AddNotificationPayload = {
69+
message: string;
70+
origin: string;
71+
options: NotificationOptions;
72+
};

0 commit comments

Comments
 (0)