diff options
author | Matjaž Črnko <m.crnko@txt.si> | 2023-11-06 15:42:28 +0100 |
---|---|---|
committer | Matjaž Črnko <m.crnko@txt.si> | 2023-11-06 15:42:28 +0100 |
commit | f81bf48cb9cd3541bf8f111770fdd42c4d8e0955 (patch) | |
tree | 5616dcf81b6378dc1c5b9a569f30766e808431aa /modern/src/UpdateController.tsx | |
parent | ee802369c95cc820a12ae62b401e17a19a073311 (diff) | |
download | trackermap-web-f81bf48cb9cd3541bf8f111770fdd42c4d8e0955.tar.gz trackermap-web-f81bf48cb9cd3541bf8f111770fdd42c4d8e0955.tar.bz2 trackermap-web-f81bf48cb9cd3541bf8f111770fdd42c4d8e0955.zip |
PWA: Rename to UpdateController
Diffstat (limited to 'modern/src/UpdateController.tsx')
-rw-r--r-- | modern/src/UpdateController.tsx | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/modern/src/UpdateController.tsx b/modern/src/UpdateController.tsx new file mode 100644 index 00000000..0f1b800b --- /dev/null +++ b/modern/src/UpdateController.tsx @@ -0,0 +1,58 @@ +import { Snackbar, IconButton } from '@mui/material'; +import RefreshIcon from '@mui/icons-material/Refresh'; +import React from 'react' +import { useTranslation } from './common/components/LocalizationProvider'; +import { useAttributePreference } from './common/util/preferences'; +import { useRegisterSW } from 'virtual:pwa-register/react' + +// Based on https://vite-pwa-org.netlify.app/frameworks/react.html +function UpdateController() { + const t = useTranslation(); + + const serviceWorkerUpdateInterval = useAttributePreference('serviceWorkerUpdateInterval', 3600000); + + const { + needRefresh: [needRefresh], + updateServiceWorker, + } = useRegisterSW({ + onRegisteredSW(swUrl, swRegistration) { + if (serviceWorkerUpdateInterval > 0 && swRegistration) { + setInterval(async () => { + if (!(!swRegistration.installing && navigator)) { + return; + } + + if (('connection' in navigator) && !navigator.onLine) { + return; + } + + const newSW = await fetch(swUrl, { + cache: 'no-store', + headers: { + 'cache': 'no-store', + 'cache-control': 'no-cache', + }, + }); + + if (newSW?.status === 200) { + await swRegistration.update(); + } + }, serviceWorkerUpdateInterval); + } + } + }); + + return ( + <Snackbar + open={needRefresh} + message={t('settingsUpdateAvailable')} + action={( + <IconButton color="inherit" onClick={() => updateServiceWorker(true)}> + <RefreshIcon /> + </IconButton> + )} + /> + ); +} + +export default UpdateController; |