import { RequestInfo } from "@goauthentik/common/api/middleware"; import { EVENT_API_DRAWER_TOGGLE, EVENT_REQUEST_POST } from "@goauthentik/common/constants"; import { AKElement } from "@goauthentik/elements/Base"; import { t } from "@lingui/macro"; import { CSSResult, TemplateResult, css, html } from "lit"; import { customElement, property } from "lit/decorators.js"; import PFButton from "@patternfly/patternfly/components/Button/button.css"; import PFContent from "@patternfly/patternfly/components/Content/content.css"; import PFDropdown from "@patternfly/patternfly/components/Dropdown/dropdown.css"; import PFNotificationDrawer from "@patternfly/patternfly/components/NotificationDrawer/notification-drawer.css"; import PFBase from "@patternfly/patternfly/patternfly-base.css"; @customElement("ak-api-drawer") export class APIDrawer extends AKElement { @property({ attribute: false }) requests: RequestInfo[] = []; static get styles(): CSSResult[] { return [ PFBase, PFNotificationDrawer, PFButton, PFContent, PFDropdown, css` .pf-c-notification-drawer__header { height: 114px; align-items: center; } .pf-c-notification-drawer__header-action, .pf-c-notification-drawer__header-action-close, .pf-c-notification-drawer__header-action-close > .pf-c-button.pf-m-plain { height: 100%; } .pf-c-notification-drawer__list-item-description { white-space: pre-wrap; font-family: monospace; } `, ]; } constructor() { super(); window.addEventListener(EVENT_REQUEST_POST, ((e: CustomEvent) => { this.requests.splice(0, 0, e.detail); if (this.requests.length > 50) { this.requests.shift(); } this.requestUpdate(); }) as EventListener); } renderItem(item: RequestInfo): TemplateResult { return html`
  • ${item.method}: ${item.status}

    ${item.path}
  • `; } render(): TemplateResult { return html`

    ${t`API Requests`}

    ${t`Open API Browser`}
      ${this.requests.map((n) => this.renderItem(n))}
    `; } }