"use client"; import { useState, useEffect, useCallback } from "react"; import { Shield, Plus, Trash2, Check, X, Users, Sparkles, Loader2, AlertCircle, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { fetchCustomRoles, createCustomRole, updateCustomRole, deleteCustomRole, getUserPermissions, } from "@/lib/supabase/rbac-actions"; import { suggestPermissionsAction } from "@/lib/supabase/ai-actions"; import { PERMISSIONS_LIST, PERMISSION_CATEGORIES, type Permission, DEFAULT_CUSTOM_ROLES, enforceDependencies, DEPENDENT_MAP, PERMISSION_DEPENDENCIES } from "@/lib/permissions"; import PageSkeleton from "./PageSkeleton"; const ROLE_COLORS = [ "#1D9E75", "#6366F1", "#F59E0B", "#EF4444", "#EC4899", "#8B5CF6", "#06B6D4", "#10B981", "#F97316", "#6B7280", ]; // ── AI Suggestion Popup ────────────────────────────────────── function AISuggestionPopup({ roleName, onApply, onClose, }: { roleName: string; onApply: (perms: Permission[]) => void; onClose: () => void; }) { const [loading, setLoading] = useState(true); const [question, setQuestion] = useState(null); const [answer, setAnswer] = useState(""); const [perms, setPerms] = useState([]); const [error, setError] = useState(null); useEffect(() => { suggestPermissionsAction(roleName) .then((res) => { if (!res.success) { setError(res.blocked ? "AI usage limit reached." : res.error || "Failed"); setLoading(false); return; } if (res.result?.askForInfo) { setQuestion(res.result.askForInfo); } else { setPerms(res.result?.permissions || []); } setLoading(false); }) .catch(() => { setError("AI request failed"); setLoading(false); }); }, [roleName]); const handleAnswerSubmit = async () => { if (!question || !answer.trim()) return; setLoading(true); setQuestion(null); try { const res = await suggestPermissionsAction(roleName, answer.trim()); if (!res.success) { setError(res.error || "Failed"); setLoading(false); return; } if (res.result?.askForInfo) { setQuestion(res.result.askForInfo); setAnswer(""); } else { setPerms(res.result?.permissions || []); } } catch { setError("AI request failed"); } finally { setLoading(false); } }; return (
e.stopPropagation()}>

AI Permission Suggestion

{loading && (

Analyzing role "{roleName}"...

)} {error && (

{error}

)} {question && !loading && (

AI needs more info:

{question}