Open History to see the last commit
1import { useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
4import { OpenRouterKeyForm } from '@/components/open-router-key-form'
5import { byok } from '@/lib/byok'
6import { tableSchema } from '@/lib/table-schema'
7
8function TablePage() {
9 const [input, setInput] = useState('')
10 const { sendMessage, isLoading, error, stop, partial, final } = useChat({
11 connection: fetchServerSentEvents('/api/chat'),
12 byok,
13 outputSchema: tableSchema,
14 })
15
16 const rows = partial.rows ?? []
17 const title = partial.title
18 const handleSendMessage = () => {
19 if (!input.trim()) return
20 sendMessage(input.trim())
21 setInput('')
22 }
23
24 return (
25 <div className="flex h-screen flex-col bg-gray-900">
26 <a
27 href="#main"
28 className="sr-only focus:not-sr-only focus:absolute focus:left-4 focus:top-4 focus:z-10 focus:rounded-lg focus:bg-orange-500 focus:px-3 focus:py-2 focus:text-white"
29 >
30 Skip to table
31 </a>
32 <header className="border-b border-orange-500/20 bg-gray-800 px-4 py-3">
33 <h1 className="mb-2 text-lg font-semibold text-white">
34 Streaming structured table
35 </h1>
36 <OpenRouterKeyForm />
37 </header>
38
39 <main id="main" className="min-h-0 flex-1 overflow-auto px-4 py-4">
40 {rows.length === 0 && !isLoading ? (
41 <p className="text-sm text-gray-400">
42 Paste an OpenRouter key. Then ask for a comparison table.
43 </p>
44 ) : (
45 <table
46 className="w-full border-collapse text-left text-sm text-white"
47 aria-busy={isLoading}
48 >
49 <caption className="mb-3 text-left text-base font-semibold text-white">
50 {title ?? 'Results'}
51 {isLoading ? ' (streaming)' : final ? ' (complete)' : ''}
52 </caption>
53 <thead>
54 <tr className="border-b border-orange-500/30">
55 <th scope="col" className="py-2 pr-3 font-medium">
56 Name
57 </th>
58 <th scope="col" className="py-2 pr-3 font-medium">
59 Year
60 </th>
61 <th scope="col" className="py-2 pr-3 font-medium">
62 Kind
63 </th>
64 <th scope="col" className="py-2 font-medium">
65 Note
66 </th>
67 </tr>
68 </thead>
69 <tbody>
70 {rows.map((row, index) => (
71 <tr key={index} className="border-b border-gray-800">
72 <th scope="row" className="py-2 pr-3 font-medium">
73 {row.name ?? '…'}
74 </th>
75 <td className="py-2 pr-3">{row.year ?? '…'}</td>
76 <td className="py-2 pr-3">{row.kind ?? '…'}</td>
77 <td className="py-2">{row.note ?? '…'}</td>
78 </tr>
79 ))}
80 </tbody>
81 </table>
82 )}
83
84 {error ? (
85 <p className="mt-3 text-sm text-red-400">{error.message}</p>
86 ) : null}
87
88 <p className="sr-only" aria-live="polite">
89 {isLoading ? 'Streaming table rows' : final ? 'Table complete' : ''}
90 </p>
91 </main>
92
93 <div className="border-t border-orange-500/10 bg-gray-900/80 px-4 py-3">
94 {isLoading ? (
95 <div className="mb-3 flex justify-center">
96 <button
97 type="button"
98 onClick={stop}
99 className="rounded-lg bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-700"
100 >
101 Stop
102 </button>
103 </div>
104 ) : null}
105 <div className="flex items-end gap-2">
106 <label className="sr-only" htmlFor="table-prompt">
107 Prompt
108 </label>
109 <textarea
110 id="table-prompt"
111 value={input}
112 onChange={(event) => setInput(event.target.value)}
113 placeholder="Compare 6 JavaScript frameworks"
114 className="w-full resize-none rounded-lg border border-orange-500/20 bg-gray-800/50 px-4 py-3 text-sm text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-orange-500/50"
115 rows={1}
116 disabled={isLoading}
117 onKeyDown={(event) => {
118 if (event.key === 'Enter' && !event.shiftKey && input.trim()) {
119 event.preventDefault()
120 handleSendMessage()
121 }
122 }}
123 />
124 <button
125 type="button"
126 onClick={handleSendMessage}
127 disabled={!input.trim() || isLoading}
128 className="rounded-lg bg-orange-500 px-4 py-3 text-sm font-medium text-white disabled:opacity-50"
129 >
130 Send
131 </button>
132 </div>
133 </div>
134 </div>
135 )
136}
137
138export const Route = createFileRoute('/')({
139 component: TablePage,
140})