Open History to see the last commit
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: Array<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 )
81}
82
83function ChatPage() {
84 const [input, setInput] = useState('')
85 const { messages, sendMessage, isLoading, error, stop } = useChat({
86 connection: fetchServerSentEvents('/api/chat'),
87 byok,
88 })
89
90 const handleSendMessage = () => {
91 if (!input.trim()) return
92 sendMessage(input.trim())
93 setInput('')
94 }
95
96 return (
97 <div className="flex h-screen bg-gray-900">
98 <div className="flex w-full flex-col">
99 <div className="border-b border-orange-500/20 bg-gray-800 px-4 py-3">
100 <OpenRouterKeyForm />
101 </div>
102
103 <Messages messages={messages} />
104
105 {error ? (
106 <div className="mx-4 mt-2 rounded-lg border border-red-500/30 bg-red-500/10 p-3 text-sm text-red-400">
107 {error.message}
108 </div>
109 ) : null}
110
111 <div className="border-t border-orange-500/10 bg-gray-900/80">
112 <div className="w-full px-4 py-3">
113 {isLoading ? (
114 <div className="mb-3 flex items-center justify-center">
115 <button
116 type="button"
117 onClick={stop}
118 className="rounded-lg bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-700"
119 >
120 Stop
121 </button>
122 </div>
123 ) : null}
124 <div className="flex items-end gap-2">
125 <textarea
126 value={input}
127 onChange={(event) => setInput(event.target.value)}
128 placeholder="Type a message..."
129 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"
130 rows={1}
131 disabled={isLoading}
132 onKeyDown={(event) => {
133 if (
134 event.key === 'Enter' &&
135 !event.shiftKey &&
136 input.trim()
137 ) {
138 event.preventDefault()
139 handleSendMessage()
140 }
141 }}
142 />
143 <button
144 type="button"
145 onClick={handleSendMessage}
146 disabled={!input.trim() || isLoading}
147 className="rounded-lg bg-orange-500 px-4 py-3 text-sm font-medium text-white disabled:opacity-50"
148 >
149 Send
150 </button>
151 </div>
152 </div>
153 </div>
154 </div>
155 </div>
156 )
157}
158
159export const Route = createFileRoute('/')({
160 component: ChatPage,
161})