'use client' import { useBreakpoint } from 'common' import { Loader2 } from 'lucide-react' import React, { ChangeEvent, memo, useRef } from 'react' import { ExpandingTextArea } from 'ui' import { cn } from 'ui/src/lib/utils' export interface FormProps extends React.FormHTMLAttributes { /* The ref for the textarea, optional. Exposed for the CommandsPopover to attach events. */ textAreaRef?: React.RefObject /* The loading state of the form */ loading: boolean /* The disabled state of the form */ disabled?: boolean /* The value of the textarea */ value?: string /* The function to handle the value change */ onValueChange: (value: ChangeEvent) => void /* Used to stop onSubmit event when Command popover is open. Use with AssistantCommandsPopover */ commandsOpen?: boolean /* Used to close Command popover when onSubmit event happens. Use with AssistantCommandsPopover */ setCommandsOpen?: (value: boolean) => void /* The icon to display on the left of the textarea */ icon?: React.ReactNode /* The function to handle the form submission */ onSubmit: React.FormHTMLAttributes['onSubmit'] /* The placeholder of the textarea */ placeholder?: string } const AssistantChatFormComponent = React.forwardRef( ( { loading = false, disabled = false, value = '', textAreaRef, commandsOpen = false, icon = null, onValueChange, setCommandsOpen, onSubmit, placeholder, ...props }, ref ) => { const formRef = useRef(null) const submitRef = useRef(null) const isMobile = useBreakpoint('md') /** * This function is used to handle the "Enter" key press */ const handleKeyDown = (event: React.KeyboardEvent) => { // Check if the pressed key is "Enter" (key code 13) without the "Shift" key // also checks if the commands popover is open if (event.key === 'Enter' && event.keyCode === 13 && !event.shiftKey && !commandsOpen) { event.preventDefault() if (submitRef.current) submitRef.current.click() } // handles closing the commands popover if open if (event.key === 'Enter' && commandsOpen) { if (setCommandsOpen) setCommandsOpen(false) } } return (
{icon && (
{icon}
)} onValueChange(event)} onKeyDown={handleKeyDown} />
{loading && ( )}
) } ) AssistantChatFormComponent.displayName = 'AssistantChatFormComponent' export const AssistantChatForm = memo(AssistantChatFormComponent)