mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-12-15 08:48:53 +00:00
[Glitch] Refresh thread replies periodically & when refocusing window
Port 7ea2af6ae2 to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
39
app/javascript/flavours/glitch/hooks/useInterval.ts
Normal file
39
app/javascript/flavours/glitch/hooks/useInterval.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { useEffect, useLayoutEffect, useRef } from 'react';
|
||||
|
||||
/**
|
||||
* Hook to create an interval that invokes a callback function
|
||||
* at a specified delay using the setInterval API.
|
||||
* Based on https://usehooks-ts.com/react-hook/use-interval
|
||||
*/
|
||||
export function useInterval(
|
||||
callback: () => void,
|
||||
{
|
||||
delay,
|
||||
isEnabled = true,
|
||||
}: {
|
||||
delay: number;
|
||||
isEnabled?: boolean;
|
||||
},
|
||||
) {
|
||||
// Write callback to a ref so we can omit it from
|
||||
// the interval effect's dependency array
|
||||
const callbackRef = useRef(callback);
|
||||
useLayoutEffect(() => {
|
||||
callbackRef.current = callback;
|
||||
}, [callback]);
|
||||
|
||||
// Set up the interval.
|
||||
useEffect(() => {
|
||||
if (!isEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const intervalId = setInterval(() => {
|
||||
callbackRef.current();
|
||||
}, delay);
|
||||
|
||||
return () => {
|
||||
clearInterval(intervalId);
|
||||
};
|
||||
}, [delay, isEnabled]);
|
||||
}
|
||||
Reference in New Issue
Block a user