mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-12 22:11:06 -05:00
29 lines
765 B
TypeScript
29 lines
765 B
TypeScript
// TODO: could be improved e.g. don't refresh when group has expired
|
|
|
|
import { useRevalidator } from "@remix-run/react";
|
|
import { useVisibilityChange } from "./useVisibilityChange";
|
|
import * as React from "react";
|
|
|
|
// or we got new data in the last 20 seconds
|
|
export function useAutoRefresh() {
|
|
const { revalidate } = useRevalidator();
|
|
const visibility = useVisibilityChange();
|
|
|
|
React.useEffect(() => {
|
|
// when user comes back to this tab
|
|
if (visibility === "visible") {
|
|
revalidate();
|
|
}
|
|
|
|
// ...as well as every 20 seconds
|
|
const interval = setInterval(() => {
|
|
if (visibility === "hidden") return;
|
|
revalidate();
|
|
}, 20 * 1000);
|
|
|
|
return () => {
|
|
clearInterval(interval);
|
|
};
|
|
}, [visibility, revalidate]);
|
|
}
|