Ratatoskr/src/app/hooks/useUserProfile.ts
2026-07-22 12:41:28 +05:30

48 lines
1.3 KiB
TypeScript

import { useCallback } from 'react';
import { useQuery } from '@tanstack/react-query';
import { getProfile, GetProfileOK } from 'neurone/lib/api/matrix/client/v3/profile';
import { parseMatrixResp } from 'neurone/lib/matrix-request';
import { useClient } from './useClient';
export type UserProfile = {
avatarUrl?: string;
displayName?: string;
bannerUrl?: string;
timezone?: string;
};
export const useUserProfile = (userId: string): UserProfile | undefined => {
const ct = useClient();
const fetchProfile = useCallback(async (): Promise<UserProfile> => {
const response = await ct.requestAuthJSON(
getProfile({
pathParams: { userId },
})
);
const [, profile] = await parseMatrixResp<
GetProfileOK & {
'chat.commet.profile_banner'?: string;
'm.banner_url'?: string;
'm.tz'?: string;
}
>(response);
return {
avatarUrl: profile?.avatar_url || undefined,
displayName: profile?.displayname || undefined,
bannerUrl: profile?.['m.banner_url'] ?? profile?.['chat.commet.profile_banner'] ?? undefined,
timezone: profile?.['m.tz'] ?? undefined,
};
}, [ct, userId]);
const { data } = useQuery({
queryKey: ['profile', userId],
queryFn: fetchProfile,
staleTime: 0,
gcTime: Infinity,
refetchOnMount: 'always',
});
return data;
};