bigbuffet-rw/app/soapbox/features/chats/components/chat-list.tsx

95 lines
2.9 KiB
TypeScript
Raw Normal View History

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-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-11-02 12:28:16 -07:00
import { Spinner, Stack } 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-10-25 05:23:33 -07:00
import { useChats } from 'soapbox/queries/chats';
2022-09-12 13:46:19 -07:00
import ChatListItem from './chat-list-item';
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-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-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-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>
);
}
2022-11-01 05:22:12 -07:00
return null;
2022-10-04 16:22:18 -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}
2022-10-25 05:23:33 -07:00
itemContent={(_index, chat) => (
<div className='px-2'>
<ChatListItem chat={chat} onClick={onClickChat} />
</div>
)
}
2022-10-03 08:03:43 -07:00
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>
);
};
export default ChatList;