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

AIAssistant.tsx

162 lines6.5 KB

1import { useEffect, useRef, useState } from "react";
2import { useStore } from "@tanstack/react-store";
3import { Send, X } from "lucide-react";
4import { Streamdown } from "streamdown";
5​
6import { useProductChat } from "@/lib/ai-hook";
7import type { ChatMessages } from "@/lib/ai-hook";
8import { showAIAssistant } from "@/store/assistant";
9​
10import ProductRecommendation from "./ProductRecommendation";
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 flex-1 items-center justify-center px-4 text-sm text-[var(--sea-ink-soft)]">
25 Ask me which product best fits your team.
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-[rgba(79,184,178,0.16)] to-[rgba(47,106,74,0.07)]"
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="flex h-6 w-6 flex-shrink-0 items-center justify-center rounded-lg bg-[linear-gradient(90deg,#56c6be,#328f97)] text-xs font-medium text-white">
47 AI
48 </div>
49 ) : (
50 <div className="flex h-6 w-6 flex-shrink-0 items-center justify-center rounded-lg bg-[rgba(23,58,64,0.65)] text-xs font-medium text-white">
51 Y
52 </div>
53 )}
54 <div className="prose prose-sm max-w-none min-w-0 flex-1 text-[var(--sea-ink)]">
55 <Streamdown>{part.content}</Streamdown>
56 </div>
57 </div>
58 );
59 }
60 if (
61 part.type === "tool-call" &&
62 part.name === "recommendProduct" &&
63 part.output
64 ) {
65 return (
66 <div key={part.id} className="max-w-[80%] mx-auto">
67 <ProductRecommendation
68 id={String((part.output as { id?: string | number })?.id)}
69 />
70 </div>
71 );
72 }
73 })}
74 </div>
75 ))}
76 </div>
77 );
79​
80export default function AIAssistant() {
81 const isOpen = useStore(showAIAssistant);
82 const { messages, sendMessage } = useProductChat();
83 const [input, setInput] = useState("");
84​
85 return (
86 <div className="relative z-50">
87 <button
88 onClick={() => showAIAssistant.setState((state) => !state)}
89 className="flex items-center gap-2 rounded-full border border-[var(--chip-line)] bg-[var(--chip-bg)] px-3 py-2 text-[var(--sea-ink)] shadow-[0_8px_22px_rgba(23,58,64,0.12)] transition hover:-translate-y-0.5"
90 >
91 <div className="flex h-5 w-5 items-center justify-center rounded-lg bg-[linear-gradient(90deg,#56c6be,#328f97)] text-xs font-medium text-white">
92 AI
93 </div>
94 Product Expert
95 </button>
96​
97 {isOpen && (
98 <div className="absolute right-0 top-full mt-2 flex h-[min(76vh,620px)] w-[min(92vw,740px)] flex-col overflow-hidden rounded-2xl border border-[var(--line)] bg-[var(--surface-strong)] shadow-2xl backdrop-blur-md">
99 <div className="flex items-center justify-between border-b border-[var(--line)] bg-[linear-gradient(90deg,rgba(79,184,178,0.16),rgba(47,106,74,0.08))] p-4">
100 <h3 className="flex items-center gap-2 font-semibold text-[var(--sea-ink)]">
101 <span className="flex h-6 w-6 items-center justify-center rounded-lg bg-[linear-gradient(90deg,#56c6be,#328f97)] text-xs text-white">
102 AI
103 </span>
104 Product Expert
105 </h3>
106 <button
107 onClick={() => showAIAssistant.setState((state) => !state)}
108 className="text-[var(--sea-ink-soft)] transition-colors hover:text-[var(--sea-ink)]"
109 >
110 <X className="w-4 h-4" />
111 </button>
112 </div>
113​
114 <Messages messages={messages} />
115​
116 <div className="border-t border-[var(--line)] bg-[linear-gradient(90deg,rgba(79,184,178,0.1),var(--assistant-footer-end))] p-4">
117 <form
118 onSubmit={(e) => {
119 e.preventDefault();
120 if (input.trim()) {
121 sendMessage(input);
122 setInput("");
123 }
124 }}
125 >
126 <div className="relative">
127 <textarea
128 value={input}
129 onChange={(e) => setInput(e.target.value)}
130 placeholder="Ask about products, bundles, or where to start..."
131 className="w-full resize-none overflow-hidden rounded-xl border border-[var(--line)] bg-[var(--assistant-input-bg)] py-3 pl-3 pr-10 text-sm text-[var(--sea-ink)] placeholder-[var(--sea-ink-soft)] shadow-inner focus:border-transparent focus:outline-none focus:ring-2 focus:ring-[rgba(79,184,178,0.35)]"
132 rows={1}
133 style={{ minHeight: "44px", maxHeight: "120px" }}
134 onInput={(e) => {
135 const target = e.target as HTMLTextAreaElement;
136 target.style.height = "auto";
137 target.style.height =
138 Math.min(target.scrollHeight, 120) + "px";
139 }}
140 onKeyDown={(e) => {
141 if (e.key === "Enter" && !e.shiftKey && input.trim()) {
142 e.preventDefault();
143 sendMessage(input);
144 setInput("");
145 }
146 }}
147 />
148 <button
149 type="submit"
150 disabled={!input.trim()}
151 className="absolute right-2 top-1/2 -translate-y-1/2 p-2 text-[var(--lagoon-deep)] transition-colors hover:text-[var(--sea-ink)] disabled:text-gray-500 focus:outline-none"
152 >
153 <Send className="w-4 h-4" />
154 </button>
155 </div>
156 </form>
157 </div>
158 </div>
159 )}
160 </div>
161 );