Files
Sable/src/app/hooks/useUserProfile.ts
T
hazre 71425f8644 refactor: route matrix-sdk imports through $types boundary
Centralize Matrix SDK imports behind a local compatibility module, migrate matrix subpath imports to the boundary, and switch to the $types alias. Upgrade TypeScript to latest so matrix-js-sdk typings resolve correctly before the next cleanup phase.
2026-02-14 17:24:46 +01:00

52 lines
1.4 KiB
TypeScript

import { useEffect, useState } from 'react';
import { UserEvent, UserEventHandlerMap } from '$types/matrix-sdk';
import { useMatrixClient } from './useMatrixClient';
export type UserProfile = {
avatarUrl?: string;
displayName?: string;
};
export const useUserProfile = (userId: string): UserProfile => {
const mx = useMatrixClient();
const [profile, setProfile] = useState<UserProfile>(() => {
const user = mx.getUser(userId);
return {
avatarUrl: user?.avatarUrl,
displayName: user?.displayName,
};
});
useEffect(() => {
const user = mx.getUser(userId);
const onAvatarChange: UserEventHandlerMap[UserEvent.AvatarUrl] = (event, myUser) => {
setProfile((cp) => ({
...cp,
avatarUrl: myUser.avatarUrl,
}));
};
const onDisplayNameChange: UserEventHandlerMap[UserEvent.DisplayName] = (event, myUser) => {
setProfile((cp) => ({
...cp,
displayName: myUser.displayName,
}));
};
mx.getProfileInfo(userId).then((info) =>
setProfile({
avatarUrl: info.avatar_url,
displayName: info.displayname,
})
);
user?.on(UserEvent.AvatarUrl, onAvatarChange);
user?.on(UserEvent.DisplayName, onDisplayNameChange);
return () => {
user?.removeListener(UserEvent.AvatarUrl, onAvatarChange);
user?.removeListener(UserEvent.DisplayName, onDisplayNameChange);
};
}, [mx, userId]);
return profile;
};