import { ChatRecordData, UserProfileComposer } from '@nitrots/nitro-renderer'; import { CSSProperties, FC, Key, useCallback } from 'react'; import { AutoSizer, CellMeasurer, CellMeasurerCache, List, ListRowProps } from 'react-virtualized'; import { TryVisitRoom } from '../../../../api'; import { Base, Button, Column, Flex, Grid, Text } from '../../../../common'; import { ModToolsOpenRoomInfoEvent } from '../../../../events/mod-tools/ModToolsOpenRoomInfoEvent'; import { dispatchUiEvent, SendMessageHook } from '../../../../hooks'; interface ChatlogViewProps { records: ChatRecordData[]; } export const ChatlogView: FC = props => { const { records = null } = props; const rowRenderer = (props: ListRowProps) => { let chatlogEntry = records[0].chatlog[props.index]; return ( { chatlogEntry.timestamp } SendMessageHook(new UserProfileComposer(chatlogEntry.userId)) }>{ chatlogEntry.userName } { chatlogEntry.message } ); }; const advancedRowRenderer = (props: ListRowProps) => { let chatlogEntry = null; let currentRecord: ChatRecordData = null; let isRoomInfo = false; let totalIndex = 0; for(let i = 0; i < records.length; i++) { currentRecord = records[i]; totalIndex++; // row for room info totalIndex = (totalIndex + currentRecord.chatlog.length); if(props.index > (totalIndex - 1)) continue; if((props.index + 1) === (totalIndex - currentRecord.chatlog.length)) { isRoomInfo = true; break; } const index = (props.index - (totalIndex - currentRecord.chatlog.length)); chatlogEntry = currentRecord.chatlog[index]; break; } return ( { (isRoomInfo && currentRecord) && } { !isRoomInfo && { chatlogEntry.timestamp } SendMessageHook(new UserProfileComposer(chatlogEntry.userId)) }>{ chatlogEntry.userName } { chatlogEntry.message } } ); } const getNumRowsForAdvanced = useCallback(() => { let count = 0; for(let i = 0; i < records.length; i++) { count++; // add room info row count = count + records[i].chatlog.length; } return count; }, [records]); const RoomInfo = (props: { roomId: number, roomName: string, uniqueKey: Key, style: CSSProperties }) => { return ( Room name: { props.roomName } ); } const cache = new CellMeasurerCache({ defaultHeight: 25, fixedWidth: true }); return ( <> { (records && (records.length === 1)) && } Time User Message { (records && (records.length > 0)) && { ({ height, width }) => { cache.clearAll(); return ( 1) ? getNumRowsForAdvanced() : records[0].chatlog.length } rowHeight={ cache.rowHeight } className={ 'log-entry-container' } rowRenderer={ (records.length > 1) ? advancedRowRenderer : rowRenderer } deferredMeasurementCache={ cache } /> ); } } } ); }