UNPKG

mod-arch-core

Version:

Core functionality and API utilities for modular architecture micro-frontend projects

32 lines 1.87 kB
import * as React from 'react'; import { useBrowserStorage } from '../context'; import { logout } from '../utilities/appUtils'; const useTimeBasedRefresh = () => { const KEY_NAME = 'kf.dashboard.last.auto.refresh'; const [lastRefreshTimestamp, setLastRefreshTimestamp] = useBrowserStorage(KEY_NAME, '0', false, true); const ref = React.useRef({ lastRefreshTimestamp, setLastRefreshTimestamp }); ref.current = { lastRefreshTimestamp, setLastRefreshTimestamp }; return React.useCallback((refreshDateMarker) => { // Intentionally avoid referential changes. We want the value at call time. // Recomputing the ref is not needed and will impact usage in hooks if it does. const lastDate = new Date(ref.current.lastRefreshTimestamp); const setNewDateString = ref.current.setLastRefreshTimestamp; /* eslint-disable no-console */ // Print into the console in case we are not refreshing or the browser has preserve log enabled console.warn('Attempting to re-trigger an auto refresh'); console.log('Last refresh was on:', lastDate); console.log('Refreshing requested after:', refreshDateMarker); lastDate.setHours(lastDate.getHours() + 1); if (lastDate < refreshDateMarker) { setNewDateString(refreshDateMarker.toString()); console.log('Logging out and refreshing'); logout().then(() => window.location.reload()); } else { console.error(`We should have refreshed but it appears the last time we auto-refreshed was less than an hour ago. '${KEY_NAME}' session storage setting can be cleared for this to refresh again within the hour from the last refresh.`); } /* eslint-enable no-console */ }, []); }; export default useTimeBasedRefresh; //# sourceMappingURL=useTimeBasedRefresh.js.map