From e98a962071e4dd62f5e3f0e67003c3cb93539f1c Mon Sep 17 00:00:00 2001
From: Paul <paulmakles@gmail.com>
Date: Tue, 22 Jun 2021 15:43:06 +0100
Subject: [PATCH] Make sure to focus window for desktop notifs. Prevent empty
 pastes of files. Log push errors.

---
 .../common/messaging/MessageBox.tsx           |  2 +
 src/context/revoltjs/Notifications.tsx        |  1 +
 src/pages/settings/panes/Notifications.tsx    | 50 ++++++++++---------
 3 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/src/components/common/messaging/MessageBox.tsx b/src/components/common/messaging/MessageBox.tsx
index 491b4b6..e25ad13 100644
--- a/src/components/common/messaging/MessageBox.tsx
+++ b/src/components/common/messaging/MessageBox.tsx
@@ -263,6 +263,8 @@ function MessageBox({ channel, draft, dispatcher }: Props) {
                         onChange={files => setUploadState({ type: "attached", files })}
                         cancel={() => uploadState.type === 'uploading' && uploadState.cancel.cancel("cancel")}
                         append={files => {
+                            if (files.length === 0) return;
+
                             if (uploadState.type === 'none') {
                                 setUploadState({ type: 'attached', files });
                             } else if (uploadState.type === 'attached') {
diff --git a/src/context/revoltjs/Notifications.tsx b/src/context/revoltjs/Notifications.tsx
index 0845a62..009ba05 100644
--- a/src/context/revoltjs/Notifications.tsx
+++ b/src/context/revoltjs/Notifications.tsx
@@ -136,6 +136,7 @@ function Notifier(props: Props) {
 
         if (notif) {
             notif.addEventListener("click", () => {
+                window.focus();
                 const id = msg.channel;
                 if (id !== channel_id) {
                     let channel = client.channels.get(id);
diff --git a/src/pages/settings/panes/Notifications.tsx b/src/pages/settings/panes/Notifications.tsx
index 37529e4..2ba745a 100644
--- a/src/pages/settings/panes/Notifications.tsx
+++ b/src/pages/settings/panes/Notifications.tsx
@@ -62,32 +62,36 @@ export function Component(props: Props & WithDispatcher) {
                 disabled={typeof pushEnabled === "undefined"}
                 checked={pushEnabled ?? false}
                 onChange={async pushEnabled => {
-                    const reg = await navigator.serviceWorker?.getRegistration();
-                    if (reg) {
-                        if (pushEnabled) {
-                            const sub = await reg.pushManager.subscribe({
-                                userVisibleOnly: true,
-                                applicationServerKey: urlBase64ToUint8Array(
-                                    client.configuration!.vapid
-                                )
-                            });
+                    try {
+                        const reg = await navigator.serviceWorker?.getRegistration();
+                        if (reg) {
+                            if (pushEnabled) {
+                                const sub = await reg.pushManager.subscribe({
+                                    userVisibleOnly: true,
+                                    applicationServerKey: urlBase64ToUint8Array(
+                                        client.configuration!.vapid
+                                    )
+                                });
 
-                            // tell the server we just subscribed
-                            const json = sub.toJSON();
-                            if (json.keys) {
-                                client.req("POST", "/push/subscribe", {
-                                    endpoint: sub.endpoint,
-                                    ...json.keys
-                                } as any);
-                                setPushEnabled(true);
-                            }
-                        } else {
-                            const sub = await reg.pushManager.getSubscription();
-                            sub?.unsubscribe();
-                            setPushEnabled(false);
+                                // tell the server we just subscribed
+                                const json = sub.toJSON();
+                                if (json.keys) {
+                                    client.req("POST", "/push/subscribe", {
+                                        endpoint: sub.endpoint,
+                                        ...json.keys
+                                    } as any);
+                                    setPushEnabled(true);
+                                }
+                            } else {
+                                const sub = await reg.pushManager.getSubscription();
+                                sub?.unsubscribe();
+                                setPushEnabled(false);
 
-                            client.req("POST", "/push/unsubscribe");
+                                client.req("POST", "/push/unsubscribe");
+                            }
                         }
+                    } catch (err) {
+                        console.error('Failed to enable push!', err);
                     }
                 }}
             >
-- 
GitLab