From a0d9e5c672205253a77e693296014dacda543c48 Mon Sep 17 00:00:00 2001
From: Dessalines <dessalines@users.noreply.github.com>
Date: Sat, 17 Jul 2021 11:38:28 -0400
Subject: [PATCH] Fix/ws error messages (#334)

* Updating translations.

* Translation updates

* Adding websocket error and reconnect toasts. Fixes #129
---
 lemmy-translations                      |  2 +-
 src/shared/components/navbar.tsx        | 15 +++++++++++++--
 src/shared/services/WebSocketService.ts |  4 ++++
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/lemmy-translations b/lemmy-translations
index 6dd3968..4c122b5 160000
--- a/lemmy-translations
+++ b/lemmy-translations
@@ -1 +1 @@
-Subproject commit 6dd3968a5e2da60884fe783a90e366bae7a96cc2
+Subproject commit 4c122b5bed79e291957b9d913dd361a75b583271
diff --git a/src/shared/components/navbar.tsx b/src/shared/components/navbar.tsx
index 6870c17..0af55dd 100644
--- a/src/shared/components/navbar.tsx
+++ b/src/shared/components/navbar.tsx
@@ -81,6 +81,8 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
   componentDidMount() {
     // Subscribe to jwt changes
     if (isBrowser()) {
+      this.websocketEvents();
+
       this.searchTextField = createRef();
       console.log(`isLoggedIn = ${this.state.isLoggedIn}`);
 
@@ -393,6 +395,7 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
       }
       return;
     } else if (msg.reconnect) {
+      toast(i18n.t("websocket_reconnected"), "success");
       WebSocketService.Instance.send(
         wsClient.userJoin({
           auth: authField(),
@@ -530,11 +533,19 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
   }
 
   get canCreateCommunity(): boolean {
-    let adminOnly = this.props.site_res.site_view?.site
-      .community_creation_admin_only;
+    let adminOnly =
+      this.props.site_res.site_view?.site.community_creation_admin_only;
     return !adminOnly || this.canAdmin;
   }
 
+  /// Listens for some websocket errors
+  websocketEvents() {
+    let msg = i18n.t("websocket_disconnected");
+    WebSocketService.Instance.closeEventListener(() => {
+      toast(msg, "danger");
+    });
+  }
+
   requestNotificationPermission() {
     if (UserService.Instance.localUserView) {
       document.addEventListener("DOMContentLoaded", function () {
diff --git a/src/shared/services/WebSocketService.ts b/src/shared/services/WebSocketService.ts
index 4c91d40..3cb3748 100644
--- a/src/shared/services/WebSocketService.ts
+++ b/src/shared/services/WebSocketService.ts
@@ -56,6 +56,10 @@ export class WebSocketService {
     this.ws.send(data);
   }
 
+  public closeEventListener(listener: (event: CloseEvent) => void) {
+    this.ws.addEventListener("close", listener);
+  }
+
   public static get Instance() {
     return this._instance || (this._instance = new this());
   }
-- 
2.44.1