Skip to content

Commit 153679c

Browse files
authored
Guard against the 'connection issue' flash not closing upon reconnection (#2383)
This approach is heavily inspired by Live Beats. I've removed the `#server-error` flash as it was sometimes caught in a race condition with the `#client-error` flash, which already covers the cases we care about.
1 parent a64cbee commit 153679c

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

assets/ui-rework/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ import dates from "../js/dates"
2727

2828
TimeAgo.addDefaultLocale(en)
2929

30+
let execJS = (selector, attr) => {
31+
document
32+
.querySelectorAll(selector)
33+
.forEach(el => liveSocket.execJS(el, el.getAttribute(attr)))
34+
}
35+
3036
let csrfToken = document
3137
.querySelector("meta[name='csrf-token']")
3238
.getAttribute("content")
@@ -81,6 +87,11 @@ window.addEventListener("phx:page-loading-stop", () => {
8187
topbar.hide()
8288
})
8389

90+
// borrowed from https://github.com/fly-apps/live_beats/blob/master/assets/js/app.js#L330
91+
// this guards against the flash not hiding after reconnection, possibly due to the browser
92+
// not passing along js events.
93+
liveSocket.getSocket().onOpen(() => execJS("#connection-status", "js-hide"))
94+
liveSocket.getSocket().onError(() => execJS("#connection-status", "js-show"))
8495
liveSocket.connect()
8596

8697
// expose liveSocket on window for web console debug logs and latency simulation:

lib/nerves_hub_web/components/core_components.ex

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule NervesHubWeb.CoreComponents do
1414
1515
Icons are provided by [heroicons](https://heroicons.com). See `icon/1` for usage.
1616
"""
17-
use Phoenix.Component
17+
use Phoenix.Component, global_prefixes: ~w(js-)
1818

1919
import NervesHubWeb.Components.Icons
2020

@@ -139,12 +139,17 @@ defmodule NervesHubWeb.CoreComponents do
139139
<div id={@id}>
140140
<.flash kind={:info} title={gettext("Success")} flash={@flash} phx-mounted={show("#flash-info")} phx-hook="Flash" hidden />
141141
<.flash kind={:error} title={gettext("Error")} flash={@flash} phx-mounted={show("#flash-error")} hidden />
142-
<.flash id="client-error" kind={:error} title={gettext("We can't find the internet")} phx-disconnected={show(".phx-client-error #client-error")} phx-connected={hide("#client-error")} hidden>
143-
{gettext("Attempting to reconnect")}
144-
</.flash>
145142
146-
<.flash id="server-error" kind={:error} title={gettext("Something went wrong!")} phx-disconnected={show(".phx-server-error #server-error")} phx-connected={hide("#server-error")} hidden>
147-
{gettext("Hang in there while we get back on track")}
143+
<.flash
144+
id="connection-status"
145+
kind={:error}
146+
title={gettext("Internet connection lost")}
147+
phx-disconnected={show("#connection-status")}
148+
js-hide={hide("#connection-status")}
149+
js-show={show("#connection-status")}
150+
hidden
151+
>
152+
{gettext("Attempting to reconnect...")}
148153
</.flash>
149154
</div>
150155
"""

0 commit comments

Comments
 (0)