"use client" import { Textarea } from "@/components/ui/textarea" import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip" import { cn } from "@/lib/utils" import React, { createContext, useContext, useEffect, useRef, useState, } from "react" type PromptInputContextType = { isLoading: boolean value: string setValue: (value: string) => void maxHeight: number | string onSubmit?: () => void disabled?: boolean textareaRef: React.RefObject } const PromptInputContext = createContext({ isLoading: false, value: "", setValue: () => {}, maxHeight: 240, onSubmit: undefined, disabled: false, textareaRef: React.createRef(), }) function usePromptInput() { const context = useContext(PromptInputContext) if (!context) { throw new Error("usePromptInput must be used within a PromptInput") } return context } type PromptInputProps = { isLoading?: boolean value?: string onValueChange?: (value: string) => void maxHeight?: number | string onSubmit?: () => void children: React.ReactNode className?: string } function PromptInput({ className, isLoading = false, maxHeight = 240, value, onValueChange, onSubmit, children, }: PromptInputProps) { const [internalValue, setInternalValue] = useState(value || "") const textareaRef = useRef(null) const handleChange = (newValue: string) => { setInternalValue(newValue) onValueChange?.(newValue) } return (
textareaRef.current?.focus()} > {children}
) } export type PromptInputTextareaProps = { disableAutosize?: boolean } & React.ComponentProps function PromptInputTextarea({ className, onKeyDown, disableAutosize = false, ...props }: PromptInputTextareaProps) { const { value, setValue, maxHeight, onSubmit, disabled, textareaRef } = usePromptInput() useEffect(() => { if (disableAutosize) return if (!textareaRef.current) return if (textareaRef.current.scrollTop === 0) { textareaRef.current.style.height = "auto" } textareaRef.current.style.height = typeof maxHeight === "number" ? `${Math.min(textareaRef.current.scrollHeight, maxHeight)}px` : `min(${textareaRef.current.scrollHeight}px, ${maxHeight})` }, [value, maxHeight, disableAutosize]) const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault() onSubmit?.() } onKeyDown?.(e) } return (