diff options
author | Anton Tananaev <anton@traccar.org> | 2024-04-06 09:22:10 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2024-04-06 09:22:10 -0700 |
commit | f418231b6b2f5e030a0d2dcc390c314602b1f740 (patch) | |
tree | 10326adf3792bc2697e06bb5f2b8ef2a8f7e55fe /src/UpdateController.jsx | |
parent | b392a4af78e01c8e0f50aad5468e9583675b24be (diff) | |
download | trackermap-web-f418231b6b2f5e030a0d2dcc390c314602b1f740.tar.gz trackermap-web-f418231b6b2f5e030a0d2dcc390c314602b1f740.tar.bz2 trackermap-web-f418231b6b2f5e030a0d2dcc390c314602b1f740.zip |
Move modern to the top
Diffstat (limited to 'src/UpdateController.jsx')
-rw-r--r-- | src/UpdateController.jsx | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/UpdateController.jsx b/src/UpdateController.jsx new file mode 100644 index 00000000..80ca6dc2 --- /dev/null +++ b/src/UpdateController.jsx @@ -0,0 +1,58 @@ +import { Snackbar, IconButton } from '@mui/material'; +import RefreshIcon from '@mui/icons-material/Refresh'; +import React from 'react'; +import { useSelector } from 'react-redux'; +import { useRegisterSW } from 'virtual:pwa-register/react'; +import { useTranslation } from './common/components/LocalizationProvider'; + +// Based on https://vite-pwa-org.netlify.app/frameworks/react.html +const UpdateController = () => { + const t = useTranslation(); + + const swUpdateInterval = useSelector((state) => state.session.server.attributes.serviceWorkerUpdateInterval || 3600000); + + const { + needRefresh: [needRefresh], + updateServiceWorker, + } = useRegisterSW({ + onRegisteredSW(swUrl, swRegistration) { + if (swUpdateInterval > 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(); + } + }, swUpdateInterval); + } + }, + }); + + return ( + <Snackbar + open={needRefresh} + message={t('settingsUpdateAvailable')} + action={( + <IconButton color="inherit" onClick={() => updateServiceWorker(true)}> + <RefreshIcon /> + </IconButton> + )} + /> + ); +}; + +export default UpdateController; |