import { CSSResultGroup, html, LitElement, TemplateResult, unsafeCSS } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { TROUBLESHOOTING_URL } from '../const.js'; import { localize } from '../localize/localize.js'; import messageStyle from '../scss/message.scss'; import { FrigateCardError, Message } from '../types.js'; import { dispatchFrigateCardEvent } from '../utils/basic.js'; @customElement('frigate-card-message') export class FrigateCardMessage extends LitElement { @property({ attribute: false }) protected message = ''; @property({ attribute: false }) protected context?: unknown; @property({ attribute: false }) protected icon?: string; // Render the menu. protected render(): TemplateResult { const icon = this.icon ? this.icon : 'mdi:information-outline'; return html`
${this.message ? html`${this.message}` : ''} ${this.context ? html`
${JSON.stringify(this.context, null, 2)}
` : ''}
`; } static get styles(): CSSResultGroup { return unsafeCSS(messageStyle); } } @customElement('frigate-card-error-message') export class FrigateCardErrorMessage extends LitElement { @property({ attribute: false }) protected message?: Message; protected render(): TemplateResult | void { if (!this.message) { return; } return html` ${localize('error.troubleshooting')}.`} .icon=${'mdi:alert-circle'} .context=${this.message.context} > `; } static get styles(): CSSResultGroup { return unsafeCSS(messageStyle); } } @customElement('frigate-card-progress-indicator') export class FrigateCardProgressIndicator extends LitElement { @property({ attribute: false }) protected message = ''; protected render(): TemplateResult { return html`
${this.message ? html`${this.message}` : html``}
`; } static get styles(): CSSResultGroup { return unsafeCSS(messageStyle); } } export function renderMessage(message: Message): TemplateResult { if (message.type == 'error') { return html` `; } else if (message.type == 'info') { return html` `; } return html``; } export function renderProgressIndicator(message?: string): TemplateResult { return html` `; } /** * Dispatch an event with a message to show to the user. * @param element The element to send the event. * @param message The message to show. * @param icon An optional icon to attach to the message. */ export function dispatchMessageEvent( element: HTMLElement, message: string, icon?: string, context?: unknown, ): void { dispatchFrigateCardEvent(element, 'message', { message: message, type: 'info', icon: icon, context: context, }); } /** * Dispatch an event with an error message to show to the user. * @param element The element to send the event. * @param message The message to show. */ export function dispatchErrorMessageEvent( element: HTMLElement, message: string, context?: unknown, ): void { dispatchFrigateCardEvent(element, 'message', { message: message, type: 'error', context: context, }); } /** * Dispatch an event with an error message to show to the user. * @param element The element to send the event. * @param message The message to show. */ export function dispatchFrigateCardErrorEvent( element: HTMLElement, error: FrigateCardError ): void { dispatchFrigateCardEvent(element, 'message', { message: error.message, type: 'error', context: error.context || '' }); }