import React, { useRef } from 'react'; import userService from '../api/services/userService'; import Exit from '../assets/exit.svg'; import { ActiveState } from '../models/misc'; import { AvailableTool } from './types'; import ConfigToolModal from './ConfigToolModal'; import { useOutsideAlerter } from '../hooks'; import { useTranslation } from 'react-i18next'; export default function AddToolModal({ message, modalState, setModalState, getUserTools, }: { message: string; modalState: ActiveState; setModalState: (state: ActiveState) => void; getUserTools: () => void; }) { const [availableTools, setAvailableTools] = React.useState( [], ); const [selectedTool, setSelectedTool] = React.useState( null, ); const [configModalState, setConfigModalState] = React.useState('INACTIVE'); const modalRef = useRef(null); const { t } = useTranslation(); useOutsideAlerter(modalRef, () => { if (modalState === 'ACTIVE') { setModalState('INACTIVE'); } }, [modalState]); const getAvailableTools = () => { userService .getAvailableTools() .then((res) => { return res.json(); }) .then((data) => { setAvailableTools(data.data); }); }; const handleAddTool = (tool: AvailableTool) => { if (Object.keys(tool.configRequirements).length === 0) { userService .createTool({ name: tool.name, displayName: tool.displayName, description: tool.description, config: {}, actions: tool.actions, status: true, }) .then((res) => { if (res.status === 200) { getUserTools(); setModalState('INACTIVE'); } }); } else { setModalState('INACTIVE'); setConfigModalState('ACTIVE'); } }; React.useEffect(() => { if (modalState === 'ACTIVE') getAvailableTools(); }, [modalState]); return ( <>

{t('settings.tools.selectToolSetup')}

{availableTools.map((tool, index) => (
{ setSelectedTool(tool); handleAddTool(tool); }} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { setSelectedTool(tool); handleAddTool(tool); } }} >

{tool.displayName}

{tool.description}

))}
); }