nitro-react/src/components/chat-history/ChatHistoryView.tsx

129 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-09-17 05:12:43 +02:00
import { ILinkEventTracker } from '@nitrots/nitro-renderer';
2022-07-19 17:29:13 +02:00
import { FC, useEffect, useMemo, useRef, useState } from 'react';
2022-09-20 21:50:35 +02:00
import { AutoSizer, CellMeasurer, CellMeasurerCache, List, ListRowProps, ListRowRenderer } from 'react-virtualized';
2022-09-17 05:12:43 +02:00
import { AddEventLinkTracker, ChatEntryType, LocalizeText, RemoveLinkEventTracker } from '../../api';
2022-03-03 08:23:01 +01:00
import { Flex, NitroCardContentView, NitroCardHeaderView, NitroCardView, Text } from '../../common';
2022-07-19 17:29:13 +02:00
import { useChatHistory } from '../../hooks';
2022-02-21 17:52:11 +01:00
export const ChatHistoryView: FC<{}> = props =>
{
const [ isVisible, setIsVisible ] = useState(false);
2022-09-20 21:50:35 +02:00
const [ searchText, setSearchText ] = useState<string>('');
2022-07-19 17:29:13 +02:00
const { chatHistory = [] } = useChatHistory();
2022-02-21 17:52:11 +01:00
const elementRef = useRef<List>(null);
2022-09-20 21:50:35 +02:00
const cache = useMemo(() => new CellMeasurerCache({ defaultHeight: 35, fixedWidth: true }), []);
2022-02-21 17:52:11 +01:00
2022-09-17 05:11:46 +02:00
const filteredChatHistory = useMemo(() =>
{
if (searchText.length === 0) return chatHistory;
2022-09-20 21:50:35 +02:00
let text = searchText.toLowerCase();
return chatHistory.filter(entry => (entry.message.toLowerCase().includes(text)) || entry.name.includes(text));
2022-09-17 05:11:46 +02:00
}, [ chatHistory, searchText ]);
2022-09-20 21:50:35 +02:00
useEffect(() =>
{
if(elementRef && elementRef.current && isVisible) elementRef.current.scrollToRow(-1);
}, [ isVisible ]);
useEffect(() =>
2022-09-17 06:12:17 +02:00
{
2022-09-20 21:50:35 +02:00
const linkTracker: ILinkEventTracker = {
linkReceived: (url: string) =>
{
const parts = url.split('/');
if(parts.length < 2) return;
switch(parts[1])
{
case 'show':
setIsVisible(true);
return;
case 'hide':
setIsVisible(false);
return;
case 'toggle':
setIsVisible(prevValue => !prevValue);
return;
}
},
eventUrlPrefix: 'chat-history/'
};
AddEventLinkTracker(linkTracker);
return () => RemoveLinkEventTracker(linkTracker);
}, []);
if(!isVisible) return null;
2022-09-17 06:12:17 +02:00
2022-02-21 17:52:11 +01:00
const RowRenderer: ListRowRenderer = (props: ListRowProps) =>
{
2022-09-17 05:11:46 +02:00
const item = filteredChatHistory[props.index];
2022-02-21 17:52:11 +01:00
2022-09-17 05:11:46 +02:00
if (!item) return null;
2022-02-21 17:52:11 +01:00
return (
<CellMeasurer cache={ cache } columnIndex={ 0 } key={ props.key } parent={ props.parent } rowIndex={ props.index }>
2022-09-20 21:50:35 +02:00
<Flex alignItems="center" style={ props.style } className="p-1" gap={ 2 }>
2022-02-21 17:52:11 +01:00
<Text variant="muted">{ item.timestamp }</Text>
{ (item.type === ChatEntryType.TYPE_CHAT) &&
2022-09-17 05:11:46 +02:00
<div className="bubble-container" style={ { position: 'relative' } }>
{ (item.style === 0) &&
<div className="user-container-bg" style={ { backgroundColor: item.color } } /> }
<div className={ `chat-bubble bubble-${ item.style } type-${ item.chatType }` } style={ { maxWidth: '100%' } }>
<div className="user-container">
{ item.imageUrl && (item.imageUrl.length > 0) &&
<div className="user-image" style={ { backgroundImage: `url(${ item.imageUrl })` } } /> }
</div>
<div className="chat-content">
<b className="username mr-1" dangerouslySetInnerHTML={ { __html: `${ item.name }: ` } } />
<span className="message" dangerouslySetInnerHTML={ { __html: `${ item.message }` } } />
</div>
</div>
</div> }
2022-02-21 17:52:11 +01:00
{ (item.type === ChatEntryType.TYPE_ROOM_INFO) &&
<>
<i className="icon icon-small-room" />
<Text textBreak wrap grow>{ item.name }</Text>
</> }
</Flex>
</CellMeasurer>
);
};
return (
2022-07-19 17:29:13 +02:00
<NitroCardView uniqueKey="chat-history" className="nitro-chat-history" theme="primary-slim">
<NitroCardHeaderView headerText={ LocalizeText('room.chathistory.button.text') } onCloseClick={ event => setIsVisible(false) }/>
2022-09-20 21:50:35 +02:00
<NitroCardContentView overflow="hidden">
2022-09-17 05:11:46 +02:00
<Flex column fullHeight gap={ 2 }>
<input type="text" className="form-control form-control-sm" placeholder={ LocalizeText('generic.search') } value={ searchText } onChange={ event => setSearchText(event.target.value) } />
<div className="h-100">
2022-09-20 21:50:35 +02:00
<AutoSizer defaultWidth={ 300 } defaultHeight={ 170 }>
2022-09-17 05:11:46 +02:00
{ ({ height, width }) =>
{
2022-09-20 21:50:35 +02:00
cache.clearAll();
2022-09-17 05:11:46 +02:00
return (
<List
ref={ elementRef }
width={ width }
height={ height }
2022-09-20 21:50:35 +02:00
rowCount={ filteredChatHistory.length }
rowHeight={ 35 }
2022-09-17 05:11:46 +02:00
className={ 'chat-history-list' }
rowRenderer={ RowRenderer }
deferredMeasurementCache={ cache } />
)
} }
</AutoSizer>
</div>
</Flex>
2022-07-19 17:29:13 +02:00
</NitroCardContentView>
</NitroCardView>
2022-02-21 17:52:11 +01:00
);
}