Files
healthchat-web/apps/web/src/components/viewmodels/avatars/MemberAvatarViewModel.ts
T
R Midhun Suresh c4606c19f8 Ongoing call tiles - Implement the view-models and store (#34198)
* Add a common view snapshot type

This will contain the common snapshot values used by both dm ongoing
call tile view and room ongoing call tile view.

* Implement the common join button component

* Implement the call icon component

* Implement the common container component

* Support single digit minutes in existing clock component

* Implement the view for duration component

* Add static profile pics to be used in storybook tests

* Implement view for member avatar component

* Implement view for face pile component

* Implement RoomOngoingCallTileView

* Implement DmOngoingCallTileView

* Support the new tiles in RootTileView

* Export all the views

* Update all the screenshots

* Implement view model for duration view

* Implement vm for face pile view

* Implement vm for member avatar view

* Expose call store via the sdk context

* Add method to get all calls in store

* Add store to track latest notification event per room

* Also expose new store via sdk context

* Create a base vm for both tiles

* Implement the room ongoing call tile vm

* Implement the dm ongoing call tile vm

* Support new tile vms in the root tile vm

* Rename getCallInRoom -> getCallOrThrow

* Change confusing comment in test

* Check existing events first and use decryption promise

* Clarify recompute vs update in code comment

* Use vi.mocked instead of ts-ignore

* Change type of size to number

* Import only the type

* Pass size as number instead of string

* Check if call is jistsi call in root tile

* Add more tests

* Rename isCallIgnored -> isCallDeclined

* Add a comment about the 10px margin

* Add comments to explain css heights and widths
2026-07-13 14:52:16 +00:00

74 lines
2.2 KiB
TypeScript

/*
* Copyright 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
import { BaseViewModel, type MemberAvatarViewSnapshot } from "@element-hq/web-shared-components";
import { type MatrixClient, RoomStateEvent, type RoomMember } from "matrix-js-sdk/src/matrix";
import UserIdentifierCustomisations from "../../../customisations/UserIdentifier";
import { mediaFromMxc } from "../../../customisations/Media";
interface Props {
/**
* The size of the avatar in pixels, eg: 20.
*/
size: number;
/**
* The room member for this avatar.
*/
member: RoomMember;
/**
* MatrixClient object to access js-sdk.
*/
cli: MatrixClient;
}
function computeSnapshot(props: Props): MemberAvatarViewSnapshot {
const size = props.size;
const member = props.member;
const id = member.userId;
const name = member.name ?? member.userId;
const title =
UserIdentifierCustomisations.getDisplayUserIdentifier(member.userId ?? "", {
roomId: member.roomId ?? "",
}) ?? member.userId;
let url: string | undefined;
if (member.getMxcAvatarUrl()) {
url =
mediaFromMxc(member.getMxcAvatarUrl() ?? "", props.cli).getThumbnailOfSourceHttp(size, size, "crop") ??
undefined;
}
return {
name,
id,
url,
size: `${size}px`,
title,
};
}
/**
* A view-model for rendering the avatar for a given room member.
*/
export class MemberAvatarViewModel extends BaseViewModel<MemberAvatarViewSnapshot, Props> {
public constructor(props: Props) {
super(props, computeSnapshot(props));
const roomId = props.member.roomId;
const room = props.cli.getRoom(roomId);
if (room) {
this.disposables.trackListener(room, RoomStateEvent.Members, this.onUpdate as (...args: unknown[]) => void);
}
}
private onUpdate = (event: unknown, state: unknown, member: RoomMember): void => {
if (member.userId === this.props.member.userId) {
const newSnapshot = computeSnapshot(this.props);
this.snapshot.set(newSnapshot);
}
};
}