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