"use client"; import { useQuery } from "@tanstack/react-query"; import { Button } from "@workspace/ui/components/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@workspace/ui/components/dialog"; import { Label } from "@workspace/ui/components/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@workspace/ui/components/select"; import { bindNodeGroups, getNodeGroupList, } from "@workspace/ui/services/admin/group"; import { Loader2 } from "lucide-react"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; interface BindNodeGroupsDialogProps { userGroupIds: number[]; userGroupNames: string[]; onOpenChange?: (open: boolean) => void; onSuccess?: () => void; } export default function BindNodeGroupsDialog({ userGroupIds, userGroupNames, onOpenChange, onSuccess, }: BindNodeGroupsDialogProps) { const { t } = useTranslation("group"); const [open, setOpen] = useState(false); const [saving, setSaving] = useState(false); const [selectedNodeGroupId, setSelectedNodeGroupId] = useState< number | undefined >(); const { data: nodeGroupsData, isLoading } = useQuery({ queryKey: ["nodeGroups"], queryFn: async () => { const { data } = await getNodeGroupList({ page: 1, size: 1000 }); return data.data?.list || []; }, }); useEffect(() => { if (open && nodeGroupsData) { // Load current binding when dialog opens loadCurrentBinding(); } }, [open]); const loadCurrentBinding = () => { // Get first user group's current node group binding // For batch binding, we'll default to unbound setSelectedNodeGroupId(undefined); }; const handleBind = async () => { if (selectedNodeGroupId === undefined) { toast.error(t("selectNodeGroupRequired", "Please select a node group")); return; } setSaving(true); try { await bindNodeGroups({ user_group_ids: userGroupIds, node_group_id: selectedNodeGroupId === 0 ? null : selectedNodeGroupId, } as API.BindNodeGroupsRequest); toast.success( t( "bindSuccess", "Successfully bound {{userGroupCount}} user groups to node group" ).replace(/{{userGroupCount}}/g, String(userGroupIds.length)) ); setOpen(false); onOpenChange?.(false); onSuccess?.(); } catch (error) { console.error("Failed to bind node group:", error); toast.error(t("bindFailed", "Failed to bind node group")); } finally { setSaving(false); } }; const displayNames = userGroupNames.length > 2 ? `${userGroupNames.slice(0, 2).join(", ")}... (${userGroupIds.length})` : userGroupNames.join(", "); return ( { setOpen(newOpen); onOpenChange?.(newOpen); }} open={open} > {t("bindNodeGroup", "Bind Node Group")} {t( "bindNodeGroupDescription", "Select a node group to bind to user groups: {{userGroups}}", { userGroups: displayNames } ).replace(/{{userGroups}}/g, displayNames)}
{isLoading ? (
) : (
)}
); }