2023-10-27 15:48:05 -04:00
|
|
|
import { useRecoilState } from 'recoil';
|
2023-10-27 17:10:07 -04:00
|
|
|
import { useRef, useEffect } from 'react';
|
|
|
|
|
import type { TShowToast } from '~/common';
|
2023-10-27 15:48:05 -04:00
|
|
|
import { NotificationSeverity } from '~/common';
|
|
|
|
|
import store from '~/store';
|
|
|
|
|
|
|
|
|
|
export default function useToast(timeoutDuration = 100) {
|
|
|
|
|
const [toast, setToast] = useRecoilState(store.toastState);
|
|
|
|
|
const timerRef = useRef<number | null>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
|
|
|
|
if (timerRef.current !== null) {
|
|
|
|
|
clearTimeout(timerRef.current);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const showToast = ({
|
|
|
|
|
message,
|
|
|
|
|
severity = NotificationSeverity.SUCCESS,
|
|
|
|
|
showIcon = true,
|
|
|
|
|
}: TShowToast) => {
|
|
|
|
|
setToast({ ...toast, open: false });
|
|
|
|
|
if (timerRef.current !== null) {
|
|
|
|
|
clearTimeout(timerRef.current);
|
|
|
|
|
}
|
|
|
|
|
timerRef.current = window.setTimeout(() => {
|
|
|
|
|
setToast({ open: true, message, severity, showIcon });
|
|
|
|
|
}, timeoutDuration);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
toast,
|
|
|
|
|
onOpenChange: (open: boolean) => setToast({ ...toast, open }),
|
|
|
|
|
showToast,
|
|
|
|
|
};
|
|
|
|
|
}
|