import React from 'react'; import format from 'date-fns/format'; import { List, ListItem } from '@material-ui/core'; import styled from 'styled-components'; import { useCallback } from 'react'; import { History } from 'history'; import { useChatsQuery } from '../../graphql/types'; import { useGetChatPrefetch } from '../ChatRoomScreen'; const Container = styled.div` height: calc(100% - 56px); overflow-y: overlay; `; const StyledList = styled(List)` padding: 0 !important; `; const StyledListItem = styled(ListItem)` height: 76px; padding: 0 15px; display: flex; `; const ChatPicture = styled.img` height: 50px; width: 50px; object-fit: cover; border-radius: 50%; `; const ChatInfo = styled.div` width: calc(100% - 60px); height: 46px; padding: 15px 0; margin-left: 10px; border-bottom: 0.5px solid silver; position: relative; `; const ChatName = styled.div` margin-top: 5px; `; const MessageContent = styled.div` color: gray; font-size: 15px; margin-top: 5px; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; `; const MessageDate = styled.div` position: absolute; color: gray; top: 20px; right: 0; font-size: 13px; `; interface ChatsListProps { history: History; } const ChatsList: React.FC = ({ history }) => { const navToChat = useCallback( (chat) => { history.push(`chats/${chat.id}`); }, [history] ); const prefetchChat = useGetChatPrefetch(); const { data } = useChatsQuery(); if (data === undefined || data.chats === undefined) { return null; } let chats = data.chats; return ( {chats.map((chat: any) => ( { prefetchChat(chat.id); }}> {chat.name} {chat.lastMessage && ( {chat.lastMessage.content} {format(chat.lastMessage.createdAt, 'HH:mm')} )} ))} ); }; export default ChatsList;