pleroma/app/soapbox/features/chats/components/chat-pane/chat-pane.tsx

111 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-08-18 09:52:04 -07:00
import React, { useState } from 'react';
2022-09-22 13:24:11 -07:00
import { Stack } from 'soapbox/components/ui';
2022-08-18 09:52:04 -07:00
import { useChatContext } from 'soapbox/contexts/chat-context';
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';
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-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;
const { unreadChatsCount } = useStatContext();
2022-08-18 09:52:04 -07:00
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();
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) => {
setChat(nextChat);
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-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={unreadChatsCount}
2022-09-09 07:24:25 -07:00
isOpen={isOpen}
onToggle={toggleChatPane}
secondaryAction={() => {
setSearching(true);
setValue(undefined);
2022-09-23 09:53:55 -07:00
if (!isOpen) {
toggleChatPane();
}
}}
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;