import React, { useCallback, useEffect, useState } from 'react'; import { Table, Button, Modal, Form, Input, Space, Tag, Popconfirm, Select, message } from 'antd'; import { PlusOutlined, DeleteOutlined } from '@ant-design/icons'; import { useApp } from '@nocobase/client-v2'; import { useT } from '../locale'; export const GitAccounts: React.FC = () => { const t = useT(); const api = useApp().apiClient; const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(false); const [modalOpen, setModalOpen] = useState(false); const [editingAccount, setEditingAccount] = useState(null); const [form] = Form.useForm(); const refreshAccounts = useCallback(async () => { setLoading(true); try { const { data } = await api.request({ url: 'gitAccounts:list', params: { pageSize: 100 } }); setAccounts(data?.data || []); } finally { setLoading(false); } }, [api]); useEffect(() => { refreshAccounts().catch(() => undefined); }, [refreshAccounts]); const handleSave = async () => { const values = await form.validateFields(); if (editingAccount && (!values.pat || values.pat === '••••••••')) { delete values.pat; } try { if (editingAccount) { await api.request({ url: 'gitAccounts:update', method: 'post', params: { filterByTk: editingAccount.id }, data: values, }); message.success(t('Account updated')); } else { await api.request({ url: 'gitAccounts:create', method: 'post', data: values, }); message.success(t('Account added')); } setModalOpen(false); setEditingAccount(null); form.resetFields(); await refreshAccounts(); } catch (err) { message.error(err?.message || t('Failed to save')); } }; const handleDelete = async (id: number) => { await api.request({ url: 'gitAccounts:destroy', method: 'post', params: { filterByTk: id } }); message.success(t('Deleted')); await refreshAccounts(); }; const columns = [ { title: t('Account Name'), dataIndex: 'name', key: 'name', }, { title: t('Provider'), dataIndex: 'provider', key: 'provider', width: 100, render: (provider: string) => ( {provider === 'github' ? 'GitHub' : 'GitLab'} ), }, { title: t('Base URL'), dataIndex: 'baseUrl', key: 'baseUrl', ellipsis: true, render: (url: string) => url || '-', }, { title: t('Username'), dataIndex: 'username', key: 'username', width: 150, }, { title: '', key: 'actions', width: 120, render: (_: any, record: any) => ( handleDelete(record.id)}> { setModalOpen(false); setEditingAccount(null); }} width={480} >
); }; export const GitAccountsSettings: React.FC = () => ;