Files
IdP/apps/frontend/components/family/chat-room-avatar-display.tsx
2026-06-25 23:48:57 +03:00

84 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { Bot } from 'lucide-react';
import { useEffect, useMemo, useState } from 'react';
import { UserAvatar } from '@/components/id/user-avatar';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { ChatRoom, apiFetch } from '@/lib/api';
import { cn } from '@/lib/utils';
interface ChatRoomAvatarDisplayProps {
room: ChatRoom;
viewerUserId: string;
token: string | null;
className?: string;
size?: 'sm' | 'md';
}
function sizeClass(size: 'sm' | 'md') {
return size === 'sm' ? 'h-9 w-9 text-xs' : 'h-11 w-11 text-sm';
}
export function ChatRoomAvatarDisplay({ room, viewerUserId, token, className, size = 'md' }: ChatRoomAvatarDisplayProps) {
const [roomAvatarUrl, setRoomAvatarUrl] = useState<string | null>(null);
const peerMember = useMemo(
() =>
room.type === 'DIRECT' || room.type === 'E2E' || room.type === 'BOT'
? room.members.find((member) => member.userId !== viewerUserId)
: undefined,
[room.members, room.type, viewerUserId]
);
useEffect(() => {
if (!token || !room.hasAvatar || room.type === 'DIRECT' || room.type === 'E2E' || room.type === 'BOT') {
setRoomAvatarUrl(null);
return;
}
apiFetch<{ accessUrl: string }>(`/media/chat/${room.id}/avatar/url`, {}, token)
.then((response) => setRoomAvatarUrl(response.accessUrl))
.catch(() => setRoomAvatarUrl(null));
}, [room.hasAvatar, room.id, room.type, token]);
if (peerMember && (room.type === 'DIRECT' || room.type === 'E2E')) {
return (
<UserAvatar
userId={peerMember.userId}
displayName={peerMember.displayName}
hasAvatar={peerMember.hasAvatar}
token={token}
isVerified={peerMember.isVerified}
verificationIcon={peerMember.verificationIcon}
className={cn(sizeClass(size), className)}
badgeSize="xs"
/>
);
}
if (room.type === 'BOT') {
return (
<Avatar className={cn(sizeClass(size), className)}>
<AvatarFallback className="bg-[#3390ec]/15 text-[#3390ec]">
<Bot className={size === 'sm' ? 'h-4 w-4' : 'h-5 w-5'} />
</AvatarFallback>
</Avatar>
);
}
if (room.hasAvatar && roomAvatarUrl) {
return (
<Avatar className={cn(sizeClass(size), className)}>
<AvatarImage src={roomAvatarUrl} alt={room.name} />
<AvatarFallback>{room.name.slice(0, 2).toUpperCase()}</AvatarFallback>
</Avatar>
);
}
return (
<Avatar className={cn(sizeClass(size), className)}>
<AvatarFallback className={room.type === 'GENERAL' ? 'bg-[#3390ec]/15 text-[#3390ec]' : undefined}>
{room.type === 'GENERAL' ? 'В' : room.name.slice(0, 2).toUpperCase()}
</AvatarFallback>
</Avatar>
);
}