import { css, html } from 'lit' import { property, query, state } from 'lit/decorators.js' import { classMap } from 'lit/directives/class-map.js' import { when } from 'lit/directives/when.js' import { unsafeHTML } from 'lit/directives/unsafe-html.js' import type { Placement } from '@floating-ui/dom' import { customElementIfNotExist } from '../../utils/customElementIfNotExist' import { dispatchCustomEvent } from '../../utils/dispatchCustomEvent' import { UsBaseElement } from '../base/UsBaseElement' import { getDataByLocale, localizeManager, localized } from '../../dependencies' import UsInsightButton_css from './insightButton.pcss' import vars_css from './vars.pcss' import type { IInsgihtOpenEventData, IInsightAction, IInsightButtonProps, IInsightCTAEventData } from './model/IInsightButtonProps' import { IInsightActionName, IInsightActionType, IInsightNotificationCounterVariant } from './model/IInsightButtonProps' import '../button/UsButton' import '../notification/UsNotification' import '../tooltip/UsTooltip' import '../badge/UsBadge' import '../alert/UsAlert' declare global { interface HTMLElementTagNameMap { 'us-insight-button': UsInsightButton } } @customElementIfNotExist('us-insight-button') @localized() export class UsInsightButton extends UsBaseElement implements IInsightButtonProps { static override styles = [ UsBaseElement.styles, css([vars_css] as TemplateStringsArray | any), css([UsInsightButton_css] as TemplateStringsArray | any), ] @property({ type: Boolean, attribute: 'item' }) isItem!: boolean @property({ type: Boolean, attribute: 'alert' }) isAlert!: boolean @property({ type: Boolean, attribute: 'read' }) isRead!: boolean @property({ type: Boolean }) demo = false @property({ type: Boolean }) disableApps = false @property({ type: Boolean }) showAction = false @property({ type: String }) insightId!: string @property({ type: String }) placementId!: string @property({ type: Number }) flipPadding!: number @property({ type: Number }) notificationCounter = 0 @property({ type: String }) tooltip = '' @property({ type: String }) tooltipCorner: Placement = 'bottom' @property({ type: String }) tooltipStrategy: 'absolute' | 'fixed' = 'absolute' @property({ type: String }) locale = '' @property({ type: String }) widgetView = '' @property({ type: Object }) actionData = {} as IInsightAction @property({ type: String }) notificationVariant: IInsightNotificationCounterVariant = IInsightNotificationCounterVariant.COUNTER @property({ type: String, attribute: 'data-testid', reflect: true }) dataTestId = 'button_insight' @state() inFocus = false @query('#item-button') itemButtonEl!: HTMLButtonElement | null render() { return html`
${this.renderCurrentView()}
` } renderCurrentView() { switch (true) { case this.isItem: return this.renderItemBtn() case this.isAlert: return this.renderAlert() default: return this.renderListBtn() } } renderListBtn() { return html`
${localizeManager.insight_list_tooltip_text()}
` } renderItemBtn() { return html` this.setInFocus(true)} @onBlur=${() => this.setInFocus(false)} >

${this.getInsightDescription()}

${localizeManager.view_insight()} ${when(this.isCTAButtonShown(), () => { const actionText = getDataByLocale(this.actionData.text, this.locale) return html` ${actionText} ` })}
` } renderAlert() { return html`
${this.getInsightDescription()} ${localizeManager.insight_alert_link()}
` } getInsightDescription() { return unsafeHTML(getDataByLocale(this.tooltip, this.locale)) } setInFocus(state: boolean) { this.inFocus = state } onItemClick(event: Event) { const eventData: IInsgihtOpenEventData = { notificationId: this.insightId, placementId: this.placementId, } event.preventDefault() event.stopPropagation() dispatchCustomEvent('openInsight', eventData, this as any) } handleLinkClick(event: MouseEvent) { event.preventDefault() this.onItemClick(event) } handleCTAClick(event: MouseEvent) { event.stopPropagation() const eventData: IInsightCTAEventData = { insightId: this.insightId, actionData: this.actionData, fromTooltip: true, placementId: this.placementId, } dispatchCustomEvent('redirect', eventData, this, false, false) } onBulbPressEnter(e: KeyboardEvent) { if (e.key === 'Enter') { e.stopPropagation() this.onItemClick(e) } } onListClick(event: Event) { event.preventDefault() event.stopPropagation() dispatchCustomEvent('openList', true, this as any) } isVerticalNarrowView() { return this.widgetView.includes('vertical-narrow') } isCTAButtonShown() { const isExternalAction = Boolean(this.actionData?.link) && this.actionData?.type === IInsightActionType.EXTERNAL const isCompanyInfoAction = this.actionData?.name === IInsightActionName.COMPANY_INFO const isConnectorAction = this.actionData?.name === IInsightActionName.CONNECTOR return ((!this.disableApps && isConnectorAction) || isExternalAction || isCompanyInfoAction) && this.actionData?.text && this.showAction } private getLightBulbClasses() { return classMap({ 'insight-button__item': true, 'insight-button__item_unread': this.isRead, 'insight-button__item_shake': !this.isRead, 'insight-button__item_focused': this.inFocus, }) } }