import React, { useCallback } from 'react'; import { Avatar, AvatarFallback, AvatarImage, Box, Button, Spinner, Text, as, color } from 'folds'; import { Room } from 'matrix-js-sdk'; import { openInviteUser, selectRoom } from '../../../client/action/navigation'; import { useStateEvent } from '../../hooks/useStateEvent'; import { IRoomCreateContent, Membership, StateEvent } from '../../../types/matrix/room'; import { getMemberDisplayName, getStateEvent } from '../../utils/room'; import { useMatrixClient } from '../../hooks/useMatrixClient'; import { getMxIdLocalPart } from '../../utils/matrix'; import { AsyncStatus, useAsyncCallback } from '../../hooks/useAsyncCallback'; import { timeDayMonthYear, timeHourMinute } from '../../utils/time'; export type RoomIntroProps = { room: Room; }; export const RoomIntro = as<'div', RoomIntroProps>(({ room, ...props }, ref) => { const mx = useMatrixClient(); const createEvent = getStateEvent(room, StateEvent.RoomCreate); const avatarEvent = useStateEvent(room, StateEvent.RoomAvatar); const nameEvent = useStateEvent(room, StateEvent.RoomName); const topicEvent = useStateEvent(room, StateEvent.RoomTopic); const createContent = createEvent?.getContent(); const ts = createEvent?.getTs(); const creatorId = createEvent?.getSender(); const creatorName = creatorId && (getMemberDisplayName(room, creatorId) ?? getMxIdLocalPart(creatorId)); const prevRoomId = createContent?.predecessor?.room_id; const avatarMxc = (avatarEvent?.getContent().url as string) || undefined; const avatarHttpUrl = avatarMxc ? mx.mxcUrlToHttp(avatarMxc) : undefined; const name = (nameEvent?.getContent().name || room.name) as string; const topic = (topicEvent?.getContent().topic as string) || undefined; const [prevRoomState, joinPrevRoom] = useAsyncCallback( useCallback(async (roomId: string) => mx.joinRoom(roomId), [mx]) ); return ( {avatarHttpUrl ? ( ) : ( {name[0]} )} {name} {typeof topic === 'string' ? topic : 'This is the beginning of conversation.'} {creatorName && ts && ( {'Created by '} @{creatorName} {` on ${timeDayMonthYear(ts)} ${timeHourMinute(ts)}`} )} {typeof prevRoomId === 'string' && (mx.getRoom(prevRoomId)?.getMyMembership() === Membership.Join ? ( ) : ( ))} ); });