Open History to see the last commit
1import { useEffect, useRef, useState } from "react";
2import { useStore } from "@tanstack/react-store";
3import { Send, X } from "lucide-react";
4import { Streamdown } from "streamdown";
5
6import { useMotorcycleChat } from "@/lib/ai-hook";
7import type { ChatMessages } from "@/lib/ai-hook";
8import { showAIAssistant } from "@/store/assistant";
9
10import MotorcycleRecommendation from "./MotorcycleRecommendation";
11
12function Messages({ messages }: { messages: ChatMessages }) {
13 const messagesContainerRef = useRef<HTMLDivElement>(null);
14
15 useEffect(() => {
16 if (messagesContainerRef.current) {
17 messagesContainerRef.current.scrollTop =
18 messagesContainerRef.current.scrollHeight;
19 }
20 }, [messages]);
21
22 if (!messages.length) {
23 return (
24 <div className="flex-1 flex items-center justify-center text-gray-400 text-sm">
25 Ask me about your next motorcycle.
26 </div>
27 );
28 }
29
30 return (
31 <div ref={messagesContainerRef} className="flex-1 overflow-y-auto">
32 {messages.map(({ id, role, parts }) => (
33 <div
34 key={id}
35 className={`py-3 ${
36 role === "assistant"
37 ? "bg-linear-to-r from-orange-500/5 to-red-600/5"
38 : "bg-transparent"
39 }`}
40 >
41 {parts.map((part, index) => {
42 if (part.type === "text" && part.content) {
43 return (
44 <div key={index} className="flex items-start gap-2 px-4">
45 {role === "assistant" ? (
46 <div className="w-6 h-6 rounded-lg bg-linear-to-r from-orange-500 to-red-600 flex items-center justify-center text-xs font-medium text-white flex-shrink-0">
47 AI
48 </div>
49 ) : (
50 <div className="w-6 h-6 rounded-lg bg-gray-700 flex items-center justify-center text-xs font-medium text-white flex-shrink-0">
51 Y
52 </div>
53 )}
54 <div className="flex-1 min-w-0 text-white prose dark:prose-invert max-w-none prose-sm">
55 <Streamdown>{part.content}</Streamdown>
56 </div>
57 </div>
58 );
59 }
60 if (
61 part.type === "tool-call" &&
62 part.name === "recommendMotorcycle" &&
63 part.output
64 ) {
65 return (
66 <div key={part.id} className="max-w-[80%] mx-auto">
67 <MotorcycleRecommendation id={String(part.output?.id)} />
68 </div>
69 );
70 }
71 })}
72 </div>
73 ))}
74 </div>
75 );
76}
77
78export default function AIAssistant() {
79 const isOpen = useStore(showAIAssistant);
80 const { messages, sendMessage } = useMotorcycleChat();
81 const [input, setInput] = useState("");
82
83 return (
84 <div className="relative z-50">
85 <button
86 onClick={() => showAIAssistant.setState((state) => !state)}
87 className="flex items-center gap-2 px-4 py-2 rounded-lg bg-black/90 text-white hover:bg-black/80 transition-all border border-orange-500/20 shadow-lg shadow-orange-500/10"
88 >
89 <div className="w-5 h-5 rounded-lg bg-linear-to-r from-orange-500 to-red-600 flex items-center justify-center text-xs font-medium">
90 AI
91 </div>
92 Motorcycle Expert
93 </button>
94
95 {isOpen && (
96 <div className="absolute top-full right-0 mt-2 w-[700px] h-[600px] bg-black/95 rounded-lg shadow-2xl border border-orange-500/20 flex flex-col backdrop-blur-sm">
97 <div className="flex items-center justify-between p-4 border-b border-orange-500/20 bg-linear-to-r from-orange-500/5 to-red-600/5">
98 <h3 className="font-semibold text-white flex items-center gap-2">
99 <span className="w-6 h-6 rounded-lg bg-linear-to-r from-orange-500 to-red-600 flex items-center justify-center text-xs">
100 AI
101 </span>
102 Motorcycle Expert
103 </h3>
104 <button
105 onClick={() => showAIAssistant.setState((state) => !state)}
106 className="text-gray-400 hover:text-white transition-colors"
107 >
108 <X className="w-4 h-4" />
109 </button>
110 </div>
111
112 <Messages messages={messages} />
113
114 <div className="p-4 border-t border-orange-500/20 bg-linear-to-r from-orange-500/5 to-red-600/5">
115 <form
116 onSubmit={(e) => {
117 e.preventDefault();
118 if (input.trim()) {
119 sendMessage(input);
120 setInput("");
121 }
122 }}
123 >
124 <div className="relative">
125 <textarea
126 value={input}
127 onChange={(e) => setInput(e.target.value)}
128 placeholder="Ask about our motorcycles..."
129 className="w-full rounded-lg border border-orange-500/20 bg-black/50 pl-3 pr-10 py-3 text-sm text-white placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-orange-500/30 focus:border-transparent resize-none overflow-hidden shadow-inner"
130 rows={1}
131 style={{ minHeight: "44px", maxHeight: "120px" }}
132 onInput={(e) => {
133 const target = e.target as HTMLTextAreaElement;
134 target.style.height = "auto";
135 target.style.height =
136 Math.min(target.scrollHeight, 120) + "px";
137 }}
138 onKeyDown={(e) => {
139 if (e.key === "Enter" && !e.shiftKey && input.trim()) {
140 e.preventDefault();
141 sendMessage(input);
142 setInput("");
143 }
144 }}
145 />
146 <button
147 type="submit"
148 disabled={!input.trim()}
149 className="absolute right-2 top-1/2 -translate-y-1/2 p-2 text-orange-500 hover:text-orange-400 disabled:text-gray-500 transition-colors focus:outline-none"
150 >
151 <Send className="w-4 h-4" />
152 </button>
153 </div>
154 </form>
155 </div>
156 </div>
157 )}
158 </div>
159 );
160}