|
3 | 3 | import { Button } from "@/components/ui/button"; |
4 | 4 | import { MessageCircle, X } from "lucide-react"; |
5 | 5 | import Image from "next/image"; |
6 | | -import { useEffect, useRef, useState } from "react"; |
| 6 | +import { useCallback, useEffect, useRef, useState } from "react"; |
7 | 7 | import { toast } from "sonner"; |
8 | 8 | import { motion, AnimatePresence } from "framer-motion"; |
9 | 9 |
|
10 | 10 | // Types |
| 11 | +type PrefillAIAgentEvent = CustomEvent<{ message: string }>; |
| 12 | + |
11 | 13 | type TMessageResponse = { |
12 | 14 | messages: TMessage[]; |
13 | 15 | messageCount: number; |
@@ -173,37 +175,64 @@ export default function LandingAIAgent() { |
173 | 175 | }, [messages]); |
174 | 176 |
|
175 | 177 | // Function to handle form submission |
176 | | - const handleSubmit = async (text: string) => { |
177 | | - if (!text.length || !inputRef.current) return; |
178 | | - |
179 | | - // Set chat input and clear input field |
180 | | - const chatInput = text.trim(); |
181 | | - setText(""); |
182 | | - inputRef.current.value = ""; |
183 | | - |
184 | | - setIsSubmitting(true); |
185 | | - try { |
186 | | - const prevMessages = messages; |
187 | | - const newMessages = [...prevMessages, { human: chatInput, ai: "" }]; |
188 | | - setMessages(newMessages); |
189 | | - const { output } = await fetchAsk({ |
190 | | - chatInput, |
191 | | - sessionId: sessionId as string |
192 | | - }); |
193 | | - |
194 | | - const newMessagesFromAI = [ |
195 | | - ...prevMessages, |
196 | | - { human: chatInput, ai: output } |
197 | | - ]; |
198 | | - setMessages(newMessagesFromAI); |
199 | | - |
200 | | - scrollToBottom(); |
201 | | - } catch (error) { |
202 | | - toast("Failed to ask question. Please try again later."); |
203 | | - } finally { |
204 | | - setIsSubmitting(false); |
205 | | - } |
206 | | - }; |
| 178 | + const handleSubmit = useCallback( |
| 179 | + async (text: string) => { |
| 180 | + if (!text.length) return; |
| 181 | + if (!text.length || !inputRef.current) return; |
| 182 | + |
| 183 | + // Set chat input and clear input field |
| 184 | + const chatInput = text.trim(); |
| 185 | + setText(""); |
| 186 | + if (inputRef.current) { |
| 187 | + inputRef.current.value = ""; |
| 188 | + } |
| 189 | + |
| 190 | + setIsSubmitting(true); |
| 191 | + try { |
| 192 | + const prevMessages = messages; |
| 193 | + const newMessages = [...prevMessages, { human: chatInput, ai: "" }]; |
| 194 | + setMessages(newMessages); |
| 195 | + const { output } = await fetchAsk({ |
| 196 | + chatInput, |
| 197 | + sessionId: sessionId as string |
| 198 | + }); |
| 199 | + |
| 200 | + const newMessagesFromAI = [ |
| 201 | + ...prevMessages, |
| 202 | + { human: chatInput, ai: output } |
| 203 | + ]; |
| 204 | + setMessages(newMessagesFromAI); |
| 205 | + |
| 206 | + scrollToBottom(); |
| 207 | + } catch (error) { |
| 208 | + toast("Failed to ask question. Please try again later."); |
| 209 | + } finally { |
| 210 | + setIsSubmitting(false); |
| 211 | + } |
| 212 | + }, |
| 213 | + [messages, sessionId] |
| 214 | + ); |
| 215 | + |
| 216 | + // Listen for custom event to prefill and submit |
| 217 | + useEffect(() => { |
| 218 | + const handlePrefillAndSubmit = (event: Event) => { |
| 219 | + const customEvent = event as PrefillAIAgentEvent; |
| 220 | + const { message } = customEvent.detail; |
| 221 | + setClosed(false); |
| 222 | + setText(message); |
| 223 | + |
| 224 | + // Wait for the chat to open and then submit |
| 225 | + setTimeout(() => { |
| 226 | + handleSubmit(message); |
| 227 | + }, 300); |
| 228 | + }; |
| 229 | + |
| 230 | + window.addEventListener("prefillAIAgent", handlePrefillAndSubmit); |
| 231 | + |
| 232 | + return () => { |
| 233 | + window.removeEventListener("prefillAIAgent", handlePrefillAndSubmit); |
| 234 | + }; |
| 235 | + }, [handleSubmit]); |
207 | 236 |
|
208 | 237 | return ( |
209 | 238 | <div className="fixed right-6 bottom-16 z-50 flex flex-col items-end gap-3"> |
|
0 commit comments