Skip to content

Commit

Permalink
Refactor removeToast
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Feb 18, 2024
1 parent 6e6c355 commit da3b61b
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions components/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
}, [])

Expand Down

0 comments on commit da3b61b

Please sign in to comment.