@@ -15,37 +15,35 @@ type ToasterToast = ToastProps & {
1515 action ?: ToastActionElement ;
1616} ;
1717
18- const actionTypes = {
19- ADD_TOAST : "ADD_TOAST" ,
20- UPDATE_TOAST : "UPDATE_TOAST" ,
21- DISMISS_TOAST : "DISMISS_TOAST" ,
22- REMOVE_TOAST : "REMOVE_TOAST" ,
23- } as const ;
24-
2518let count = 0 ;
2619
2720function genId ( ) {
2821 count = ( count + 1 ) % Number . MAX_SAFE_INTEGER ;
2922 return count . toString ( ) ;
3023}
3124
32- type ActionType = typeof actionTypes ;
25+ const enum ActionType {
26+ ADD_TOAST = "ADD_TOAST" ,
27+ UPDATE_TOAST = "UPDATE_TOAST" ,
28+ DISMISS_TOAST = "DISMISS_TOAST" ,
29+ REMOVE_TOAST = "REMOVE_TOAST" ,
30+ }
3331
3432type Action =
3533 | {
36- type : ActionType [ " ADD_TOAST" ] ;
34+ type : ActionType . ADD_TOAST ;
3735 toast : ToasterToast ;
3836 }
3937 | {
40- type : ActionType [ " UPDATE_TOAST" ] ;
38+ type : ActionType . UPDATE_TOAST ;
4139 toast : Partial < ToasterToast > ;
4240 }
4341 | {
44- type : ActionType [ " DISMISS_TOAST" ] ;
42+ type : ActionType . DISMISS_TOAST ;
4543 toastId ?: ToasterToast [ "id" ] ;
4644 }
4745 | {
48- type : ActionType [ " REMOVE_TOAST" ] ;
46+ type : ActionType . REMOVE_TOAST ;
4947 toastId ?: ToasterToast [ "id" ] ;
5048 } ;
5149
@@ -63,7 +61,7 @@ const addToRemoveQueue = (toastId: string) => {
6361 const timeout = setTimeout ( ( ) => {
6462 toastTimeouts . delete ( toastId ) ;
6563 dispatch ( {
66- type : " REMOVE_TOAST" ,
64+ type : ActionType . REMOVE_TOAST ,
6765 toastId : toastId ,
6866 } ) ;
6967 } , TOAST_REMOVE_DELAY ) ;
@@ -73,21 +71,21 @@ const addToRemoveQueue = (toastId: string) => {
7371
7472export const reducer = ( state : State , action : Action ) : State => {
7573 switch ( action . type ) {
76- case " ADD_TOAST" :
74+ case ActionType . ADD_TOAST :
7775 return {
7876 ...state ,
7977 toasts : [ action . toast , ...state . toasts ] . slice ( 0 , TOAST_LIMIT ) ,
8078 } ;
8179
82- case " UPDATE_TOAST" :
80+ case ActionType . UPDATE_TOAST :
8381 return {
8482 ...state ,
8583 toasts : state . toasts . map ( ( t ) =>
8684 t . id === action . toast . id ? { ...t , ...action . toast } : t ,
8785 ) ,
8886 } ;
8987
90- case " DISMISS_TOAST" : {
88+ case ActionType . DISMISS_TOAST : {
9189 const { toastId } = action ;
9290
9391 // ! Side effects ! - This could be extracted into a dismissToast() action,
@@ -112,7 +110,7 @@ export const reducer = (state: State, action: Action): State => {
112110 ) ,
113111 } ;
114112 }
115- case " REMOVE_TOAST" :
113+ case ActionType . REMOVE_TOAST :
116114 if ( action . toastId === undefined ) {
117115 return {
118116 ...state ,
@@ -144,13 +142,14 @@ function toast({ ...props }: Toast) {
144142
145143 const update = ( props : ToasterToast ) =>
146144 dispatch ( {
147- type : " UPDATE_TOAST" ,
145+ type : ActionType . UPDATE_TOAST ,
148146 toast : { ...props , id } ,
149147 } ) ;
150- const dismiss = ( ) => dispatch ( { type : "DISMISS_TOAST" , toastId : id } ) ;
148+ const dismiss = ( ) =>
149+ dispatch ( { type : ActionType . DISMISS_TOAST , toastId : id } ) ;
151150
152151 dispatch ( {
153- type : " ADD_TOAST" ,
152+ type : ActionType . ADD_TOAST ,
154153 toast : {
155154 ...props ,
156155 id,
@@ -184,7 +183,8 @@ function useToast() {
184183 return {
185184 ...state ,
186185 toast,
187- dismiss : ( toastId ?: string ) => dispatch ( { type : "DISMISS_TOAST" , toastId } ) ,
186+ dismiss : ( toastId ?: string ) =>
187+ dispatch ( { type : ActionType . DISMISS_TOAST , toastId } ) ,
188188 } ;
189189}
190190
0 commit comments