import { useCallback } from "react"; import { subject } from "@casl/ability"; import { 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 { BanIcon, RefreshCwIcon } from "lucide-react"; import { createNotification } from "@app/components/notifications"; import { ProjectPermissionCan } from "@app/components/permissions"; import { DeleteSecretSyncModal, SecretSyncImportSecretsModal, SecretSyncImportStatusBadge, SecretSyncRemoveSecretsModal, SecretSyncRemoveStatusBadge } from "@app/components/secret-syncs"; import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, IconButton, Tooltip } from "@app/components/v2"; import { Badge } from "@app/components/v3"; import { ROUTE_PATHS } from "@app/const/routes"; import { ProjectPermissionSub } from "@app/context"; import { ProjectPermissionSecretSyncActions } from "@app/context/ProjectPermissionContext/types"; import { SECRET_SYNC_MAP } from "@app/helpers/secretSyncs"; import { usePopUp, useToggle } from "@app/hooks"; import { TSecretSync, useSecretSyncOption, useTriggerSecretSyncSyncSecrets, useUpdateSecretSync } from "@app/hooks/api/secretSyncs"; import { IntegrationsListPageTabs } from "@app/types/integrations"; type Props = { secretSync: TSecretSync; }; export const SecretSyncActionTriggers = ({ secretSync }: Props) => { const { popUp, handlePopUpOpen, handlePopUpToggle } = usePopUp([ "importSecrets", "removeSecrets", "deleteSync" ] as const); const navigate = useNavigate(); const triggerSyncSecrets = useTriggerSecretSyncSyncSecrets(); const updateSync = useUpdateSecretSync(); const { destination, environment, folder } = secretSync; const destinationName = SECRET_SYNC_MAP[destination].name; const { syncOption } = useSecretSyncOption(destination); const [isIdCopied, setIsIdCopied] = useToggle(false); const handleCopyId = useCallback(() => { setIsIdCopied.on(); navigator.clipboard.writeText(secretSync.id); createNotification({ text: "Secret Sync ID copied to clipboard", type: "info" }); const timer = setTimeout(() => setIsIdCopied.off(), 2000); // eslint-disable-next-line consistent-return return () => clearTimeout(timer); }, [isIdCopied]); const handleToggleEnableSync = async () => { const isAutoSyncEnabled = !secretSync.isAutoSyncEnabled; try { await updateSync.mutateAsync({ syncId: secretSync.id, destination: secretSync.destination, isAutoSyncEnabled, projectId: secretSync.projectId }); createNotification({ text: `Successfully ${isAutoSyncEnabled ? "enabled" : "disabled"} auto-sync for ${destinationName} Sync`, type: "success" }); } catch { createNotification({ text: `Failed to ${isAutoSyncEnabled ? "enable" : "disable"} auto-sync for ${destinationName} Sync`, type: "error" }); } }; const handleTriggerSync = async () => { try { await triggerSyncSecrets.mutateAsync({ syncId: secretSync.id, destination: secretSync.destination, projectId: secretSync.projectId }); createNotification({ text: `Successfully triggered ${destinationName} Sync`, type: "success" }); } catch { createNotification({ text: `Failed to trigger ${destinationName} Sync`, type: "error" }); } }; const permissionSubject = environment && folder ? subject(ProjectPermissionSub.SecretSyncs, { environment: environment.slug, secretPath: folder.path }) : ProjectPermissionSub.SecretSyncs; return ( <>