import { useState } from "react"; import usePolling from "../util/Polling"; export default function useLeaderboardApi() { const [leaderboard, setLeaderboard] = useState(null); const usePoll = (pollingReducer) => { const [polling, dispatchPolling] = pollingReducer; const mode = (polling.enabled === true) ? { interval_sec: 300 } // update leaderbord stats every 5 min : { interval_stop: true } // user has fliped OfflineToggel const isPolling = usePolling('/api/leaderboard', setLeaderboard, mode); if (isPolling !== polling.leaderboard) { dispatchPolling({ type: 'next', leaderboard: isPolling }); } return leaderboard; } return { poll: usePoll } }