import { Controller, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { useNavigate } from "@tanstack/react-router"; import { z } from "zod"; import { createNotification } from "@app/components/notifications"; import { Button, FormControl, Input, Modal, ModalContent, Spinner } from "@app/components/v2"; import { useOrganization } from "@app/context"; import { useCreateOrgRole, useGetOrgRole } from "@app/hooks/api"; import { TOrgRole } from "@app/hooks/api/roles/types"; import { slugSchema } from "@app/lib/schemas"; type Props = { isOpen: boolean; onOpenChange: (isOpen: boolean) => void; roleId?: string; }; const schema = z .object({ name: z.string().min(1, "Name required"), description: z.string(), slug: slugSchema({ min: 1 }) }) .required(); export type FormData = z.infer; type ContentProps = { role: TOrgRole; onClose: () => void; }; const Content = ({ role, onClose }: ContentProps) => { const { control, handleSubmit, formState: { isSubmitting } } = useForm({ defaultValues: { name: `${role.name} Duplicate` }, resolver: zodResolver(schema) }); const createRole = useCreateOrgRole(); const navigate = useNavigate(); const handleDuplicateRole = async (form: FormData) => { const newRole = await createRole.mutateAsync({ orgId: role.orgId, permissions: role.permissions, ...form }); createNotification({ type: "success", text: "Role duplicated successfully" }); navigate({ to: "/organization/roles/$roleId", params: { roleId: newRole.id } }); onClose(); }; return (
( )} /> ( )} /> ( )} />
); }; export const DuplicateOrgRoleModal = ({ isOpen, onOpenChange, roleId }: Props) => { const { currentOrg } = useOrganization(); const { data: role, isPending } = useGetOrgRole(currentOrg.id, roleId ?? ""); if (!roleId) return null; return ( {/* eslint-disable-next-line no-nested-ternary */} {isPending ? (

Loading Role...

) : role ? ( onOpenChange(false)} /> ) : (

Error: could not find role with slug "{roleId}"

)}
); };