From fb6545e1863ac50cfc9147ec728ca261fd8cac71 Mon Sep 17 00:00:00 2001
From: Kit La Touche <kit@transneptune.net>
Date: Wed, 23 Jul 2025 21:54:33 -0400
Subject: Clean up proof of concept a bit

---
 ui/routes/+layout.svelte | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

(limited to 'ui/routes/+layout.svelte')

diff --git a/ui/routes/+layout.svelte b/ui/routes/+layout.svelte
index a1f8d84..9089c7e 100644
--- a/ui/routes/+layout.svelte
+++ b/ui/routes/+layout.svelte
@@ -11,15 +11,10 @@
   function doSubscribe() {
     navigator.serviceWorker.ready
       .then(async(registration) => {
-        console.debug("Beginning subscription");
         const response = await fetch("/api/vapid");
         // and if we fail to get it?
         const vapidPublicKey = await response.text();
-        // Chrome doesn't accept the base64-encoded (string) vapidPublicKey yet
-        // urlBase64ToUint8Array() is defined in /tools.js
-        // const convertedVapidKey = urlBase64ToUint8Array(vapidPublicKey);
         const convertedVapidKey = vapidPublicKey;
-        // Subscribe the user
         return registration.pushManager.subscribe({
           userVisibleOnly: true,
           applicationServerKey: convertedVapidKey
@@ -41,7 +36,7 @@
       }).then((subscription) => {
         return subscription.unsubscribe()
           .then(function() {
-            console.log("Unsubscribed", subscription.endpoint);
+            subscriptionJson = null;
             return fetch("/api/unregister", {
               method: "post",
               headers: { "Content-type": "application/json" },
@@ -51,6 +46,14 @@
       });
   }
 
+  function toggleSub() {
+    if (subscriptionJson !== null) {
+      doUnsubscribe();
+    } else {
+      doSubscribe();
+    }
+  }
+
   function notifyMe() {
     fetch("/api/echo", {
       method: "post",
@@ -67,9 +70,7 @@
       return registration.pushManager.getSubscription();
     }).then((subscription) => {
       if (subscription) {
-        console.debug("Already subscribed", subscription.endpoint);
-      } else {
-        doSubscribe();
+        subscriptionJson = JSON.parse(JSON.stringify(subscription));
       }
     });
   });
@@ -103,9 +104,12 @@
   <button onclick="{notifyMe}">
     notify
   </button>
-  <button onclick="{doUnsubscribe}">
-    unsub
+  <button onclick="{toggleSub}">
+    toggle sub
   </button>
+  <div>
+    hasSub: {subscriptionJson !== null}
+  </div>
   <div class="trail">
     {#if session}
       <div>
-- 
cgit v1.2.3

