From fab86f9b874524a4beabb3c45a9e59e8b00ca495 Mon Sep 17 00:00:00 2001
From: Ivan Chen <ivan@tagg.id>
Date: Thu, 8 Apr 2021 18:42:28 -0400
Subject: added chat header, created isMember util, fixed KB padding issue

---
 src/components/messages/ChannelPreview.tsx | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

(limited to 'src/components/messages/ChannelPreview.tsx')

diff --git a/src/components/messages/ChannelPreview.tsx b/src/components/messages/ChannelPreview.tsx
index 867e0a38..8ec6060a 100644
--- a/src/components/messages/ChannelPreview.tsx
+++ b/src/components/messages/ChannelPreview.tsx
@@ -2,7 +2,7 @@ import {useNavigation} from '@react-navigation/core';
 import React, {useContext} from 'react';
 import {Image, StyleSheet, Text, View} from 'react-native';
 import {TouchableOpacity} from 'react-native-gesture-handler';
-import {useSelector} from 'react-redux';
+import {useSelector, useStore} from 'react-redux';
 import {ChannelPreviewMessengerProps} from 'stream-chat-react-native';
 import {ChatContext} from '../../App';
 import {RootState} from '../../store/rootReducer';
@@ -16,7 +16,7 @@ import {
   LocalUserType,
 } from '../../types';
 import {normalize, SCREEN_HEIGHT} from '../../utils';
-import {isOnline} from '../../utils/messages';
+import {getMember, isOnline} from '../../utils/messages';
 
 const ChannelPreview: React.FC<
   ChannelPreviewMessengerProps<
@@ -29,18 +29,11 @@ const ChannelPreview: React.FC<
     LocalUserType
   >
 > = (props) => {
-  const navigation = useNavigation();
   const {setChannel} = useContext(ChatContext);
+  const state = useStore().getState();
+  const navigation = useNavigation();
   const {channel} = props;
-  const {userId: loggedInUserId} = useSelector(
-    (state: RootState) => state.user.user,
-  );
-  const otherMembers = channel
-    ? Object.values(channel.state.members).filter(
-        (member) => member.user?.id !== loggedInUserId,
-      )
-    : [];
-  const member = otherMembers.length === 1 ? otherMembers[0] : undefined;
+  const member = getMember(channel, state);
   const online = isOnline(member?.user?.last_active);
   const unread = channel.state.unreadCount > 0;
 
@@ -55,8 +48,8 @@ const ChannelPreview: React.FC<
         <Image
           style={styles.avatar}
           source={
-            otherMembers.length === 1
-              ? {uri: member?.user?.thumbnail_url}
+            member
+              ? {uri: member.user?.thumbnail_url}
               : require('../../assets/images/avatar-placeholder.png')
           }
         />
@@ -71,7 +64,9 @@ const ChannelPreview: React.FC<
         <Text
           style={[styles.lastMessage, unread ? styles.unread : {}]}
           numberOfLines={1}>
-          {channel.state.messages[channel.state.messages.length - 1].text}
+          {channel.state.messages.length > 0
+            ? channel.state.messages[channel.state.messages.length - 1].text
+            : ''}
         </Text>
       </View>
       {unread && <View style={styles.purpleDot} />}
-- 
cgit v1.2.3-70-g09d2

