import React from 'react'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import userService from '../api/services/userService'; import CogwheelIcon from '../assets/cogwheel.svg'; import Input from '../components/Input'; import Spinner from '../components/Spinner'; import ToggleSwitch from '../components/ToggleSwitch'; import AddToolModal from '../modals/AddToolModal'; import { ActiveState } from '../models/misc'; import { selectToken } from '../preferences/preferenceSlice'; import ToolConfig from './ToolConfig'; import { APIToolType, UserToolType } from './types'; export default function Tools() { const { t } = useTranslation(); const token = useSelector(selectToken); 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 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 .filter((tool) => tool.displayName .toLowerCase() .includes(searchTerm.toLowerCase()), ) .map((tool, index) => (
{`${tool.displayName}

{tool.displayName}

{tool.description}

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