Refactor removeToast (#854)

This commit is contained in:
ekzyis 2024-02-18 20:33:32 +01:00 committed by GitHub
parent 2d5b1f090d
commit a7018e25c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 5 deletions

View File

@ -24,12 +24,22 @@ export const ToastProvider = ({ children }) => {
const removeToast = useCallback(({ id, onCancel, tag }) => {
setToasts(toasts => toasts.filter(toast => {
if (tag && !onCancel) {
// if tag onCancel is not set, toast did show X for closing.
// if additionally tag is set, we close all toasts with same tag.
return toast.tag !== tag
if (toast.id === id) {
// remove the toast with the passed id with no exceptions
return false
}
return toast.id !== id
const sameTag = tag && tag === toast.tag
if (!sameTag) {
// don't touch toasts with different tags
return true
}
const toRemoveHasCancel = !!toast.onCancel
if (toRemoveHasCancel) {
// don't remove this toast so the user can decide to cancel this toast now
return true
}
// remove toasts with same tag if they are not cancelable
return false
}))
}, [])