feat: adds scroll when a new quote is added to the list (#5516)

This commit is contained in:
Gleidson Daniel Silva 2024-01-26 17:43:57 -03:00 committed by GitHub
parent cedd0b98f2
commit 501e42196d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, { useEffect, useRef } from 'react';
import { FlatList } from 'react-native'; import { FlatList } from 'react-native';
import { Quote } from './Quote'; import { Quote } from './Quote';
@ -6,9 +6,29 @@ import { useRoomContext } from '../../../../views/RoomView/context';
export const Quotes = (): React.ReactElement | null => { export const Quotes = (): React.ReactElement | null => {
const { selectedMessages, action } = useRoomContext(); const { selectedMessages, action } = useRoomContext();
const nQuotesRef = useRef(0);
const listRef = useRef<FlatList>(null);
useEffect(() => {
if (nQuotesRef.current && nQuotesRef.current < selectedMessages.length) {
setTimeout(() => {
listRef.current?.scrollToEnd({ animated: true });
}, 100);
}
nQuotesRef.current = selectedMessages.length;
}, [selectedMessages.length]);
if (action !== 'quote') { if (action !== 'quote') {
return null; return null;
} }
return <FlatList data={selectedMessages} renderItem={({ item }) => <Quote messageId={item} />} horizontal />;
return (
<FlatList
ref={listRef}
data={selectedMessages}
renderItem={({ item }) => <Quote messageId={item} />}
horizontal
keyExtractor={item => item}
/>
);
}; };