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

index.tsx

162 lines5.1 KB

1import { useEffect, useRef, useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import {
4 fetchServerSentEvents,
5 useChat,
6 type UIMessage,
7} from '@tanstack/ai-react'
8import { OpenRouterKeyForm } from '@/components/open-router-key-form'
9import { byok } from '@/lib/byok'
10​
11function Messages({ messages }: { messages: ReadonlyArray<UIMessage> }) {
12 const messagesContainerRef = useRef<HTMLDivElement>(null)
13 const visibleMessages = messages.filter((message) =>
14 message.parts.some((part) => part.type === 'text' && part.content.trim()),
15 )
16​
17 useEffect(() => {
18 if (messagesContainerRef.current) {
19 messagesContainerRef.current.scrollTop =
20 messagesContainerRef.current.scrollHeight
21 }
22 }, [visibleMessages])
23​
24 if (!visibleMessages.length) {
25 return (
26 <div className="flex-1 overflow-y-auto px-4 py-8">
27 <div className="mx-auto max-w-2xl text-center">
28 <h2 className="mb-2 text-xl font-semibold text-white">Basic Chat</h2>
29 <p className="text-sm text-gray-400">
30 Paste an OpenRouter key. Then send a message.
31 </p>
32 </div>
33 </div>
34 )
35 }
36​
37 return (
38 <div
39 ref={messagesContainerRef}
40 className="flex-1 overflow-y-auto px-4 py-4"
41 >
42 {visibleMessages.map((message) => (
43 <div
44 key={message.id}
45 className={`mb-2 rounded-lg p-4 ${
46 message.role === 'assistant'
47 ? 'bg-linear-to-r from-orange-500/5 to-red-600/5'
48 : 'bg-transparent'
49 }`}
50 >
51 <div className="flex items-start gap-4">
52 <div
53 className={`flex h-8 w-8 shrink-0 items-center justify-center rounded-lg text-sm font-medium text-white ${
54 message.role === 'assistant'
55 ? 'bg-linear-to-r from-orange-500 to-red-600'
56 : 'bg-gray-700'
57 }`}
58 >
59 {message.role === 'assistant' ? 'AI' : 'U'}
60 </div>
61 <div className="min-w-0 flex-1">
62 {message.parts.map((part, index) => {
63 if (part.type === 'text' && part.content) {
64 return (
65 <div
66 key={`text-${index}`}
67 className="whitespace-pre-wrap text-white"
68 >
69 {part.content}
70 </div>
71 )
72 }
73 return null
74 })}
75 </div>
76 </div>
77 </div>
78 ))}
79 </div>
80 )
82​
83function ChatPage() {
84 const [input, setInput] = useState('')
85 const { messages, sendMessage, isLoading, error, stop } = useChat({
86 connection: fetchServerSentEvents('/api/chat'),
87 threadId: 'basic-chat',
88 byok,
89 })
90​
91 const handleSendMessage = () => {
92 if (!input.trim()) return
93 sendMessage(input.trim())
94 setInput('')
95 }
96​
97 return (
98 <div className="flex h-screen bg-gray-900">
99 <div className="flex w-full flex-col">
100 <div className="border-b border-orange-500/20 bg-gray-800 px-4 py-3">
101 <OpenRouterKeyForm />
102 </div>
103​
104 <Messages messages={messages} />
105​
106 {error ? (
107 <div className="mx-4 mt-2 rounded-lg border border-red-500/30 bg-red-500/10 p-3 text-sm text-red-400">
108 {error.message}
109 </div>
110 ) : null}
111​
112 <div className="border-t border-orange-500/10 bg-gray-900/80">
113 <div className="w-full px-4 py-3">
114 {isLoading ? (
115 <div className="mb-3 flex items-center justify-center">
116 <button
117 type="button"
118 onClick={stop}
119 className="rounded-lg bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-700"
120 >
121 Stop
122 </button>
123 </div>
124 ) : null}
125 <div className="flex items-end gap-2">
126 <textarea
127 value={input}
128 onChange={(event) => setInput(event.target.value)}
129 placeholder="Type a message..."
130 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"
131 rows={1}
132 disabled={isLoading}
133 onKeyDown={(event) => {
134 if (
135 event.key === 'Enter' &&
136 !event.shiftKey &&
137 input.trim()
138 ) {
139 event.preventDefault()
140 handleSendMessage()
141 }
142 }}
143 />
144 <button
145 type="button"
146 onClick={handleSendMessage}
147 disabled={!input.trim() || isLoading}
148 className="rounded-lg bg-orange-500 px-4 py-3 text-sm font-medium text-white disabled:opacity-50"
149 >
150 Send
151 </button>
152 </div>
153 </div>
154 </div>
155 </div>
156 </div>
157 )
159​
160export const Route = createFileRoute('/')({
161 component: ChatPage,
162})