@trellixio/roaster-coffee
Version:
Beans' product component library
52 lines (49 loc) • 1.24 kB
JavaScript
import 'react';
import { useQueue } from '../../../../utils/useQueue/index.js';
import '@floating-ui/react';
function useToastsState({ limit }) {
const { state, queue, update, cleanQueue } = useQueue({
initialValues: [],
limit
});
const showToast = (toast) => {
const { id } = toast;
update((toasts) => {
if (toast.id && toasts.some((n) => n.id === toast.id)) {
return toasts;
}
return [...toasts, { ...toast, id }];
});
return id;
};
const updateToast = (toast) => update((toasts) => {
const index = toasts.findIndex((n) => n.id === toast.id);
if (index === -1) {
return toasts;
}
const newToasts = [...toasts];
newToasts[index] = toast;
return newToasts;
});
const hideToast = (id) => update(
(toasts) => toasts.filter((toast) => {
if (toast.id === id) {
typeof toast.onClose === "function" && toast.onClose(toast);
return false;
}
return true;
})
);
const clean = () => update(() => []);
return {
toasts: state,
queue,
showToast,
updateToast,
hideToast,
cleanQueue,
clean
};
}
export { useToastsState as default };
//# sourceMappingURL=useToastsState.js.map