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

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-08-18 09:52:04 -07:00
import React from 'react';
import { useDispatch } from 'react-redux';
2022-04-15 13:19:34 -07:00
import { Virtuoso } from 'react-virtuoso';
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-08-18 09:52:04 -07:00
import { Stack } from 'soapbox/components/ui';
import PlaceholderChat from 'soapbox/features/placeholder/components/placeholder-chat';
import { useChats } from 'soapbox/queries/chats';
import Chat from './chat';
2022-08-18 09:52:04 -07:00
import Blankslate from './chat-pane/blankslate';
interface IChatList {
onClickChat: (chat: any) => void,
2022-04-15 13:19:34 -07:00
useWindowScroll?: boolean,
}
2022-04-15 13:19:34 -07:00
const ChatList: React.FC<IChatList> = ({ onClickChat, useWindowScroll = false }) => {
const dispatch = useDispatch();
2022-08-18 09:52:04 -07:00
const { chatsQuery: { data: chats, isFetching } } = useChats();
2022-08-18 09:52:04 -07:00
const isEmpty = chats?.length === 0;
// const handleLoadMore = useCallback(() => {
// if (hasMore && !isLoading) {
// dispatch(expandChats());
// }
// }, [dispatch, hasMore, isLoading]);
const handleLoadMore = () => console.log('load more');
2022-04-15 13:19:34 -07:00
const handleRefresh = () => {
return dispatch(fetchChats()) as any;
};
2022-08-18 09:52:04 -07:00
const renderEmpty = () => {
if (isFetching) {
return (
<Stack space={2}>
<PlaceholderChat />
<PlaceholderChat />
<PlaceholderChat />
</Stack>
);
} else {
return <Blankslate />;
}
};
return (
2022-04-15 13:19:34 -07:00
<PullToRefresh onRefresh={handleRefresh}>
{isEmpty ? renderEmpty() : (
<Virtuoso
useWindowScroll={useWindowScroll}
2022-08-18 09:52:04 -07:00
data={chats}
endReached={handleLoadMore}
2022-08-18 09:52:04 -07:00
itemContent={(_index, chat) => (
<Chat chat={chat} onClick={onClickChat} />
)}
components={{
ScrollSeekPlaceholder: () => <PlaceholderChat />,
2022-08-18 09:52:04 -07:00
// Footer: () => hasMore ? <PlaceholderChat /> : null,
EmptyPlaceholder: renderEmpty,
}}
/>
)}
2022-04-15 13:19:34 -07:00
</PullToRefresh>
);
};
export default ChatList;