import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import type { AxiosError } from "axios"; import { ArrowLeft, Shield, Trash2, UserPlus, Users } from "lucide-react"; import { useState } from "react"; import { Link, useParams } from "react-router-dom"; import { toast } from "../../../components/ui/use-toast"; import { Badge } from "../../../components/ui/badge"; import { Button } from "../../../components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "../../../components/ui/card"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "../../../components/ui/dialog"; import { Input } from "../../../components/ui/input"; import { Label } from "../../../components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "../../../components/ui/select"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "../../../components/ui/table"; import { addGroupMember, assignGroupRole, fetchGroup, fetchGroupRoles, fetchTenants, fetchUsers, removeGroupMember, removeGroupRole, } from "../../../lib/adminApi"; import { t } from "../../../lib/i18n"; export function UserGroupDetailPage() { const { tenantId, id } = useParams<{ tenantId: string; id: string }>(); const queryClient = useQueryClient(); const [isAddMemberOpen, setIsAddMemberOpen] = useState(false); const [selectedUserId, setSelectedUserId] = useState(""); const [searchUser, setSearchUser] = useState(""); const [isAddRoleOpen, setIsAddRoleOpen] = useState(false); const [selectedTargetTenantId, setSelectedTargetTenantId] = useState(""); const [selectedRelation, setSelectedRelation] = useState("view"); const { data: currentGroup, isLoading: isGroupLoading, error, } = useQuery({ queryKey: ["user-group-detail", id], queryFn: () => fetchGroup(tenantId ?? "", id ?? ""), enabled: !!id && !!tenantId, retry: false, }); // Fetch assigned roles const { data: groupRoles, isLoading: isRolesLoading } = useQuery({ queryKey: ["user-group-roles", id], queryFn: () => fetchGroupRoles(tenantId ?? "", id ?? ""), enabled: !!id && !!tenantId, }); // Fetch all users for selection const { data: userList } = useQuery({ queryKey: ["admin-users", searchUser], queryFn: () => fetchUsers(20, 0, searchUser), enabled: isAddMemberOpen, }); // Fetch all tenants for role assignment const { data: tenantList } = useQuery({ queryKey: ["admin-tenants"], queryFn: () => fetchTenants(100, 0), enabled: isAddRoleOpen, }); const addMemberMutation = useMutation({ mutationFn: (userId: string) => addGroupMember(tenantId ?? "", id ?? "", userId), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["user-group-detail", id] }); setIsAddMemberOpen(false); setSelectedUserId(""); toast.success( t("msg.admin.groups.members.add_success", "구성원이 추가되었습니다."), ); }, onError: (error: AxiosError<{ error?: string }>) => { toast.error( error.response?.data?.error || error.message || t("err.common.unknown", "오류가 발생했습니다."), ); }, }); const removeMemberMutation = useMutation({ mutationFn: (userId: string) => removeGroupMember(tenantId ?? "", id ?? "", userId), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["user-group-detail", id] }); toast.success( t( "msg.admin.groups.members.remove_success", "구성원이 제외되었습니다.", ), ); }, }); const assignRoleMutation = useMutation({ mutationFn: () => assignGroupRole( tenantId ?? "", id ?? "", selectedTargetTenantId, selectedRelation, ), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["user-group-roles", id] }); setIsAddRoleOpen(false); toast.success( t("msg.admin.groups.roles.assign_success", "역할이 할당되었습니다."), ); }, onError: (error: AxiosError<{ error?: string }>) => { toast.error( error.response?.data?.error || error.message || t("err.common.unknown", "오류가 발생했습니다."), ); }, }); const removeRoleMutation = useMutation({ mutationFn: (role: { targetTenantId: string; relation: string }) => removeGroupRole( tenantId ?? "", id ?? "", role.targetTenantId, role.relation, ), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["user-group-roles", id] }); toast.success( t("msg.admin.groups.roles.remove_success", "역할이 회수되었습니다."), ); }, }); if (isGroupLoading) return (
Error:{" "} {(error as AxiosError<{ error?: string }>)?.response?.data?.error || (error as any)?.message || "Not found"}
{currentGroup.description || t("msg.common.no_description", "설명이 없습니다.")}