Skip to content

Commit a750787

Browse files
authored
Revert "Hook Setup"
1 parent 9e6c8d2 commit a750787

File tree

13 files changed

+77
-187
lines changed

13 files changed

+77
-187
lines changed

example/App.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { NavigationContainer } from '@react-navigation/native'
2+
import React from 'react'
23
import { Button, Platform } from 'react-native'
34
import { QueryClient, QueryClientProvider } from 'react-query'
4-
import { XmtpProvider } from 'xmtp-react-native-sdk'
55

66
import ConversationCreateScreen from './src/ConversationCreateScreen'
77
import ConversationScreen from './src/ConversationScreen'
88
import HomeScreen from './src/HomeScreen'
99
import LaunchScreen from './src/LaunchScreen'
1010
import { Navigator } from './src/Navigation'
1111
import TestScreen from './src/TestScreen'
12+
import { XmtpContextProvider } from './src/XmtpContext'
1213

1314
const queryClient = new QueryClient()
1415
export default function App() {
1516
return (
1617
<QueryClientProvider client={queryClient}>
17-
<XmtpProvider>
18+
<XmtpContextProvider>
1819
<NavigationContainer>
1920
<Navigator.Navigator>
2021
<Navigator.Screen
@@ -70,7 +71,7 @@ export default function App() {
7071
/>
7172
</Navigator.Navigator>
7273
</NavigationContainer>
73-
</XmtpProvider>
74+
</XmtpContextProvider>
7475
</QueryClientProvider>
7576
)
7677
}

example/src/ConversationCreateScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { NativeStackScreenProps } from '@react-navigation/native-stack'
22
import React, { useState } from 'react'
33
import { Button, ScrollView, Text, TextInput } from 'react-native'
4-
import { useXmtp } from 'xmtp-react-native-sdk'
54

65
import { NavigationParamList } from './Navigation'
6+
import { useXmtp } from './XmtpContext'
77

88
export default function ConversationCreateScreen({
99
route,

example/src/ConversationScreen.tsx

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as ImagePicker from 'expo-image-picker'
88
import type { ImagePickerAsset } from 'expo-image-picker'
99
import { PermissionStatus } from 'expo-modules-core'
1010
import moment from 'moment'
11-
import React, { useCallback, useMemo, useRef, useState } from 'react'
11+
import React, { useRef, useState } from 'react'
1212
import {
1313
Button,
1414
FlatList,
@@ -30,10 +30,11 @@ import {
3030
DecodedMessage,
3131
StaticAttachmentContent,
3232
ReplyContent,
33-
useClient,
33+
Client,
3434
} from 'xmtp-react-native-sdk'
3535

3636
import { NavigationParamList } from './Navigation'
37+
import { useXmtp } from './XmtpContext'
3738
import {
3839
useConversation,
3940
useMessage,
@@ -56,7 +57,7 @@ export default function ConversationScreen({
5657
}: NativeStackScreenProps<NavigationParamList, 'conversation'>) {
5758
const { topic } = route.params
5859
const messageListRef = useRef<FlatList>(null)
59-
const {
60+
let {
6061
data: messages,
6162
refetch: refreshMessages,
6263
isFetching,
@@ -73,15 +74,10 @@ export default function ConversationScreen({
7374
fileUri: attachment?.image?.uri || attachment?.file?.uri,
7475
mimeType: attachment?.file?.mimeType,
7576
})
76-
77-
const filteredMessages = useMemo(
78-
() =>
79-
(messages ?? [])?.filter(
80-
(message) => !hiddenMessageTypes.includes(message.contentTypeId)
81-
),
82-
[messages]
77+
messages = (messages || []).filter(
78+
(message) => !hiddenMessageTypes.includes(message.contentTypeId)
8379
)
84-
80+
// console.log("messages", JSON.stringify(messages, null, 2));
8581
const sendMessage = async (content: any) => {
8682
setSending(true)
8783
console.log('Sending message', content)
@@ -106,22 +102,16 @@ export default function ConversationScreen({
106102
const sendRemoteAttachmentMessage = () =>
107103
sendMessage({ remoteAttachment }).then(() => setAttachment(null))
108104
const sendTextMessage = () => sendMessage({ text }).then(() => setText(''))
109-
const scrollToMessageId = useCallback(
110-
(messageId: string) => {
111-
const index = (filteredMessages || []).findIndex(
112-
(m) => m.id === messageId
113-
)
114-
if (index === -1) {
115-
return
116-
}
117-
return messageListRef.current?.scrollToIndex({
118-
index,
119-
animated: true,
120-
})
121-
},
122-
[filteredMessages]
123-
)
124-
105+
const scrollToMessageId = (messageId: string) => {
106+
const index = (messages || []).findIndex((m) => m.id === messageId)
107+
if (index === -1) {
108+
return
109+
}
110+
return messageListRef.current?.scrollToIndex({
111+
index,
112+
animated: true,
113+
})
114+
}
125115
return (
126116
<SafeAreaView style={{ flex: 1 }}>
127117
<KeyboardAvoidingView
@@ -152,7 +142,7 @@ export default function ConversationScreen({
152142
contentContainerStyle={{ paddingBottom: 100 }}
153143
refreshing={isFetching || isRefetching}
154144
onRefresh={refreshMessages}
155-
data={filteredMessages}
145+
data={messages}
156146
inverted
157147
keyboardDismissMode="none"
158148
keyExtractor={(message) => message.id}
@@ -164,9 +154,9 @@ export default function ConversationScreen({
164154
onReply={() => setReplyingTo(message.id)}
165155
onMessageReferencePress={scrollToMessageId}
166156
showSender={
167-
index === (filteredMessages || []).length - 1 ||
168-
(index + 1 < (filteredMessages || []).length &&
169-
filteredMessages![index + 1].senderAddress !==
157+
index === (messages || []).length - 1 ||
158+
(index + 1 < (messages || []).length &&
159+
messages![index + 1].senderAddress !==
170160
message.senderAddress)
171161
}
172162
/>
@@ -1056,7 +1046,7 @@ function MessageContents({
10561046
contentTypeId: string
10571047
content: any
10581048
}) {
1059-
const { client } = useClient()
1049+
const { client }: { client: Client<any> } = useXmtp()
10601050

10611051
if (contentTypeId === 'xmtp.org/text:1.0') {
10621052
const text: string = content
@@ -1090,8 +1080,8 @@ function MessageContents({
10901080
if (contentTypeId === 'xmtp.org/reply:1.0') {
10911081
const replyContent: ReplyContent = content
10921082
const replyContentType = replyContent.contentType
1093-
const codec = client?.codecRegistry[replyContentType]
1094-
const actualReplyContent = codec?.decode(replyContent.content)
1083+
const codec = client.codecRegistry[replyContentType]
1084+
const actualReplyContent = codec.decode(replyContent.content)
10951085

10961086
return (
10971087
<View>

example/src/HomeScreen.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import {
99
Text,
1010
View,
1111
} from 'react-native'
12-
import { Conversation, Client, useXmtp } from 'xmtp-react-native-sdk'
12+
import { Conversation, Client } from 'xmtp-react-native-sdk'
1313

14+
import { useXmtp } from './XmtpContext'
1415
import { useConversationList, useMessages } from './hooks'
1516

1617
/// Show the user's list of conversations.

example/src/LaunchScreen.tsx

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { NativeStackScreenProps } from '@react-navigation/native-stack'
2-
import React, { useCallback } from 'react'
2+
import React from 'react'
33
import { Button, ScrollView, StyleSheet, Text, View } from 'react-native'
44
import * as XMTP from 'xmtp-react-native-sdk'
5-
import { useXmtp } from 'xmtp-react-native-sdk'
65

76
import { NavigationParamList } from './Navigation'
7+
import { useXmtp } from './XmtpContext'
88
import { useSavedKeys } from './hooks'
99

1010
const appVersion = 'XMTP_RN_EX/0.0.1'
@@ -22,26 +22,24 @@ export default function LaunchScreen({
2222
}: NativeStackScreenProps<NavigationParamList, 'launch'>) {
2323
const { setClient } = useXmtp()
2424
const savedKeys = useSavedKeys()
25-
const configureWallet = useCallback(
26-
(label: string, configuring: Promise<XMTP.Client>) => {
27-
console.log('Connecting XMTP client', label)
28-
configuring
29-
.then(async (client) => {
30-
console.log('Connected XMTP client', label, {
31-
address: client.address,
32-
})
33-
setClient(client)
34-
navigation.navigate('home')
35-
// Save the configured client keys for use in later sessions.
36-
const keyBundle = await client.exportKeyBundle()
37-
await savedKeys.save(keyBundle)
25+
const configureWallet = (
26+
label: string,
27+
configuring: Promise<XMTP.Client>
28+
) => {
29+
console.log('Connecting XMTP client', label)
30+
configuring
31+
.then(async (client) => {
32+
console.log('Connected XMTP client', label, {
33+
address: client.address,
3834
})
39-
.catch((err) =>
40-
console.log('Unable to connect XMTP client', label, err)
41-
)
42-
},
43-
[]
44-
)
35+
setClient(client)
36+
navigation.navigate('home')
37+
// Save the configured client keys for use in later sessions.
38+
const keyBundle = await client.exportKeyBundle()
39+
await savedKeys.save(keyBundle)
40+
})
41+
.catch((err) => console.log('Unable to connect XMTP client', label, err))
42+
}
4543
return (
4644
<ScrollView>
4745
<Text

example/src/XmtpContext.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
createContext,
3+
FC,
4+
ReactNode,
5+
useContext,
6+
useMemo,
7+
useState,
8+
} from 'react'
9+
import * as XMTP from 'xmtp-react-native-sdk'
10+
11+
const XmtpContext = createContext<{
12+
client: XMTP.Client | null
13+
setClient: (client: XMTP.Client | null) => void
14+
}>({
15+
client: null,
16+
setClient: () => {},
17+
})
18+
export const useXmtp = () => useContext(XmtpContext)
19+
type Props = {
20+
children: ReactNode
21+
}
22+
export const XmtpContextProvider: FC<Props> = ({ children }) => {
23+
const [client, setClient] = useState<XMTP.Client | null>(null)
24+
const context = useMemo(() => ({ client, setClient }), [client, setClient])
25+
return <XmtpContext.Provider value={context}>{children}</XmtpContext.Provider>
26+
}

example/src/hooks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
EncryptedLocalAttachment,
88
ReactionContent,
99
RemoteAttachmentContent,
10-
useXmtp,
1110
} from 'xmtp-react-native-sdk'
1211

12+
import { useXmtp } from './XmtpContext'
1313
import { downloadFile, uploadFile } from './storage'
1414

1515
/**

src/context/XmtpContext.tsx

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/context/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/hooks/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/hooks/useClient.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)