import { useEffect, useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { getAgentGroups, deleteAgentGroup, createAgentGroup, updateAgentGroup } 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}
}
);
}
// EditAgentGroupModal — B-1 master closure (cat-b-31ceb6aaa9f1).
// Mirrors CreateAgentGroupModal; pre-populates from the editing group;
// calls updateAgentGroup(id, fields) to close the destructive-rename
// hazard. Membership-rule fields (match_os, match_architecture,
// match_ip_cidr, match_version) are editable like the rest — operators
// frequently want to widen/narrow group membership without recreating.
interface EditAgentGroupModalProps {
group: AgentGroup | null;
onClose: () => void;
onSuccess: () => void;
isLoading: boolean;
error: string | null;
}
function EditAgentGroupModal({ group, onClose, onSuccess, isLoading, error }: EditAgentGroupModalProps) {
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);
useEffect(() => {
if (group) {
setName(group.name);
setDescription(group.description || '');
setMatchOs(group.match_os || '');
setMatchArch(group.match_architecture || '');
setMatchIpCidr(group.match_ip_cidr || '');
setMatchVersion(group.match_version || '');
setEnabled(group.enabled);
}
}, [group]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!group || !name.trim()) return;
await updateAgentGroup(group.id, {
name: name.trim(),
description: description.trim(),
match_os: matchOs.trim(),
match_architecture: matchArch.trim(),
match_ip_cidr: matchIpCidr.trim(),
match_version: matchVersion.trim(),
enabled,
});
onSuccess();
};
if (!group) return null;
return (
);
}
export default function AgentGroupsPage() {
const queryClient = useQueryClient();
const [showCreate, setShowCreate] = useState(false);
const [editingGroup, setEditingGroup] = useState(null);
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 updateMutation = useMutation({
mutationFn: ({ id, data }: { id: string; data: Partial }) => updateAgentGroup(id, data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['agent-groups'] });
setEditingGroup(null);
},
});
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}
/>
setEditingGroup(null)}
onSuccess={() => {
queryClient.invalidateQueries({ queryKey: ['agent-groups'] });
setEditingGroup(null);
}}
isLoading={updateMutation.isPending}
error={updateMutation.error ? (updateMutation.error as Error).message : null}
/>
>
);
}