-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel.tsx
153 lines (138 loc) · 4.77 KB
/
channel.tsx
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
143
144
145
146
147
148
149
150
151
152
153
import React, {ReactElement, ReactNode, useCallback, useRef} from 'react';
import {
View,
Text,
Button,
FlatList,
ScrollView,
NativeScrollEvent,
ActivityIndicator,
useWindowDimensions,
Dimensions,
} from 'react-native';
import {TabView, SceneMap, TabBar} from 'react-native-tab-view';
import {createQuery, runQuery, queryChannels} from '@amityco/ts-sdk';
import ChatList, {IChatListProps} from '../../components/ChatList';
import useAuth from '../../hooks/useAuth';
import {useEffect, useState} from 'react';
import moment from 'moment';
import LoadingIndicator from '../../components/LoadingIndicator';
import styles from './styles';
import CustomText from '../../components/CustomText';
export default function RecentChat() {
const {client} = useAuth();
const [channelObjects, setChannelObjects] = useState<IChatListProps[]>([]);
console.log('channelObjects: ', channelObjects);
const scrollViewRef = useRef<ScrollView>(null);
const [isScrollEnd, setIsScrollEnd] = useState(false);
const [channelOptions, setChannelOptions] = useState<Amity.RunQueryOptions<typeof queryChannels>>();
const {loading, nextPage, error} = channelOptions ?? {};
const initialLayout = {width: Dimensions.get('window').width, };
const [index, setIndex] = React.useState(0);
const [routes] = React.useState([{key: 'first', title: 'Recent'}]);
const handleScroll = ({nativeEvent}: {nativeEvent: NativeScrollEvent}) => {
const {layoutMeasurement, contentOffset, contentSize} = nativeEvent;
const isEnd =
layoutMeasurement.height + contentOffset.y >= contentSize.height;
setIsScrollEnd(isEnd);
};
useEffect(() => {
onQueryChannel({limit: 10}, true);
}, [client.userId]);
const onQueryChannel = useCallback((page = {limit: 10}, reset = false) => {
const query = createQuery(queryChannels, {
membership: 'member',
page: page,
});
runQuery(query, ({data: channels, ...options}) => {
setChannelOptions(options);
if (channels) {
const formattedChannelObjects: IChatListProps[] =
channels &&
channels.map((item: Amity.Channel<any>) => {
const lastActivityDate: string = moment(item.lastActivity).format(
'DD/MM/YYYY',
);
const todayDate = moment(Date.now()).format('DD/MM/YYYY');
let dateDisplay;
if (lastActivityDate == todayDate) {
dateDisplay = moment(item.lastActivity).format('hh:mm A');
} else {
dateDisplay = moment(item.lastActivity).format('DD/MM/YYYY');
}
return {
chatId: item.defaultSubChannelId ?? '',
chatName: item.displayName ?? '',
chatMemberNumber: item.memberCount ?? 0,
unReadMessage: item.messageCount ?? 0, // change to defaultSubChannelUnreadCount later after product team fix this !!!!!!!!!
messageDate: dateDisplay ?? '',
channelType: item.type??'',
};
});
setChannelObjects(prevChannels =>
reset
? formattedChannelObjects
: [...prevChannels, ...formattedChannelObjects],
);
}
});
}, []);
useEffect(() => {
if (isScrollEnd) {
handleLoadMore();
}
}, [isScrollEnd]);
const handleLoadMore = () => {
if (nextPage) {
onQueryChannel(nextPage);
}
};
const renderRecentChat = (): ReactElement => {
return (
<View>
<ScrollView
scrollEventThrottle={16}
ref={scrollViewRef}
onScroll={handleScroll}>
{channelObjects?.map((item: IChatListProps) => {
return (
<ChatList
key={item.chatId}
chatId={item.chatId}
chatName={item.chatName}
chatMemberNumber={item.chatMemberNumber}
unReadMessage={item.unReadMessage}
messageDate={item.messageDate}
channelType={item.channelType}
/>
);
})}
{loading && <LoadingIndicator />}
</ScrollView>
</View>
);
};
const renderScene = SceneMap({
first: renderRecentChat,
});
return (
<TabView
style={{ backgroundColor: '#FFFFF' }}
navigationState={{index, routes}}
renderScene={renderScene}
onIndexChange={setIndex}
initialLayout={initialLayout}
renderTabBar={props =>
<TabBar
{...props}
indicatorStyle={styles.indicatorStyle}
style={{ backgroundColor: 'white', }}
tabStyle={styles.tabStyle} // here
renderLabel={({ route, focused, color }) => (
<CustomText style={styles.fontStyle}>
{route.title}
</CustomText>
)}
/>} />
);
}