import { useEffect, useState } from 'react';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useTrackedMutation } from '../hooks/useTrackedMutation';
import { getTeams, deleteTeam, createTeam, updateTeam } from '../api/client';
import PageHeader from '../components/PageHeader';
import DataTable from '../components/DataTable';
import type { Column } from '../components/DataTable';
import ErrorState from '../components/ErrorState';
import { formatDateTime } from '../api/utils';
import type { Team } from '../api/types';
interface CreateTeamModalProps {
isOpen: boolean;
onClose: () => void;
onSuccess: () => void;
isLoading: boolean;
error: string | null;
}
function CreateTeamModal({ isOpen, onClose, onSuccess, isLoading, error }: CreateTeamModalProps) {
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!name.trim()) return;
await createTeam({
name: name.trim(),
description: description.trim(),
});
setName('');
setDescription('');
onSuccess();
};
if (!isOpen) return null;
return (
e.stopPropagation()}>
Create Team
{error &&
{error}
}
);
}
// EditTeamModal — B-1 master closure (cat-b-31ceb6aaa9f1). Mirrors
// CreateTeamModal; pre-populates from the editing team; calls
// updateTeam(id, fields) to close the destructive-rename hazard.
interface EditTeamModalProps {
team: Team | null;
onClose: () => void;
onSuccess: () => void;
isLoading: boolean;
error: string | null;
}
function EditTeamModal({ team, onClose, onSuccess, isLoading, error }: EditTeamModalProps) {
const [name, setName] = useState('');
const [description, setDescription] = useState('');
useEffect(() => {
if (team) {
setName(team.name);
setDescription(team.description || '');
}
}, [team]);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!team || !name.trim()) return;
await updateTeam(team.id, { name: name.trim(), description: description.trim() });
onSuccess();
};
if (!team) return null;
return (
e.stopPropagation()}>
Edit Team
{team.id}
{error &&
{error}
}
);
}
export default function TeamsPage() {
const queryClient = useQueryClient();
const [showCreate, setShowCreate] = useState(false);
const [editingTeam, setEditingTeam] = useState(null);
const { data, isLoading, error, refetch } = useQuery({
queryKey: ['teams'],
queryFn: () => getTeams(),
});
const deleteMutation = useTrackedMutation({
mutationFn: deleteTeam,
invalidates: [['teams']],
onError: (err: Error) => alert(`Delete failed: ${err.message}`),
});
const createMutation = useTrackedMutation({
mutationFn: createTeam,
invalidates: [['teams']],
onSuccess: () => {
setShowCreate(false);
},
});
const updateMutation = useTrackedMutation({
mutationFn: ({ id, data }: { id: string; data: Partial }) => updateTeam(id, data),
invalidates: [['teams']],
onSuccess: () => {
setEditingTeam(null);
},
});
const columns: Column[] = [
{
key: 'name',
label: 'Team',
render: (t) => (
),
},
{
key: 'description',
label: 'Description',
render: (t) => (
{t.description || '\u2014'}
),
},
{
key: 'created',
label: 'Created',
render: (t) => {formatDateTime(t.created_at)},
},
{
key: 'actions',
label: '',
render: (t) => (
),
},
];
return (
<>
setShowCreate(true)} className="btn btn-primary">
+ New Team
}
/>
{error ? (
refetch()} />
) : (
)}
setShowCreate(false)}
onSuccess={() => {
queryClient.invalidateQueries({ queryKey: ['teams'] });
setShowCreate(false);
}}
isLoading={createMutation.isPending}
error={createMutation.error ? (createMutation.error as Error).message : null}
/>
setEditingTeam(null)}
onSuccess={() => {
queryClient.invalidateQueries({ queryKey: ['teams'] });
setEditingTeam(null);
}}
isLoading={updateMutation.isPending}
error={updateMutation.error ? (updateMutation.error as Error).message : null}
/>
>
);
}