import { forwardRef, useImperativeHandle, useState } from 'react'; import { Form, Input, Button, App, Space } from 'antd'; import { useTranslation } from 'react-i18next'; import { CopyOutlined, EyeInvisibleOutlined, EyeOutlined } from '@ant-design/icons'; import { createMarketConfig,updateMarketConfig } from '@/api/tools'; import RbModal from '@/components/RbModal'; const FormItem = Form.Item; interface MarketSource { id: string; name: string; logo_url: string; url: string; description: string; token?: string; connected: boolean; configId?: string; } interface MarketConfigModalProps { onConnect: (sourceId: string, configId: string) => void; } export interface MarketConfigModalRef { handleOpen: (source: MarketSource) => void; handleClose: () => void; } const MarketConfigModal = forwardRef(({ onConnect }, ref) => { const { t } = useTranslation(); const { message } = App.useApp(); const [visible, setVisible] = useState(false); const [form] = Form.useForm(); const [loading, setLoading] = useState(false); const [currentSource, setCurrentSource] = useState(null); const [showApiKey, setShowApiKey] = useState(false); const formValues = Form.useWatch([], form); const handleClose = () => { setVisible(false); form.resetFields(); setLoading(false); setCurrentSource(null); setShowApiKey(false); }; const handleOpen = (source: MarketSource) => { setCurrentSource(source); form.setFieldsValue({ token: source.token || '', }); setVisible(true); }; const handleSave = () => { form .validateFields() .then(async (values) => { if (!currentSource) return; setLoading(true); try { let res: any; if (currentSource.configId) { // 更新配置 res = await updateMarketConfig({ mcp_market_config_id: currentSource.configId, token: values.token || '', status: 1, }); message.success(t('tool.marketConfigUpdated', { name: currentSource.name })); } else { // 创建配置 res = await createMarketConfig({ mcp_market_id: currentSource.id || '', token: values.token || '', status: 1, }); message.success(t('tool.marketConnecting', { name: currentSource.name })); } onConnect(currentSource.id, res.id || currentSource.configId); handleClose(); } catch (error) { console.error('保存配置失败:', error); } finally { setLoading(false); } }) .catch((err) => { console.log('表单验证失败:', err); }); }; const handleCopyUrl = () => { if (currentSource?.url) { navigator.clipboard.writeText(currentSource.url).then(() => { message.success(t('common.copySuccess')); }); } }; // 检查是否可以保存:token 字段必须有值 const canSave = formValues?.token && formValues.token.trim().length > 0; useImperativeHandle(ref, () => ({ handleOpen, handleClose })); if (!currentSource) return null; return (
{/* 市场源信息头部 */}
{currentSource.logo_url ? ( {currentSource.name} { e.currentTarget.style.display = 'none'; const parent = e.currentTarget.parentElement; if (parent) { parent.innerHTML = '🏪'; parent.style.fontSize = '32px'; } }} /> ) : ( 🏪 )}

{currentSource.name}

{currentSource.description}

API Key } extra={{t('tool.marketApiKeyExtra')}} >
); }); export default MarketConfigModal;