From 7aa3d8f59bab59bed6c14fb49ecf878e00b08f13 Mon Sep 17 00:00:00 2001
From: nimbleghost <132819643+nimbleghost@users.noreply.github.com>
Date: Wed, 31 May 2023 18:43:06 +0200
Subject: [PATCH] Hide web push toggles if disabled on server

---
 web/src/app/Notifier.js                | 2 +-
 web/src/components/SubscribeDialog.jsx | 7 +++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/web/src/app/Notifier.js b/web/src/app/Notifier.js
index 0ace7b39..73bb51ac 100644
--- a/web/src/app/Notifier.js
+++ b/web/src/app/Notifier.js
@@ -115,7 +115,7 @@ class Notifier {
   }
 
   pushSupported() {
-    return "serviceWorker" in navigator && "PushManager" in window;
+    return config.enable_web_push && "serviceWorker" in navigator && "PushManager" in window;
   }
 
   /**
diff --git a/web/src/components/SubscribeDialog.jsx b/web/src/components/SubscribeDialog.jsx
index 57281661..60b69863 100644
--- a/web/src/components/SubscribeDialog.jsx
+++ b/web/src/components/SubscribeDialog.jsx
@@ -103,6 +103,7 @@ const SubscribeDialog = (props) => {
 const browserNotificationsSupported = notifier.supported();
 const pushNotificationsSupported = notifier.pushSupported();
 const iosInstallRequired = notifier.iosSupportedButInstallRequired();
+const pushPossible = pushNotificationsSupported && iosInstallRequired;
 
 const getNotificationTypeFromToggles = (browserNotificationsEnabled, backgroundNotificationsEnabled) => {
   if (backgroundNotificationsEnabled) {
@@ -138,12 +139,14 @@ const SubscribePage = (props) => {
   const [notificationsExplicitlyDenied, setNotificationsExplicitlyDenied] = useState(notifier.denied());
   // default to on if notifications are already granted
   const [browserNotificationsEnabled, setBrowserNotificationsEnabled] = useState(notifier.granted());
-  const [backgroundNotificationsEnabled, setBackgroundNotificationsEnabled] = useState(props.webPushDefaultEnabled === "enabled");
+  const [backgroundNotificationsEnabled, setBackgroundNotificationsEnabled] = useState(
+    pushPossible && props.webPushDefaultEnabled === "enabled"
+  );
 
   const handleBrowserNotificationsChanged = async (e) => {
     if (e.target.checked && (await notifier.maybeRequestPermission())) {
       setBrowserNotificationsEnabled(true);
-      if (props.webPushDefaultEnabled === "enabled") {
+      if (pushPossible && props.webPushDefaultEnabled === "enabled") {
         setBackgroundNotificationsEnabled(true);
       }
     } else {