"use client"; import { useState } from "react"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { useGetGroupsQuery } from "@/app/api/queries/useGetGroupsQuery"; import { useCreateGroupMutation } from "@/app/api/mutations/useCreateGroupMutation"; import { useDeleteGroupMutation } from "@/app/api/mutations/useDeleteGroupMutation"; import { Plus, Trash2, Loader2, Users } from "lucide-react"; import { toast } from "sonner"; interface ManageGroupsModalProps { open: boolean; onOpenChange: (open: boolean) => void; } export function ManageGroupsModal({ open, onOpenChange, }: ManageGroupsModalProps) { const [newGroupName, setNewGroupName] = useState(""); const [newGroupDescription, setNewGroupDescription] = useState(""); const { data: groupsData, isLoading: groupsLoading } = useGetGroupsQuery({ enabled: open, }); const createGroupMutation = useCreateGroupMutation({ onSuccess: () => { setNewGroupName(""); setNewGroupDescription(""); toast.success("Group created successfully"); }, onError: (error) => { toast.error("Failed to create group", { description: error.message }); }, }); const deleteGroupMutation = useDeleteGroupMutation({ onSuccess: () => { toast.success("Group deleted successfully"); }, onError: (error) => { toast.error("Failed to delete group", { description: error.message }); }, }); const handleCreateGroup = () => { if (!newGroupName.trim()) { toast.error("Please enter a group name"); return; } createGroupMutation.mutate({ name: newGroupName.trim(), description: newGroupDescription.trim(), }); }; const handleDeleteGroup = (groupId: string, groupName: string) => { if (confirm(`Are you sure you want to delete the group "${groupName}"?`)) { deleteGroupMutation.mutate({ group_id: groupId }); } }; const groups = groupsData?.groups || []; return ( Manage User Groups Create and manage user groups for access control. Groups can be assigned to API keys to restrict document access.
{/* Add new group section */}
setNewGroupName(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleCreateGroup(); } }} className="flex-1" />
setNewGroupDescription(e.target.value)} className="text-sm" />
{/* Existing groups list */}
{groupsLoading ? (
) : groups.length > 0 ? (
{groups.map((group) => (

{group.name}

{group.description && (

{group.description}

)}
))}
) : (

No groups yet. Create one above.

)}
); }