import React, { useState, useEffect, useRef, useCallback } from 'react'; import { View, Text, TouchableOpacity, StyleSheet, Image, Platform, TouchableHighlight } from 'react-native'; import { HorizontalTabView, Input, PageWrapper } from 'src/components'; import { NAVIGATION_PAGES } from 'src/types'; import { useFocusEffect, useNavigation } from '@react-navigation/native'; import AddChatIcon from 'assets/icons/messages/chat-plus.svg'; import { API_HOST } from 'src/constants'; import { Colors } from 'src/theme'; import { getFontSize } from 'src/utils'; import SwipeableRow from './Components/SwipeableRow'; import { FlashList } from '@shopify/flash-list'; import ReadIcon from 'assets/icons/messages/check-read.svg'; import UnreadIcon from 'assets/icons/messages/check-unread.svg'; import SearchModal from './Components/SearchUsersModal'; import { SheetManager } from 'react-native-actions-sheet'; import MoreModal from './Components/MoreModal'; import SearchIcon from 'assets/icons/search.svg'; import { storage, StoreType } from 'src/storage'; import { usePostGetChatsListQuery } from '@api/chat'; import moment from 'moment'; import { Chat } from './types'; import PinIcon from 'assets/icons/messages/pin.svg'; type Routes = { key: 'all' | 'unread' | 'archived'; title: string; }; const MessagesScreen = () => { const navigation = useNavigation(); const token = storage.get('token', StoreType.STRING) as string; const { data: chatsData, refetch } = usePostGetChatsListQuery(token, 0, true); const [chats, setChats] = useState([]); const [index, setIndex] = useState(0); const [filteredChats, setFilteredChats] = useState([]); const [search, setSearch] = useState(''); const openRowRef = useRef(null); const routes: Routes[] = [ { key: 'all', title: 'All' }, { key: 'unread', title: 'Unread' }, { key: 'archived', title: 'Archived' } ]; const handleRowOpen = (ref: any) => { if (openRowRef.current && openRowRef.current !== ref) { openRowRef.current.close(); } openRowRef.current = ref; }; useFocusEffect(() => { navigation.getParent()?.setOptions({ tabBarStyle: { display: 'flex', ...Platform.select({ android: { height: 58 } }) } }); }); useEffect(() => { if (chatsData && chatsData.conversations) { setChats(chatsData.conversations); } }, [chatsData]); useFocusEffect( useCallback(() => { refetch(); }, []) ); const filterChatsByTab = () => { if (index === 1) { setFilteredChats( chats .filter((chat: Chat) => chat.unread_count > 0) .sort((a: Chat, b: Chat) => b.pin - a.pin || b.pin_order - a.pin_order) ); } else if (index === 2) { setFilteredChats([]); } else { setFilteredChats( chats.sort((a: Chat, b: Chat) => b.pin - a.pin || b.pin_order - a.pin_order) ); } }; useEffect(() => { filterChatsByTab(); }, [index, chats]); const handleDeleteChat = (id: number) => { const updatedChats = chats.filter((chat: Chat) => chat.uid !== id); setChats(updatedChats); }; const searchFilter = (text: string) => { if (text) { const newData = chats?.filter((item: Chat) => { const itemData = item.short ? item.short.toLowerCase() : ''.toLowerCase(); const textData = text.toLowerCase(); return itemData.indexOf(textData) > -1; }) ?? []; setFilteredChats(newData); setSearch(text); } else { filterChatsByTab(); setSearch(text); } }; const formatDate = (dateString: Date) => { const inputDate = moment.utc(dateString).local(); const today = moment().local(); const yesterday = moment().local().subtract(1, 'days'); if (inputDate.isSame(today, 'day')) { return inputDate.format('HH:mm'); } if (inputDate.isSame(yesterday, 'day')) { return 'yesterday'; } if (!inputDate.isSame(today, 'year')) { return inputDate.format('DD.MM.YYYY'); } return inputDate.format('DD.MM'); }; const renderChatItem = ({ item }: { item: Chat }) => { return ( navigation.navigate( ...([ NAVIGATION_PAGES.CHAT, { id: item.uid, name: item.name, avatar: item.avatar } ] as never) ) } underlayColor={Colors.FILL_LIGHT} > {item.name} {item.pin === 1 ? : null} {item.sent_by !== item.uid && item.status === 3 ? ( ) : item.sent_by !== item.uid && (item.status === 2 || item.status === 1) ? ( ) : null} {formatDate(item.updated)} {item.short} {item.unread_count > 0 ? ( {item.unread_count > 99 ? '99+' : item.unread_count} ) : null} ); }; return ( Messages SheetManager.show('search-modal')} style={{ width: 30, alignItems: 'flex-end' }} > searchFilter(text)} value={search} icon={} height={38} /> ( `${item.uid}-${index}`} estimatedItemSize={78} removeClippedSubviews={true} /> )} /> ); }; const styles = StyleSheet.create({ title: { color: Colors.DARK_BLUE, fontFamily: 'redhat-700', fontSize: getFontSize(14) }, header: { alignItems: 'center', paddingVertical: 16, flexDirection: 'row', justifyContent: 'space-between', marginLeft: '5%', marginRight: '5%' }, chatItem: { flexDirection: 'row', paddingVertical: 12, backgroundColor: Colors.WHITE, borderBottomWidth: 1, borderBottomColor: Colors.FILL_LIGHT, gap: 8, paddingHorizontal: '4%' }, avatar: { width: 54, height: 54, borderRadius: 27, borderWidth: 1, borderColor: Colors.FILL_LIGHT }, rowContainer: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }, chatName: { flex: 1, fontFamily: 'montserrat-700', fontSize: getFontSize(14), color: Colors.DARK_BLUE }, chatMessage: { flex: 1, fontSize: getFontSize(12), fontWeight: '600', color: Colors.DARK_BLUE, height: '100%' }, chatTimeContainer: { justifyContent: 'center', alignItems: 'flex-end' }, chatTime: { fontSize: getFontSize(12), fontWeight: '600', color: Colors.LIGHT_GRAY }, unreadBadge: { backgroundColor: Colors.ORANGE, borderRadius: 9, paddingHorizontal: 6, alignItems: 'center', justifyContent: 'center', height: 18, minWidth: 18 }, unreadText: { color: Colors.WHITE, fontSize: getFontSize(11), fontFamily: 'montserrat-700' }, swipeActions: { flexDirection: 'row', alignItems: 'center' }, swipeButton: { paddingHorizontal: 20 } }); export default MessagesScreen;