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

main.ts

100 lines3.1 KB

1import { LitElement, html } from 'lit'
2import { repeat } from 'lit/directives/repeat.js'
3import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
4import './style.css'
5import './chat-message'
6​
7const queryClient = new QueryClient()
8​
9class ChatQueryProvider extends QueryClientProvider {
10 constructor() {
11 super()
12 this.client = queryClient
13 }
15​
16class ChatApp extends LitElement {
17 static properties = {
18 questions: { state: true },
19 currentQuestion: { state: true },
20 }
21​
22 private questions: Array<{ id: string; question: string }> = []
23 private currentQuestion = ''
24​
25 protected override createRenderRoot(): HTMLElement | DocumentFragment {
26 return this
27 }
28​
29 private submitMessage(event: SubmitEvent) {
30 event.preventDefault()
31 this.questions = [
32 ...this.questions,
33 { id: crypto.randomUUID(), question: this.currentQuestion },
34 ]
35 this.currentQuestion = ''
36 }
37​
38 render() {
39 return html`
40 <chat-query-provider>
41 <div class="flex flex-col h-screen max-w-3xl mx-auto p-4">
42 <h1 class="text-3xl font-bold text-gray-800">
43 TanStack Chat Example
44 </h1>
45 <div class="overflow-y-auto mb-4 space-y-4">
46 ${repeat(
47 this.questions,
48 ({ id }) => id,
49 ({ id, question }) =>
50 html`<chat-message
51 .messageId=${id}
52 .question=${question}
53 ></chat-message>`,
54 )}
55 </div>
56 <form
57 class="flex items-center space-x-2"
58 @submit=${this.submitMessage}
59 >
60 <input
61 .value=${this.currentQuestion}
62 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"
63 placeholder="Type your message..."
64 @input=${(event: Event) =>
65 (this.currentQuestion = (
66 event.currentTarget as HTMLInputElement
67 ).value)}
68 />
69 <button
70 type="submit"
71 ?disabled=${!this.currentQuestion.trim()}
72 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"
73 >
74 <span>Send</span>
75 <svg
76 xmlns="http://www.w3.org/2000/svg"
77 width="24"
78 height="24"
79 viewBox="0 0 24 24"
80 fill="none"
81 stroke="currentColor"
82 stroke-width="2"
83 stroke-linecap="round"
84 stroke-linejoin="round"
85 >
86 <path
87 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"
88 />
89 <path d="m21.854 2.147-10.94 10.939" />
90 </svg>
91 </button>
92 </form>
93 </div>
94 </chat-query-provider>
95 `
96 }
98​
99customElements.define('chat-query-provider', ChatQueryProvider)
100customElements.define('chat-app', ChatApp)