Skip to content

Commit

Permalink
PWA: remove useServerAttributePreference and shorten the line/variabl…
Browse files Browse the repository at this point in the history
…e a bit
  • Loading branch information
jinzo committed Nov 6, 2023
1 parent 93b8ec8 commit 85d5322
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
8 changes: 4 additions & 4 deletions modern/src/UpdateController.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Snackbar, IconButton } from '@mui/material';
import RefreshIcon from '@mui/icons-material/Refresh';
import React from 'react'
import { useSelector } from 'react-redux';
import { useTranslation } from './common/components/LocalizationProvider';
import { useServerAttributePreference } 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 = useServerAttributePreference('serviceWorkerUpdateInterval', 3600000);
const swUpdateInterval = useSelector((state) => state.session.server.attributes.serviceWorkerUpdateInterval || 3600000);

const {
needRefresh: [needRefresh],
updateServiceWorker,
} = useRegisterSW({
onRegisteredSW(swUrl, swRegistration) {
if (serviceWorkerUpdateInterval > 0 && swRegistration) {
if (swUpdateInterval > 0 && swRegistration) {
setInterval(async () => {
if (!(!swRegistration.installing && navigator)) {
return;
Expand All @@ -37,7 +37,7 @@ function UpdateController() {
if (newSW?.status === 200) {
await swRegistration.update();
}
}, serviceWorkerUpdateInterval);
}, swUpdateInterval);
}
}
});
Expand Down
7 changes: 0 additions & 7 deletions modern/src/common/util/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,3 @@ export const useAttributePreference = (key, defaultValue) => useSelector((state)
}
return defaultValue;
});

export const useServerAttributePreference = (key, defaultValue) => useSelector((state) => {
if (containsProperty(state.session.server.attributes, key)) {
return state.session.server.attributes[key];
}
return defaultValue;
});

0 comments on commit 85d5322

Please sign in to comment.