Skip to content

Commit

Permalink
[native] Lift renderItem to ConnectedChatThreadList
Browse files Browse the repository at this point in the history
Summary:
This diff is just one step in the process of converting ChatThreadList into a functional component. When this work is done, we will be able to avoid a lot of re-rendering and hopefully improve performance quite a bit.

---

Depends on D9172

Test Plan: ChatThreadList + search experience continue to work as expected.

Reviewers: ginsu, tomek, rohan

Reviewed By: ginsu

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D9173
  • Loading branch information
atulsmadhugiri committed Sep 13, 2023
1 parent bc069c4 commit ae6086f
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions native/chat/chat-thread-list.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type Props = {
) => void,
+onPressSeeMoreSidebars: (threadInfo: ThreadInfo) => void,
+hardwareBack: () => boolean,
+renderItem: (row: { item: Item, ... }) => React.Node,
};

class ChatThreadList extends React.PureComponent<Props> {
Expand Down Expand Up @@ -225,30 +226,6 @@ class ChatThreadList extends React.PureComponent<Props> {
}
};

renderItem = (row: { item: Item, ... }) => {
const item = row.item;
if (item.type === 'search') {
return (
<TouchableWithoutFeedback onPress={this.props.onSearchFocus}>
{this.props.renderSearch({ active: false })}
</TouchableWithoutFeedback>
);
}
if (item.type === 'empty') {
const EmptyItem = item.emptyItem;
return <EmptyItem />;
}
return (
<ChatThreadListItem
data={item}
onPressItem={this.props.onPressItem}
onPressSeeMoreSidebars={this.props.onPressSeeMoreSidebars}
onSwipeableWillOpen={this.props.onSwipeableWillOpen}
currentlyOpenedSwipeableId={this.props.openedSwipeableID}
/>
);
};

listDataSelector = createSelector(
(props: Props) => props.chatListData,
(props: Props) => props.searchStatus,
Expand Down Expand Up @@ -341,7 +318,7 @@ class ChatThreadList extends React.PureComponent<Props> {
{fixedSearch}
<FlatList
data={this.listData}
renderItem={this.renderItem}
renderItem={this.props.renderItem}
keyExtractor={keyExtractor}
getItemLayout={getItemLayout}
extraData={extraData}
Expand Down Expand Up @@ -636,6 +613,40 @@ function ConnectedChatThreadList(props: BaseProps): React.Node {
return true;
}, [navigation, onSearchCancel, searchStatus]);

const renderItem = React.useCallback(
(row: { item: Item, ... }) => {
const item = row.item;
if (item.type === 'search') {
return (
<TouchableWithoutFeedback onPress={onSearchFocus}>
{renderSearch({ active: false })}
</TouchableWithoutFeedback>
);
}
if (item.type === 'empty') {
const EmptyItem = item.emptyItem;
return <EmptyItem />;
}
return (
<ChatThreadListItem
data={item}
onPressItem={onPressItem}
onPressSeeMoreSidebars={onPressSeeMoreSidebars}
onSwipeableWillOpen={onSwipeableWillOpen}
currentlyOpenedSwipeableId={openedSwipeableID}
/>
);
},
[
onPressItem,
onPressSeeMoreSidebars,
onSearchFocus,
onSwipeableWillOpen,
openedSwipeableID,
renderSearch,
],
);

return (
<ChatThreadList
navigation={navigation}
Expand Down Expand Up @@ -676,6 +687,7 @@ function ConnectedChatThreadList(props: BaseProps): React.Node {
onPressItem={onPressItem}
onPressSeeMoreSidebars={onPressSeeMoreSidebars}
hardwareBack={hardwareBack}
renderItem={renderItem}
/>
);
}
Expand Down

0 comments on commit ae6086f

Please sign in to comment.