1
+ import { v4 as uuidv4 } from 'uuid' ;
1
2
import { StateStore } from '../store' ;
2
3
import type {
4
+ AddNotificationPayload ,
3
5
Notification ,
4
6
NotificationManagerConfig ,
5
- NotificationOptions ,
6
- NotificationSeverity ,
7
7
NotificationState ,
8
8
} from './types' ;
9
- import { v4 as uuidv4 } from 'uuid' ;
10
9
11
10
const DURATIONS : NotificationManagerConfig [ 'durations' ] = {
12
11
error : 10000 ,
@@ -28,11 +27,27 @@ export class NotificationManager {
28
27
} ;
29
28
}
30
29
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 {
36
51
const id = uuidv4 ( ) ;
37
52
const now = Date . now ( ) ;
38
53
@@ -63,20 +78,20 @@ export class NotificationManager {
63
78
return id ;
64
79
}
65
80
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' } } ) ;
68
83
}
69
84
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' } } ) ;
72
87
}
73
88
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' } } ) ;
76
91
}
77
92
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' } } ) ;
80
95
}
81
96
82
97
remove ( id : string ) : void {
@@ -97,18 +112,4 @@ export class NotificationManager {
97
112
98
113
this . store . partialNext ( { notifications : [ ] } ) ;
99
114
}
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
- }
114
115
}
0 commit comments