From 1f49ee77dfb2fd778d6e85ab7da86a601a0a37d3 Mon Sep 17 00:00:00 2001 From: "Jens L." Date: Mon, 13 Jan 2025 22:40:48 +0100 Subject: [PATCH] web: improve notification and API drawers (#12659) * web: move clear all notification button to header, add empty state Signed-off-by: Jens Langhammer * improve sorting for API requests Signed-off-by: Jens Langhammer --------- Signed-off-by: Jens Langhammer --- web/src/common/api/middleware.ts | 2 + web/src/elements/notifications/APIDrawer.ts | 7 +- .../notifications/NotificationDrawer.ts | 80 ++++++++++--------- 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/web/src/common/api/middleware.ts b/web/src/common/api/middleware.ts index 8aee822c7a..2dcd412db5 100644 --- a/web/src/common/api/middleware.ts +++ b/web/src/common/api/middleware.ts @@ -12,6 +12,7 @@ import { export const CSRFHeaderName = "X-authentik-CSRF"; export interface RequestInfo { + time: number; method: string; path: string; status: number; @@ -47,6 +48,7 @@ export class CSRFMiddleware implements Middleware { export class EventMiddleware implements Middleware { post?(context: ResponseContext): Promise { const request: RequestInfo = { + time: new Date().getTime(), method: (context.init.method || "GET").toUpperCase(), path: context.url, status: context.response.status, diff --git a/web/src/elements/notifications/APIDrawer.ts b/web/src/elements/notifications/APIDrawer.ts index c460a327dc..436eeec2f5 100644 --- a/web/src/elements/notifications/APIDrawer.ts +++ b/web/src/elements/notifications/APIDrawer.ts @@ -1,6 +1,7 @@ import { RequestInfo } from "@goauthentik/common/api/middleware"; import { EVENT_API_DRAWER_TOGGLE, EVENT_REQUEST_POST } from "@goauthentik/common/constants"; import { globalAK } from "@goauthentik/common/global"; +import { getRelativeTime } from "@goauthentik/common/utils"; import { AKElement } from "@goauthentik/elements/Base"; import { msg } from "@lit/localize"; @@ -55,7 +56,8 @@ export class APIDrawer extends AKElement { constructor() { super(); window.addEventListener(EVENT_REQUEST_POST, ((e: CustomEvent) => { - this.requests.splice(0, 0, e.detail); + this.requests.push(e.detail); + this.requests.sort((a, b) => a.time - b.time).reverse(); if (this.requests.length > 50) { this.requests.shift(); } @@ -76,6 +78,9 @@ export class APIDrawer extends AKElement { href=${item.path} >${item.path} +
+ ${getRelativeTime(new Date(item.time))} +
`; } diff --git a/web/src/elements/notifications/NotificationDrawer.ts b/web/src/elements/notifications/NotificationDrawer.ts index 88c7ad5913..871565cc46 100644 --- a/web/src/elements/notifications/NotificationDrawer.ts +++ b/web/src/elements/notifications/NotificationDrawer.ts @@ -6,6 +6,7 @@ import { MessageLevel } from "@goauthentik/common/messages"; import { me } from "@goauthentik/common/users"; import { getRelativeTime } from "@goauthentik/common/utils"; import { AKElement } from "@goauthentik/elements/Base"; +import "@goauthentik/elements/EmptyState"; import { showMessage } from "@goauthentik/elements/messages/MessageContainer"; import { PaginatedResponse } from "@goauthentik/elements/table/Table"; import "@patternfly/elements/pf-tooltip/pf-tooltip.js"; @@ -51,9 +52,6 @@ export class NotificationDrawer extends AKElement { .pf-c-notification-drawer__list-item-description { white-space: pre-wrap; } - .pf-c-notification-drawer__footer { - margin: 1rem; - } `); } @@ -142,6 +140,34 @@ export class NotificationDrawer extends AKElement { `; } + clearNotifications() { + new EventsApi(DEFAULT_CONFIG).eventsNotificationsMarkAllSeenCreate().then(() => { + showMessage({ + level: MessageLevel.success, + message: msg("Successfully cleared notifications"), + }); + this.firstUpdated(); + this.dispatchEvent( + new CustomEvent(EVENT_REFRESH, { + bubbles: true, + composed: true, + }), + ); + this.dispatchEvent( + new CustomEvent(EVENT_NOTIFICATION_DRAWER_TOGGLE, { + bubbles: true, + composed: true, + }), + ); + }); + } + + renderEmpty() { + return html` +
${msg("You don't have any notifications currently.")}
+
`; + } + render(): TemplateResult { if (!this.notifications) { return html``; @@ -156,6 +182,18 @@ export class NotificationDrawer extends AKElement { ${msg(str`${this.unread} unread`)}
+
+ +
    - ${this.notifications.results.map((n) => this.renderItem(n))} + ${this.notifications.pagination.count < 1 + ? this.renderEmpty() + : this.notifications.results.map((n) => this.renderItem(n))}
-
`; }