import React, { useRef, useState } from 'react'; import { Plus, X } from 'lucide-react'; import { TooltipAnchor } from '@librechat/client'; import { Transition } from 'react-transition-group'; import { Constants } from 'librechat-data-provider'; import { useLocalize } from '~/hooks'; interface AssistantConversationStartersProps { field: { value: string[]; onChange: (value: string[]) => void; }; inputClass: string; labelClass: string; } const AssistantConversationStarters: React.FC = ({ field, inputClass, labelClass, }) => { const localize = useLocalize(); const inputRefs = useRef<(HTMLInputElement | null)[]>([]); const nodeRef = useRef(null); const [newStarter, setNewStarter] = useState(''); const handleAddStarter = () => { if (newStarter.trim() && field.value.length < Constants.MAX_CONVO_STARTERS) { const newValues = [newStarter, ...field.value]; field.onChange(newValues); setNewStarter(''); } }; const handleDeleteStarter = (index: number) => { const newValues = field.value.filter((_, i) => i !== index); field.onChange(newValues); }; const defaultStyle = { transition: 'opacity 200ms ease-in-out', opacity: 0, }; const triggerShake = (element: HTMLElement) => { element.classList.remove('shake'); void element.offsetWidth; element.classList.add('shake'); setTimeout(() => { element.classList.remove('shake'); }, 200); }; const transitionStyles = { entering: { opacity: 1 }, entered: { opacity: 1 }, exiting: { opacity: 0 }, exited: { opacity: 0 }, }; const hasReachedMax = field.value.length >= Constants.MAX_CONVO_STARTERS; const addConversationStarterLabel = hasReachedMax ? localize('com_assistants_max_starters_reached') : localize('com_ui_add'); return (
{/* Persistent starter, used for creating only */}
(inputRefs.current[0] = el)} value={newStarter} maxLength={64} className={`${inputClass} pr-10`} type="text" placeholder={ hasReachedMax ? localize('com_assistants_max_starters_reached') : localize('com_assistants_conversation_starters_placeholder') } onChange={(e) => setNewStarter(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); if (hasReachedMax) { triggerShake(e.currentTarget); } else { handleAddStarter(); } } }} /> {(state: string) => (
)}
{field.value.map((starter, index) => (
(inputRefs.current[index + 1] = el)} value={starter} onChange={(e) => { const newValue = [...field.value]; newValue[index] = e.target.value; field.onChange(newValue); }} className={`${inputClass} pr-10`} type="text" maxLength={64} /> handleDeleteStarter(index)} >
))}
); }; export default AssistantConversationStarters;