import React from 'react'; import { FlatList, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import dayjs from 'dayjs'; import { Avatar, Spinner, useTheme, vw } from 'stream-chat-react-native'; import { MESSAGE_SEARCH_LIMIT } from '../../hooks/usePaginatedSearchedMessages'; import type { MessageResponse } from 'stream-chat'; import type { LocalAttachmentType, LocalChannelType, LocalCommandType, LocalMessageType, LocalReactionType, LocalUserType, } from '../../types'; const styles = StyleSheet.create({ contentContainer: { flexGrow: 1 }, date: { fontSize: 12, marginLeft: 2, textAlign: 'right', }, detailsText: { fontSize: 12 }, flex: { flex: 1 }, indicatorContainer: { alignItems: 'center', height: '100%', justifyContent: 'center', }, itemContainer: { borderBottomWidth: 1, flex: 1, flexDirection: 'row', paddingHorizontal: 8, paddingVertical: 12, }, message: { flexShrink: 1, fontSize: 12, }, row: { alignItems: 'center', flex: 1, flexDirection: 'row', justifyContent: 'space-between', paddingLeft: 8, }, title: { fontSize: 14, fontWeight: '700' }, titleContainer: { maxWidth: vw(80) - 16 - 40, }, }); export type MessageSearchListProps = { EmptySearchIndicator: React.ComponentType; loading: boolean; loadMore: () => void; messages: | MessageResponse< LocalAttachmentType, LocalChannelType, LocalCommandType, LocalMessageType, LocalReactionType, LocalUserType >[] | undefined; refreshing: boolean; refreshList: () => void; showResultCount?: boolean; }; export const MessageSearchList: React.FC = React.forwardRef( ( props, scrollRef: React.Ref > | null>, ) => { const { EmptySearchIndicator, loading, loadMore, messages, refreshing, refreshList, showResultCount = false, } = props; const { theme: { colors: { black, border, grey, white_snow }, }, } = useTheme(); const navigation = useNavigation(); if (loading && !refreshing && (!messages || messages.length === 0)) { return ( ); } if (!messages && !refreshing) return null; return ( <> {messages && showResultCount && ( {`${ messages.length >= MESSAGE_SEARCH_LIMIT ? MESSAGE_SEARCH_LIMIT : messages.length }${messages.length >= MESSAGE_SEARCH_LIMIT ? '+ ' : ' '} result${ messages.length === 1 ? '' : 's' }`} )} !parent_id) : []} keyboardDismissMode='on-drag' ListEmptyComponent={EmptySearchIndicator} onEndReached={loadMore} onRefresh={refreshList} ref={scrollRef} refreshing={refreshing} renderItem={({ item }) => ( { navigation.navigate('ChannelScreen', { channelId: item.channel?.id, messageId: item.id, }); }} style={[styles.itemContainer, { borderBottomColor: border }]} testID='channel-preview-button' > {`${item.user?.name} `} {!!item.channel?.name && ( in {` ${item.channel?.name}`} )} {item.text} {dayjs(item.created_at).calendar(undefined, { lastDay: 'DD/MM', // The day before ( Yesterday at 2:30 AM ) lastWeek: 'DD/MM', // Last week ( Last Monday at 2:30 AM ) sameDay: 'h:mm A', // The same day ( Today at 2:30 AM ) sameElse: 'DD/MM/YYYY', // Everything else ( 17/10/2011 ) })} )} style={styles.flex} /> ); }, );