2023-08-17 19:56:18 +10:00
|
|
|
import { useCallback, useEffect } from 'react';
|
|
|
|
|
|
2025-01-02 15:33:37 +11:00
|
|
|
import { useRevalidator } from 'react-router';
|
2023-08-17 19:56:18 +10:00
|
|
|
|
|
|
|
|
export const RefreshOnFocus = () => {
|
2025-02-25 15:14:45 +11:00
|
|
|
const { revalidate, state } = useRevalidator();
|
2023-08-17 19:56:18 +10:00
|
|
|
|
|
|
|
|
const onFocus = useCallback(() => {
|
2025-02-25 15:14:45 +11:00
|
|
|
if (state === 'idle') {
|
|
|
|
|
void revalidate();
|
|
|
|
|
}
|
2025-01-02 15:33:37 +11:00
|
|
|
}, [revalidate]);
|
2023-08-17 19:56:18 +10:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener('focus', onFocus);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('focus', onFocus);
|
|
|
|
|
};
|
|
|
|
|
}, [onFocus]);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|