1
- import { type FC , useCallback , useState } from "react" ;
1
+ import { type FC , useState } from "react" ;
2
2
import { useCustomEvent } from "hooks/events" ;
3
- import type { CustomEventListener } from "utils/events" ;
4
3
import { EnterpriseSnackbar } from "./EnterpriseSnackbar" ;
5
4
import { ErrorIcon } from "../Icons/ErrorIcon" ;
6
5
import { Typography } from "../Typography/Typography" ;
@@ -29,54 +28,10 @@ export const GlobalSnackbar: FC = () => {
29
28
const [ open , setOpen ] = useState < boolean > ( false ) ;
30
29
const [ notification , setNotification ] = useState < NotificationMsg > ( ) ;
31
30
32
- const handleNotification = useCallback < CustomEventListener < NotificationMsg > > (
33
- ( event ) => {
34
- setNotification ( event . detail ) ;
35
- setOpen ( true ) ;
36
- } ,
37
- [ ] ,
38
- ) ;
39
-
40
- useCustomEvent ( SnackbarEventType , handleNotification ) ;
41
-
42
- const renderAdditionalMessage = ( msg : AdditionalMessage , idx : number ) => {
43
- if ( isNotificationText ( msg ) ) {
44
- return (
45
- < Typography
46
- key = { idx }
47
- gutterBottom
48
- variant = "body2"
49
- css = { styles . messageSubtitle }
50
- >
51
- { msg }
52
- </ Typography >
53
- ) ;
54
- } else if ( isNotificationTextPrefixed ( msg ) ) {
55
- return (
56
- < Typography
57
- key = { idx }
58
- gutterBottom
59
- variant = "body2"
60
- css = { styles . messageSubtitle }
61
- >
62
- < strong > { msg . prefix } :</ strong > { msg . text }
63
- </ Typography >
64
- ) ;
65
- } else if ( isNotificationList ( msg ) ) {
66
- return (
67
- < ul css = { styles . list } key = { idx } >
68
- { msg . map ( ( item , idx ) => (
69
- < li key = { idx } >
70
- < Typography variant = "body2" css = { styles . messageSubtitle } >
71
- { item }
72
- </ Typography >
73
- </ li >
74
- ) ) }
75
- </ ul >
76
- ) ;
77
- }
78
- return null ;
79
- } ;
31
+ useCustomEvent < NotificationMsg > ( SnackbarEventType , ( event ) => {
32
+ setNotification ( event . detail ) ;
33
+ setOpen ( true ) ;
34
+ } ) ;
80
35
81
36
if ( ! notification ) {
82
37
return null ;
@@ -87,26 +42,27 @@ export const GlobalSnackbar: FC = () => {
87
42
key = { notification . msg }
88
43
open = { open }
89
44
variant = { variantFromMsgType ( notification . msgType ) }
45
+ onClose = { ( ) => setOpen ( false ) }
46
+ autoHideDuration = { notification . msgType === MsgType . Error ? 22000 : 6000 }
47
+ anchorOrigin = { { vertical : "bottom" , horizontal : "right" } }
90
48
message = {
91
49
< div css = { styles . messageWrapper } >
92
50
{ notification . msgType === MsgType . Error && (
93
51
< ErrorIcon css = { styles . errorIcon } />
94
52
) }
53
+
95
54
< div css = { styles . message } >
96
55
< Typography variant = "body1" css = { styles . messageTitle } >
97
56
{ notification . msg }
98
57
</ Typography >
58
+
99
59
{ notification . additionalMsgs &&
100
- notification . additionalMsgs . map ( renderAdditionalMessage ) }
60
+ notification . additionalMsgs . map ( ( msg , index ) => (
61
+ < AdditionalMessageDisplay key = { index } message = { msg } />
62
+ ) ) }
101
63
</ div >
102
64
</ div >
103
65
}
104
- onClose = { ( ) => setOpen ( false ) }
105
- autoHideDuration = { notification . msgType === MsgType . Error ? 22000 : 6000 }
106
- anchorOrigin = { {
107
- vertical : "bottom" ,
108
- horizontal : "right" ,
109
- } }
110
66
/>
111
67
) ;
112
68
} ;
@@ -133,3 +89,37 @@ const styles = {
133
89
marginRight : 16 ,
134
90
} ) ,
135
91
} satisfies Record < string , Interpolation < Theme > > ;
92
+
93
+ function AdditionalMessageDisplay ( { message } : { message : AdditionalMessage } ) {
94
+ if ( isNotificationText ( message ) ) {
95
+ return (
96
+ < Typography gutterBottom variant = "body2" css = { styles . messageSubtitle } >
97
+ { message }
98
+ </ Typography >
99
+ ) ;
100
+ }
101
+
102
+ if ( isNotificationTextPrefixed ( message ) ) {
103
+ return (
104
+ < Typography gutterBottom variant = "body2" css = { styles . messageSubtitle } >
105
+ < strong > { message . prefix } :</ strong > { message . text }
106
+ </ Typography >
107
+ ) ;
108
+ }
109
+
110
+ if ( isNotificationList ( message ) ) {
111
+ return (
112
+ < ul css = { styles . list } >
113
+ { message . map ( ( item , idx ) => (
114
+ < li key = { idx } >
115
+ < Typography variant = "body2" css = { styles . messageSubtitle } >
116
+ { item }
117
+ </ Typography >
118
+ </ li >
119
+ ) ) }
120
+ </ ul >
121
+ ) ;
122
+ }
123
+
124
+ return null ;
125
+ }
0 commit comments