(state: State, action: Action)
| 79 | }; |
| 80 | |
| 81 | export const reducer = (state: State, action: Action): State => { |
| 82 | switch (action.type) { |
| 83 | case "ADD_TOAST": |
| 84 | return { |
| 85 | ...state, |
| 86 | toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT), |
| 87 | }; |
| 88 | |
| 89 | case "UPDATE_TOAST": |
| 90 | return { |
| 91 | ...state, |
| 92 | toasts: state.toasts.map((t) => |
| 93 | t.id === action.toast.id ? { ...t, ...action.toast } : t, |
| 94 | ), |
| 95 | }; |
| 96 | |
| 97 | case "DISMISS_TOAST": { |
| 98 | const { toastId } = action; |
| 99 | |
| 100 | // ! Side effects ! - This could be extracted into a dismissToast() action, |
| 101 | // but I'll keep it here for simplicity |
| 102 | if (toastId) { |
| 103 | addToRemoveQueue(toastId); |
| 104 | } else { |
| 105 | state.toasts.forEach((toast) => { |
| 106 | addToRemoveQueue(toast.id); |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | return { |
| 111 | ...state, |
| 112 | toasts: state.toasts.map((t) => |
| 113 | t.id === toastId || toastId === undefined |
| 114 | ? { |
| 115 | ...t, |
| 116 | open: false, |
| 117 | } |
| 118 | : t, |
| 119 | ), |
| 120 | }; |
| 121 | } |
| 122 | case "REMOVE_TOAST": |
| 123 | if (action.toastId === undefined) { |
| 124 | return { |
| 125 | ...state, |
| 126 | toasts: [], |
| 127 | }; |
| 128 | } |
| 129 | return { |
| 130 | ...state, |
| 131 | toasts: state.toasts.filter((t) => t.id !== action.toastId), |
| 132 | }; |
| 133 | case "UPSERT_TOAST": { |
| 134 | const existingIndex = state.toasts.findIndex( |
| 135 | (t) => t.id === action.toast.id, |
| 136 | ); |
| 137 | if (existingIndex > -1) { |
| 138 | return { |
searching dependent graphs…