From f81bf48cb9cd3541bf8f111770fdd42c4d8e0955 Mon Sep 17 00:00:00 2001
From: Matjaž Črnko <m.crnko@txt.si>
Date: Mon, 6 Nov 2023 15:42:28 +0100
Subject: PWA: Rename to UpdateController

---
 modern/src/UpdateController.tsx | 58 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
 create mode 100644 modern/src/UpdateController.tsx

(limited to 'modern/src/UpdateController.tsx')

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;
-- 
cgit v1.2.3


From 41b513e0245e5c354508ef26e1a4ec89e67e7598 Mon Sep 17 00:00:00 2001
From: Matjaž Črnko <m.crnko@txt.si>
Date: Mon, 6 Nov 2023 15:48:27 +0100
Subject: PWA: use only server attributes for serviceWorkerUpdateInterval

---
 modern/src/UpdateController.tsx       | 4 ++--
 modern/src/common/util/preferences.js | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)

(limited to 'modern/src/UpdateController.tsx')

diff --git a/modern/src/UpdateController.tsx b/modern/src/UpdateController.tsx
index 0f1b800b..991ea346 100644
--- a/modern/src/UpdateController.tsx
+++ b/modern/src/UpdateController.tsx
@@ -2,14 +2,14 @@ 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 { 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 = useAttributePreference('serviceWorkerUpdateInterval', 3600000);
+  const serviceWorkerUpdateInterval = useServerAttributePreference('serviceWorkerUpdateInterval', 3600000);
 
   const {
     needRefresh: [needRefresh],
diff --git a/modern/src/common/util/preferences.js b/modern/src/common/util/preferences.js
index 229b6f17..0604d848 100644
--- a/modern/src/common/util/preferences.js
+++ b/modern/src/common/util/preferences.js
@@ -39,3 +39,10 @@ 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;
+});
\ No newline at end of file
-- 
cgit v1.2.3


From 85d5322a78332175fd870e588469fc9653fa8257 Mon Sep 17 00:00:00 2001
From: Matjaž Črnko <m.crnko@txt.si>
Date: Mon, 6 Nov 2023 16:27:26 +0100
Subject: PWA: remove useServerAttributePreference and shorten the
 line/variable a bit

---
 modern/src/UpdateController.tsx       | 8 ++++----
 modern/src/common/util/preferences.js | 7 -------
 2 files changed, 4 insertions(+), 11 deletions(-)

(limited to 'modern/src/UpdateController.tsx')

diff --git a/modern/src/UpdateController.tsx b/modern/src/UpdateController.tsx
index 991ea346..0b2b7985 100644
--- a/modern/src/UpdateController.tsx
+++ b/modern/src/UpdateController.tsx
@@ -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;
@@ -37,7 +37,7 @@ function UpdateController() {
           if (newSW?.status === 200) {
             await swRegistration.update();
           }
-        }, serviceWorkerUpdateInterval);
+        }, swUpdateInterval);
       }
     }
   });
diff --git a/modern/src/common/util/preferences.js b/modern/src/common/util/preferences.js
index 54dfbada..229b6f17 100644
--- a/modern/src/common/util/preferences.js
+++ b/modern/src/common/util/preferences.js
@@ -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;
-});
-- 
cgit v1.2.3

