Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

SSR: Add admin UI global notices and event-stream toasts.

Kailash Nadh66374aa6ecb9
diff --git a/frontend-vue/feathericons/icons.txt b/frontend-vue/feathericons/icons.txt
index 74c0c7e0..bebd9e7c 100644
--- a/frontend-vue/feathericons/icons.txt
+++ b/frontend-vue/feathericons/icons.txt
@@ -19,6 +19,7 @@ eye
file-text
grid
image
+info
link-2
list
log-out
diff --git a/static/admin/partials/global-notices.html b/static/admin/partials/global-notices.html
new file mode 100644
index 00000000..c9e3ed66
--- /dev/null
+++ b/static/admin/partials/global-notices.html
@@ -0,0 +1,64 @@
+{{ define "admin-global-notices" }}
+ {{- /* Upstream update notices: a new release and/or broadcast messages. */ -}}
+ {{- $hasUpdate := false }}
+ {{- with .ConfigJS.Update }}{{ if or .Update.IsNew .Messages }}{{ $hasUpdate = true }}{{ end }}{{ end }}
+
+ {{- if or .ConfigJS.NeedsRestart .ConfigJS.HasLegacyUser $hasUpdate }}
+ <div class="global-notices vstack gap-4 mb-8">
+ {{- if .ConfigJS.NeedsRestart }}
+ <div role="alert" data-cy="needs-restart" class="hstack justify-between">
+ <span>{{ .L.T "settings.needsRestart" }}</span>
+ {{- if .Data.Can "settings:manage" }}
+ <button type="button" class="small" @click="restartApp">{{ Icon "refresh-cw" }} {{ .L.T "settings.restart" }}</button>
+ {{- end }}
+ </div>
+ {{- end }}
+
+ {{- with .ConfigJS.Update }}
+ {{- if .Update.IsNew }}
+ {{- $version := .Update.ReleaseVersion }}
+ {{- $date := toDate "2006-01-02" .Update.ReleaseDate }}
+ {{- if not $date.IsZero }}{{ $version = printf "%s (%s)" $version ($date.Format "02 Jan 2006") }}{{ end }}
+ <div role="alert" data-cy="update-available">
+ <div class="row">
+ <div class="col-10">{{ Icon "download-cloud" }} {{ $.L.Ts "settings.updateAvailable" "version" $version }}</div>
+ <div class="col-2 hstack justify-end">
+ <a href="{{ .Update.URL }}" target="_blank" rel="noopener noreferrer" class="button small" data-variant="secondary">{{ $.L.T "globals.buttons.view" }}</a>
+ </div>
+ </div>
+ </div>
+ {{- end }}
+
+ {{- range .Messages }}
+ {{ $high := (eq .Priority "high") }}
+ <div role="alert" data-cy="update-message" {{ if $high }}data-variant="danger"{{ end }}>
+ <div class="row">
+ <div class="col-10">
+ {{ if $high }}{{ Icon "alert-triangle" }}{{ else }}{{ Icon "info" }}{{ end }}
+ {{ if .Title }}<strong>{{ .Title }}</strong>{{ end }}
+ {{- if .Description }}<div>{{ .Description }}</div>{{ end }}
+ </div>
+ <div class="col-2 hstack justify-end">
+ {{- if .URL }}
+ <a href="{{ .URL }}" target="_blank" rel="noopener noreferrer" class="button small" data-variant="secondary">{{ $.L.T "globals.buttons.view" }}</a>
+ {{- end }}
+ </div>
+ </div>
+ </div>
+ {{- end }}
+ {{- end }}
+
+ {{- if .ConfigJS.HasLegacyUser }}
+ <div role="alert" data-cy="legacy-user">
+ <div class="row">
+ <div class="col-10">{{ .L.T "settings.hasLegacyUser" }}</div>
+ <div class="col-2 hstack justify-end">
+ <a href="https://listmonk.app/docs/upgrade/#upgrading-to-v4xx" target="_blank" rel="noopener noreferrer">{{ .L.T "globals.buttons.learnMore" }}</a>
+ <a href="{{ URI "/admin/users" }}" class="button small" data-variant="secondary">{{ .L.T "globals.terms.users" }}</a>
+ </div>
+ </div>
+ </div>
+ {{- end }}
+ </div>
+ {{- end }}
+{{ end }}
diff --git a/static/admin/src/css/style.css b/static/admin/src/css/style.css
index 80f946fa..65dcebe4 100644
--- a/static/admin/src/css/style.css
+++ b/static/admin/src/css/style.css
@@ -631,12 +631,6 @@ main > .container {
text-overflow: ellipsis;
}
-.global-notices {
- display: grid;
- gap: var(--space-2);
- margin-block-end: var(--space-4);
-}
-
.spaced-links {
display: flex;
flex-wrap: wrap;
diff --git a/static/admin/src/js/main.js b/static/admin/src/js/main.js
index 209a9a75..f624b55a 100644
--- a/static/admin/src/js/main.js
+++ b/static/admin/src/js/main.js
@@ -52,6 +52,43 @@ function setupAlpine() {
}));
}
+// Subscribe to the server's error event stream and toast server-side errors as
+// they happen. The stream is only advertised (data-error-events on <body>) to
+// users who have permission to read it.
+function listenErrorEvents() {
+ if (!document.body.hasAttribute('data-error-events')) {
+ return;
+ }
+
+ // Extract the message out of `file.go:123: message` log lines.
+ const reMatchLog = /(.+?)\.go:\d+:(.+?)$/im;
+ const src = new EventSource(`${urls.api}/events?type=error`, { withCredentials: true });
+
+ let numEv = 0;
+ src.onmessage = (e) => {
+ // Cap the number of toasts to not flood the UI on a burst of errors.
+ if (numEv >= 50) {
+ src.close();
+ return;
+ }
+ numEv += 1;
+
+ let d = null;
+ try {
+ d = JSON.parse(e.data);
+ } catch {
+ return;
+ }
+
+ if (!d || d.type !== 'error' || !d.message) {
+ return;
+ }
+
+ const msg = reMatchLog.exec(d.message.trim());
+ u.toast(msg ? msg[2].trim() : d.message, 'error');
+ };
+}
+
// Return the global Alpine store.
function getStore() {
return window.Alpine.store(storeName);
@@ -153,4 +190,6 @@ export async function api(name, uri, method, data) {
// Init global Alpine component.
document.addEventListener('alpine:init', setupAlpine, { once: true });
+
+ listenErrorEvents();
})();
diff --git a/static/admin/src/static/icons.svg b/static/admin/src/static/icons.svg
index 31f12a73..aca7dfbb 100644
--- a/static/admin/src/static/icons.svg
+++ b/static/admin/src/static/icons.svg
@@ -83,6 +83,10 @@
<rect x="3" y="3" width="18" height="18" rx="2" ry="2" /><circle cx="8.5" cy="8.5" r="1.5" /><polyline points="21 15 16 10 5 21" />
</symbol>
+ <symbol id="icon-info" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
+ <circle cx="12" cy="12" r="10" /><line x1="12" y1="16" x2="12" y2="12" /><line x1="12" y1="8" x2="12.01" y2="8" />
+ </symbol>
+
<symbol id="icon-link-2" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3" /><line x1="8" y1="12" x2="16" y2="12" />
</symbol>
diff --git a/static/admin/views/base.html b/static/admin/views/base.html
index 175eec70..89c688da 100644
--- a/static/admin/views/base.html
+++ b/static/admin/views/base.html
@@ -25,7 +25,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
{{ template "admin-head-assets" . }}
</head>
-<body x-data="adminApp">
+<body x-data="adminApp"{{ if .Data.Can "settings:get" }} data-error-events{{ end }}>
<div id="app" data-sidebar-layout>
<nav data-topnav>
<div class="row">
@@ -69,23 +69,7 @@
<main>
<div class="container">
- {{- if .ConfigJS.NeedsRestart }}
- <div role="alert" data-cy="needs-restart" class="hstack justify-between">
- <span>{{ .L.T "settings.needsRestart" }}</span>
- {{- if .Data.Can "settings:manage" }}
- <button type="button" class="small" @click="restartApp">{{ Icon "refresh-cw" }} {{ .L.T "settings.restart" }}</button>
- {{- end }}
- </div>
- {{- end }}
- {{- with .ConfigJS.Update }}{{ if .Update.IsNew }}
- <div role="status">
- {{ $.L.Ts "settings.updateAvailable" "version" .Update.ReleaseVersion }}
- <a href="{{ .Update.URL }}" target="_blank" rel="noopener noreferrer">{{ $.L.T "globals.buttons.view" }}</a>
- </div>
- {{ end }}{{- end }}
- {{- if .ConfigJS.HasLegacyUser }}
- <div role="alert" data-cy="legacy-user">{{ .L.T "settings.hasLegacyUser" }}</div>
- {{- end }}
+ {{ template "admin-global-notices" . }}
{{ end }}
{{ define "admin-footer" }}