@helpwave/hightide
Version:
helpwave's component and theming library
43 lines • 1.27 kB
JavaScript
// src/hooks/useSaveDelay.ts
import { useEffect, useState } from "react";
function useSaveDelay(setNotificationStatus, delay) {
const [updateTimer, setUpdateTimer] = useState(void 0);
const [notificationTimer, setNotificationTimer] = useState(void 0);
const restartTimer = (onSave) => {
clearTimeout(updateTimer);
setUpdateTimer(setTimeout(() => {
onSave();
setNotificationStatus(true);
clearTimeout(notificationTimer);
setNotificationTimer(setTimeout(() => {
setNotificationStatus(false);
clearTimeout(notificationTimer);
}, delay));
clearTimeout(updateTimer);
}, delay));
};
const clearUpdateTimer = (hasSaved = true) => {
clearTimeout(updateTimer);
if (hasSaved) {
setNotificationStatus(true);
clearTimeout(notificationTimer);
setNotificationTimer(setTimeout(() => {
setNotificationStatus(false);
clearTimeout(notificationTimer);
}, delay));
} else {
setNotificationStatus(false);
}
};
useEffect(() => {
return () => {
clearTimeout(updateTimer);
clearTimeout(notificationTimer);
};
}, []);
return { restartTimer, clearUpdateTimer };
}
export {
useSaveDelay
};
//# sourceMappingURL=useSaveDelay.mjs.map