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

index.tsx

111 lines3.2 KB

1import { render } from 'preact'
2import { useState } from 'preact/hooks'
3import {
4 QueryClient,
5 QueryClientProvider,
6 useQuery,
7} from '@tanstack/preact-query'
8import { PreactQueryDevtools } from '@tanstack/preact-query-devtools'
9​
10import './style.css'
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 <PreactQueryDevtools />
20 <Example />
21 </QueryClientProvider>
22 )
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 )
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 onInput={(e) => setCurrentQuestion(e.currentTarget.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 stroke-width="2"
97 stroke-linecap="round"
98 stroke-linejoin="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 )
108​
109const rootElement = document.getElementById('root')
110if (!rootElement) throw new Error('Missing #root element')
111render(<App />, rootElement)