Open History to see the last commit
1import ReactDOM from 'react-dom/client'
2import {
3 QueryClient,
4 QueryClientProvider,
5 useQuery,
6} from '@tanstack/react-query'
7import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
8β
9import './style.css'
10import { useState } from 'react'
11import { messagesQueries } from './services/messages/queries'
12import { Message } from './message'
13β
14const queryClient = new QueryClient()
15β
16export default function App() {
17 return (
18 <QueryClientProvider client={queryClient}>
19 <ReactQueryDevtools />
20 <Example />
21 </QueryClientProvider>
22 )
23}
24β
25function ChatMessage({ id, question }: { id: string; question: string }) {
26 const {
27 error,
28 data = [],
29 isFetching,
30 } = useQuery(messagesQueries.detail(id, question))
31β
32 if (error) return 'An error has occurred: ' + error.message
33β
34 return (
35 <div>
36 <Message message={{ content: question, isQuestion: true }} />
37 <Message
38 inProgress={isFetching}
39 message={{ content: data.join(' '), isQuestion: false }}
40 />
41 </div>
42 )
43}
44β
45function Example() {
46 const [questions, setQuestions] = useState<
47 Array<{ id: string; question: string }>
48 >([])
49 const [currentQuestion, setCurrentQuestion] = useState('')
50β
51 const submitMessage = () => {
52 setQuestions([
53 ...questions,
54 { id: crypto.randomUUID(), question: currentQuestion },
55 ])
56 setCurrentQuestion('')
57 }
58β
59 return (
60 <div className="flex flex-col h-screen max-w-3xl mx-auto p-4">
61 <h1 className="text-3xl font-bold text-gray-800">
62 TanStack Chat Example
63 </h1>
64 <div className="overflow-y-auto mb-4 space-y-4">
65 {questions.map(({ id, question }) => (
66 <ChatMessage key={id} id={id} question={question} />
67 ))}
68 </div>
69β
70 <form
71 className="flex items-center space-x-2"
72 onSubmit={(e) => {
73 e.preventDefault()
74 submitMessage()
75 }}
76 >
77 <input
78 className="flex-1 p-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-gray-100"
79 value={currentQuestion}
80 onChange={(e) => setCurrentQuestion(e.target.value)}
81 placeholder="Type your message..."
82 />
83 <button
84 type="submit"
85 disabled={!currentQuestion.trim()}
86 className="flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold px-4 py-2 rounded-2xl shadow-md transition"
87 >
88 <span>Send</span>
89 <svg
90 xmlns="http://www.w3.org/2000/svg"
91 width="24"
92 height="24"
93 viewBox="0 0 24 24"
94 fill="none"
95 stroke="currentColor"
96 strokeWidth="2"
97 strokeLinecap="round"
98 strokeLinejoin="round"
99 >
100 <path d="M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z" />
101 <path d="m21.854 2.147-10.94 10.939" />
102 </svg>
103 </button>
104 </form>
105 </div>
106 )
107}
108β
109const rootElement = document.getElementById('root')
110if (!rootElement) throw new Error('Missing #root element')
111ReactDOM.createRoot(rootElement).render(<App />)