import { css, html, nothing, unsafeCSS } from "lit"; import { customElement } from "lit/decorators.js"; import { msg } from "@lit/localize"; import { Task } from "@lit/task"; import type { PropertiesPicker, Resource } from "@src/tems"; import * as rdf from "@src/rdf"; import * as utils from "@helpers"; import "~icons/mingcute/notification-line"; import ComponentStyle from "@styles/orbit/notification/tems-notification.scss?inline"; @customElement("solid-tems-notification") export class TemsNotification extends utils.OrbitComponent { constructor() { super(); utils.setupCacheInvalidation(this, { keywords: ["notifications", "inbox"], }); } static styles = css` ${unsafeCSS(ComponentStyle)} `; cherryPickedProperties: PropertiesPicker[] = [ { key: "@id", value: "@id" }, { key: "@type", value: "@type" }, { key: "author", value: "author", expand: true }, { key: "name", value: "name" }, { key: "first_name", value: "first_name" }, { key: "last_name", value: "last_name" }, { key: "account", value: "account", expand: true }, { key: "picture", value: "picture" }, { key: "object", value: "object", expand: true }, { key: "title", value: "title" }, { key: "type", value: "type" }, { key: "summary", value: "summary" }, { key: "date", value: "date" }, { key: "unread", value: "unread" }, ]; _getResource = new Task(this, { task: async ([dataSrc]) => { if ( !dataSrc || !this.orbit || (!this.noRouter && this.route && !this.route.startsWith(this.currentRoute)) ) return; if (!this.hasCachedDatas || this.oldDataSrc !== dataSrc) { this.datas = await this._getProxyValue(`${dataSrc}inbox/`); this.hasCachedDatas = true; } if (this.oldDataSrc !== dataSrc) { this.oldDataSrc = dataSrc; } return utils.sort(this.datas, "date", "desc"); }, args: () => [this.dataSrc, this.caching, this.currentRoute], }); async markAsReadOrUnread(data: Resource, force = false) { await window.sibStore.patch( { unread: force ? false : !data.unread, "@context": data._originalResource?.serverContext, }, data["@id"] ); document.dispatchEvent( new CustomEvent("save", { detail: { resource: { "@id": data["@id"] } }, bubbles: true, }) ); } private openNotification(e: Event, data: Resource) { this.markAsReadOrUnread(data, true); this._navigate(e); } render() { return ( this.gatekeeper() || this._getResource.render({ pending: () => html``, complete: (datas) => { if (!datas) { return nothing; } return html`
${datas.length > 0 ? datas.map( (data) => html`
${data.author ? html`` : nothing}
${data.object && (data.object.name || data.object.title) && (this.hasType(rdf.RDFTYPE_OBJECT, [data.object]) || this.hasType(rdf.RDFTYPE_DATAOFFER, [ data.object, ]) || this.hasType(rdf.RDFTYPE_SERVICE, [data.object]) || this.hasType(rdf.RDFTYPE_PROVIDER, [data.object])) ? html`
this.openNotification(e, data)} navigation-target="" navigation-resource=${data.object["@id"]} navigation-rdf-type=${data.object["@type"]?.at( -1 ) ?? data.object["@type"]} > ${data.object.name || data.object.title}
` : data.author ? html`
${data.author.name}
` : nothing}
${data.summary}
${utils.formatDate(data.date, { hour: "2-digit", minute: "2-digit", day: "2-digit", month: "2-digit", year: "2-digit", })}
` ) : html`

${msg("Currently, nothing to report!")}

${msg( "This area will light with new notifications once there's activity in your workspaces." )}

`}
`; }, }) ); } }