import { useCallback } from "react"; import { subject } from "@casl/ability"; import { faBan, faCheck, faCopy, faDownload, faEllipsisV, faEraser, faInfoCircle, faRotate, faToggleOff, faToggleOn, faTrash } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { useNavigate } from "@tanstack/react-router"; import { createNotification } from "@app/components/notifications"; import { ProjectPermissionCan } from "@app/components/permissions"; import { DeletePkiSyncModal, PkiSyncImportCertificatesModal, PkiSyncImportStatusBadge, PkiSyncRemoveCertificatesModal, PkiSyncRemoveStatusBadge } from "@app/components/pki-syncs"; import { Badge, Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, IconButton, Tooltip } from "@app/components/v2"; import { ROUTE_PATHS } from "@app/const/routes"; import { ProjectPermissionSub } from "@app/context"; import { ProjectPermissionPkiSyncActions } from "@app/context/ProjectPermissionContext/types"; import { PKI_SYNC_MAP } from "@app/helpers/pkiSyncs"; import { usePopUp, useToggle } from "@app/hooks"; import { TPkiSync, usePkiSyncOption, useTriggerPkiSyncSyncCertificates, useUpdatePkiSync } from "@app/hooks/api/pkiSyncs"; import { IntegrationsListPageTabs } from "@app/types/integrations"; type Props = { pkiSync: TPkiSync; }; export const PkiSyncActionTriggers = ({ pkiSync }: Props) => { const { destination, subscriberId, projectId, id } = pkiSync; const navigate = useNavigate(); const { popUp, handlePopUpOpen, handlePopUpToggle } = usePopUp([ "importCertificates", "removeCertificates", "deleteSync" ] as const); const [isIdCopied, setIsIdCopied] = useToggle(false); // API mutations const triggerSyncMutation = useTriggerPkiSyncSyncCertificates(); const updatePkiSyncMutation = useUpdatePkiSync(); const { syncOption } = usePkiSyncOption(destination); const destinationName = PKI_SYNC_MAP[destination].name; const handleCopyId = useCallback(() => { setIsIdCopied.on(); navigator.clipboard.writeText(pkiSync.id); createNotification({ text: "PKI Sync ID copied to clipboard", type: "info" }); const timer = setTimeout(() => setIsIdCopied.off(), 2000); return () => clearTimeout(timer); }, [pkiSync.id, setIsIdCopied]); const handleTriggerSync = useCallback(async () => { try { await triggerSyncMutation.mutateAsync({ syncId: id, destination, projectId }); createNotification({ text: "PKI sync job queued successfully", type: "success" }); } catch (error) { console.error("Failed to trigger sync:", error); createNotification({ text: "Failed to trigger PKI sync", type: "error" }); } }, [triggerSyncMutation, id, destination, projectId]); const handleToggleAutoSync = useCallback(async () => { try { await updatePkiSyncMutation.mutateAsync({ syncId: id, projectId, destination, isAutoSyncEnabled: !pkiSync.isAutoSyncEnabled }); createNotification({ text: `Auto-sync ${pkiSync.isAutoSyncEnabled ? "disabled" : "enabled"} successfully`, type: "success" }); } catch (error) { console.error("Failed to toggle auto-sync:", error); createNotification({ text: "Failed to toggle auto-sync", type: "error" }); } }, [updatePkiSyncMutation, id, projectId, pkiSync.isAutoSyncEnabled]); const permissionSubject = subject(ProjectPermissionSub.PkiSyncs, { subscriberId: subscriberId || "" }); return ( <>
{syncOption?.canImportCertificates && } {pkiSync.isAutoSyncEnabled ? ( Auto-Sync Enabled ) : (
Auto-Sync Disabled
)}
{(isAllowed: boolean) => ( )} } onClick={(e) => { e.stopPropagation(); handleCopyId(); }} > Copy Sync ID {syncOption?.canImportCertificates && ( {(isAllowed: boolean) => ( } onClick={() => handlePopUpOpen("importCertificates")} isDisabled={!isAllowed} >
Import Certificates
)}
)} {(isAllowed: boolean) => ( } onClick={() => handlePopUpOpen("removeCertificates")} isDisabled={!isAllowed} >
Remove Certificates
)}
{(isAllowed: boolean) => ( } onClick={handleToggleAutoSync} > {pkiSync.isAutoSyncEnabled ? "Disable" : "Enable"} Auto-Sync )} {(isAllowed: boolean) => ( } onClick={() => handlePopUpOpen("deleteSync")} > Delete Sync )}
{syncOption?.canImportCertificates && ( handlePopUpToggle("importCertificates", isOpen)} isOpen={popUp.importCertificates.isOpen} pkiSync={pkiSync} /> )} handlePopUpToggle("removeCertificates", isOpen)} isOpen={popUp.removeCertificates.isOpen} pkiSync={pkiSync} /> handlePopUpToggle("deleteSync", isOpen)} isOpen={popUp.deleteSync.isOpen} pkiSync={pkiSync} onComplete={() => navigate({ to: ROUTE_PATHS.CertManager.IntegrationsListPage.path, params: { projectId }, search: { selectedTab: IntegrationsListPageTabs.PkiSyncs } }) } /> ); };