2022-08-18 09:52:04 -07:00
|
|
|
import sumBy from 'lodash/sumBy';
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
|
2022-09-14 07:35:32 -07:00
|
|
|
import { Icon, Input, Stack } from 'soapbox/components/ui';
|
2022-08-18 09:52:04 -07:00
|
|
|
import { useChatContext } from 'soapbox/contexts/chat-context';
|
2022-09-14 07:35:32 -07:00
|
|
|
import { useDebounce } from 'soapbox/hooks';
|
2022-08-26 09:41:25 -07:00
|
|
|
import { IChat, useChats } from 'soapbox/queries/chats';
|
2022-08-18 09:52:04 -07:00
|
|
|
|
|
|
|
import ChatList from '../chat-list';
|
|
|
|
import ChatPaneHeader from '../chat-pane-header';
|
2022-09-13 08:55:13 -07:00
|
|
|
import ChatSearch from '../chat-search/chat-search';
|
2022-09-14 07:35:32 -07:00
|
|
|
import EmptyResultsBlankslate from '../chat-search/empty-results-blankslate';
|
2022-08-18 09:52:04 -07:00
|
|
|
import ChatWindow from '../chat-window';
|
|
|
|
import { Pane } from '../ui';
|
|
|
|
|
2022-09-14 07:35:32 -07:00
|
|
|
import Blankslate from './blankslate';
|
|
|
|
|
2022-08-18 09:52:04 -07:00
|
|
|
const messages = defineMessages({
|
2022-09-09 07:24:25 -07:00
|
|
|
searchPlaceholder: { id: 'chats.search_placeholder', defaultMessage: 'Search inbox' },
|
2022-08-18 09:52:04 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
const ChatPane = () => {
|
|
|
|
const intl = useIntl();
|
|
|
|
const debounce = useDebounce;
|
|
|
|
|
|
|
|
const [value, setValue] = useState<string>();
|
|
|
|
const debouncedValue = debounce(value as string, 300);
|
|
|
|
|
2022-09-09 07:24:25 -07:00
|
|
|
const { chat, setChat, isOpen, isSearching, setSearching, toggleChatPane } = useChatContext();
|
|
|
|
const { chatsQuery: { data: chats } } = useChats(debouncedValue);
|
2022-08-18 09:52:04 -07:00
|
|
|
|
|
|
|
const unreadCount = sumBy(chats, (chat) => chat.unread);
|
|
|
|
|
2022-09-14 07:35:32 -07:00
|
|
|
const hasSearchValue = Number(debouncedValue?.length) > 0;
|
2022-09-09 07:24:25 -07:00
|
|
|
|
2022-09-13 08:55:13 -07:00
|
|
|
const handleClickChat = (chat: IChat) => {
|
|
|
|
setChat(chat);
|
|
|
|
setValue(undefined);
|
|
|
|
};
|
2022-08-18 09:52:04 -07:00
|
|
|
|
|
|
|
const clearValue = () => {
|
|
|
|
if (hasSearchValue) {
|
|
|
|
setValue('');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderBody = () => {
|
2022-09-09 07:24:25 -07:00
|
|
|
if (hasSearchValue || Number(chats?.length) > 0) {
|
2022-08-18 09:52:04 -07:00
|
|
|
return (
|
|
|
|
<Stack space={4} className='flex-grow h-full'>
|
|
|
|
<div className='px-4'>
|
|
|
|
<Input
|
|
|
|
type='text'
|
|
|
|
autoFocus
|
|
|
|
placeholder={intl.formatMessage(messages.searchPlaceholder)}
|
|
|
|
className='rounded-full'
|
|
|
|
value={value || ''}
|
|
|
|
onChange={(event) => setValue(event.target.value)}
|
|
|
|
isSearch
|
|
|
|
append={
|
|
|
|
<button onClick={clearValue}>
|
|
|
|
<Icon
|
|
|
|
src={hasSearchValue ? require('@tabler/icons/x.svg') : require('@tabler/icons/search.svg')}
|
|
|
|
className='h-4 w-4 text-gray-700 dark:text-gray-600'
|
|
|
|
aria-hidden='true'
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2022-09-09 07:24:25 -07:00
|
|
|
{Number(chats?.length) > 0 ? (
|
|
|
|
<ChatList
|
|
|
|
searchValue={debouncedValue}
|
|
|
|
onClickChat={handleClickChat}
|
2022-09-12 11:50:02 -07:00
|
|
|
fade
|
2022-09-09 07:24:25 -07:00
|
|
|
/>
|
|
|
|
) : (
|
2022-09-14 07:35:32 -07:00
|
|
|
<EmptyResultsBlankslate />
|
2022-09-09 07:24:25 -07:00
|
|
|
)}
|
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
} else if (chats?.length === 0) {
|
|
|
|
return (
|
2022-09-14 07:35:32 -07:00
|
|
|
<Blankslate onSearch={() => setSearching(true)} />
|
2022-09-09 07:24:25 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Active chat
|
|
|
|
if (chat?.id) {
|
|
|
|
return (
|
|
|
|
<Pane isOpen={isOpen} index={0} main>
|
|
|
|
<ChatWindow />
|
|
|
|
</Pane>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSearching) {
|
|
|
|
return <ChatSearch />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Pane isOpen={isOpen} index={0} main>
|
|
|
|
<ChatPaneHeader
|
|
|
|
title='Messages'
|
|
|
|
unreadCount={unreadCount}
|
|
|
|
isOpen={isOpen}
|
|
|
|
onToggle={toggleChatPane}
|
2022-09-13 08:55:13 -07:00
|
|
|
secondaryAction={() => {
|
|
|
|
setSearching(true);
|
|
|
|
setValue(undefined);
|
|
|
|
}}
|
2022-09-09 07:24:25 -07:00
|
|
|
secondaryActionIcon={require('@tabler/icons/edit.svg')}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{isOpen ? renderBody() : null}
|
2022-08-18 09:52:04 -07:00
|
|
|
</Pane>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChatPane;
|