import { type PropertyValueMap, html, nothing } from "lit";
import { customElement } from "./utils/decorators/custom-element";
import { property, state } from "lit/decorators.js";
import { localized, msg } from "@lit/localize";
import type { OverlayAppearanceType, BadgeAppearanceType, PositionType } from "./types/ui.types";
import { NotificationTypes } from "./types/notifications.types";
import {} from "./types/app.types";
import {
UnreadNotificationsController,
NotificationFilterProps,
UnreadNotificationsProps,
} from "./controllers/unread-notifications-controller";
import { NotificationFilterEventType } from "./types/notifications.events";
import { WeavyOptionalAppComponent } from "./classes/weavy-optional-app-component";
import { ThemeController } from "./controllers/theme-controller";
import hostContentsCss from "./scss/host-contents.scss";
import colorModesCss from "./scss/color-modes.scss";
import hostFontCss from "./scss/host-font.scss";
import "./components/ui/wy-icon";
import "./components/ui/wy-button";
import "./components/ui/wy-container";
import "./components/ui/wy-overlay";
import "./components/wy-notification-list";
import "./components/ui/wy-badge";
import "./components/ui/wy-dropdown";
declare global {
interface HTMLElementTagNameMap {
"wy-notification-button": WyNotificationButton;
}
}
/**
* @import { WyAppEventType } from "./types/app.events"
* @import { WyLinkEventType } from "./types/notifications.events"
* @import { WyUnreadEventType } from "./types/ui.events"
* @import { WyActionEventType } from "./types/action.events"
*/
/**
* Weavy notification button component to render a button complete with a realtime badge for the number of unread notifications. When clicking the button, it opens a built in overlay displaying the full list of notifications.
*
* Weavy automatically creates notifications for reactions, mentions and other activities.
*
* The notifications can be clicked or marked as read or unread. When clicked the notification fires a `"wy-link"` event with information about the originating Weavy component for the notification.
* This can be used to [handle navigation](https://www.weavy.com/docs/learn/notifications) in the app, back to where the originating Weavy component is placed.
*
* The notification button component can optionally be tailored to only show notifications for a specific app or notifications of a specific type, such as _mentions_ or _reactions_.
* When setting a predefined notification type, it removes the possibility for the user to filter the list by type in the UI.
*
* > Complement this with the [``](./wy-notification-toasts.ts) component to also get realtime _in-app notifications_ or _browser notifications_.
*
* **Component Layout**
*
* The button is displayed as a clickable icon that acts as a button.
* The default size of the button is the icon size `1.5rem`/`24px` together with the set `--wy-padding` (which defaults to `0.5rem`/`8px`) on every side of the icon, which makes a total of `2.5rem`/`40px` per default.
*
* The openable notification list renders in an overlay in the [top layer](https://developer.mozilla.org/en-US/docs/Glossary/Top_layer).
* That means that the notification list does not occupy any visual layout space where it's placed in the DOM, it only renders in the top layer.
*
* You can add additional styling using _CSS Custom Properties_ and _CSS Shadow Parts_ and further customization using _slots_.
*
* **Used sub components:**
*
* - [``](./ui/wy-button.ts)
* - [``](./ui/wy-container.ts)
* - [``](./ui/wy-icon.ts)
* - [``](./ui/wy-overlay.ts)
* - [``](./wy-notification-list.ts)
* - [``](./wy-notification-list.ts)
* - [``](./ui/wy-badge.ts)
* - [``](./ui/wy-dropdown.ts)
* - [``](./ui/wy-dropdown.ts)
* - [``](./wy-empty.ts)
*
* @tagname wy-notification-button
* @slot actions - Slot for action buttons in the notification list header.
* @slot title - Slot for title in the notification list header.
* @fires {WyActionEventType} wy-action - Emitted when a notification is selected.
* @fires {WyAppEventType} wy-app - Fired whenever the app property changes.
* @fires {WyLinkEventType} wy-link - Fired when a notification is clicked.
* @fires {WyUnreadEventType} wy-unread - Fired when the number of unread notifications change.
*
* @example
Notification button with notification list overlay
*
* Display notification button with a badge that tracks notifications for the authenticated user and can open an overlay with a notification list on click.
*
* ```html
*
* ```
*
* @example
Filter notifications by app
*
* Display notifications for the authenticated user that originated from app with the specified `uid`. Also, show a dot instead of a count when there are unread notifications.
*
* ```html
*
* ```
*
* @example
Styling the badge
*
* Changes the styles of the badge in the notification button. Set the background color of the `wy-badge` CSS shadow part.
*
* ```css
* wy-notification-button::part(wy-badge) {
* background-color: violet;
* }
* ```
*/
@customElement("wy-notification-button")
@localized()
export class WyNotificationButton
extends WeavyOptionalAppComponent
implements UnreadNotificationsProps, NotificationFilterProps
{
static override styles = [hostContentsCss, colorModesCss, hostFontCss];
/** @internal */
protected theme = new ThemeController(this, WyNotificationButton.styles);
/** @internal */
protected unreadNotifications: UnreadNotificationsController = new UnreadNotificationsController(this);
/**
* Disabled link matching/consuming.
* @internal
*/
override matchesLink(): boolean {
return false;
}
/**
* Overlay appearance used for the notification list.
*
* @type {"modal" | "sheet" | "drawer" | "full" | "none"}
* @default "sheet"
*/
@property({ type: String })
overlay: OverlayAppearanceType = "sheet";
/**
* Overlay appearance used for the notification list.
*
* @deprecated
* @internal
* @type {"modal" | "sheet" | "drawer" | "full" | "none"}
* @default "sheet"
*/
@property({ type: String })
set list(list: OverlayAppearanceType) {
console.warn(`.list is deprecated. Use .overlay = "${list}"; instead`);
this.overlay = list;
}
get list() {
return this.overlay;
}
/**
* Notification badge appearance variant.
*
* @type {"count" | "dot" | "none"}
* @default "count"
*/
@property({ type: String })
badge: BadgeAppearanceType = "count";
/**
* Set the position of the badge. `"inline"` means no positioning.
*
* @type {"inline" | "top-right" | "bottom-right" | "bottom-left" | "top-left"}
* @default "top-right"
*/
@property({ type: String })
badgePosition: PositionType = "top-right";
/**
* Notification type filter. No filter (`""`) will allow the user to filter.
*
* @type {"" | "activity" | "mention" | "reaction"}
* @default ""
*/
@property()
typeFilter: NotificationTypes = NotificationTypes.All;
/**
* Optional directory identifier (`id` or `name`) to scope the notification list.
* When set, only notifications from apps belonging to this directory are shown.
*/
@property()
override directory?: string | number;
/** @internal */
@state()
currentTypeFilter: NotificationTypes = NotificationTypes.All;
/** @internal */
@state()
showNotificationList = false;
/** 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")) {
this.currentTypeFilter = this.typeFilter;
}
if (changedProperties.has("typeFilter") || changedProperties.has("app") || changedProperties.has("directory")) {
await this.unreadNotifications.track(this.typeFilter, this.app?.id, this.directory);
}
}
override render() {
const showNotificationHeader = this.typeFilter === NotificationTypes.All;
return html`
(this.showNotificationList = !this.showNotificationList)}
>
${this.user && this.badge !== "none"
? html`
${this.unreadNotifications.isUnreadPending ? "" : this.unreadNotifications.unread}
`
: nothing}
${this.overlay !== "none"
? html` (this.showNotificationList = false)}
>
this.markAllAsRead()} title=${msg("Mark all as read")}>
${this.app
? html`
${this.app?.is_subscribed
? html`
this.subscribe(false)}>
${msg("Unsubscribe")}
`
: html`
this.subscribe(true)}>
${msg("Subscribe")}
`}
`
: nothing}
${msg("Notifications")}
${this.showNotificationList
? html`
${showNotificationHeader
? html`
{
this.currentTypeFilter = e.detail.typeFilter;
}}
>
`
: nothing}
${this.app && !this.app?.is_subscribed
? html`