import { FC, useEffect, useRef } from 'react'; import { useTranslation } from 'next-i18next'; import { Plugin, PluginList } from '@/types/plugin'; interface Props { plugin: Plugin | null; onPluginChange: (plugin: Plugin) => void; onKeyDown: (e: React.KeyboardEvent) => void; } export const PluginSelect: FC = ({ plugin, onPluginChange, onKeyDown, }) => { const { t } = useTranslation('chat'); const selectRef = useRef(null); const handleKeyDown = (e: React.KeyboardEvent) => { const selectElement = selectRef.current; const optionCount = selectElement?.options.length || 0; if (e.key === '/' && e.metaKey) { e.preventDefault(); if (selectElement) { selectElement.selectedIndex = (selectElement.selectedIndex + 1) % optionCount; selectElement.dispatchEvent(new Event('change')); } } else if (e.key === '/' && e.shiftKey && e.metaKey) { e.preventDefault(); if (selectElement) { selectElement.selectedIndex = (selectElement.selectedIndex - 1 + optionCount) % optionCount; selectElement.dispatchEvent(new Event('change')); } } else if (e.key === 'Enter') { e.preventDefault(); if (selectElement) { selectElement.dispatchEvent(new Event('change')); } onPluginChange( PluginList.find( (plugin) => plugin.name === selectElement?.selectedOptions[0].innerText, ) as Plugin, ); } else { onKeyDown(e); } }; useEffect(() => { if (selectRef.current) { selectRef.current.focus(); } }, []); return (
); };