import { useCallback, useEffect } from 'react'; import { Col, StyleSystemThemedProps } from 'components/layout'; import { Text, Avatar, Chip, Visible, TouchableTile } from 'components'; import { RoomMember, RoomTypeEnum, useMultipleRoomsQuery } from 'expo-schema'; import { metrics } from 'config'; import { getActiveRoomId } from 'utils/room'; import { useProfile, useRouting } from 'hooks'; const { LIMITS } = metrics; export const Direct: React.FC = ({ openRoom, ...props }) => { const { routeSegments } = useRouting(); const { profile } = useProfile(); const { data } = useMultipleRoomsQuery({ variables: { input: { type: RoomTypeEnum.Direct, limit: LIMITS.rooms, offset: 0, }, }, }); const getVariant = useCallback( (roomId) => { return routeSegments[0] === 'rooms' && routeSegments.includes(roomId) ? 'hover' : 'text'; }, [routeSegments] ); const opnLastActiveRoom = useCallback(async () => { const lastActiveRoom = await getActiveRoomId(); if (!lastActiveRoom) { const [firstRoom] = data?.multipleRooms || []; if (firstRoom) { openRoom(firstRoom.id); } } }, [data?.multipleRooms, openRoom]); useEffect(() => { opnLastActiveRoom(); }, [opnLastActiveRoom]); const getRoomNameLogo = useCallback( (members: RoomMember[]): LogoResponseType => { const participantMember = members.find((member: RoomMember) => member.userId != profile?.id); const profileUrl = participantMember?.userProfilePhotoUrl; const profilePhotoUrl = profileUrl ? (profileUrl as string) : null; const name = participantMember?.userName.first as string; return { profilePhotoUrl, name }; }, [profile?.id] ); const directRooms = data?.multipleRooms; const isDirectRooms = directRooms && directRooms?.length > 0; return ( DIRECT ROOMS {directRooms?.map((room) => ( openRoom(room.id)} variant={getVariant(room.id)} title={getRoomNameLogo(room.members as RoomMember[]).name} px="xs" leftElement={ } rightElement={ } sizeVariant="large" /> ))} ); }; type AllRoomType = StyleSystemThemedProps & DirectProps; interface DirectProps { openRoom: (id: string) => void; } type LogoResponseType = { profilePhotoUrl: string | null; name: string; };