diff --git a/src/components/Shakespeare/ChatInput.tsx b/src/components/Shakespeare/ChatInput.tsx index 3045fc6b..92f7ae66 100644 --- a/src/components/Shakespeare/ChatInput.tsx +++ b/src/components/Shakespeare/ChatInput.tsx @@ -1,13 +1,20 @@ -import { useState, useCallback, memo } from 'react'; +import { useState, useCallback, memo, useRef, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; import { CircularProgress } from '@/components/ui/circular-progress'; import { FileAttachment } from '@/components/ui/file-attachment'; +import { Command, CommandGroup, CommandItem, CommandList } from '@/components/ui/command'; import { Square, ArrowUp } from 'lucide-react'; import { ModelSelector } from '@/components/ModelSelector'; +export interface SlashCommand { + name: string; + description: string; + action: () => void; +} + interface ChatInputProps { isLoading: boolean; isConfigured: boolean; @@ -29,6 +36,7 @@ interface ChatInputProps { onDragOver: (e: React.DragEvent) => void; onDragLeave: (e: React.DragEvent) => void; onDrop: (e: React.DragEvent) => void; + slashCommands?: SlashCommand[]; } export const ChatInput = memo(function ChatInput({ @@ -52,10 +60,15 @@ export const ChatInput = memo(function ChatInput({ onDragOver, onDragLeave, onDrop, + slashCommands = [], }: ChatInputProps) { const { t } = useTranslation(); const [input, setInput] = useState(''); const [attachedFiles, setAttachedFiles] = useState([]); + const [showSlashCommands, setShowSlashCommands] = useState(false); + const [filteredCommands, setFilteredCommands] = useState([]); + const [selectedCommandIndex, setSelectedCommandIndex] = useState(0); + const textareaRef = useRef(null); const handleFileSelect = useCallback((file: File) => { setAttachedFiles(prev => [...prev, file]); @@ -78,16 +91,72 @@ export const ChatInput = memo(function ChatInput({ onSend(currentInput, currentFiles); }, [input, attachedFiles, isLoading, isConfigured, providerModel, onSend]); + const executeSlashCommand = useCallback((command: SlashCommand) => { + setInput(''); + setShowSlashCommands(false); + setSelectedCommandIndex(0); + command.action(); + }, []); + const handleKeyDown = useCallback((e: React.KeyboardEvent) => { + // Handle arrow key navigation when slash commands are showing + if (showSlashCommands && filteredCommands.length > 0) { + if (e.key === 'ArrowDown') { + e.preventDefault(); + setSelectedCommandIndex(prev => + prev < filteredCommands.length - 1 ? prev + 1 : 0 + ); + return; + } + + if (e.key === 'ArrowUp') { + e.preventDefault(); + setSelectedCommandIndex(prev => + prev > 0 ? prev - 1 : filteredCommands.length - 1 + ); + return; + } + + // If slash commands are showing and user presses Enter or Tab, select the highlighted command + if ((e.key === 'Enter' && !e.shiftKey) || e.key === 'Tab') { + e.preventDefault(); + executeSlashCommand(filteredCommands[selectedCommandIndex]); + return; + } + + // If slash commands are showing and user presses Escape, close the popover + if (e.key === 'Escape') { + e.preventDefault(); + setShowSlashCommands(false); + setSelectedCommandIndex(0); + return; + } + } + if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } - }, [handleSend]); + }, [handleSend, showSlashCommands, filteredCommands, selectedCommandIndex, executeSlashCommand]); const handleInputChange = useCallback((e: React.ChangeEvent) => { - setInput(e.target.value); - }, []); + const value = e.target.value; + setInput(value); + + // Check if slash command autocomplete should be shown + if (value.startsWith('/') && !value.includes(' ') && slashCommands.length > 0) { + const query = value.slice(1).toLowerCase(); + const filtered = slashCommands.filter(cmd => + cmd.name.toLowerCase().includes(query) + ); + setFilteredCommands(filtered); + setShowSlashCommands(filtered.length > 0); + setSelectedCommandIndex(0); // Reset selection when filtering + } else { + setShowSlashCommands(false); + setSelectedCommandIndex(0); + } + }, [slashCommands]); const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault(); @@ -104,6 +173,29 @@ export const ChatInput = memo(function ChatInput({ setAttachedFiles(prev => [...prev, ...files]); }, [onDrop]); + // Close popover when clicking outside + useEffect(() => { + if (!showSlashCommands) return; + + const handleClickOutside = (e: MouseEvent) => { + // Check if click is outside the popover and textarea + const target = e.target as Node; + if (textareaRef.current && !textareaRef.current.contains(target)) { + setShowSlashCommands(false); + } + }; + + // Use a slight delay to avoid closing immediately on focus + const timeoutId = setTimeout(() => { + document.addEventListener('click', handleClickOutside); + }, 100); + + return () => { + clearTimeout(timeoutId); + document.removeEventListener('click', handleClickOutside); + }; + }, [showSlashCommands]); + return (
{/* Chat Input Container */} @@ -116,33 +208,59 @@ export const ChatInput = memo(function ChatInput({ onDragLeave={onDragLeave} onDrop={handleDrop} > -