Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Adds reactivity to contact history message list #33505

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
TextInput,
Throbber,
} from '@rocket.chat/fuselage';
import { useSetting, useTranslation, useUserPreference } from '@rocket.chat/ui-contexts';
import { useResizeObserver } from '@rocket.chat/fuselage-hooks';
import { useSetting, useTranslation, useUserPreference, useUserId } from '@rocket.chat/ui-contexts';
import type { ChangeEvent, ReactElement } from 'react';
import React, { useMemo, useState } from 'react';
import { Virtuoso } from 'react-virtuoso';
Expand Down Expand Up @@ -43,9 +44,15 @@ const ContactHistoryMessagesList = ({ chatId, onClose, onOpenRoom }: ContactHist
const t = useTranslation();
const [text, setText] = useState('');
const showUserAvatar = !!useUserPreference<boolean>('displayAvatars');
const userId = useUserId();

const { ref, contentBoxSize: { inlineSize = 378, blockSize = 1 } = {} } = useResizeObserver<HTMLElement>({
debounceDelay: 200,
});

const { itemsList: messageList, loadMoreItems } = useHistoryMessageList(
useMemo(() => ({ roomId: chatId, filter: text }), [chatId, text]),
userId,
);

const handleSearchChange = (event: ChangeEvent<HTMLInputElement>): void => {
Expand All @@ -59,7 +66,7 @@ const ContactHistoryMessagesList = ({ chatId, onClose, onOpenRoom }: ContactHist
<>
<ContextualbarHeader>
<ContextualbarIcon name='history' />
<ContextualbarTitle>{t('Chat_History')}</ContextualbarTitle>
<ContextualbarTitle>{t('Conversation')}</ContextualbarTitle>
<ContextualbarClose onClick={onClose} />
</ContextualbarHeader>

Expand Down Expand Up @@ -97,14 +104,22 @@ const ContactHistoryMessagesList = ({ chatId, onClose, onOpenRoom }: ContactHist
</States>
)}
{phase !== AsyncStatePhase.LOADING && totalItemCount === 0 && <ContextualbarEmptyContent title={t('No_results_found')} />}
<Box flexGrow={1} flexShrink={1} overflow='hidden' display='flex'>
<Box flexGrow={1} flexShrink={1} overflow='hidden' display='flex' ref={ref}>
{!error && totalItemCount > 0 && history.length > 0 && (
<Virtuoso
totalCount={totalItemCount}
initialTopMostItemIndex={{ index: 'LAST' }}
followOutput
style={{
height: blockSize,
width: inlineSize,
}}
endReached={
phase === AsyncStatePhase.LOADING
? (): void => undefined
: (start): unknown => loadMoreItems(start, Math.min(50, totalItemCount - start))
: (start): void => {
loadMoreItems(start, Math.min(50, totalItemCount - start));
}
}
overscan={25}
data={messages}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { IUser } from '@rocket.chat/core-typings';
import { useEndpoint } from '@rocket.chat/ui-contexts';
import { useCallback, useState } from 'react';
import { useCallback, useMemo, useState } from 'react';

import { useScrollableMessageList } from '../../../../hooks/lists/useScrollableMessageList';
import { useStreamUpdatesForMessageList } from '../../../../hooks/lists/useStreamUpdatesForMessageList';
import { useComponentDidUpdate } from '../../../../hooks/useComponentDidUpdate';
import { MessageList } from '../../../../lib/lists/MessageList';
import { getConfig } from '../../../../lib/utils/getConfig';

type HistoryMessageListOptions = {
filter: string;
Expand All @@ -12,6 +15,7 @@ type HistoryMessageListOptions = {

export const useHistoryMessageList = (
options: HistoryMessageListOptions,
uid: IUser['_id'] | null,
): {
itemsList: MessageList;
initialItemCount: number;
Expand Down Expand Up @@ -42,7 +46,12 @@ export const useHistoryMessageList = (
[getMessages, options.filter],
);

const { loadMoreItems, initialItemCount } = useScrollableMessageList(itemsList, fetchMessages, 25);
const { loadMoreItems, initialItemCount } = useScrollableMessageList(
itemsList,
fetchMessages,
useMemo(() => parseInt(`${getConfig('historyMessageListSize', 10)}`), []),
dougfabris marked this conversation as resolved.
Show resolved Hide resolved
);
useStreamUpdatesForMessageList(itemsList, uid, options.roomId);

return {
itemsList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const ChatTable = () => {
const query = useMemo(
() => ({
sort: `{ "${sortBy}": ${sortDirection === 'asc' ? 1 : -1} }`,
open: false,
roomName: text || '',
agents: userIdLoggedIn ? [userIdLoggedIn] : [],
...(itemsPerPage && { count: itemsPerPage }),
Expand Down
Loading