@patreon/studio
Version:
Patreon Studio Design System
35 lines • 1.7 kB
JavaScript
import { DEFAULT_TOAST_KEY_PREFIX } from './constants';
// TODO (legacied consistent-return)
// This failure is legacied in and should be updated. DO NOT COPY.
// eslint-disable-next-line consistent-return
export const toasterReducer = (state, action) => {
switch (action.type) {
case 'add': {
// If this toast is automatically dismissable, then we need to see
// if there's another automatically dismissable that's not part of a
// chain of toasts (aka toasts with an explicit key) to replace
if (action.payload.duration !== 'infinite') {
for (let i = 0; i < state.length; i++) {
const toast = state[i];
if (toast.duration !== 'infinite' && toast.key.startsWith(DEFAULT_TOAST_KEY_PREFIX)) {
// Remove the old toast and add the new toast to the end
// of state so that it shows as newest
return [...state.slice(0, i), ...state.slice(i + 1), action.payload];
}
}
}
// If we got here, we didn't replace a toast and so we append it instead
return [...state, action.payload];
}
case 'update': {
return state.map((toast) => action.payload.id === toast.id || action.payload.key === toast.key ? { ...toast, ...action.payload } : toast);
}
case 'remove': {
return state.filter((toast) => action.payload.id !== toast.id);
}
case 'closeAll': {
return state.map((toast) => ({ ...toast, isClosing: true }));
}
}
};
//# sourceMappingURL=reducers.js.map