@@ -22,6 +22,14 @@ function genId() {
2222 return count . toString ( ) ;
2323}
2424
25+ // Define the action types constant
26+ const actionTypes = {
27+ ADD_TOAST : "ADD_TOAST" ,
28+ UPDATE_TOAST : "UPDATE_TOAST" ,
29+ DISMISS_TOAST : "DISMISS_TOAST" ,
30+ REMOVE_TOAST : "REMOVE_TOAST" ,
31+ } as const ;
32+
2533type ActionType = typeof actionTypes ;
2634
2735type Action =
@@ -58,7 +66,7 @@ const addToRemoveQueue = (toastId: string) => {
5866 const timeout = setTimeout ( ( ) => {
5967 toastTimeouts . delete ( idAsString ) ;
6068 dispatch ( {
61- type : " REMOVE_TOAST" ,
69+ type : actionTypes . REMOVE_TOAST ,
6270 toastId : idAsString ,
6371 } ) ;
6472 } , TOAST_REMOVE_DELAY ) ;
@@ -68,14 +76,14 @@ const addToRemoveQueue = (toastId: string) => {
6876
6977const reducer = ( state : State , action : Action ) : State => {
7078 switch ( action . type ) {
71- case " ADD_TOAST" : {
79+ case actionTypes . ADD_TOAST : {
7280 const newToasts : ToasterToast [ ] = [ action . toast , ...state . toasts ] . slice ( 0 , TOAST_LIMIT ) ;
7381 return {
7482 ...state ,
7583 toasts : newToasts ,
7684 } ;
7785 }
78- case " UPDATE_TOAST" : {
86+ case actionTypes . UPDATE_TOAST : {
7987 const updatedToasts = state . toasts . map ( ( t ) : ToasterToast =>
8088 t . id === action . toast . id ? ( { ...t , ...action . toast } as ToasterToast ) : t
8189 ) ;
@@ -84,7 +92,7 @@ const reducer = (state: State, action: Action): State => {
8492 toasts : updatedToasts ,
8593 } ;
8694 }
87- case " DISMISS_TOAST" : {
95+ case actionTypes . DISMISS_TOAST : {
8896 const { toastId } = action ;
8997
9098 if ( toastId !== undefined ) {
@@ -112,7 +120,7 @@ const reducer = (state: State, action: Action): State => {
112120 } ;
113121 }
114122 }
115- case " REMOVE_TOAST" : {
123+ case actionTypes . REMOVE_TOAST : {
116124 if ( action . toastId === undefined ) {
117125 return {
118126 ...state ,
@@ -128,6 +136,9 @@ const reducer = (state: State, action: Action): State => {
128136 } ;
129137 }
130138 default :
139+ // Check if the action type is one of the known types
140+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-unsafe-assignment
141+ const _exhaustiveCheck : never = action . type ;
131142 return state ;
132143 }
133144} ;
@@ -150,13 +161,13 @@ function toast({ ...props }: Toast) {
150161
151162 const update = ( props : ToasterToast ) =>
152163 dispatch ( {
153- type : " UPDATE_TOAST" ,
164+ type : actionTypes . UPDATE_TOAST ,
154165 toast : { ...props , id } ,
155166 } ) ;
156- const dismiss = ( ) => dispatch ( { type : " DISMISS_TOAST" , toastId : id } ) ;
167+ const dismiss = ( ) => dispatch ( { type : actionTypes . DISMISS_TOAST , toastId : id } ) ;
157168
158169 dispatch ( {
159- type : " ADD_TOAST" ,
170+ type : actionTypes . ADD_TOAST ,
160171 toast : {
161172 ...props ,
162173 id,
@@ -195,7 +206,7 @@ function useToast(): UseToastReturn {
195206 return {
196207 ...state ,
197208 toast,
198- dismiss : ( toastId ?: string ) => dispatch ( { type : " DISMISS_TOAST" , toastId } ) ,
209+ dismiss : ( toastId ?: string ) => dispatch ( { type : actionTypes . DISMISS_TOAST , toastId } ) ,
199210 } ;
200211}
201212
0 commit comments