import { useMutation, useQuery } from "@tanstack/react-query"; import type { AxiosError } from "axios"; import { CornerDownRight, Pencil, Plus, RefreshCw, Trash2 } from "lucide-react"; import * as React from "react"; import { Link, useNavigate } from "react-router-dom"; import { RoleGuard } from "../../../components/auth/RoleGuard"; import { Badge } from "../../../components/ui/badge"; import { Button } from "../../../components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "../../../components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "../../../components/ui/table"; import { type TenantSummary, deleteTenant, fetchMe, fetchTenants, } from "../../../lib/adminApi"; import { t } from "../../../lib/i18n"; function TenantListPage() { const navigate = useNavigate(); const { data: profile } = useQuery({ queryKey: ["me"], queryFn: fetchMe, }); // Redirect tenant_admin ONLY if they have one or fewer manageable tenants in the list React.useEffect(() => { if (profile?.role === "tenant_admin") { const manageableCount = profile.manageableTenants?.length ?? 0; // If only 1 in array, OR array is empty but we have a primary tenantId if ( (manageableCount === 1 || manageableCount === 0) && profile.tenantId ) { navigate(`/tenants/${profile.tenantId}`, { replace: true }); } } }, [profile, navigate]); const query = useQuery({ queryKey: ["tenants", { limit: 1000, offset: 0 }], queryFn: () => fetchTenants(1000, 0), enabled: profile?.role === "super_admin" || (profile?.role === "tenant_admin" && (profile.manageableTenants?.length ?? 0) > 1), }); const deleteMutation = useMutation({ mutationFn: (tenantId: string) => deleteTenant(tenantId), onSuccess: () => { query.refetch(); }, }); if ( profile && profile.role !== "super_admin" && profile.role !== "tenant_admin" ) { return (
{t( "msg.admin.tenants.subtitle", "시스템에 등록된 모든 테넌트를 평면 목록으로 확인하고 관리합니다.", )}