-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathApp.tsx
More file actions
576 lines (539 loc) · 17.5 KB
/
Copy pathApp.tsx
File metadata and controls
576 lines (539 loc) · 17.5 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import { NavigationContainer } from '@react-navigation/native'
// import { Ethereum } from '@thirdweb-dev/chains'
import 'react-native-get-random-values'
import '@ethersproject/shims'
import { Buffer as BufferPolyfill } from 'buffer'
// Make Buffer globally available
global.Buffer = global.Buffer || BufferPolyfill
// import {
// ThirdwebProvider,
// metamaskWallet,
// rainbowWallet,
// } from '@thirdweb-dev/react-native'
import {
Button,
Platform,
ActionSheetIOS,
Modal,
View,
Text,
TouchableOpacity,
StyleSheet,
FlatList,
} from 'react-native'
import Config from 'react-native-config'
// Used to polyfill webCrypto in react-native
import { QueryClient, QueryClientProvider } from 'react-query'
import { XmtpProvider, Client } from 'xmtp-react-native-sdk'
import { useState, useEffect } from 'react'
import React from 'react'
import ConversationCreateScreen from './src/ConversationCreateScreen'
import ConversationScreen from './src/ConversationScreen'
import GroupScreen from './src/GroupScreen'
import HomeScreen from './src/HomeScreen'
import LaunchScreen from './src/LaunchScreen'
import { Navigator } from './src/Navigation'
import StreamScreen from './src/StreamScreen'
import TestScreen from './src/TestScreen'
import { LogLevel, LogRotation } from 'xmtp-react-native-sdk/lib/types/LogTypes'
const queryClient = new QueryClient()
interface LogFilesModalProps {
visible: boolean
onClose: () => void
client?: Client
}
interface DropdownOption {
title: string
onPress: () => void
}
interface AndroidDropdownProps {
visible: boolean
onClose: () => void
options: DropdownOption[]
}
// Component for the log files modal
const LogFilesModal: React.FC<LogFilesModalProps> = ({ visible, onClose }) => {
const [logFiles, setLogFiles] = useState<string[]>([])
const [fileSizes, setFileSizes] = useState<Record<string, string>>({})
const [isLoading, setIsLoading] = useState<boolean>(false)
const [logStatus, setLogStatus] = useState<string>('')
// Fetch log files when modal becomes visible
useEffect(() => {
if (visible) {
const fetchLogFiles = async (): Promise<void> => {
try {
setIsLoading(true)
setFileSizes({}) // Reset file sizes when reopening
setLogStatus('Checking log status...')
// Check if logging is active
const isActive = await Client.isLogWriterActive()
setLogStatus(`Logging ${isActive ? 'is active' : 'is NOT active'}`)
const files = await Client.getXMTPLogFilePaths()
console.log('Found log files:', files)
setLogFiles(files)
if (files.length === 0) {
setLogStatus(
(prev) =>
`${prev}\nNo log files found. Try activating logs first.`
)
} else {
setLogStatus((prev) => `${prev}\nFound ${files.length} log file(s)`)
}
// Load file sizes in parallel
const sizePromises = files.map(async (path) => {
try {
const content = await Client.readXMTPLogFile(path)
console.log(`Log file ${path} size: ${content.length} bytes`)
return {
path,
size: `${(content.length / 1024).toFixed(2)} KB`,
isEmpty: content.length === 0,
}
} catch (error) {
console.error(`Error loading size for ${path}:`, error)
return { path, size: 'Error loading size', isEmpty: true }
}
})
const results = await Promise.all(sizePromises)
// Update all sizes at once
const newSizes: Record<string, string> = {}
let emptyCount = 0
results.forEach(({ path, size, isEmpty }) => {
newSizes[path] = isEmpty ? `${size} (empty)` : size
if (isEmpty) emptyCount++
})
setFileSizes(newSizes)
if (emptyCount > 0) {
setLogStatus(
(prev) =>
`${prev}\n${emptyCount} empty log file(s) found. Make sure logging is properly activated.`
)
}
} catch (error) {
console.error('Error fetching log files:', error)
setLogStatus(
`Error: ${error instanceof Error ? error.message : String(error)}`
)
} finally {
setIsLoading(false)
}
}
fetchLogFiles()
}
}, [visible])
// Function to share a log file
const shareLogFile = async (filePath: string): Promise<void> => {
try {
// Read the log file content
const content = await Client.readXMTPLogFile(filePath)
if (content.length === 0) {
alert('This log file is empty. Try generating some logs first.')
return
}
// Use the Share API to open the native share dialog
if (Platform.OS === 'ios' || Platform.OS === 'android') {
const Share = require('react-native').Share
await Share.share({
title: 'XMTP Log File',
message: content,
// On iOS, you can also specify a subject
...(Platform.OS === 'ios' && {
subject: `XMTP Log: ${filePath.split('/').pop()}`,
}),
})
}
} catch (error) {
console.error('Error sharing log file:', error)
alert('Failed to share log file')
}
}
return (
<Modal
visible={visible}
transparent={true}
animationType="slide"
onRequestClose={onClose}
>
<View style={styles.modalContainer}>
<View style={styles.modalContent}>
<Text style={styles.modalTitle}>Log Files</Text>
{/* Display log status information */}
{logStatus ? (
<View style={styles.statusContainer}>
<Text style={styles.statusText}>{logStatus}</Text>
</View>
) : null}
{isLoading ? (
<Text style={styles.noLogsText}>Loading log files...</Text>
) : logFiles.length === 0 ? (
<Text style={styles.noLogsText}>No log files found</Text>
) : (
<FlatList
data={logFiles}
keyExtractor={(item) => item}
renderItem={({ item }) => (
<TouchableOpacity
style={styles.logFileItem}
onPress={() => shareLogFile(item)}
>
<Text
style={styles.logFilePath}
numberOfLines={1}
ellipsizeMode="middle"
>
{item}
</Text>
<Text style={styles.logFileSize}>
{fileSizes[item] || 'Loading...'}
</Text>
<Text style={styles.tapToShareText}>Tap to share</Text>
</TouchableOpacity>
)}
/>
)}
<TouchableOpacity style={styles.closeButton} onPress={onClose}>
<Text style={styles.closeButtonText}>Close</Text>
</TouchableOpacity>
</View>
</View>
</Modal>
)
}
// Styles for the modal
const styles = StyleSheet.create({
modalContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
padding: 20,
},
modalContent: {
backgroundColor: 'white',
borderRadius: 10,
padding: 20,
width: '100%',
maxHeight: '80%',
},
modalTitle: {
fontSize: 18,
fontWeight: 'bold',
marginBottom: 15,
textAlign: 'center',
},
logFileItem: {
padding: 10,
borderBottomWidth: 1,
borderBottomColor: '#eee',
},
logFilePath: {
fontSize: 14,
},
logFileSize: {
fontSize: 12,
color: '#666',
marginTop: 4,
},
tapToShareText: {
fontSize: 11,
color: 'rgb(49 0 110)',
marginTop: 4,
fontStyle: 'italic',
},
noLogsText: {
textAlign: 'center',
marginVertical: 20,
color: '#666',
},
closeButton: {
marginTop: 20,
padding: 10,
backgroundColor: 'rgb(49 0 110)',
borderRadius: 5,
alignItems: 'center',
},
closeButtonText: {
color: 'white',
fontWeight: 'bold',
},
statusContainer: {
backgroundColor: '#f5f5f5',
padding: 10,
borderRadius: 5,
marginBottom: 15,
},
statusText: {
fontSize: 12,
color: '#333',
},
})
// Custom dropdown for Android
const AndroidDropdown: React.FC<AndroidDropdownProps> = ({
visible,
onClose,
options,
}) => {
return (
<Modal
visible={visible}
transparent={true}
animationType="fade"
onRequestClose={onClose}
>
<TouchableOpacity
style={{
flex: 1,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
}}
onPress={onClose}
>
<View
style={{
position: 'absolute',
top: 60,
right: 10,
backgroundColor: 'white',
borderRadius: 5,
elevation: 5,
width: 200,
}}
>
{options.map((option, index) => (
<TouchableOpacity
key={index}
style={{
padding: 15,
borderBottomWidth: index < options.length - 1 ? 1 : 0,
borderBottomColor: '#eee',
}}
onPress={() => {
onClose()
option.onPress()
}}
>
<Text>{option.title}</Text>
</TouchableOpacity>
))}
</View>
</TouchableOpacity>
</Modal>
)
}
export default function App() {
// Uncomment below to ensure correct id loaded from .env
// console.log("Thirdweb client id: " + Config.THIRD_WEB_CLIENT_ID)
const [showAndroidDropdown, setShowAndroidDropdown] = useState<boolean>(false)
const [showLogFilesModal, setShowLogFilesModal] = useState<boolean>(false)
// Function to clear all log files
const clearLogFiles = async (): Promise<void> => {
try {
const files = await Client.getXMTPLogFilePaths()
if (files.length === 0) {
alert('No log files to clear')
return
}
let successCount = await Client.clearXMTPLogs()
if (successCount === files.length) {
alert('All log files cleared successfully')
} else if (successCount > 0) {
alert(`Cleared ${successCount} of ${files.length} log files`)
} else {
alert('Failed to clear log files')
}
} catch (error) {
console.error('Error clearing log files:', error)
alert('Failed to clear log files')
}
}
return (
// <ThirdwebProvider
// activeChain={Ethereum}
// supportedChains={[Ethereum]}
// clientId={Config.THIRD_WEB_CLIENT_ID}
// dAppMeta={{
// name: 'XMTP Example',
// description: 'Example app from xmtp-react-native repo',
// logoUrl:
// 'https://pbs.twimg.com/profile_images/1668323456935510016/2c_Ue8dF_400x400.jpg',
// url: 'https://xmtp.org',
// }}
// supportedWallets={[metamaskWallet(), rainbowWallet()]}
// >
<QueryClientProvider client={queryClient}>
<XmtpProvider>
<NavigationContainer>
<Navigator.Navigator>
<Navigator.Screen
name="launch"
component={LaunchScreen}
options={{
title: 'XMTP RN Example',
headerStyle: {
backgroundColor: 'rgb(49 0 110)',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
}}
/>
<Navigator.Screen
name="test"
component={TestScreen}
options={{ title: 'Unit Tests' }}
/>
<Navigator.Screen
name="home"
component={HomeScreen}
options={({ navigation }) => {
// Define dropdown options
const dropdownOptions: DropdownOption[] = [
{
title: 'New Conversation',
onPress: () => navigation.navigate('conversationCreate'),
},
{
title: 'Activate Logs',
onPress: async () => {
await Client.activatePersistentLibXMTPLogWriter(
LogLevel.DEBUG,
LogRotation.MINUTELY,
5
) // Using default values
alert('Logs activated')
},
},
{
title: 'Deactivate Logs',
onPress: () => {
Client.deactivatePersistentLibXMTPLogWriter()
alert('Logs deactivated')
},
},
{
title: 'View Log Files',
onPress: () => setShowLogFilesModal(true),
},
{
title: 'Clear Log Files',
onPress: () => {
// Show confirmation dialog before clearing logs
if (Platform.OS === 'ios') {
ActionSheetIOS.showActionSheetWithOptions(
{
options: ['Clear All Log Files', 'Cancel'],
destructiveButtonIndex: 0,
cancelButtonIndex: 1,
title:
'Are you sure you want to delete all log files?',
message: 'This action cannot be undone.',
},
(buttonIndex) => {
if (buttonIndex === 0) {
clearLogFiles()
}
}
)
} else {
// For Android, use Alert.alert
const Alert = require('react-native').Alert
Alert.alert(
'Clear Log Files',
'Are you sure you want to delete all log files? This action cannot be undone.',
[
{
text: 'Cancel',
style: 'cancel',
},
{
text: 'Clear',
onPress: clearLogFiles,
style: 'destructive',
},
]
)
}
},
},
]
// Platform-specific dropdown handling
const showDropdown = (): void => {
if (Platform.OS === 'ios') {
ActionSheetIOS.showActionSheetWithOptions(
{
options: [
...dropdownOptions.map((option) => option.title),
'Cancel',
],
cancelButtonIndex: dropdownOptions.length,
},
(buttonIndex) => {
if (buttonIndex < dropdownOptions.length) {
dropdownOptions[buttonIndex].onPress()
}
}
)
} else {
setShowAndroidDropdown(true)
}
}
return {
title: 'My Conversations',
headerStyle: {
backgroundColor: 'rgb(49 0 110)',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
headerRight: () => (
<>
<Button
onPress={showDropdown}
title="Options"
color={Platform.OS === 'ios' ? '#fff' : 'rgb(49 0 110)'}
/>
{/* Android dropdown */}
<AndroidDropdown
visible={showAndroidDropdown}
onClose={() => setShowAndroidDropdown(false)}
options={dropdownOptions}
/>
{/* Log files modal */}
<LogFilesModal
visible={showLogFilesModal}
onClose={() => setShowLogFilesModal(false)}
/>
</>
),
}
}}
/>
<Navigator.Screen
name="conversation"
component={ConversationScreen}
options={{ title: 'Conversation' }}
initialParams={{ topic: '' }}
/>
<Navigator.Screen
name="group"
component={GroupScreen}
options={{ title: 'Group' }}
/>
<Navigator.Screen
name="conversationCreate"
component={ConversationCreateScreen}
options={{ title: 'New Conversation' }}
/>
<Navigator.Screen
name="streamTest"
component={StreamScreen}
options={{ title: 'Stream Tests' }}
/>
</Navigator.Navigator>
</NavigationContainer>
</XmtpProvider>
</QueryClientProvider>
// </ThirdwebProvider>
)
}