import React, { useState, useEffect, useCallback } from 'react' import { fs, joinPath } from '@janhq/core' import { Button } from '@janhq/joi' import { useAtomValue } from 'jotai' import { janDataFolderPathAtom } from '@/helpers/atoms/AppConfig.atom' const MCP = () => { const janDataFolderPath = useAtomValue(janDataFolderPathAtom) const [configContent, setConfigContent] = useState('') const [isSaving, setIsSaving] = useState(false) const [error, setError] = useState('') const [success, setSuccess] = useState('') console.log(janDataFolderPath, 'janDataFolderPath') const readConfigFile = useCallback(async () => { try { const configPath = await joinPath([janDataFolderPath, 'mcp_config.json']) // Check if the file exists const fileExists = await fs.existsSync(configPath) if (fileExists) { // Read the file const content = await fs.readFileSync(configPath, 'utf-8') setConfigContent(content) } else { // Create a default config if it doesn't exist const defaultConfig = JSON.stringify( { servers: [], settings: { enabled: true, }, }, null, 2 ) await fs.writeFileSync(configPath, defaultConfig) setConfigContent(defaultConfig) } setError('') } catch (err) { console.error('Error reading config file:', err) setError('Failed to read config file') } }, [janDataFolderPath]) useEffect(() => { if (janDataFolderPath) { readConfigFile() } }, [janDataFolderPath, readConfigFile]) const saveConfigFile = useCallback(async () => { try { setIsSaving(true) setSuccess('') setError('') // Validate JSON try { JSON.parse(configContent) } catch (err) { setError('Invalid JSON format') setIsSaving(false) return } const configPath = await joinPath([janDataFolderPath, 'mcp_config.json']) // Write to the file await fs.writeFileSync(configPath, configContent) setSuccess('Config saved successfully') setIsSaving(false) } catch (err) { console.error('Error saving config file:', err) setError('Failed to save config file') setIsSaving(false) } }, [janDataFolderPath, configContent]) return (