"use client"; import { ColumnDef } from "@tanstack/react-table"; import { RuleTemplatesDataTable } from "./RuleTemplatesDataTable"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { Button } from "@app/components/ui/button"; import { ArrowRight, ArrowUpDown, MoreHorizontal, Trash2, Plus } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import { formatAxiosError } from "@app/lib/api"; import { toast } from "@app/hooks/useToast"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { useTranslations } from "next-intl"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import * as z from "zod"; export type TemplateRow = { id: string; name: string; description: string; orgId: string; }; type RuleTemplatesTableProps = { templates: TemplateRow[]; orgId: string; }; const createTemplateSchema = z.object({ name: z.string().min(1, "Name is required").max(100, "Name must be less than 100 characters"), description: z.string().max(500, "Description must be less than 500 characters").optional() }); export function RuleTemplatesTable({ templates, orgId }: RuleTemplatesTableProps) { const router = useRouter(); const t = useTranslations(); const api = createApiClient(useEnvContext()); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [selectedTemplate, setSelectedTemplate] = useState(null); const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false); const form = useForm>({ resolver: zodResolver(createTemplateSchema), defaultValues: { name: "", description: "" } }); const deleteTemplate = (templateId: string) => { api.delete(`/org/${orgId}/rule-templates/${templateId}`) .catch((e) => { console.error("Failed to delete template:", e); toast({ variant: "destructive", title: t("ruleTemplateErrorDelete"), description: formatAxiosError(e, t("ruleTemplateErrorDelete")) }); }) .then(() => { router.refresh(); setIsDeleteModalOpen(false); }); }; const handleCreateTemplate = async (values: z.infer) => { try { const response = await api.post(`/org/${orgId}/rule-templates`, values); if (response.status === 201) { setIsCreateDialogOpen(false); form.reset(); toast({ title: "Success", description: "Rule template created successfully" }); router.refresh(); } else { toast({ title: "Error", description: response.data.message || "Failed to create rule template", variant: "destructive" }); } } catch (error) { toast({ title: "Error", description: formatAxiosError(error, "Failed to create rule template"), variant: "destructive" }); } }; const columns: ColumnDef[] = [ { accessorKey: "name", header: ({ column }) => { return ( ); } }, { accessorKey: "description", header: "Description", cell: ({ row }) => { const template = row.original; return ( {template.description || "No description provided"} ); } }, { id: "actions", cell: ({ row }) => { const template = row.original; return (
{ setSelectedTemplate(template); setIsDeleteModalOpen(true); }} > Delete
); } } ]; return ( <> {selectedTemplate && ( { setIsDeleteModalOpen(val); setSelectedTemplate(null); }} dialog={

Are you sure you want to delete the template "{selectedTemplate?.name}"?

This action cannot be undone and will remove all rules associated with this template.

This will also unassign the template from any resources that are using it.

To confirm, please type {selectedTemplate?.name} below.

} buttonText="Delete Template" onConfirm={async () => deleteTemplate(selectedTemplate!.id)} string={selectedTemplate.name} title="Delete Rule Template" /> )} {/* Create Template Dialog */} Create Rule Template Create a new rule template to define access control rules
( Name )} /> ( Description