'use client' import { Loader2 } from 'lucide-react' import { useParams } from 'next/navigation' import { ChangeEvent, FormHTMLAttributes, ReactNode, forwardRef, useRef } from 'react' import { useFormStatus } from 'react-dom' import { TextArea_Shadcn_, cn } from 'ui' import { ChatSuggestions } from './ChatSuggestions' import { upsertMessageFormAction } from './action' export interface FormProps extends FormHTMLAttributes { loading?: boolean disabled?: boolean message?: string children?: ReactNode chatContext: 'new' | 'edit' } const SubmitButton = forwardRef( ({ canSubmit }, ref) => { const { pending } = useFormStatus() return (
{pending && ( )}
) } ) SubmitButton.displayName = 'SubmitButton' interface AssistantChatFormProps { chatContext: string placeholder: string canSubmit?: () => Promise } const AssistantChatForm = ({ chatContext, placeholder, canSubmit = () => Promise.resolve(true), }: AssistantChatFormProps) => { const { thread_id } = useParams() const textAreaRef = useRef(null) const disabled = false const handleKeyDown = (e: React.KeyboardEvent) => { if (!e.shiftKey && (e.key === 'Enter' || e.key === 'NumpadEnter')) { e.preventDefault() e.currentTarget.form?.requestSubmit() } } const handleChange = (e: ChangeEvent) => { const target = e.target let newHeight = target.scrollHeight + 'px' const input = target.value if (!input) { newHeight = '40px' } if (target.style.height !== newHeight) { target.style.height = newHeight } } return ( <>
{ const flag = await canSubmit() if (flag) { upsertMessageFormAction(formData) } }} >
0} isLoading={disabled} /> {chatContext === 'new' && ( { if (textAreaRef?.current) textAreaRef.current.value = v }} /> )} ) } AssistantChatForm.displayName = 'AssistantChatForm' export { AssistantChatForm }