"use client"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@workspace/ui/components/alert-dialog"; import { Button } from "@workspace/ui/components/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@workspace/ui/components/card"; import { getGroupConfig, resetGroups, updateGroupConfig, } 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"; export default function GroupConfig() { const { t } = useTranslation("group"); const [saving, setSaving] = useState(false); const [resetting, setResetting] = useState(false); const [showResetDialog, setShowResetDialog] = useState(false); const [config, setConfig] = useState<{ enabled: boolean; mode: "average" | "subscribe" | "traffic"; }>({ enabled: false, mode: "average", }); const loadConfig = async () => { try { const { data } = await getGroupConfig(); if (data.data) { setConfig({ enabled: data.data.enabled, mode: (data.data.mode || "average") as | "average" | "subscribe" | "traffic", }); } } catch (error) { console.error("Failed to load group config:", error); toast.error(t("loadFailed", "Failed to load configuration")); } }; useEffect(() => { loadConfig(); }, []); const handleUpdateEnabled = async (enabled: boolean) => { setSaving(true); try { const payload: any = { enabled, mode: config.mode, }; await updateGroupConfig(payload); setConfig({ ...config, enabled }); toast.success(t("saved", "Configuration saved successfully")); } catch (error) { console.error("Failed to update group config:", error); toast.error(t("saveFailed", "Failed to save configuration")); } finally { setSaving(false); } }; const handleUpdateMode = async ( mode: "average" | "subscribe" | "traffic" ) => { setSaving(true); try { const payload: any = { enabled: config.enabled, mode, }; await updateGroupConfig(payload); setConfig({ ...config, mode }); toast.success(t("saved", "Configuration saved successfully")); } catch (error) { console.error("Failed to update group config:", error); toast.error(t("saveFailed", "Failed to save configuration")); } finally { setSaving(false); } }; const handleResetGroups = async () => { setResetting(true); try { await resetGroups({ confirm: true }); toast.success( t("resetSuccess", "All groups have been reset successfully") ); setShowResetDialog(false); // Reload config after reset await loadConfig(); } catch (error) { console.error("Failed to reset groups:", error); toast.error(t("resetFailed", "Failed to reset groups")); } finally { setResetting(false); } }; return (
{t("groupConfig", "Group Configuration")} {t( "groupConfigDescription", "Configure user group and node group settings" )} {/* Enable/Disable */}

{t( "enableGroupingDescription", "When enabled, users will only see nodes from their assigned group" )}

handleUpdateEnabled(e.target.checked)} type="checkbox" />
{/* Mode Selection */} {config.enabled && (

{t("groupingMode", "Grouping Mode")}

)} {/* Reset Button */}
{t("resetGroupsTitle", "Reset All Groups")} {t( "resetGroupsDescription", "This action will delete all node groups and user groups, reset all users' group ID to 0, clear all products' node group IDs, and clear all nodes' node group IDs. This action cannot be undone." )} {t("cancel", "Cancel")} {resetting && ( )} {t("confirm", "Confirm")}
); }