import { type PropertyValueMap, html, nothing } from "lit"; import { customElement } from "./utils/decorators/custom-element"; import { property } from "lit/decorators.js"; import type { BadgeAppearanceType, PositionType } from "./types/ui.types"; import { NotificationTypes } from "./types/notifications.types"; import { WeavyOptionalAppComponent } from "./classes/weavy-optional-app-component"; import { UnreadNotificationsController, NotificationFilterProps, UnreadNotificationsProps, } from "./controllers/unread-notifications-controller"; import { ThemeController } from "./controllers/theme-controller"; import hostFlexCss from "./scss/host-flex.scss"; import colorModesCss from "./scss/color-modes.scss"; import hostFontCss from "./scss/host-font.scss"; import "./components/ui/wy-badge"; declare global { interface HTMLElementTagNameMap { "wy-notification-badge": WyNotificationBadge; } } /** * Weavy notification badge component that renders a realtime badge for the number of unread notifications. * The badge fits well into a button or similar. * Weavy automatically creates notifications for reactions, mentions and other activities. * * The badge can optionally be provided with an app `uid` or a `typeFilter` to only display the unread count of a given app or a given notification type, such as _mention_, _reaction_ or _activity_. * * When the `unread` count property changes, the component fires a `"wy-unread"` event with the updated count. * You can listen to the event to use the unread count for something else, and you can also turn off the component rendering by setting the `badge` property to `"none"`. * * ** Component layout ** * * The badge displays as an inline badge, matching normal text per default. * It has a filled background with rounded edge. * The badge scales with the current font-size where it's placed, using `0.75em` for the badge `font-size`. * The badge is only displayed when there is an unread count of 1 or higher. * * > When placing the badge in a ` * ``` */ @customElement("wy-notification-badge") export class WyNotificationBadge extends WeavyOptionalAppComponent implements UnreadNotificationsProps, NotificationFilterProps { static override styles = [hostFlexCss, colorModesCss, hostFontCss]; /** @internal */ protected theme = new ThemeController(this, WyNotificationBadge.styles); /** @internal */ protected unreadNotifications: UnreadNotificationsController = new UnreadNotificationsController(this); /** * Disabled link matching/consuming. * @internal */ override matchesLink(): boolean { return false; } /** * Display size of the badge. * * @type {"count" | "dot" | "none"} * @default "count" */ @property({ type: String }) badge: BadgeAppearanceType = "count"; /** * Positioning of the badge. * * @type {"inline" | "top-right" | "bottom-right" | "bottom-left" | "top-left"} * @default "inline" */ @property({ type: String }) badgePosition: PositionType = "inline"; /** * Notification type filter used when tracking unread notifications. * * @type {"" | "activity" | "mention" | "reaction"} * @default "" */ @property() typeFilter: NotificationTypes = NotificationTypes.All; /** * Optional directory identifier (`id` or `name`) to scope the unread count. * When set, only unread notifications from apps belonging to this directory are counted. */ @property() override directory?: string | number; /** Current unread notification count. */ get unread(): number { return this.unreadNotifications.unread; } /** Marks all tracked notifications as read. */ async markAllAsRead() { await this.unreadNotifications.markAllAsRead(); } protected override async willUpdate(changedProperties: PropertyValueMap): Promise { await super.willUpdate(changedProperties); if (changedProperties.has("typeFilter") || changedProperties.has("app") || changedProperties.has("directory")) { await this.unreadNotifications.track(this.typeFilter, this.app?.id, this.directory); } } override render() { return this.user && this.badge !== "none" ? html` ${this.unreadNotifications.isUnreadPending ? "" : this.unreadNotifications.unread} ` : nothing; } }