aboutsummaryrefslogtreecommitdiff
path: root/src/screens/chat/ChatListScreen.tsx
blob: 0f5d80733524f58c170541e81ba8257e4c391cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {useBottomTabBarHeight} from '@react-navigation/bottom-tabs';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {useContext, useEffect, useMemo, useState} from 'react';
import {Alert, SafeAreaView, StatusBar, StyleSheet, View} from 'react-native';
import {useStore} from 'react-redux';
import {ChannelList, Chat} from 'stream-chat-react-native';
import {ChatContext} from '../../App';
import {TabsGradient} from '../../components';
import EmptyContentView from '../../components/common/EmptyContentView';
import {ChannelPreview, MessagesHeader} from '../../components/messages';
import {MainStackParams} from '../../routes';
import {RootState} from '../../store/rootReducer';
import {
  LocalAttachmentType,
  LocalChannelType,
  LocalCommandType,
  LocalEventType,
  LocalMessageType,
  LocalReactionType,
  LocalUserType,
} from '../../types';
import {connectChatAccount, HeaderHeight} from '../../utils';
import NewChatModal from './NewChatModal';

type ChatListScreenNavigationProp = StackNavigationProp<
  MainStackParams,
  'ChatList'
>;
interface ChatListScreenProps {
  navigation: ChatListScreenNavigationProp;
}
/*
 * Screen that displays all of the user's active conversations.
 */
const ChatListScreen: React.FC<ChatListScreenProps> = () => {
  const {chatClient} = useContext(ChatContext);
  const [modalVisible, setChatModalVisible] = useState(false);
  const state: RootState = useStore().getState();
  const loggedInUserId = state.user.user.userId;
  const tabbarHeight = useBottomTabBarHeight();

  const memoizedFilters = useMemo(
    () => ({
      members: {$in: [loggedInUserId]},
      type: 'messaging',
    }),
    [],
  );

  const chatTheme = {
    channelListMessenger: {
      flatListContent: {
        backgroundColor: 'white',
        paddingBottom: tabbarHeight + HeaderHeight + 20,
      },
    },
  };

  useEffect(() => {
    if (loggedInUserId) {
      connectChatAccount(loggedInUserId, chatClient)
        .then((success) => {
          if (!success) {
            Alert.alert('Something wrong with chat');
          }
        })
        .catch((err) => {
          console.log('Error connecting to chat: ', err);
          Alert.alert('Something wrong with chat');
        });
    }
  }, [loggedInUserId]);

  return (
    <View style={styles.background}>
      <SafeAreaView>
        <StatusBar barStyle="dark-content" />
        <MessagesHeader
          createChannel={() => {
            setChatModalVisible(true);
          }}
        />
        <Chat client={chatClient} style={chatTheme}>
          <View style={styles.chatContainer}>
            <ChannelList<
              LocalAttachmentType,
              LocalChannelType,
              LocalCommandType,
              LocalEventType,
              LocalMessageType,
              LocalReactionType,
              LocalUserType
            >
              filters={memoizedFilters}
              options={{
                presence: true,
                state: true,
                watch: true,
              }}
              sort={{last_message_at: -1}}
              maxUnreadCount={99}
              Preview={ChannelPreview}
              EmptyStateIndicator={() => {
                return <EmptyContentView viewType={'ChatList'} />;
              }}
            />
          </View>
        </Chat>
        <NewChatModal {...{modalVisible, setChatModalVisible}} />
      </SafeAreaView>
      <TabsGradient />
    </View>
  );
};

const styles = StyleSheet.create({
  background: {
    flex: 1,
    backgroundColor: 'white',
  },
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  placeholder: {
    fontSize: 14,
    fontWeight: 'bold',
    marginBottom: 10,
  },
  button: {
    backgroundColor: '#CCE4FC',
    padding: 15,
    borderRadius: 5,
  },
  chatContainer: {
    height: '100%',
    marginTop: 10,
  },
});

export default ChatListScreen;