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