import { getClient } from "@/api/AxiosClient"; import { GarbageIcon } from "@/components/icons/GarbageIcon"; import { Button } from "@/components/ui/button"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuPortal, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { toast } from "@/components/ui/use-toast"; import { useCredentialGetter } from "@/hooks/useCredentialGetter"; import { CopyIcon, DotsHorizontalIcon, DownloadIcon, ReloadIcon, } from "@radix-ui/react-icons"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { AxiosError } from "axios"; import { useNavigate } from "react-router-dom"; import { stringify as convertToYAML } from "yaml"; import { convert } from "./editor/workflowEditorUtils"; import { useWorkflowQuery } from "./hooks/useWorkflowQuery"; import { WorkflowApiResponse } from "./types/workflowTypes"; import { WorkflowCreateYAMLRequest } from "./types/workflowYamlTypes"; type Props = { id: string; }; function downloadFile(fileName: string, contents: string) { const element = document.createElement("a"); element.setAttribute( "href", "data:text/plain;charset=utf-8," + encodeURIComponent(contents), ); element.setAttribute("download", fileName); element.style.display = "none"; document.body.appendChild(element); element.click(); document.body.removeChild(element); } function WorkflowActions({ id }: Props) { const credentialGetter = useCredentialGetter(); const queryClient = useQueryClient(); const { data: workflow } = useWorkflowQuery({ workflowPermanentId: id }); const navigate = useNavigate(); function handleExport(type: "json" | "yaml") { if (!workflow) { return; } const fileName = `${workflow.title}.${type}`; const contents = type === "json" ? JSON.stringify(convert(workflow), null, 2) : convertToYAML(convert(workflow)); downloadFile(fileName, contents); } const createWorkflowMutation = useMutation({ mutationFn: async (workflow: WorkflowCreateYAMLRequest) => { const client = await getClient(credentialGetter); const yaml = convertToYAML(workflow); return client.post( "/workflows", yaml, { headers: { "Content-Type": "text/plain", }, }, ); }, onSuccess: (response) => { queryClient.invalidateQueries({ queryKey: ["workflows"], }); navigate(`/workflows/${response.data.workflow_permanent_id}/edit`); }, }); const deleteWorkflowMutation = useMutation({ mutationFn: async (id: string) => { const client = await getClient(credentialGetter); return client.delete(`/workflows/${id}`); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["workflows"], }); }, onError: (error: AxiosError) => { toast({ variant: "destructive", title: "Failed to delete workflow", description: error.message, }); }, }); return ( { if (!workflow) { return; } const clonedWorkflow = convert({ ...workflow, title: `Copy of ${workflow.title}`, }); createWorkflowMutation.mutate(clonedWorkflow); }} className="p-2" > Clone Workflow Export as... { handleExport("yaml"); }} > YAML { handleExport("json"); }} > JSON Delete Workflow e.preventDefault()}> Are you sure? This workflow will be deleted. ); } export { WorkflowActions };