|
| 1 | +// import { useState, useEffect, useRef, useCallback } from "react"; |
| 2 | +// import { Textarea } from "@/components/ui/textarea"; |
| 3 | +// import { Button } from "@/components/ui/button"; |
| 4 | +// import { Send } from "lucide-react"; |
| 5 | +// import { |
| 6 | +// PromptInput, |
| 7 | +// PromptInputModelSelect, |
| 8 | +// PromptInputModelSelectContent, |
| 9 | +// PromptInputSubmit, |
| 10 | +// PromptInputTextarea, |
| 11 | +// PromptInputToolbar, |
| 12 | +// PromptInputTools, |
| 13 | +// } from "./elements/prompt-input"; |
| 14 | +// import { |
| 15 | +// ArrowUpIcon, |
| 16 | +// ChevronDownIcon, |
| 17 | +// CpuIcon, |
| 18 | +// PaperclipIcon, |
| 19 | +// StopIcon, |
| 20 | +// } from "./icons"; |
| 21 | +// import { useChatMessages } from "./context/chat-messages-provider"; |
| 22 | +// import { cn } from "@/lib/utils"; |
| 23 | +// import { useWindowSize } from "usehooks-ts"; |
| 24 | + |
| 25 | + |
| 26 | +// export function ChatInput({className}: {className?: string}) { |
| 27 | +// const { sendMessage, isLoading: isSessionLoading, status: chatStatus } = useChatMessages(); |
| 28 | +// const [message, setMessage] = useState(""); |
| 29 | +// const [isSendingMessage, setIsSendingMessage] = useState(false); |
| 30 | + |
| 31 | +// const textareaRef = useRef<HTMLTextAreaElement>(null); |
| 32 | +// const { width } = useWindowSize(); |
| 33 | + |
| 34 | +// const adjustHeight = useCallback(() => { |
| 35 | +// if (textareaRef.current) { |
| 36 | +// textareaRef.current.style.height = "44px"; |
| 37 | +// } |
| 38 | +// }, []); |
| 39 | + |
| 40 | +// useEffect(() => { |
| 41 | +// if (textareaRef.current) { |
| 42 | +// adjustHeight(); |
| 43 | +// } |
| 44 | +// }, [adjustHeight]); |
| 45 | + |
| 46 | +// const resetHeight = useCallback(() => { |
| 47 | +// if (textareaRef.current) { |
| 48 | +// textareaRef.current.style.height = "44px"; |
| 49 | +// } |
| 50 | +// }, []); |
| 51 | + |
| 52 | +// // Determine overall loading state (session loading OR message sending) |
| 53 | +// const isLoading = isSessionLoading || isSendingMessage; |
| 54 | + |
| 55 | +// const handleSend = async (e?: React.FormEvent) => { |
| 56 | +// e?.preventDefault(); |
| 57 | +// const trimmedMessage = message.trim(); |
| 58 | + |
| 59 | +// if (!trimmedMessage || isLoading) return; // Prevent sending if empty or loading |
| 60 | + |
| 61 | +// setIsSendingMessage(true); // Set local sending state |
| 62 | +// setMessage(""); // Clear input immediately for better UX |
| 63 | + |
| 64 | +// try { |
| 65 | +// await sendMessage(trimmedMessage); // Call the hook's sendMessage |
| 66 | +// // toast.success("Message sent."); // useChatSession already handles toasts for success/failure |
| 67 | +// } catch (error) { |
| 68 | +// console.error("Error sending message from input:", error); |
| 69 | +// // The hook should already show a toast for this, but if not, you could add one here. |
| 70 | +// setMessage(trimmedMessage); // Restore message on failure |
| 71 | +// } finally { |
| 72 | +// setIsSendingMessage(false); // Reset local sending state |
| 73 | +// } |
| 74 | +// }; |
| 75 | + |
| 76 | +// const handleInput = (event: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 77 | +// setMessage(event.target.value); |
| 78 | +// }; |
| 79 | + |
| 80 | +// return ( |
| 81 | +// <div className={cn("relative flex w-full flex-col gap-4", className)}> |
| 82 | +// <PromptInput |
| 83 | +// className="rounded-xl border border-border bg-background p-3 shadow-xs transition-all duration-200 focus-within:border-border hover:border-muted-foreground/50" |
| 84 | +// onSubmit={(event) => { |
| 85 | +// event.preventDefault(); |
| 86 | +// if (status !== "ready") { |
| 87 | +// toast.error("Please wait for the model to finish its response!"); |
| 88 | +// } else { |
| 89 | +// handleSend(); |
| 90 | +// } |
| 91 | +// }} |
| 92 | +// > |
| 93 | +// <div className="flex flex-row items-start gap-1 sm:gap-2"> |
| 94 | +// <PromptInputTextarea |
| 95 | +// autoFocus |
| 96 | +// className="grow resize-none border-0! border-none! bg-transparent p-2 text-sm outline-none ring-0 [-ms-overflow-style:none] [scrollbar-width:none] placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-0 focus-visible:ring-offset-0 [&::-webkit-scrollbar]:hidden" |
| 97 | +// data-testid="multimodal-input" |
| 98 | +// disableAutoResize={true} |
| 99 | +// maxHeight={200} |
| 100 | +// minHeight={44} |
| 101 | +// onChange={handleInput} |
| 102 | +// placeholder="Send a message..." |
| 103 | +// ref={textareaRef} |
| 104 | +// rows={1} |
| 105 | +// value={message} |
| 106 | +// />{" "} |
| 107 | +// <Context {...contextProps} /> |
| 108 | +// </div> |
| 109 | +// <PromptInputToolbar className="!border-top-0 border-t-0! p-0 shadow-none dark:border-0 dark:border-transparent!"> |
| 110 | + |
| 111 | +// {status === "submitted" ? ( |
| 112 | +// <StopButton setMessages={setMessages} stop={stop} /> |
| 113 | +// ) : ( |
| 114 | +// <PromptInputSubmit |
| 115 | +// className="size-8 rounded-full bg-primary text-primary-foreground transition-colors duration-200 hover:bg-primary/90 disabled:bg-muted disabled:text-muted-foreground" |
| 116 | +// disabled={!message.trim()} |
| 117 | +// status={status} |
| 118 | +// data-testid="send-button" |
| 119 | +// > |
| 120 | +// <ArrowUpIcon size={14} /> |
| 121 | +// </PromptInputSubmit> |
| 122 | +// )} |
| 123 | +// </PromptInputToolbar> |
| 124 | +// </PromptInput> |
| 125 | +// </div> |
| 126 | +// // <form onSubmit={handleSend} className="max-w-4xl mx-auto"> |
| 127 | +// // <div className="relative flex items-end w-xl"> |
| 128 | +// // <Textarea |
| 129 | +// // placeholder="Send a message..." |
| 130 | +// // value={message} |
| 131 | +// // onChange={(e) => setMessage(e.target.value)} |
| 132 | +// // onKeyDown={(e) => { |
| 133 | +// // if (e.key === "Enter" && !e.shiftKey) { |
| 134 | +// // handleSend(e); |
| 135 | +// // } |
| 136 | +// // }} |
| 137 | +// // className="min-h-[50px] pr-16 resize-none" |
| 138 | +// // disabled={isLoading} |
| 139 | +// // /> |
| 140 | + |
| 141 | +// // {/* Buttons overlay */} |
| 142 | +// // <div className="absolute right-2 bottom-2 flex items-center space-x-1"> |
| 143 | +// // <Button |
| 144 | +// // type="submit" |
| 145 | +// // size="icon" |
| 146 | +// // disabled={!message.trim() || isLoading} |
| 147 | +// // title="Send Message" |
| 148 | +// // > |
| 149 | +// // <Send className="h-5 w-5" /> |
| 150 | +// // </Button> |
| 151 | +// // </div> |
| 152 | +// // </div> |
| 153 | +// // </form> |
| 154 | +// ); |
| 155 | +// } |
0 commit comments