2026-07-18 21:10:33 -04:00
|
|
|
import { Room } from 'matrix-js-sdk';
|
2026-07-07 23:01:39 -04:00
|
|
|
import { useMatrixClient } from './useMatrixClient';
|
|
|
|
|
import { useMediaAuthentication } from './useMediaAuthentication';
|
2026-07-18 21:10:33 -04:00
|
|
|
import { useRoomMemberChange } from './useRoomMemberChange';
|
2026-07-07 23:01:39 -04:00
|
|
|
import { getMemberName } from '../utils/room';
|
|
|
|
|
import { mxcUrlToHttp } from '../utils/matrix';
|
|
|
|
|
|
|
|
|
|
export type MemberAvatar = {
|
|
|
|
|
name: string;
|
|
|
|
|
avatarUrl: string | undefined;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve a room member's display name and avatar http url, staying reactive to
|
|
|
|
|
* that member's profile (name/avatar/membership) changes.
|
|
|
|
|
*
|
2026-07-18 21:10:33 -04:00
|
|
|
* Stays reactive to this member's profile (name/avatar/membership) changes via
|
|
|
|
|
* the shared member-change store (one global listener for the whole app).
|
2026-07-07 23:01:39 -04:00
|
|
|
*/
|
|
|
|
|
export const useMemberAvatar = (
|
|
|
|
|
room: Room,
|
|
|
|
|
userId: string,
|
|
|
|
|
width = 32,
|
|
|
|
|
height = 32,
|
|
|
|
|
resizeMethod = 'crop',
|
|
|
|
|
): MemberAvatar => {
|
|
|
|
|
const mx = useMatrixClient();
|
|
|
|
|
const useAuthentication = useMediaAuthentication();
|
|
|
|
|
|
2026-07-18 21:10:33 -04:00
|
|
|
useRoomMemberChange(room.roomId, userId);
|
2026-07-07 23:01:39 -04:00
|
|
|
|
|
|
|
|
const name = getMemberName(room, userId);
|
|
|
|
|
const avatarMxc = room.getMember(userId)?.getMxcAvatarUrl();
|
|
|
|
|
const avatarUrl = avatarMxc
|
|
|
|
|
? (mxcUrlToHttp(mx, avatarMxc, useAuthentication, width, height, resizeMethod) ?? undefined)
|
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
|
|
return { name, avatarUrl };
|
|
|
|
|
};
|