chore: Migrate List room to ts

This commit is contained in:
AlexAlexandre 2021-12-03 10:06:25 -03:00
parent dc3e21056e
commit a6146bd02b
2 changed files with 53 additions and 27 deletions

View File

@ -18,6 +18,16 @@ const styles = StyleSheet.create({
interface IRoomListProps { interface IRoomListProps {
listRef: any; listRef: any;
onScroll?: any;
scrollEventThrottle?: number;
data?: any;
renderItem?: Function;
onEndReached?: Function;
ListFooterComponent?: Function;
onScrollToIndexFailed?: Function;
onViewableItemsChanged?: Function;
viewabilityConfig?: any;
refreshControl?: any;
} }
const List = ({ listRef, ...props }: IRoomListProps) => ( const List = ({ listRef, ...props }: IRoomListProps) => (

View File

@ -1,10 +1,11 @@
import React from 'react'; import React from 'react';
import { RefreshControl } from 'react-native'; import { RefreshControl } from 'react-native';
import PropTypes from 'prop-types';
import { Q } from '@nozbe/watermelondb'; import { Q } from '@nozbe/watermelondb';
import moment from 'moment'; import moment from 'moment';
import { dequal } from 'dequal'; import { dequal } from 'dequal';
import { Value, event } from 'react-native-reanimated'; import { Value, event } from 'react-native-reanimated';
import { Observable, Subscription } from 'rxjs';
import Model from '@nozbe/watermelondb/Model';
import database from '../../../lib/database'; import database from '../../../lib/database';
import RocketChat from '../../../lib/rocketchat'; import RocketChat from '../../../lib/rocketchat';
@ -20,7 +21,7 @@ import NavBottomFAB from './NavBottomFAB';
const QUERY_SIZE = 50; const QUERY_SIZE = 50;
const onScroll = ({ y }) => const onScroll = ({ y }: any) =>
event( event(
[ [
{ {
@ -32,23 +33,37 @@ const onScroll = ({ y }) =>
{ useNativeDriver: true } { useNativeDriver: true }
); );
class ListContainer extends React.Component { interface IRoomListContainerProps {
static propTypes = { renderRow: Function;
renderRow: PropTypes.func, rid: string;
rid: PropTypes.string, tmid: string;
tmid: PropTypes.string, theme: string;
theme: PropTypes.string, loading: boolean;
loading: PropTypes.bool, listRef: any;
listRef: PropTypes.func, hideSystemMessages: [];
hideSystemMessages: PropTypes.array, tunread: [];
tunread: PropTypes.array, ignored: [];
ignored: PropTypes.array, navigation: any; // TODO - change this after merge with navigation ts;
navigation: PropTypes.object, showMessageInMainThread: boolean;
showMessageInMainThread: PropTypes.bool, serverVersion: string;
serverVersion: PropTypes.string }
};
constructor(props) { class ListContainer extends React.Component<IRoomListContainerProps, any> {
private count: number;
private mounted: boolean;
private animated: boolean;
private jumping: boolean;
private y: any;
private onScroll: (...args: any[]) => void;
private unsubscribeFocus: any;
private viewabilityConfig: { itemVisiblePercentThreshold: number };
private highlightedMessageTimeout: any;
private thread: any;
private messagesObservable?: Observable<Model>;
private messagesSubscription?: Subscription;
private viewableItems: any;
constructor(props: IRoomListContainerProps) {
super(props); super(props);
console.time(`${this.constructor.name} init`); console.time(`${this.constructor.name} init`);
console.time(`${this.constructor.name} mount`); console.time(`${this.constructor.name} mount`);
@ -78,7 +93,7 @@ class ListContainer extends React.Component {
console.timeEnd(`${this.constructor.name} mount`); console.timeEnd(`${this.constructor.name} mount`);
} }
shouldComponentUpdate(nextProps, nextState) { shouldComponentUpdate(nextProps: IRoomListContainerProps, nextState: any) {
const { refreshing, highlightedMessage } = this.state; const { refreshing, highlightedMessage } = this.state;
const { hideSystemMessages, theme, tunread, ignored, loading } = this.props; const { hideSystemMessages, theme, tunread, ignored, loading } = this.props;
if (theme !== nextProps.theme) { if (theme !== nextProps.theme) {
@ -105,7 +120,7 @@ class ListContainer extends React.Component {
return false; return false;
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps: IRoomListContainerProps) {
const { hideSystemMessages } = this.props; const { hideSystemMessages } = this.props;
if (!dequal(hideSystemMessages, prevProps.hideSystemMessages)) { if (!dequal(hideSystemMessages, prevProps.hideSystemMessages)) {
this.reload(); this.reload();
@ -170,7 +185,7 @@ class ListContainer extends React.Component {
if (rid) { if (rid) {
this.unsubscribeMessages(); this.unsubscribeMessages();
this.messagesSubscription = this.messagesObservable.subscribe(messages => { this.messagesSubscription = this.messagesObservable?.subscribe((messages: any) => {
if (tmid && this.thread) { if (tmid && this.thread) {
messages = [...messages, this.thread]; messages = [...messages, this.thread];
} }
@ -186,6 +201,7 @@ class ListContainer extends React.Component {
if (this.mounted) { if (this.mounted) {
this.setState({ messages }, () => this.update()); this.setState({ messages }, () => this.update());
} else { } else {
// @ts-ignore
this.state.messages = messages; this.state.messages = messages;
} }
// TODO: move it away from here // TODO: move it away from here
@ -254,21 +270,21 @@ class ListContainer extends React.Component {
return null; return null;
}; };
handleScrollToIndexFailed = params => { handleScrollToIndexFailed = (params: any) => {
const { listRef } = this.props; const { listRef } = this.props;
listRef.current.getNode().scrollToIndex({ index: params.highestMeasuredFrameIndex, animated: false }); listRef.current.getNode().scrollToIndex({ index: params.highestMeasuredFrameIndex, animated: false });
}; };
jumpToMessage = messageId => jumpToMessage = (messageId: string): Promise<void> =>
new Promise(async resolve => { new Promise(async resolve => {
this.jumping = true; this.jumping = true;
const { messages } = this.state; const { messages } = this.state;
const { listRef } = this.props; const { listRef } = this.props;
const index = messages.findIndex(item => item.id === messageId); const index = messages.findIndex((item: { id: string }) => item.id === messageId);
if (index > -1) { if (index > -1) {
listRef.current.getNode().scrollToIndex({ index, viewPosition: 0.5, viewOffset: 100 }); listRef.current.getNode().scrollToIndex({ index, viewPosition: 0.5, viewOffset: 100 });
await new Promise(res => setTimeout(res, 300)); await new Promise(res => setTimeout(res, 300));
if (!this.viewableItems.map(vi => vi.key).includes(messageId)) { if (!this.viewableItems.map((vi: { key: string }) => vi.key).includes(messageId)) {
if (!this.jumping) { if (!this.jumping) {
return resolve(); return resolve();
} }
@ -308,13 +324,13 @@ class ListContainer extends React.Component {
return null; return null;
}; };
renderItem = ({ item, index }) => { renderItem = ({ item, index }: { item: any; index: number }) => {
const { messages, highlightedMessage } = this.state; const { messages, highlightedMessage } = this.state;
const { renderRow } = this.props; const { renderRow } = this.props;
return renderRow(item, messages[index + 1], highlightedMessage); return renderRow(item, messages[index + 1], highlightedMessage);
}; };
onViewableItemsChanged = ({ viewableItems }) => { onViewableItemsChanged = ({ viewableItems }: any) => {
this.viewableItems = viewableItems; this.viewableItems = viewableItems;
}; };