2022-06-25 15:44:17 +00:00
|
|
|
import { SentryIgnoredError } from "@goauthentik/web/common/errors";
|
|
|
|
import { WSMessage } from "@goauthentik/web/common/ws";
|
|
|
|
import { EVENT_WS_MESSAGE, WS_MSG_TYPE_MESSAGE } from "@goauthentik/web/constants";
|
|
|
|
import "@goauthentik/web/elements/messages/Message";
|
|
|
|
import { APIMessage } from "@goauthentik/web/elements/messages/Message";
|
|
|
|
|
2021-10-28 07:48:51 +00:00
|
|
|
import { CSSResult, LitElement, TemplateResult, css, html } from "lit";
|
2021-11-04 21:34:48 +00:00
|
|
|
import { customElement, property } from "lit/decorators.js";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-03-17 12:23:33 +00:00
|
|
|
import PFAlertGroup from "@patternfly/patternfly/components/AlertGroup/alert-group.css";
|
|
|
|
import PFBase from "@patternfly/patternfly/patternfly-base.css";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-11-16 10:21:30 +00:00
|
|
|
export function showMessage(message: APIMessage, unique = false): void {
|
2020-12-12 19:46:02 +00:00
|
|
|
const container = document.querySelector<MessageContainer>("ak-message-container");
|
|
|
|
if (!container) {
|
2021-12-21 18:44:44 +00:00
|
|
|
throw new SentryIgnoredError("failed to find message container");
|
2020-12-12 19:46:02 +00:00
|
|
|
}
|
2021-11-16 10:21:30 +00:00
|
|
|
container.addMessage(message, unique);
|
2020-12-12 19:46:02 +00:00
|
|
|
container.requestUpdate();
|
2020-11-20 21:08:00 +00:00
|
|
|
}
|
2020-10-16 13:26:51 +00:00
|
|
|
|
2020-12-12 19:46:02 +00:00
|
|
|
@customElement("ak-message-container")
|
|
|
|
export class MessageContainer extends LitElement {
|
2021-08-03 15:52:21 +00:00
|
|
|
@property({ attribute: false })
|
2020-12-12 19:46:02 +00:00
|
|
|
messages: APIMessage[] = [];
|
|
|
|
|
2021-03-16 16:09:52 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-08-03 15:52:21 +00:00
|
|
|
return [
|
|
|
|
PFBase,
|
|
|
|
PFAlertGroup,
|
|
|
|
css`
|
|
|
|
/* Fix spacing between messages */
|
|
|
|
ak-message {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
];
|
2020-10-16 13:26:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 16:23:29 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2021-07-22 11:47:27 +00:00
|
|
|
this.addEventListener(EVENT_WS_MESSAGE, ((e: CustomEvent<WSMessage>) => {
|
2021-07-22 12:15:54 +00:00
|
|
|
if (e.detail.message_type !== WS_MSG_TYPE_MESSAGE) return;
|
2021-07-22 11:47:27 +00:00
|
|
|
this.addMessage(e.detail as unknown as APIMessage);
|
|
|
|
}) as EventListener);
|
2020-10-16 18:11:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 10:21:30 +00:00
|
|
|
addMessage(message: APIMessage, unique = false): void {
|
|
|
|
if (unique) {
|
|
|
|
const matchingMessages = this.messages.filter((m) => m.message == message.message);
|
|
|
|
if (matchingMessages.length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 18:09:31 +00:00
|
|
|
this.messages.push(message);
|
2021-03-12 11:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
render(): TemplateResult {
|
2020-12-12 19:46:02 +00:00
|
|
|
return html`<ul class="pf-c-alert-group pf-m-toast">
|
|
|
|
${this.messages.map((m) => {
|
2021-03-01 19:33:00 +00:00
|
|
|
return html`<ak-message
|
2020-12-12 19:46:02 +00:00
|
|
|
.message=${m}
|
|
|
|
.onRemove=${(m: APIMessage) => {
|
2021-03-01 19:33:00 +00:00
|
|
|
this.messages = this.messages.filter((v) => v !== m);
|
2021-03-12 11:41:17 +00:00
|
|
|
this.requestUpdate();
|
2021-08-03 15:52:21 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
</ak-message>`;
|
|
|
|
})}
|
2020-12-12 19:46:02 +00:00
|
|
|
</ul>`;
|
2020-10-16 13:26:51 +00:00
|
|
|
}
|
|
|
|
}
|