import React from 'react'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import userService from '../api/services/userService'; import ThreeDotsIcon from '../assets/three-dots.svg'; import NoFilesIcon from '../assets/no-files.svg'; import NoFilesDarkIcon from '../assets/no-files-dark.svg'; import Input from '../components/Input'; import Spinner from '../components/Spinner'; import ToggleSwitch from '../components/ToggleSwitch'; import { useDarkTheme } from '../hooks'; import AddToolModal from '../modals/AddToolModal'; import { ActiveState } from '../models/misc'; import { selectToken } from '../preferences/preferenceSlice'; import ToolConfig from './ToolConfig'; import { APIToolType, UserToolType } from './types'; import ContextMenu, { MenuOption } from '../components/ContextMenu'; import Edit from '../assets/edit.svg'; import Trash from '../assets/red-trash.svg'; import ConfirmationModal from '../modals/ConfirmationModal'; export default function Tools() { const { t } = useTranslation(); const token = useSelector(selectToken); const [isDarkTheme] = useDarkTheme(); const [searchTerm, setSearchTerm] = React.useState(''); const [addToolModalState, setAddToolModalState] = React.useState('INACTIVE'); const [userTools, setUserTools] = React.useState([]); const [selectedTool, setSelectedTool] = React.useState< UserToolType | APIToolType | null >(null); const [loading, setLoading] = React.useState(false); const [activeMenuId, setActiveMenuId] = React.useState(null); const menuRefs = React.useRef<{ [key: string]: React.RefObject; }>({}); const [deleteModalState, setDeleteModalState] = React.useState('INACTIVE'); const [toolToDelete, setToolToDelete] = React.useState( null, ); React.useEffect(() => { userTools.forEach((tool) => { if (!menuRefs.current[tool.id]) { menuRefs.current[tool.id] = React.createRef(); } }); }, [userTools]); const handleDeleteTool = (tool: UserToolType) => { setToolToDelete(tool); setDeleteModalState('ACTIVE'); }; const confirmDeleteTool = () => { if (toolToDelete) { userService.deleteTool({ id: toolToDelete.id }, token).then(() => { getUserTools(); setDeleteModalState('INACTIVE'); setToolToDelete(null); }); } }; const getMenuOptions = (tool: UserToolType): MenuOption[] => [ { icon: Edit, label: t('settings.tools.edit'), onClick: () => handleSettingsClick(tool), variant: 'primary', iconWidth: 14, iconHeight: 14, }, { icon: Trash, label: t('settings.tools.delete'), onClick: () => handleDeleteTool(tool), variant: 'danger', iconWidth: 12, iconHeight: 12, }, ]; const getUserTools = () => { setLoading(true); userService .getUserTools(token) .then((res) => { return res.json(); }) .then((data) => { setUserTools(data.tools); setLoading(false); }) .catch((error) => { console.error('Error fetching tools:', error); setLoading(false); }); }; const updateToolStatus = (toolId: string, newStatus: boolean) => { userService .updateToolStatus({ id: toolId, status: newStatus }, token) .then(() => { setUserTools((prevTools) => prevTools.map((tool) => tool.id === toolId ? { ...tool, status: newStatus } : tool, ), ); }) .catch((error) => { console.error('Failed to update tool status:', error); }); }; const handleSettingsClick = (tool: UserToolType) => { setSelectedTool(tool); }; const handleGoBack = () => { setSelectedTool(null); getUserTools(); }; const handleToolAdded = (toolId: string) => { userService .getUserTools(token) .then((res) => res.json()) .then((data) => { const newTool = data.tools.find( (tool: UserToolType) => tool.id === toolId, ); if (newTool) { setSelectedTool(newTool); } else { console.error('Newly added tool not found'); } }) .catch((error) => console.error('Error fetching tools:', error)); }; React.useEffect(() => { getUserTools(); }, []); return (
{selectedTool ? ( ) : (
setSearchTerm(e.target.value)} borderVariant="thin" />
{loading ? (
) : (
{userTools.length === 0 ? (
No tools found

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

) : ( userTools .filter((tool) => (tool.customName || tool.displayName) .toLowerCase() .includes(searchTerm.toLowerCase()), ) .map((tool, index) => (
{ e.stopPropagation(); setActiveMenuId( activeMenuId === tool.id ? null : tool.id, ); }} className="absolute top-4 right-4 z-10 cursor-pointer" > {t('settings.tools.settingsIconAlt')} { setActiveMenuId(isOpen ? tool.id : null); }} options={getMenuOptions(tool)} anchorRef={menuRefs.current[tool.id]} position="bottom-right" offset={{ x: 0, y: 0 }} />
{`${tool.displayName}

{tool.customName || tool.displayName}

{tool.description}

updateToolStatus(tool.id, checked) } size="small" id={`toolToggle-${index}`} ariaLabel={t('settings.tools.toggleToolAria', { toolName: tool.customName || tool.displayName, })} />
)) )}
)}
)}
); }