Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

App.tsx

171 lines3.8 KB

1import { useMemo, useState } from 'react'
2import {
3 Button,
4 SafeAreaView,
5 ScrollView,
6 StyleSheet,
7 Text,
8 TextInput,
9 View,
10} from 'react-native'
11import {
12 fetchHttpStream,
13 useChat,
14 xhrHttpStream,
15 xhrServerSentEvents,
16 type ConnectionAdapter,
17} from '@tanstack/ai-react'
18​
19const chatUrl =
20 process.env.EXPO_PUBLIC_TANSTACK_AI_CHAT_URL ?? 'http://127.0.0.1:8787/chat'
21​
22type TransportKey = 'fetch-http' | 'xhr-sse' | 'xhr-http'
23​
24const transportLabels: Record<TransportKey, string> = {
25 'fetch-http': 'Fetch HTTP stream',
26 'xhr-sse': 'XHR server-sent events',
27 'xhr-http': 'XHR HTTP stream',
29​
30function useConnection(transport: TransportKey): ConnectionAdapter {
31 return useMemo(() => {
32 switch (transport) {
33 case 'fetch-http':
34 return fetchHttpStream(chatUrl)
35 case 'xhr-sse':
36 return xhrServerSentEvents(chatUrl)
37 case 'xhr-http':
38 return xhrHttpStream(chatUrl)
39 }
40 }, [transport])
42​
43export default function App() {
44 const [transport, setTransport] = useState<TransportKey>('fetch-http')
45 const [input, setInput] = useState('Hello from React Native')
46 const connection = useConnection(transport)
47​
48 const { messages, sendMessage, isLoading, error, stop } = useChat({
49 threadId: `rn-smoke-${transport}`,
50 connection,
51 })
52​
53 async function send() {
54 const next = input.trim()
55 if (!next || isLoading) return
56 setInput('')
57 await sendMessage(next)
58 }
59​
60 return (
61 <SafeAreaView style={styles.screen}>
62 <View style={styles.header}>
63 <Text style={styles.title}>TanStack AI RN Smoke</Text>
64 <Text style={styles.subtitle}>{chatUrl}</Text>
65 </View>
66​
67 <View style={styles.transportRow}>
68 {Object.entries(transportLabels).map(([key, label]) => (
69 <Button
70 key={key}
71 title={label}
72 color={transport === key ? '#0f766e' : '#475569'}
73 onPress={() => setTransport(key as TransportKey)}
74 />
75 ))}
76 </View>
77​
78 <ScrollView style={styles.messages}>
79 {messages.map((message) => (
80 <View key={message.id} style={styles.message}>
81 <Text style={styles.role}>{message.role}</Text>
82 {message.parts.map((part, index) =>
83 part.type === 'text' ? (
84 <Text key={index} style={styles.part}>
85 {part.content}
86 </Text>
87 ) : null,
88 )}
89 </View>
90 ))}
91 {error ? <Text style={styles.error}>{error.message}</Text> : null}
92 </ScrollView>
93​
94 <View style={styles.composer}>
95 <TextInput
96 accessibilityLabel="Message"
97 style={styles.input}
98 value={input}
99 onChangeText={setInput}
100 editable={!isLoading}
101 multiline
102 />
103 <Button
104 title={isLoading ? 'Stop' : 'Send'}
105 onPress={isLoading ? stop : send}
106 />
107 </View>
108 </SafeAreaView>
109 )
111​
112const styles = StyleSheet.create({
113 screen: {
114 flex: 1,
115 backgroundColor: '#f8fafc',
116 },
117 header: {
118 padding: 16,
119 gap: 4,
120 },
121 title: {
122 color: '#0f172a',
123 fontSize: 22,
124 fontWeight: '700',
125 },
126 subtitle: {
127 color: '#475569',
128 fontSize: 12,
129 },
130 transportRow: {
131 gap: 8,
132 paddingHorizontal: 16,
133 },
134 messages: {
135 flex: 1,
136 padding: 16,
137 },
138 message: {
139 marginBottom: 12,
140 borderRadius: 12,
141 backgroundColor: '#ffffff',
142 padding: 12,
143 },
144 role: {
145 color: '#0f766e',
146 fontWeight: '700',
147 marginBottom: 4,
148 textTransform: 'uppercase',
149 },
150 part: {
151 color: '#0f172a',
152 lineHeight: 20,
153 },
154 error: {
155 color: '#b91c1c',
156 },
157 composer: {
158 borderTopColor: '#cbd5e1',
159 borderTopWidth: 1,
160 gap: 8,
161 padding: 16,
162 },
163 input: {
164 minHeight: 72,
165 borderColor: '#94a3b8',
166 borderRadius: 12,
167 borderWidth: 1,
168 color: '#0f172a',
169 padding: 12,
170 },
171})