import { useState } from 'react'; import { StyleSheet, TextInput, Alert } from 'react-native'; import { ScrollView } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { reportApplicationEvent, identifyApplicationUser, ApplicationTrackingOptions, } from 'tianji-react-native'; import { Collapsible } from '@/components/Collapsible'; import { ThemedText } from '@/components/ThemedText'; import { ThemedView } from '@/components/ThemedView'; import { IconSymbol } from '@/components/ui/IconSymbol'; import { useColorScheme } from '@/hooks/useColorScheme'; import { Colors } from '@/constants/Colors'; import { VStack } from '@/components/ui/vstack'; import { Toast, ToastDescription, ToastTitle, useToast, } from '@/components/ui/toast'; import { FormControl, FormControlLabel, FormControlLabelText, } from '@/components/ui/form-control'; import { Input, InputField } from '@/components/ui/input'; import { Button, ButtonText } from '@/components/ui/button'; import { HStack } from '@/components/ui/hstack'; import { useBottomTabBarHeight } from '@react-navigation/bottom-tabs'; interface EventResult { success: boolean; message: string; timestamp: string; } export default function ApplicationTestScreen() { const [serverUrl, setServerUrl] = useState('http://localhost:12345'); const [applicationId, setApplicationId] = useState( 'cm8aepds100qrge0xiv982zj4' ); const [eventName, setEventName] = useState('test_event'); const [eventData, setEventData] = useState('{"value": 1, "action": "click"}'); const [results, setResults] = useState([]); const [loading, setLoading] = useState(false); const toast = useToast(); const insets = useSafeAreaInsets(); const buttonTabbarHeight = useBottomTabBarHeight(); const colorScheme = useColorScheme() ?? 'light'; const sendApplicationEvent = async () => { if (!applicationId) { Alert.alert('Error', 'Please enter application ID'); return; } try { setLoading(true); const timestamp = new Date().toISOString(); // Parse event data from JSON string let parsedEventData; try { parsedEventData = JSON.parse(eventData); } catch (e) { Alert.alert('Error', 'Invalid JSON format in event data'); setLoading(false); return; } // Create tracking options const trackingOptions: ApplicationTrackingOptions = { serverUrl, applicationId, }; // Use the client SDK to report the event const result = await reportApplicationEvent( trackingOptions, eventName, parsedEventData ); toast.show({ placement: 'top', duration: 3000, render: () => { return ( Success! Event has been sent. ); }, }); setResults((prev) => [ { success: true, message: `Event sent successfully: ${result}`, timestamp, }, ...prev, ]); } catch (error) { setResults((prev) => [ { success: false, message: `Exception: ${error instanceof Error ? error.message : String(error)}`, timestamp: new Date().toISOString(), }, ...prev, ]); } finally { setLoading(false); } }; const sendIdentifyEvent = async () => { if (!applicationId) { Alert.alert('Error', 'Please enter application ID'); return; } try { setLoading(true); const timestamp = new Date().toISOString(); const userData = { userId: 'user123', email: 'test@example.com', name: 'Test User', plan: 'premium', signupDate: new Date().toISOString(), }; // Create tracking options const trackingOptions: ApplicationTrackingOptions = { serverUrl, applicationId, }; // Use the client SDK to identify the user const result = await identifyApplicationUser(trackingOptions, userData); toast.show({ placement: 'top', duration: 3000, render: () => { return ( Success! Event has been sent. ); }, }); setResults((prev) => [ { success: true, message: `User identification event sent successfully: ${result}`, timestamp, }, ...prev, ]); } catch (error) { setResults((prev) => [ { success: false, message: `Exception: ${error instanceof Error ? error.message : String(error)}`, timestamp: new Date().toISOString(), }, ...prev, ]); } finally { setLoading(false); } }; const clearResults = () => { setResults([]); }; return ( Application Event Test Server Address Application ID Event Name Event Data (JSON) Send user identification event, including user ID, email and other information Event Sending Records {results.length === 0 ? ( No records ) : ( results.map((result, index) => ( {result.timestamp} {result.message} )) )} ); } const styles = StyleSheet.create({ container: { flex: 1, paddingHorizontal: 16, }, titleContainer: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', marginBottom: 20, marginTop: 12, }, formSection: { marginBottom: 16, }, jsonInput: { borderWidth: 1, borderColor: '#ccc', borderRadius: 8, padding: 12, minHeight: 100, fontFamily: 'SpaceMono', fontSize: 14, shadowColor: '#000', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.1, shadowRadius: 2, elevation: 2, }, resultItem: { padding: 16, borderWidth: 1, borderRadius: 12, marginBottom: 12, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1, shadowRadius: 3, elevation: 3, }, });