This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/web/src/elements/messages/MessageContainer.ts

64 lines
2 KiB
TypeScript
Raw Normal View History

import { LitElement, html, TemplateResult, CSSResult, css } from "lit";
import { customElement, property } from "lit/decorators";
import "./Message";
import { APIMessage } from "./Message";
import PFAlertGroup from "@patternfly/patternfly/components/AlertGroup/alert-group.css";
import PFBase from "@patternfly/patternfly/patternfly-base.css";
import { EVENT_WS_MESSAGE, WS_MSG_TYPE_MESSAGE } from "../../constants";
import { WSMessage } from "../../common/ws";
export function showMessage(message: APIMessage): void {
const container = document.querySelector<MessageContainer>("ak-message-container");
if (!container) {
throw new Error("failed to find message container");
}
2021-03-12 11:41:17 +00:00
container.addMessage(message);
container.requestUpdate();
}
@customElement("ak-message-container")
export class MessageContainer extends LitElement {
@property({ attribute: false })
messages: APIMessage[] = [];
static get styles(): CSSResult[] {
return [
PFBase,
PFAlertGroup,
css`
/* Fix spacing between messages */
ak-message {
display: block;
}
`,
];
}
constructor() {
super();
this.addEventListener(EVENT_WS_MESSAGE, ((e: CustomEvent<WSMessage>) => {
if (e.detail.message_type !== WS_MSG_TYPE_MESSAGE) return;
this.addMessage(e.detail as unknown as APIMessage);
}) as EventListener);
}
2021-03-12 11:41:17 +00:00
addMessage(message: APIMessage): void {
this.messages.push(message);
2021-03-12 11:41:17 +00:00
}
render(): TemplateResult {
return html`<ul class="pf-c-alert-group pf-m-toast">
${this.messages.map((m) => {
return html`<ak-message
.message=${m}
.onRemove=${(m: APIMessage) => {
this.messages = this.messages.filter((v) => v !== m);
2021-03-12 11:41:17 +00:00
this.requestUpdate();
}}
>
</ak-message>`;
})}
</ul>`;
}
}