Skip to content

Commit

Permalink
updates: switch strategies to defer
Browse files Browse the repository at this point in the history
  • Loading branch information
arthyn committed Feb 9, 2024
1 parent a2dd66e commit 817e09a
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion ui/src/logic/useAppUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createContext, useCallback } from 'react';
import useKilnState, { usePike } from '@/state/kiln';
import { createContext, useCallback, useEffect, useState } from 'react';
import { useRegisterSW } from 'virtual:pwa-register/react';

const CHECK_FOR_UPDATES_INTERVAL = 10 * 60 * 1000; // 10 minutes
Expand Down Expand Up @@ -42,6 +43,30 @@ function useServiceWorker() {

export default function useAppUpdates() {
const { needRefresh, updateServiceWorker } = useServiceWorker();
const pike = usePike('groups');

const [needsUpdate, setNeedsUpdate] = useState(false);
const [initialHash, setInitialHash] = useState<string | null>(null);

useEffect(() => {
const interval = setInterval(() => {
useKilnState.getState().fetchPikes();
}, CHECK_FOR_UPDATES_INTERVAL);

return () => clearInterval(interval);
}, []);

useEffect(() => {
if (pike) {
if (!initialHash) {
setInitialHash(pike.hash);
} else if (initialHash !== pike.hash && !needsUpdate) {
// wait 5 minutes before showing the update prompt in case there
// are multiple updates in quick succession
setTimeout(() => setNeedsUpdate(true), 5 * 60 * 1000);
}
}
}, [pike, initialHash, needsUpdate]);

const triggerUpdate = useCallback(
async (returnToRoot: boolean) => {
Expand Down

0 comments on commit 817e09a

Please sign in to comment.