import { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getAgentGroups, deleteAgentGroup, createAgentGroup } from '../api/client';
import PageHeader from '../components/PageHeader';
import DataTable from '../components/DataTable';
import type { Column } from '../components/DataTable';
import StatusBadge from '../components/StatusBadge';
import ErrorState from '../components/ErrorState';
import { formatDateTime } from '../api/utils';
import type { AgentGroup } from '../api/types';
interface CreateAgentGroupModalProps {
isOpen: boolean;
onClose: () => void;
onSuccess: () => void;
isLoading: boolean;
error: string | null;
}
function CreateAgentGroupModal({ isOpen, onClose, onSuccess, isLoading, error }: CreateAgentGroupModalProps) {
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [matchOs, setMatchOs] = useState('');
const [matchArch, setMatchArch] = useState('');
const [matchIpCidr, setMatchIpCidr] = useState('');
const [matchVersion, setMatchVersion] = useState('');
const [enabled, setEnabled] = useState(true);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!name.trim()) return;
await createAgentGroup({
name: name.trim(),
description: description.trim(),
match_os: matchOs.trim() || undefined,
match_architecture: matchArch.trim() || undefined,
match_ip_cidr: matchIpCidr.trim() || undefined,
match_version: matchVersion.trim() || undefined,
enabled,
});
setName('');
setDescription('');
setMatchOs('');
setMatchArch('');
setMatchIpCidr('');
setMatchVersion('');
setEnabled(true);
onSuccess();
};
if (!isOpen) return null;
return (
e.stopPropagation()}>
Create Agent Group
{error &&
{error}
}
);
}
export default function AgentGroupsPage() {
const queryClient = useQueryClient();
const [showCreate, setShowCreate] = useState(false);
const { data, isLoading, error, refetch } = useQuery({
queryKey: ['agent-groups'],
queryFn: () => getAgentGroups(),
});
const deleteMutation = useMutation({
mutationFn: deleteAgentGroup,
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['agent-groups'] }),
});
const createMutation = useMutation({
mutationFn: createAgentGroup,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['agent-groups'] });
setShowCreate(false);
},
});
const columns: Column[] = [
{
key: 'name',
label: 'Group',
render: (g) => (
{g.name}
{g.id}
{g.description && (
{g.description}
)}
),
},
{
key: 'criteria',
label: 'Match Criteria',
render: (g) => {
const criteria: string[] = [];
if (g.match_os) criteria.push(`OS: ${g.match_os}`);
if (g.match_architecture) criteria.push(`Arch: ${g.match_architecture}`);
if (g.match_ip_cidr) criteria.push(`IP: ${g.match_ip_cidr}`);
if (g.match_version) criteria.push(`Ver: ${g.match_version}`);
return criteria.length > 0 ? (
{criteria.map((c, i) => (
{c}
))}
) : (
Manual only
);
},
},
{
key: 'enabled',
label: 'Status',
render: (g) => ,
},
{
key: 'created',
label: 'Created',
render: (g) => {formatDateTime(g.created_at)},
},
{
key: 'actions',
label: '',
render: (g) => (
),
},
];
return (
<>
setShowCreate(true)} className="btn btn-primary">
+ New Group
}
/>
{error ? (
refetch()} />
) : (
)}
setShowCreate(false)}
onSuccess={() => {
queryClient.invalidateQueries({ queryKey: ['agent-groups'] });
setShowCreate(false);
}}
isLoading={createMutation.isPending}
error={createMutation.error ? (createMutation.error as Error).message : null}
/>
>
);
}