|
| 1 | +import { FlatList, Pressable, StyleSheet, Text, View } from 'react-native'; |
| 2 | +import { DraftsIcon } from '../icons/DraftIcon'; |
| 3 | +import { useChatContext, useStateStore, useTheme } from 'stream-chat-react-native'; |
| 4 | +import { DraftManagerState, DraftsManager } from '../utils/DraftsManager'; |
| 5 | +import { useEffect, useMemo } from 'react'; |
| 6 | +import dayjs from 'dayjs'; |
| 7 | +import { useNavigation } from '@react-navigation/native'; |
| 8 | +import { ChannelResponse, MessageResponseBase } from 'stream-chat'; |
| 9 | + |
| 10 | +export type DraftItemProps = { |
| 11 | + type?: 'channel' | 'thread'; |
| 12 | + channel?: ChannelResponse; |
| 13 | + date?: string; |
| 14 | + content?: string; |
| 15 | + // TODO: Fix the type for thread |
| 16 | + thread?: MessageResponseBase; |
| 17 | + parentId?: string; |
| 18 | +}; |
| 19 | + |
| 20 | +export const DraftItem = ({ type, channel, date, content, parentId, thread }: DraftItemProps) => { |
| 21 | + const { |
| 22 | + theme: { |
| 23 | + colors: { grey }, |
| 24 | + }, |
| 25 | + } = useTheme(); |
| 26 | + const navigation = useNavigation(); |
| 27 | + const { client } = useChatContext(); |
| 28 | + |
| 29 | + const onNavigationHandler = async () => { |
| 30 | + if (channel?.type && channel.id) { |
| 31 | + console.log(channel); |
| 32 | + const resultChannel = client.channel(channel?.type, channel?.id); |
| 33 | + await resultChannel?.watch(); |
| 34 | + if (type === 'thread' && parentId) { |
| 35 | + navigation.navigate('ThreadScreen', { |
| 36 | + thread: thread, |
| 37 | + channel: resultChannel, |
| 38 | + }); |
| 39 | + } else if (type === 'channel') { |
| 40 | + navigation.navigate('ChannelScreen', { channel: resultChannel }); |
| 41 | + } |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + return ( |
| 46 | + <Pressable |
| 47 | + style={({ pressed }) => [styles.itemContainer, { opacity: pressed ? 0.8 : 1 }]} |
| 48 | + onPress={onNavigationHandler} |
| 49 | + > |
| 50 | + <View style={styles.header}> |
| 51 | + <Text style={styles.name}> |
| 52 | + {type === 'channel' ? `# ${channel?.name}` : `Thread in # ${channel?.name}`} |
| 53 | + </Text> |
| 54 | + <Text style={[styles.date, { color: grey }]}>{dayjs(date).fromNow()}</Text> |
| 55 | + </View> |
| 56 | + <View style={styles.content}> |
| 57 | + <View style={styles.icon}> |
| 58 | + <DraftsIcon /> |
| 59 | + </View> |
| 60 | + <Text style={[styles.text, { color: grey }]} numberOfLines={1}> |
| 61 | + {content} |
| 62 | + </Text> |
| 63 | + </View> |
| 64 | + </Pressable> |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +const selector = (nextValue: DraftManagerState) => |
| 69 | + ({ |
| 70 | + isLoading: nextValue.pagination.isLoading, |
| 71 | + isLoadingNext: nextValue.pagination.isLoadingNext, |
| 72 | + drafts: nextValue.drafts, |
| 73 | + }) as const; |
| 74 | + |
| 75 | +export const DraftsList = () => { |
| 76 | + const { client } = useChatContext(); |
| 77 | + const draftsManager = useMemo(() => new DraftsManager({ client }), [client]); |
| 78 | + |
| 79 | + useEffect(() => { |
| 80 | + draftsManager.reload({ force: true }); |
| 81 | + }, [draftsManager]); |
| 82 | + |
| 83 | + const { isLoading, drafts, isLoadingNext } = useStateStore(draftsManager.state, selector); |
| 84 | + |
| 85 | + return ( |
| 86 | + <View> |
| 87 | + <FlatList |
| 88 | + data={drafts} |
| 89 | + refreshing={isLoading} |
| 90 | + keyExtractor={(item) => item.message.id} |
| 91 | + renderItem={({ item }) => ( |
| 92 | + <DraftItem |
| 93 | + channel={item.channel} |
| 94 | + type={item.parent_id ? 'thread' : 'channel'} |
| 95 | + date={item.created_at} |
| 96 | + content={item.message.text} |
| 97 | + thread={item.parent_message} |
| 98 | + parentId={item.parent_id} |
| 99 | + /> |
| 100 | + )} |
| 101 | + onRefresh={() => draftsManager.reload({ force: true })} |
| 102 | + ListEmptyComponent={ |
| 103 | + !isLoading && drafts.length === 0 ? ( |
| 104 | + <Text style={{ textAlign: 'center', padding: 20 }}>No drafts available</Text> |
| 105 | + ) : null |
| 106 | + } |
| 107 | + onEndReached={() => { |
| 108 | + if (!isLoadingNext) { |
| 109 | + draftsManager.loadNextPage(); |
| 110 | + } |
| 111 | + }} |
| 112 | + /> |
| 113 | + </View> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +const styles = StyleSheet.create({ |
| 118 | + itemContainer: { |
| 119 | + paddingVertical: 8, |
| 120 | + marginHorizontal: 8, |
| 121 | + borderBottomWidth: 1, |
| 122 | + borderBottomColor: '#ccc', |
| 123 | + }, |
| 124 | + header: { |
| 125 | + flexDirection: 'row', |
| 126 | + alignItems: 'center', |
| 127 | + justifyContent: 'space-between', |
| 128 | + }, |
| 129 | + name: { |
| 130 | + fontSize: 16, |
| 131 | + fontWeight: 'bold', |
| 132 | + }, |
| 133 | + date: {}, |
| 134 | + content: { |
| 135 | + flexDirection: 'row', |
| 136 | + alignItems: 'center', |
| 137 | + marginTop: 4, |
| 138 | + }, |
| 139 | + icon: {}, |
| 140 | + text: { |
| 141 | + marginLeft: 8, |
| 142 | + flexShrink: 1, |
| 143 | + }, |
| 144 | +}); |
0 commit comments