import sumBy from 'lodash/sumBy'; import React, { useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { Icon, Input, Stack } from 'soapbox/components/ui'; import { useChatContext } from 'soapbox/contexts/chat-context'; import { useDebounce } from 'soapbox/hooks'; import { IChat, useChats } from 'soapbox/queries/chats'; import ChatList from '../chat-list'; import ChatPaneHeader from '../chat-pane-header'; import ChatSearch from '../chat-search/chat-search'; import EmptyResultsBlankslate from '../chat-search/empty-results-blankslate'; import ChatWindow from '../chat-window'; import { Pane } from '../ui'; import Blankslate from './blankslate'; const messages = defineMessages({ searchPlaceholder: { id: 'chats.search_placeholder', defaultMessage: 'Search inbox' }, }); const ChatPane = () => { const intl = useIntl(); const debounce = useDebounce; const [value, setValue] = useState(); const debouncedValue = debounce(value as string, 300); const { chat, setChat, isOpen, isSearching, setSearching, toggleChatPane } = useChatContext(); const { chatsQuery: { data: chats } } = useChats(debouncedValue); const unreadCount = sumBy(chats, (chat) => chat.unread); const hasSearchValue = Number(debouncedValue?.length) > 0; const handleClickChat = (chat: IChat) => { setChat(chat); setValue(undefined); }; const clearValue = () => { if (hasSearchValue) { setValue(''); } }; const renderBody = () => { if (hasSearchValue || Number(chats?.length) > 0) { return (
setValue(event.target.value)} isSearch append={ } />
{Number(chats?.length) > 0 ? ( ) : ( )}
); } else if (chats?.length === 0) { return ( setSearching(true)} /> ); } }; // Active chat if (chat?.id) { return ( ); } if (isSearching) { return ; } return ( { setSearching(true); setValue(undefined); }} secondaryActionIcon={require('@tabler/icons/edit.svg')} /> {isOpen ? renderBody() : null} ); }; export default ChatPane;