import React, { useEffect, useState } from 'react'; import { useApp } from '@nocobase/client-v2'; import { Table, Checkbox, Spin, Typography, message } from 'antd'; export const RepositoryPermissions = ({ activeRole }) => { const api = useApp().apiClient; const [loading, setLoading] = useState(false); const [repositories, setRepositories] = useState([]); const [permissions, setPermissions] = useState({ read: [], write: [] }); useEffect(() => { if (activeRole?.name) { loadData(); } }, [activeRole?.name]); const loadData = async () => { setLoading(true); try { const [repoRes, permRes] = await Promise.all([ api.request({ resource: 'gitRepositories', action: 'list', params: { paginate: false, sort: ['name'] }, }), api.request({ url: 'gitManager:rolePermissions', method: 'get', params: { roleName: activeRole.name }, }), ]); const fetchedPerms = permRes?.data?.data || permRes?.data || { read: [], write: [] }; setRepositories(repoRes?.data?.data || []); setPermissions({ read: fetchedPerms.read || [], write: fetchedPerms.write || [], }); } catch (e) { console.error(e); message.error('Failed to load repository permissions'); } setLoading(false); }; const handleToggle = async (repositoryId: number, actionName: string, checked: boolean) => { const currentList = permissions[actionName] || []; const newList = checked ? [...new Set([...currentList, repositoryId])] : currentList.filter((id: number) => id !== repositoryId); const newPermissions = { ...permissions, [actionName]: newList }; const prevPermissions = permissions; setPermissions(newPermissions); try { await api.request({ url: 'gitManager:updateRolePermissions', method: 'post', data: { roleName: activeRole.name, values: newPermissions, }, }); } catch { message.error('Failed to update permissions'); setPermissions(prevPermissions); } }; const columns = [ { title: 'Repository Name', dataIndex: 'name', key: 'name', render: (text: string) => {text}, }, { title: 'Repository URL', dataIndex: 'repoUrl', key: 'repoUrl', ellipsis: true, render: (text: string) => {text}, }, { title: 'Read (List, Browse Files, View History)', key: 'read', width: 100, render: (_: any, record: any) => ( handleToggle(record.id, 'read', e.target.checked)} /> ), }, { title: 'Write (Clone, Pull, Push, Review)', key: 'write', width: 100, render: (_: any, record: any) => ( handleToggle(record.id, 'write', e.target.checked)} /> ), }, ]; if (loading && !repositories.length) { return ; } return (
Configure access permissions for each repository for the role{' '} {activeRole?.title || activeRole?.name}.
); };