2023-09-21 18:32:47 +00:00
|
|
|
import React, { forwardRef, useImperativeHandle } from 'react';
|
2023-10-13 19:34:59 +00:00
|
|
|
import { Platform, StyleSheet, View } from 'react-native';
|
2021-05-26 17:24:54 +00:00
|
|
|
|
2022-03-02 14:18:01 +00:00
|
|
|
import ActivityIndicator from '../../../containers/ActivityIndicator';
|
2023-10-13 19:34:59 +00:00
|
|
|
import { isAndroid, useDebounce } from '../../../lib/methods/helpers';
|
2023-10-09 16:18:02 +00:00
|
|
|
import { EmptyRoom, List } from './components';
|
2023-09-21 18:32:47 +00:00
|
|
|
import { IListContainerProps, IListContainerRef, IListProps } from './definitions';
|
2023-10-13 19:34:59 +00:00
|
|
|
import { useMessages, useScroll } from './hooks';
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
inverted: {
|
|
|
|
...Platform.select({
|
|
|
|
android: {
|
|
|
|
scaleY: -1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Container = ({ children }: { children: React.ReactElement }) =>
|
|
|
|
isAndroid ? <View style={{ flex: 1, scaleY: -1 }}>{children}</View> : <>{children}</>;
|
2022-09-22 12:49:59 +00:00
|
|
|
|
2023-09-21 18:32:47 +00:00
|
|
|
const ListContainer = forwardRef<IListContainerRef, IListContainerProps>(
|
|
|
|
({ rid, tmid, renderRow, showMessageInMainThread, serverVersion, hideSystemMessages, listRef, loading }, ref) => {
|
|
|
|
const [messages, messagesIds, fetchMessages] = useMessages({
|
|
|
|
rid,
|
|
|
|
tmid,
|
|
|
|
showMessageInMainThread,
|
|
|
|
serverVersion,
|
|
|
|
hideSystemMessages
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2023-09-21 18:32:47 +00:00
|
|
|
const {
|
|
|
|
jumpToBottom,
|
|
|
|
jumpToMessage,
|
|
|
|
cancelJumpToMessage,
|
|
|
|
viewabilityConfigCallbackPairs,
|
|
|
|
handleScrollToIndexFailed,
|
|
|
|
highlightedMessageId
|
|
|
|
} = useScroll({ listRef, messagesIds });
|
|
|
|
|
|
|
|
const onEndReached = useDebounce(() => {
|
|
|
|
fetchMessages();
|
|
|
|
}, 300);
|
|
|
|
|
|
|
|
useImperativeHandle(ref, () => ({
|
|
|
|
jumpToMessage,
|
|
|
|
cancelJumpToMessage
|
|
|
|
}));
|
|
|
|
|
|
|
|
const renderFooter = () => {
|
|
|
|
if (loading && rid) {
|
|
|
|
return <ActivityIndicator />;
|
2022-08-19 21:14:37 +00:00
|
|
|
}
|
2023-09-21 18:32:47 +00:00
|
|
|
return null;
|
|
|
|
};
|
2021-05-26 17:24:54 +00:00
|
|
|
|
2023-10-13 19:34:59 +00:00
|
|
|
const renderItem: IListProps['renderItem'] = ({ item, index }) => (
|
|
|
|
<View style={styles.inverted}>{renderRow(item, messages[index + 1], highlightedMessageId)}</View>
|
|
|
|
);
|
2019-04-17 17:01:03 +00:00
|
|
|
|
2019-03-27 20:06:57 +00:00
|
|
|
return (
|
2019-09-16 20:26:32 +00:00
|
|
|
<>
|
2023-09-21 18:32:47 +00:00
|
|
|
<EmptyRoom rid={rid} length={messages.length} />
|
2023-10-13 19:34:59 +00:00
|
|
|
<Container>
|
|
|
|
<List
|
|
|
|
listRef={listRef}
|
|
|
|
data={messages}
|
|
|
|
renderItem={renderItem}
|
|
|
|
onEndReached={onEndReached}
|
|
|
|
ListFooterComponent={renderFooter}
|
|
|
|
onScrollToIndexFailed={handleScrollToIndexFailed}
|
|
|
|
viewabilityConfigCallbackPairs={viewabilityConfigCallbackPairs.current}
|
|
|
|
jumpToBottom={jumpToBottom}
|
|
|
|
isThread={!!tmid}
|
|
|
|
/>
|
|
|
|
</Container>
|
2019-09-16 20:26:32 +00:00
|
|
|
</>
|
2019-03-27 20:06:57 +00:00
|
|
|
);
|
|
|
|
}
|
2023-09-21 18:32:47 +00:00
|
|
|
);
|
2022-05-02 22:58:23 +00:00
|
|
|
|
2021-05-26 17:24:54 +00:00
|
|
|
export default ListContainer;
|