2022-08-31 10:24:01 -07:00
|
|
|
import classNames from 'clsx';
|
2022-08-26 09:53:19 -07:00
|
|
|
import React, { useRef, useState } from 'react';
|
2022-10-04 16:22:18 -07:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2022-04-15 13:19:34 -07:00
|
|
|
import { Virtuoso } from 'react-virtuoso';
|
2022-04-12 09:52:56 -07:00
|
|
|
|
2022-08-18 09:52:04 -07:00
|
|
|
import { fetchChats } from 'soapbox/actions/chats';
|
2022-04-15 13:19:34 -07:00
|
|
|
import PullToRefresh from 'soapbox/components/pull-to-refresh';
|
2022-10-04 16:22:18 -07:00
|
|
|
import { Spinner, Stack, Text } from 'soapbox/components/ui';
|
2022-08-18 09:52:04 -07:00
|
|
|
import PlaceholderChat from 'soapbox/features/placeholder/components/placeholder-chat';
|
2022-09-14 07:35:32 -07:00
|
|
|
import { useAppDispatch } from 'soapbox/hooks';
|
2022-09-16 05:57:09 -07:00
|
|
|
import { useChats, useChatSilences } from 'soapbox/queries/chats';
|
2022-04-12 09:52:56 -07:00
|
|
|
|
2022-09-12 13:46:19 -07:00
|
|
|
import ChatListItem from './chat-list-item';
|
2022-04-12 09:52:56 -07:00
|
|
|
|
|
|
|
interface IChatList {
|
|
|
|
onClickChat: (chat: any) => void,
|
2022-04-15 13:19:34 -07:00
|
|
|
useWindowScroll?: boolean,
|
2022-09-09 07:24:25 -07:00
|
|
|
searchValue?: string
|
2022-04-12 09:52:56 -07:00
|
|
|
}
|
|
|
|
|
2022-09-27 07:50:01 -07:00
|
|
|
const ChatList: React.FC<IChatList> = ({ onClickChat, useWindowScroll = false, searchValue }) => {
|
2022-09-14 07:35:32 -07:00
|
|
|
const dispatch = useAppDispatch();
|
2022-04-12 09:52:56 -07:00
|
|
|
|
2022-08-26 09:53:19 -07:00
|
|
|
const chatListRef = useRef(null);
|
|
|
|
|
2022-09-09 07:24:25 -07:00
|
|
|
const { chatsQuery: { data: chats, isFetching, hasNextPage, fetchNextPage } } = useChats(searchValue);
|
2022-04-12 09:52:56 -07:00
|
|
|
|
2022-09-16 05:57:09 -07:00
|
|
|
const { data: chatSilences } = useChatSilences();
|
|
|
|
|
2022-08-26 09:53:19 -07:00
|
|
|
const [isNearBottom, setNearBottom] = useState<boolean>(false);
|
|
|
|
const [isNearTop, setNearTop] = useState<boolean>(true);
|
|
|
|
|
2022-08-26 09:41:25 -07:00
|
|
|
const handleLoadMore = () => {
|
|
|
|
if (hasNextPage && !isFetching) {
|
|
|
|
fetchNextPage();
|
|
|
|
}
|
|
|
|
};
|
2022-04-15 13:19:34 -07:00
|
|
|
|
2022-10-03 08:03:43 -07:00
|
|
|
const handleRefresh = () => dispatch(fetchChats());
|
2022-04-15 13:19:34 -07:00
|
|
|
|
2022-10-04 16:22:18 -07:00
|
|
|
const renderEmpty = () => {
|
|
|
|
if (isFetching) {
|
|
|
|
return (
|
|
|
|
<Stack space={2}>
|
|
|
|
<PlaceholderChat />
|
|
|
|
<PlaceholderChat />
|
|
|
|
<PlaceholderChat />
|
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Stack className='p-6' space={2}>
|
|
|
|
<Text size='2xl' weight='bold' tag='h3'>
|
|
|
|
<FormattedMessage
|
|
|
|
id='chats.getting_started.title'
|
|
|
|
defaultMessage='Getting started'
|
|
|
|
/>
|
|
|
|
</Text>
|
|
|
|
|
|
|
|
<Text theme='muted'>
|
|
|
|
<FormattedMessage
|
|
|
|
id='chats.getting_started.hint'
|
|
|
|
defaultMessage='To start a chat, search for a user in the field above.'
|
|
|
|
/>
|
|
|
|
</Text>
|
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2022-07-18 14:00:58 -07:00
|
|
|
|
2022-04-12 09:52:56 -07:00
|
|
|
return (
|
2022-08-26 09:53:19 -07:00
|
|
|
<div className='relative h-full'>
|
|
|
|
<PullToRefresh onRefresh={handleRefresh}>
|
2022-10-03 08:03:43 -07:00
|
|
|
<Virtuoso
|
|
|
|
ref={chatListRef}
|
|
|
|
atTopStateChange={(atTop) => setNearTop(atTop)}
|
|
|
|
atBottomStateChange={(atBottom) => setNearBottom(atBottom)}
|
|
|
|
useWindowScroll={useWindowScroll}
|
|
|
|
data={chats}
|
|
|
|
endReached={handleLoadMore}
|
|
|
|
itemContent={(_index, chat) => {
|
|
|
|
const chatSilence = chatSilences?.find((chatSilence) => String(chatSilence.target_account_id) === chat.account.id);
|
|
|
|
return (
|
|
|
|
<div className='px-2'>
|
|
|
|
<ChatListItem chat={chat} onClick={onClickChat} chatSilence={chatSilence} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
components={{
|
|
|
|
ScrollSeekPlaceholder: () => <PlaceholderChat />,
|
|
|
|
Footer: () => hasNextPage ? <Spinner withText={false} /> : null,
|
|
|
|
EmptyPlaceholder: renderEmpty,
|
|
|
|
}}
|
|
|
|
/>
|
2022-08-26 09:53:19 -07:00
|
|
|
</PullToRefresh>
|
|
|
|
|
2022-09-27 07:50:01 -07:00
|
|
|
<>
|
|
|
|
<div
|
|
|
|
className={classNames('inset-x-0 top-0 flex rounded-t-lg justify-center bg-gradient-to-b from-white to-transparent pb-12 pt-8 pointer-events-none dark:from-gray-900 absolute transition-opacity duration-500', {
|
|
|
|
'opacity-0': isNearTop,
|
|
|
|
'opacity-100': !isNearTop,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
className={classNames('inset-x-0 bottom-0 flex rounded-b-lg justify-center bg-gradient-to-t from-white to-transparent pt-12 pb-8 pointer-events-none dark:from-gray-900 absolute transition-opacity duration-500', {
|
|
|
|
'opacity-0': isNearBottom,
|
|
|
|
'opacity-100': !isNearBottom,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</>
|
2022-08-26 09:53:19 -07:00
|
|
|
</div>
|
2022-04-12 09:52:56 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChatList;
|