2022-08-18 09:52:04 -07:00
|
|
|
import React, { useState } from 'react';
|
2022-12-06 15:10:16 -08:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-08-18 09:52:04 -07:00
|
|
|
|
2022-09-22 13:24:11 -07:00
|
|
|
import { Stack } from 'soapbox/components/ui';
|
2022-11-02 12:28:16 -07:00
|
|
|
import { ChatWidgetScreens, useChatContext } from 'soapbox/contexts/chat-context';
|
2022-09-27 13:05:19 -07:00
|
|
|
import { useStatContext } from 'soapbox/contexts/stat-context';
|
2022-09-22 14:52:31 -07:00
|
|
|
import { useDebounce, useFeatures } 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';
|
2022-09-22 13:24:11 -07:00
|
|
|
import ChatSearchInput from '../chat-search-input';
|
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-10-17 09:23:03 -07:00
|
|
|
import ChatPaneHeader from '../chat-widget/chat-pane-header';
|
2022-10-17 05:34:19 -07:00
|
|
|
import ChatWindow from '../chat-widget/chat-window';
|
2022-12-08 11:37:04 -08:00
|
|
|
import ChatSearchHeader from '../chat-widget/headers/chat-search-header';
|
2022-08-18 09:52:04 -07:00
|
|
|
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 ChatPane = () => {
|
2022-09-22 14:52:31 -07:00
|
|
|
const features = useFeatures();
|
2022-08-18 09:52:04 -07:00
|
|
|
const debounce = useDebounce;
|
2022-09-27 13:05:19 -07:00
|
|
|
const { unreadChatsCount } = useStatContext();
|
2022-08-18 09:52:04 -07:00
|
|
|
|
|
|
|
const [value, setValue] = useState<string>();
|
|
|
|
const debouncedValue = debounce(value as string, 300);
|
|
|
|
|
2022-11-02 12:28:16 -07:00
|
|
|
const { screen, changeScreen, isOpen, toggleChatPane } = useChatContext();
|
2022-09-23 06:24:20 -07:00
|
|
|
const { chatsQuery: { data: chats, isLoading } } = useChats(debouncedValue);
|
2022-08-18 09:52:04 -07:00
|
|
|
|
2022-09-14 07:35:32 -07:00
|
|
|
const hasSearchValue = Number(debouncedValue?.length) > 0;
|
2022-09-09 07:24:25 -07:00
|
|
|
|
2022-10-26 10:28:50 -07:00
|
|
|
const handleClickChat = (nextChat: IChat) => {
|
2022-11-02 12:28:16 -07:00
|
|
|
changeScreen(ChatWidgetScreens.CHAT, nextChat.id);
|
2022-09-13 08:55:13 -07:00
|
|
|
setValue(undefined);
|
|
|
|
};
|
2022-08-18 09:52:04 -07:00
|
|
|
|
|
|
|
const clearValue = () => {
|
|
|
|
if (hasSearchValue) {
|
|
|
|
setValue('');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderBody = () => {
|
2022-09-23 06:24:20 -07:00
|
|
|
if (hasSearchValue || Number(chats?.length) > 0 || isLoading) {
|
2022-08-18 09:52:04 -07:00
|
|
|
return (
|
|
|
|
<Stack space={4} className='flex-grow h-full'>
|
2022-09-22 14:52:31 -07:00
|
|
|
{features.chatsSearch && (
|
|
|
|
<div className='px-4'>
|
|
|
|
<ChatSearchInput
|
|
|
|
value={value || ''}
|
|
|
|
onChange={(event) => setValue(event.target.value)}
|
|
|
|
onClear={clearValue}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-08-18 09:52:04 -07:00
|
|
|
|
2022-09-23 06:24:20 -07:00
|
|
|
{(Number(chats?.length) > 0 || isLoading) ? (
|
2022-09-09 07:24:25 -07:00
|
|
|
<ChatList
|
|
|
|
searchValue={debouncedValue}
|
|
|
|
onClickChat={handleClickChat}
|
|
|
|
/>
|
|
|
|
) : (
|
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-11-02 12:28:16 -07:00
|
|
|
<Blankslate
|
|
|
|
onSearch={() => {
|
|
|
|
changeScreen(ChatWidgetScreens.SEARCH);
|
|
|
|
}}
|
|
|
|
/>
|
2022-09-09 07:24:25 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Active chat
|
2022-11-02 12:28:16 -07:00
|
|
|
if (screen === ChatWidgetScreens.CHAT || screen === ChatWidgetScreens.CHAT_SETTINGS) {
|
2022-09-09 07:24:25 -07:00
|
|
|
return (
|
|
|
|
<Pane isOpen={isOpen} index={0} main>
|
|
|
|
<ChatWindow />
|
|
|
|
</Pane>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-02 12:28:16 -07:00
|
|
|
if (screen === ChatWidgetScreens.SEARCH) {
|
2022-12-08 11:37:04 -08:00
|
|
|
return (
|
|
|
|
<Pane isOpen={isOpen} index={0} main>
|
|
|
|
<ChatSearchHeader />
|
|
|
|
|
|
|
|
{isOpen ? <ChatSearch /> : null}
|
|
|
|
</Pane>
|
|
|
|
);
|
2022-09-09 07:24:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Pane isOpen={isOpen} index={0} main>
|
|
|
|
<ChatPaneHeader
|
2022-12-06 15:10:16 -08:00
|
|
|
title={<FormattedMessage id='column.chats' defaultMessage='Chats' />}
|
2022-09-27 13:05:19 -07:00
|
|
|
unreadCount={unreadChatsCount}
|
2022-09-09 07:24:25 -07:00
|
|
|
isOpen={isOpen}
|
|
|
|
onToggle={toggleChatPane}
|
2022-09-13 08:55:13 -07:00
|
|
|
secondaryAction={() => {
|
2022-11-02 12:28:16 -07:00
|
|
|
changeScreen(ChatWidgetScreens.SEARCH);
|
2022-09-13 08:55:13 -07:00
|
|
|
setValue(undefined);
|
2022-09-23 09:53:55 -07:00
|
|
|
|
|
|
|
if (!isOpen) {
|
|
|
|
toggleChatPane();
|
|
|
|
}
|
2022-09-13 08:55:13 -07:00
|
|
|
}}
|
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;
|