import { useState } from 'react' import { Box, Button, FormControl, FormHelperText, IconButton, InputBase, Menu, MenuItem, Typography, } from '@mui/material' import { ArrowUpwardOutlined, StopCircleOutlined, KeyboardArrowDown, } from '@mui/icons-material' import type { ChatFooterProps } from '../types' import { styles } from './styles' const DEFAULT_CAPTION = 'Responses are AI-generated. Please verify key information.' export function ChatFooter({ value, onChange, onSend, onStop, isGenerating = false, disabled = false, placeholder = 'Type a message...', labels = {}, caption = DEFAULT_CAPTION, models, selectedModel, onModelChange, startToolbarSlot, endToolbarSlot, sx, }: ChatFooterProps) { const [modelAnchor, setModelAnchor] = useState(null) const canSend = value.trim() && !disabled && !isGenerating // Only worth showing when there's an actual choice — a single option is dead UI. const showModelSelector = (models?.length ?? 0) > 1 const modelLabel = labels.model ?? 'Select model' // Visible trigger text: the matched option's label, the raw value as a // fallback, or the placeholder when nothing is selected yet. const selectedLabel = models?.find((m) => m.value === selectedModel)?.label ?? (selectedModel && selectedModel.length > 0 ? selectedModel : modelLabel) // The slots row (start slot + model selector + end slot) renders under the input // only when there's something to show; otherwise the grid is just input | send. const hasSlots = showModelSelector || Boolean(startToolbarSlot) || Boolean(endToolbarSlot) const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() if (canSend) { onSend() } } } const sendButton = isGenerating && onStop ? ( ) : ( ) return ( {/* Two grid areas: [input + slots] | [send button, aligned to the end]. */} onChange(e.target.value)} onKeyDown={handleKeyDown} placeholder={placeholder} disabled={disabled || isGenerating} sx={styles.footerInput} /> {hasSlots && ( {startToolbarSlot} {showModelSelector && ( <> setModelAnchor(null)} variant='menu' elevation={8} anchorOrigin={{ vertical: 'top', horizontal: 'left' }} transformOrigin={{ vertical: 'bottom', horizontal: 'left' }} > {models?.map((m) => ( { onModelChange?.(m.value) setModelAnchor(null) }} > {m.label} ))} )} {endToolbarSlot} )} {sendButton} {caption ? ( {caption} ) : null} ) }