import { html, nothing, type PropertyValueMap } from "lit"; import { customElement } from "../utils/decorators/custom-element"; import { property, state } from "lit/decorators.js"; import { WeavySubComponent } from "../classes/weavy-sub-component"; import type { MemberType, MembersResultType } from "../types/members.types"; import { getMemberOptions } from "../data/members"; import { QueryController } from "../controllers/query-controller"; import { localized, msg } from "@lit/localize"; import { AddMembersToConversationMutationType, LeaveConversationMutationType, UpdateConversationMutationType, UpdateMemberMutationType, getAddMembersToConversationMutation, getLeaveConversationMutation, getUpdateConversationMutation, getUpdateMemberMutation, } from "../data/conversation"; import { ifDefined } from "lit/directives/if-defined.js"; import { inputBlurOnEnter } from "../utils/keyboard"; import { partMap } from "../utils/directives/shadow-part-map"; import type { RealtimeAppEventType, RealtimePresenceEventType } from "../types/realtime.types"; import { ShadowPartsController } from "../controllers/shadow-parts-controller"; import { BlobType } from "../types/files.types"; import { type AppType, AppTypeGuid, AccessType, PermissionType } from "../types/app.types"; import { hasPermission } from "../utils/permission"; import type { BlobUploadedEventType } from "../types/files.events"; import type { MemberSearchSubmitEventType } from "../types/members.events"; import { ActionType } from "../types/action.types"; import inputCss from "../scss/components/input.scss"; import paneCss from "../scss/components/pane.scss"; import scrollCss from "../scss/scroll.scss"; import hostContentsCss from "../scss/host-contents.scss"; import "./ui/wy-avatar"; import "./ui/wy-button"; import "./ui/wy-icon"; import "./ui/wy-dropdown"; import "./ui/wy-item"; import "./ui/wy-overlay"; import "./ui/wy-presence"; import "./ui/wy-titlebar"; import "./wy-upload"; import "./wy-users-search"; declare global { interface HTMLElementTagNameMap { "wy-conversation-header": WyConversationHeader; } } /** * Header section for a conversation. Displays title and optional conversation details with possibility to edit. * * **Used sub components** * * - [``](./ui/wy-avatar.ts) * - [``](./ui/wy-avatar.ts) * - [``](./ui/wy-avatar.ts) * - [``](./ui/wy-button.ts) * - [``](./ui/wy-dropdown.ts) * - [``](./ui/wy-dropdown.ts) * - [``](./ui/wy-icon.ts) * - [``](./ui/wy-item.ts) * - [``](./ui/wy-item.ts) * - [``](./ui/wy-presence.ts) * - [``](./ui/wy-titlebar.ts) * - [``](./ui/wy-titlebar.ts) * - [``](./ui/wy-overlay.ts) * - [``](./wy-upload.ts) * - [``](./wy-users-search.ts) * * @slot icon - Icon or button to display in the beginning of the header. * * @csspart wy-conversation-titlebar - The titlebar for the conversation. * @csspart wy-conversation-details - The conversation details overlay modal. * @csspart wy-conversation-add-members - The add members overlay modal. * @csspart wy-scroll-y - Scrollable content area inside the details overlay. * @csspart wy-pane-group - Grouping wrapper for room details fields. * @csspart wy-label - Labels for inputs in the details view. * @csspart wy-input - Inputs (room name) in the details view. * @csspart wy-description - Description/help text in the details view. * @csspart wy-conversation-details-add-members-action - Menu item for opening the add-members flow. * * @fires {WyActionEventType} wy-action - Emitted when the conversation is deselected/cleared. */ @customElement("wy-conversation-header") @localized() export class WyConversationHeader extends WeavySubComponent { static override styles = [inputCss, paneCss, scrollCss, hostContentsCss]; /** * Controller for exporting named shadow parts. * * @internal */ protected exportParts = new ShadowPartsController(this); /** * The id of the current conversation. * * Set this to load or target a specific conversation. */ @property({ attribute: false }) conversationId?: number; /** * Conversation data object for the current conversation. */ @property({ attribute: false }) conversation?: AppType; /** * Whether to show the details modal. * * @internal */ @state() protected showDetails: boolean = false; /** * Whether to show the add members modal. * * @internal */ @state() protected showAddMembers: boolean = false; /** * The title of the conversation. * * @internal */ @state() protected conversationTitle: string = ""; /** * State for the conversation title input. * * @internal */ @state() protected conversationTitleInput: string = ""; /** Checks whether the current or provided conversation is an agent chat. */ protected isAgentChat(conversation?: AppType) { return (conversation ?? this.conversation)?.type === AppTypeGuid.AgentChat; } /** Checks whether the current or provided conversation is a chat room. */ protected isChatRoom(conversation?: AppType) { return (conversation ?? this.conversation)?.type === AppTypeGuid.ChatRoom; } /** Checks whether the current or provided conversation is a private chat. */ protected isPrivateChat(conversation?: AppType) { return (conversation ?? this.conversation)?.type === AppTypeGuid.PrivateChat; } /** * Query controller for loading members of the conversation. * * @internal */ private membersQuery = new QueryController(this); /** * Mutation for adding members to a conversation. * * @internal */ private addMembersMutation?: AddMembersToConversationMutationType; /** * Mutation for leaving a conversation. * * @internal */ private leaveConversationMutation?: LeaveConversationMutationType; /** * Mutation for updating a member's access. * * @internal */ private updateMemberMutation?: UpdateMemberMutationType; /** * Mutation for updating conversation properties (name/avatar). * * @internal */ private updateConversationMutation?: UpdateConversationMutationType; /** * Handler invoked when an app update realtime event is received for the current conversation. * * Updates the local title state from the realtime payload. * * @internal */ private handleRealtimeAppUpdated = (realtimeEvent: RealtimeAppEventType) => { if (!this.conversationId || realtimeEvent.app.id !== this.conversationId) { return; } this.conversationTitle = this.conversationTitleInput = realtimeEvent.app.name; }; /** * Adds a list of members to the conversation. * * @internal */ private async addMembers(members: MemberType[]) { this.showAddMembers = false; this.showDetails = true; if (!this.weavy || !this.conversationId) { return; } // add members await this.addMembersMutation?.mutate({ appId: this.conversationId, members: members.map((m) => m.id) }); await this.membersQuery.result.refetch(); await this.weavy.queryClient.invalidateQueries({ queryKey: ["apps"] }); } /** * Handles saving the conversation name from the conversation name input. * * @internal */ private async handleSaveConversationName() { if (!this.weavy || !this.conversationId) { return; } const name = this.conversationTitleInput.trim() === "" ? null : this.conversationTitleInput.trim(); await this.updateConversationMutation?.mutate({ appId: this.conversationId, name }); } /** * Updates the avatar with an uploaded blob picture. * * @internal */ private async handleAvatarUploaded(blob: BlobType) { if (!this.weavy || !this.conversationId) { return; } await this.updateConversationMutation?.mutate({ appId: this.conversationId, blobId: blob.id, thumbnailUrl: blob.thumbnail_url, }); } /** * Clears the set avatar for the conversation. * * @internal */ private async clearAvatar() { if (!this.weavy || !this.conversationId) { return; } await this.updateConversationMutation?.mutate({ appId: this.conversationId, blobId: null, thumbnailUrl: null }); } /** * Updates the access for a member in the conversation. * * @internal */ private async updateMember(id: number, access: AccessType) { if (!this.weavy || !this.conversationId) { return; } await this.updateMemberMutation?.mutate({ appId: this.conversationId, userId: id, access, }); await this.membersQuery.result.refetch(); } /** * Removes the current or provided member from the conversation. * * If no memberId is provided, the current user leaves the conversation and the component state is cleared. * * @internal */ private async leaveConversation(memberId?: number) { if (!this.weavy || !this.conversationId || !this.user) { return; } if (memberId) { await this.leaveConversationMutation?.mutate({ appId: this.conversationId, members: [memberId], userId: this.user.id, }); } if (!memberId || memberId === this.user.id) { this.showDetails = false; this.conversation = undefined; this.conversationId = undefined; this.dispatchAction({ action: ActionType.Select, app: null }); } else { await this.membersQuery.result.refetch(); } await this.weavy.queryClient.invalidateQueries({ queryKey: ["apps"] }); } /** * Updates member presence data from realtime presence events. * * @internal */ private handlePresenceChange = (data: RealtimePresenceEventType) => { if (!this.weavy || !this.isPrivateChat()) { return; } // payload returns a single id as a string instead of number[] if (!Array.isArray(data)) { data = [parseInt(data)]; } const updateMembersInApps = (members: MemberType[] = []) => { members.forEach((m) => { m.presence = data.indexOf(m.id) != -1 ? "active" : "away"; }); return members; }; this.weavy.queryClient.setQueryData(["apps", this.conversationId], (app: AppType | undefined) => { if (!app?.members) { return app; } app.members.data = updateMembersInApps(app.members.data); return app; }); this.weavy.queryClient.setQueryData(["members", this.conversationId], (members: MembersResultType | undefined) => members ? { ...members, data: updateMembersInApps(members.data) } : members, ); }; /** * Unsubscribe function for realtime subscriptions. * * @internal */ #unsubscribeToRealtime?: () => void; protected override async willUpdate(changedProperties: PropertyValueMap): Promise { super.willUpdate(changedProperties); // if context updated if (changedProperties.has("weavy") && this.weavy) { this.leaveConversationMutation = getLeaveConversationMutation(this.weavy); this.addMembersMutation = getAddMembersToConversationMutation(this.weavy); this.updateConversationMutation = getUpdateConversationMutation(this.weavy); this.updateMemberMutation = getUpdateMemberMutation(this.weavy); } // ConversationId doesn't exist anymore if (changedProperties.has("conversationId")) { this.showDetails = false; } // conversationId is changed if ((changedProperties.has("weavy") || changedProperties.has("conversationId")) && this.weavy) { this.#unsubscribeToRealtime?.(); if (this.conversationId) { await this.membersQuery.trackQuery( getMemberOptions(this.weavy, this.conversationId, { initialData: () => { // Use any data from the conversation query as the initial data for the member query if (this.conversationId) { return this.weavy?.queryClient.getQueryData(["apps", this.conversationId])?.members; } return undefined; }, }) ); const subscribeGroup = `a${this.conversationId}`; void this.weavy.subscribe(subscribeGroup, "app_updated", this.handleRealtimeAppUpdated); void this.weavy.subscribe(null, "online", this.handlePresenceChange); this.#unsubscribeToRealtime = () => { void this.weavy?.unsubscribe(subscribeGroup, "app_updated", this.handleRealtimeAppUpdated); void this.weavy?.unsubscribe(null, "online", this.handlePresenceChange); this.#unsubscribeToRealtime = undefined; }; } else { this.membersQuery.untrackQuery(); } } // conversation object is updated if (changedProperties.has("conversation") && this.conversation) { this.conversationTitleInput = this.conversationTitle = this.conversation.name; } } override render() { const { data: membersData } = this.membersQuery.result ?? {}; const adminCount: number = (membersData?.data || []).filter((user) => user.access === AccessType.Admin).length; const otherMember = this.user && this.isPrivateChat() ? (this.conversation?.members?.data || []).filter((member) => member.id !== this.user?.id)?.[0] ?? this.user : null; const isBlockedPrivateChat = this.conversation?.type === AppTypeGuid.PrivateChat && Boolean(otherMember?.is_blocked); return this.conversationId ? html` ${this.conversation && this.user ? html` ${this.conversation.type === AppTypeGuid.PrivateChat && !isBlockedPrivateChat ? html`` : nothing} ${isBlockedPrivateChat ? html`` : nothing} ${this.conversationTitle} ` : nothing} ${this.isChatRoom() ? html` (this.showDetails = true)} title="${msg("Details")}" > ` : nothing} ${this.weavy ? html` { this.showDetails = false; }} > { this.showDetails = false; }} > ${this.conversationTitle} ${this.isChatRoom() ? html` { this.showDetails = false; this.showAddMembers = true; }} > ${msg("Add members")} ` : nothing}
${this.showDetails && this.conversation && this.user ? html` ${this.isChatRoom() ? html` this.handleAvatarUploaded(e.detail.blob)} .accept=${"image/*"} .label=${msg("Select picture")} >
${this.conversation.avatar_url ? html`` : html``}
${this.conversation.avatar_url ? html`
this.clearAvatar()} >${msg("Remove picture")}
` : nothing}
` : html` `}
${this.isChatRoom() ? html`
{ this.conversationTitleInput = (e.target as HTMLInputElement).value; }} @keyup=${inputBlurOnEnter} @blur=${() => this.handleSaveConversationName()} />
${msg("Changing the name of a group chat changes it for everyone.")}

${membersData ? html` ${membersData.data?.map( (member) => html` ${member.name} ${member.is_blocked ? html`` : nothing} ${member.access === AccessType.Admin ? html` ` : nothing} ${this.user && this.user.id === member.id && !hasPermission(PermissionType.Admin, this.conversation?.permissions) ? html` this.leaveConversation(member.id)} title=${msg("Leave conversation")} kind="icon" > ` : hasPermission(PermissionType.Admin, this.conversation?.permissions) ? html` this.leaveConversation(member.id)} > ${this.user && this.user.id === member.id ? msg("Leave conversation") : msg("Remove member")} ${adminCount > 1 && member.access === AccessType.Admin ? html` this.updateMember(member.id, AccessType.Write)} > ${msg("Remove as admin")} ` : member.access !== AccessType.Admin ? html` this.updateMember(member.id, AccessType.Admin)} > ${msg("Make admin")} ` : nothing} ` : nothing} ` ) ?? nothing} ` : nothing}
` : nothing} ` : nothing}
` : nothing} ${this.weavy ? html` { this.showAddMembers = false; }} > { this.showAddMembers = false; }} > ${msg("Add members")} ${this.showAddMembers ? html` this.addMembers(e.detail.members)} > ` : nothing} ` : nothing} ` : nothing; } override disconnectedCallback(): void { this.#unsubscribeToRealtime?.(); super.disconnectedCallback(); } }