-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Merge pull request #96 from xmtp-labs/ar/streaming-refactor
refactor: Refactor Conversation List
Showing
29 changed files
with
604 additions
and
496 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import {useAddress, useENS} from '@thirdweb-dev/react-native'; | ||
import {Box, HStack, VStack} from 'native-base'; | ||
import React, {FC, useCallback} from 'react'; | ||
import {Pressable} from 'react-native'; | ||
import {AvatarWithFallback} from '../components/AvatarWithFallback'; | ||
import {Button} from '../components/common/Button'; | ||
import {Icon} from '../components/common/Icon'; | ||
import {Text} from '../components/common/Text'; | ||
import {useTypedNavigation} from '../hooks/useTypedNavigation'; | ||
import {translate} from '../i18n'; | ||
import {ScreenNames} from '../navigation/ScreenNames'; | ||
import {colors} from '../theme/colors'; | ||
|
||
export interface ConversationListHeaderProps { | ||
list: 'ALL_MESSAGES' | 'MESSAGE_REQUESTS'; | ||
showPickerModal: () => void; | ||
messageRequestCount: number; | ||
onShowMessageRequests: () => void; | ||
} | ||
|
||
export const ConversationListHeader: FC<ConversationListHeaderProps> = ({ | ||
list, | ||
showPickerModal, | ||
messageRequestCount, | ||
onShowMessageRequests, | ||
}) => { | ||
const {navigate} = useTypedNavigation(); | ||
const address = useAddress(); | ||
const {data} = useENS(); | ||
const {avatarUrl} = data ?? {}; | ||
|
||
const handleAccountPress = useCallback(() => { | ||
navigate(ScreenNames.Account); | ||
}, [navigate]); | ||
const navigateToDev = useCallback(() => { | ||
if (__DEV__) { | ||
navigate(ScreenNames.Dev); | ||
} | ||
}, [navigate]); | ||
return ( | ||
<VStack marginTop={'4px'}> | ||
<HStack | ||
w={'100%'} | ||
justifyContent={'space-between'} | ||
alignItems={'center'} | ||
paddingX={'16px'} | ||
paddingBottom={'8px'}> | ||
<Pressable onPress={handleAccountPress}> | ||
<AvatarWithFallback | ||
address={address ?? ''} | ||
avatarUri={avatarUrl} | ||
size={40} | ||
/> | ||
</Pressable> | ||
<Box | ||
borderRadius={'24px'} | ||
zIndex={10} | ||
paddingX={'16px'} | ||
paddingY={'8px'} | ||
backgroundColor={'white'} | ||
shadow={4}> | ||
<Box> | ||
<Button | ||
onLongPress={navigateToDev} | ||
_pressed={{backgroundColor: 'transparent'}} | ||
size={'sm'} | ||
variant={'ghost'} | ||
leftIcon={ | ||
<Icon | ||
name="chat-bubble-oval-left" | ||
size={16} | ||
type="mini" | ||
color="#0F172A" | ||
/> | ||
} | ||
rightIcon={ | ||
<Icon | ||
name="chevron-down-thick" | ||
size={16} | ||
type="mini" | ||
color="#0F172A" | ||
/> | ||
} | ||
onPress={showPickerModal}> | ||
<Text typography="text-sm/heavy"> | ||
{list === 'ALL_MESSAGES' | ||
? translate('all_messages') | ||
: translate('message_requests')} | ||
</Text> | ||
</Button> | ||
</Box> | ||
</Box> | ||
<Box width={10} /> | ||
</HStack> | ||
{list === 'MESSAGE_REQUESTS' ? ( | ||
<Box | ||
backgroundColor={colors.actionAlertBG} | ||
paddingY={'8px'} | ||
paddingX={'16px'}> | ||
<Text | ||
typography="text-caption/regular" | ||
textAlign={'center'} | ||
color={colors.actionAlertText}> | ||
{translate('message_requests_from_new_addresses')} | ||
</Text> | ||
</Box> | ||
) : messageRequestCount > 0 ? ( | ||
<Pressable onPress={onShowMessageRequests}> | ||
<HStack | ||
backgroundColor={colors.actionPrimary} | ||
padding={'8px'} | ||
borderRadius={'8px'} | ||
alignItems={'center'} | ||
marginX={'16px'}> | ||
<Box paddingLeft={'8px'} paddingRight={'16px'}> | ||
<Icon name="inbox-arrow-down" color={colors.actionPrimaryText} /> | ||
</Box> | ||
<Text | ||
flex={1} | ||
color={colors.actionPrimaryText} | ||
typography="text-xs/bold"> | ||
{translate('message_requests_count', { | ||
count: String(messageRequestCount), | ||
})} | ||
</Text> | ||
<Box | ||
paddingLeft={'8px'} | ||
paddingRight={'16px'} | ||
justifyContent={'flex-end'}> | ||
<Icon | ||
name="chevron-right-thick" | ||
size={16} | ||
color={colors.actionPrimaryText} | ||
/> | ||
</Box> | ||
</HStack> | ||
</Pressable> | ||
) : ( | ||
<Box /> | ||
)} | ||
</VStack> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
import {Platform} from 'react-native'; | ||
|
||
// Just a way to gate some features that are not ready yet | ||
export const AppConfig = { | ||
LENS_ENABLED: false, | ||
XMTP_ENV: 'dev' as 'local' | 'dev' | 'production', | ||
MULTI_WALLET: false, | ||
PUSH_NOTIFICATIONS: Platform.OS === 'ios', | ||
GROUP_CONSENT: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import {useMemo} from 'react'; | ||
import {useGroupsQuery} from '../queries/useGroupsQuery'; | ||
|
||
export const useGroup = (id: string) => { | ||
export const useGroup = (topic: string) => { | ||
const groupsQuery = useGroupsQuery(); | ||
|
||
return useMemo(() => { | ||
const {data, ...rest} = groupsQuery; | ||
const {entities} = data ?? {}; | ||
|
||
const groupData = entities?.[id]; | ||
const groupData = entities?.[topic]; | ||
return {data: groupData, ...rest}; | ||
}, [groupsQuery, id]); | ||
}, [groupsQuery, topic]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {useQueryClient} from '@tanstack/react-query'; | ||
import {useEffect, useState} from 'react'; | ||
import {DeviceEventEmitter} from 'react-native'; | ||
import {EventEmitterEvents} from '../consts/EventEmitters'; | ||
import {QueryKeys} from '../queries/QueryKeys'; | ||
import {useGroupParticipantsQuery} from '../queries/useGroupParticipantsQuery'; | ||
import {mmkvStorage} from '../services/mmkvStorage'; | ||
|
||
export const useGroupParticipants = (topic: string) => { | ||
const queryClient = useQueryClient(); | ||
const [localParticipants] = useState(mmkvStorage.getGroupParticipants(topic)); | ||
|
||
useEffect(() => { | ||
const groupChangeSubscription = DeviceEventEmitter.addListener( | ||
`${EventEmitterEvents.GROUP_CHANGED}_${topic}`, | ||
() => { | ||
queryClient.refetchQueries({ | ||
queryKey: [QueryKeys.GroupParticipants, topic], | ||
}); | ||
}, | ||
); | ||
|
||
return () => { | ||
groupChangeSubscription.remove(); | ||
}; | ||
}, [topic, queryClient]); | ||
|
||
const query = useGroupParticipantsQuery(topic); | ||
|
||
useEffect(() => { | ||
if (query.isSuccess) { | ||
mmkvStorage.saveGroupParticipants(topic, query.data ?? []); | ||
} | ||
}, [query.data, query.isSuccess, topic]); | ||
|
||
return query.isSuccess ? query.data : localParticipants ?? []; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {useQuery} from '@tanstack/react-query'; | ||
import {DecodedMessage} from '@xmtp/react-native-sdk'; | ||
import {SupportedContentTypes} from '../consts/ContentTypes'; | ||
import {useGroup} from '../hooks/useGroup'; | ||
import {QueryKeys} from './QueryKeys'; | ||
|
||
export type GroupMessagesQueryRequestData = | ||
| DecodedMessage<SupportedContentTypes>[] | ||
| undefined; | ||
export type GroupMessagesQueryError = unknown; | ||
|
||
export const useFirstGroupMessageQuery = (topic: string) => { | ||
const {data: group} = useGroup(topic); | ||
|
||
return useQuery<GroupMessagesQueryRequestData, GroupMessagesQueryError>({ | ||
queryKey: [QueryKeys.FirstGroupMessage, topic], | ||
queryFn: async () => { | ||
if (!group) { | ||
return undefined; | ||
} | ||
const messages: DecodedMessage<SupportedContentTypes>[] = | ||
await group.messages(false, { | ||
// limit: 1, | ||
// direction: 'SORT_DIRECTION_ASCENDING', | ||
}); | ||
return messages; | ||
}, | ||
enabled: !!group, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {Client, DecodedMessage, Group} from '@xmtp/react-native-sdk'; | ||
import {Platform} from 'react-native'; | ||
import {SupportedContentTypes} from '../consts/ContentTypes'; | ||
import {QueryKeys} from '../queries/QueryKeys'; | ||
import {PushNotifications} from '../services/pushNotifications'; | ||
import {queryClient} from '../services/queryClient'; | ||
|
||
let cancelStreamGroups: null | Promise<() => void> = null; | ||
|
||
export const streamAllMessages = async ( | ||
client: Client<SupportedContentTypes>, | ||
) => { | ||
cancelStreamGroups = client.conversations.streamGroups(async newGroup => { | ||
console.log('NEW GROUP:', newGroup); | ||
if (Platform.OS !== 'android') { | ||
const pushClient = new PushNotifications(client); | ||
pushClient.subscribeToGroup(newGroup.topic); | ||
} | ||
queryClient.setQueryData<Group<SupportedContentTypes>[]>( | ||
[QueryKeys.List, client?.address], | ||
prev => { | ||
return [newGroup, ...(prev ?? [])]; | ||
}, | ||
); | ||
}); | ||
|
||
client.conversations.streamAllGroupMessages(async newMessage => { | ||
console.log('NEW MESSAGE:', newMessage); | ||
queryClient.setQueryData<DecodedMessage<SupportedContentTypes>[]>( | ||
[QueryKeys.FirstGroupMessage, newMessage.topic], | ||
prev => { | ||
return [newMessage, ...(prev ?? [])]; | ||
}, | ||
); | ||
}); | ||
}; | ||
|
||
export const cancelStreamAllMessages = async ( | ||
client?: Client<SupportedContentTypes> | null, | ||
) => { | ||
if (cancelStreamGroups) { | ||
const cancel = await cancelStreamGroups; | ||
cancel(); | ||
} | ||
client?.conversations.cancelStreamAllMessages(); | ||
}; |