From 05068460e07f92002ee5d51d3f1a2b0889bc10de Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 15 Apr 2022 15:19:34 -0500 Subject: [PATCH] ChatList: use Virtuoso --- app/soapbox/actions/chats.js | 6 +- .../features/chats/components/chat_list.tsx | 72 +++++++++++-------- app/soapbox/features/chats/index.tsx | 8 +-- app/styles/chats.scss | 6 +- 4 files changed, 51 insertions(+), 41 deletions(-) diff --git a/app/soapbox/actions/chats.js b/app/soapbox/actions/chats.js index 8003bb8cb..0a8d1d75c 100644 --- a/app/soapbox/actions/chats.js +++ b/app/soapbox/actions/chats.js @@ -61,14 +61,14 @@ export function fetchChatsV2() { export function fetchChats() { return (dispatch, getState) => { const state = getState(); - const instance = state.get('instance'); + const { instance } = state; const features = getFeatures(instance); dispatch({ type: CHATS_FETCH_REQUEST }); if (features.chatsV2) { - dispatch(fetchChatsV2()); + return dispatch(fetchChatsV2()); } else { - dispatch(fetchChatsV1()); + return dispatch(fetchChatsV1()); } }; } diff --git a/app/soapbox/features/chats/components/chat_list.tsx b/app/soapbox/features/chats/components/chat_list.tsx index 874c71041..2608a8f03 100644 --- a/app/soapbox/features/chats/components/chat_list.tsx +++ b/app/soapbox/features/chats/components/chat_list.tsx @@ -1,12 +1,13 @@ import { Map as ImmutableMap } from 'immutable'; -import { debounce } from 'lodash'; -import React from 'react'; +import React, { useCallback } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useDispatch } from 'react-redux'; +import { Virtuoso } from 'react-virtuoso'; import { createSelector } from 'reselect'; -import { expandChats } from 'soapbox/actions/chats'; -import ScrollableList from 'soapbox/components/scrollable_list'; +import { fetchChats, expandChats } from 'soapbox/actions/chats'; +import PullToRefresh from 'soapbox/components/pull-to-refresh'; +import { Text } from 'soapbox/components/ui'; import PlaceholderChat from 'soapbox/features/placeholder/components/placeholder_chat'; import { useAppSelector } from 'soapbox/hooks'; @@ -16,10 +17,6 @@ const messages = defineMessages({ emptyMessage: { id: 'chat_panels.main_window.empty', defaultMessage: 'No chats found. To start a chat, visit a user\'s profile' }, }); -const handleLoadMore = debounce((dispatch) => { - dispatch(expandChats()); -}, 300, { leading: true }); - const getSortedChatIds = (chats: ImmutableMap) => ( chats .toList() @@ -45,10 +42,10 @@ const sortedChatIdsSelector = createSelector( interface IChatList { onClickChat: (chat: any) => void, - onRefresh: () => void, + useWindowScroll?: boolean, } -const ChatList: React.FC = ({ onClickChat, onRefresh }) => { +const ChatList: React.FC = ({ onClickChat, useWindowScroll = false }) => { const dispatch = useDispatch(); const intl = useIntl(); @@ -56,28 +53,41 @@ const ChatList: React.FC = ({ onClickChat, onRefresh }) => { const hasMore = useAppSelector(state => !!state.chats.get('next')); const isLoading = useAppSelector(state => state.chats.get('isLoading')); + const handleLoadMore = useCallback(() => { + if (hasMore && !isLoading) { + dispatch(expandChats()); + } + }, [dispatch, hasMore, isLoading]); + + const handleRefresh = () => { + return dispatch(fetchChats()) as any; + }; + return ( - handleLoadMore(dispatch)} - onRefresh={onRefresh} - placeholderComponent={PlaceholderChat} - placeholderCount={20} - > - {chatIds.map((chatId: string) => ( -
- -
- ))} -
+ + ( + + )} + components={{ + ScrollSeekPlaceholder: () => , + Footer: () => hasMore ? : null, + EmptyPlaceholder: () => { + if (isLoading) { + return ( + <>{Array(20).fill()} + ); + } else { + return {intl.formatMessage(messages.emptyMessage)}; + } + }, + }} + /> + ); }; diff --git a/app/soapbox/features/chats/index.tsx b/app/soapbox/features/chats/index.tsx index c13335ff3..cd53ca1c9 100644 --- a/app/soapbox/features/chats/index.tsx +++ b/app/soapbox/features/chats/index.tsx @@ -3,7 +3,7 @@ import { defineMessages, useIntl } from 'react-intl'; import { useDispatch } from 'react-redux'; import { useHistory } from 'react-router-dom'; -import { fetchChats, launchChat } from 'soapbox/actions/chats'; +import { launchChat } from 'soapbox/actions/chats'; import AccountSearch from 'soapbox/components/account_search'; import AudioToggle from 'soapbox/features/chats/components/audio_toggle'; @@ -29,10 +29,6 @@ const ChatIndex: React.FC = () => { history.push(`/chats/${chat.id}`); }; - const handleRefresh = () => { - return dispatch(fetchChats()); - }; - return (
@@ -46,7 +42,7 @@ const ChatIndex: React.FC = () => { ); diff --git a/app/styles/chats.scss b/app/styles/chats.scss index 72d845c28..f41aaf4e9 100644 --- a/app/styles/chats.scss +++ b/app/styles/chats.scss @@ -90,12 +90,16 @@ &__content { @apply flex flex-1 flex-col overflow-hidden bg-white dark:bg-slate-800; + > div { + @apply max-h-full; + } + .chat-box { @apply flex flex-1 flex-col overflow-hidden; } .chat-list { - @apply overflow-y-auto; + @apply overflow-y-auto max-h-full; } }