|
| 1 | +import { type ReactNode, useState } from "react"; |
| 2 | +import { FaPython } from "react-icons/fa"; |
| 3 | + |
| 4 | +import { Icon } from "@/components/ui/icon"; |
| 5 | +import { InlineStack } from "@/components/ui/layout"; |
| 6 | +import { Paragraph } from "@/components/ui/typography"; |
| 7 | +import useToastNotification from "@/hooks/useToastNotification"; |
| 8 | +import type { ComponentSpec } from "@/utils/componentSpec"; |
| 9 | +import { downloadYamlFromComponentText } from "@/utils/URL"; |
| 10 | +import copyToYaml from "@/utils/yaml"; |
| 11 | + |
| 12 | +import { |
| 13 | + ActionBlock, |
| 14 | + type ActionOrReactNode, |
| 15 | +} from "../ContextPanel/Blocks/ActionBlock"; |
| 16 | + |
| 17 | +interface TaskActionsProps { |
| 18 | + displayName: string; |
| 19 | + componentSpec: ComponentSpec; |
| 20 | + actions?: ReactNode[]; |
| 21 | + onDelete?: () => void; |
| 22 | + hasTwoStepDeletion?: boolean; |
| 23 | + readOnly?: boolean; |
| 24 | + className?: string; |
| 25 | +} |
| 26 | + |
| 27 | +const TaskActions = ({ |
| 28 | + displayName, |
| 29 | + componentSpec, |
| 30 | + actions = [], |
| 31 | + onDelete, |
| 32 | + hasTwoStepDeletion = false, |
| 33 | + readOnly = false, |
| 34 | + className, |
| 35 | +}: TaskActionsProps) => { |
| 36 | + const notify = useToastNotification(); |
| 37 | + const [confirmDelete, setConfirmDelete] = useState(false); |
| 38 | + |
| 39 | + const pythonOriginalCode = |
| 40 | + componentSpec?.metadata?.annotations?.original_python_code; |
| 41 | + |
| 42 | + const stringToPythonCodeDownload = () => { |
| 43 | + if (!pythonOriginalCode) return; |
| 44 | + |
| 45 | + const blob = new Blob([pythonOriginalCode], { type: "text/x-python" }); |
| 46 | + const url = URL.createObjectURL(blob); |
| 47 | + const a = document.createElement("a"); |
| 48 | + a.href = url; |
| 49 | + a.download = `${componentSpec?.name || displayName}.py`; |
| 50 | + document.body.appendChild(a); |
| 51 | + a.click(); |
| 52 | + document.body.removeChild(a); |
| 53 | + URL.revokeObjectURL(url); |
| 54 | + }; |
| 55 | + |
| 56 | + const handleDownloadYaml = () => { |
| 57 | + downloadYamlFromComponentText(componentSpec, displayName); |
| 58 | + }; |
| 59 | + |
| 60 | + const handleCopyYaml = () => { |
| 61 | + copyToYaml( |
| 62 | + componentSpec, |
| 63 | + (message) => notify(message, "success"), |
| 64 | + (message) => notify(message, "error"), |
| 65 | + ); |
| 66 | + }; |
| 67 | + |
| 68 | + const handleDelete = () => { |
| 69 | + if (confirmDelete || !hasTwoStepDeletion) { |
| 70 | + try { |
| 71 | + onDelete?.(); |
| 72 | + } catch (error) { |
| 73 | + console.error("Error deleting component:", error); |
| 74 | + notify(`Error deleting component`, "error"); |
| 75 | + } |
| 76 | + } else if (hasTwoStepDeletion) { |
| 77 | + setConfirmDelete(true); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + const orderedActions: ActionOrReactNode[] = []; |
| 82 | + |
| 83 | + orderedActions.push({ |
| 84 | + label: "Download YAML", |
| 85 | + icon: "Download", |
| 86 | + onClick: handleDownloadYaml, |
| 87 | + }); |
| 88 | + |
| 89 | + orderedActions.push({ |
| 90 | + label: "Download Python Code", |
| 91 | + content: <FaPython />, |
| 92 | + hidden: !pythonOriginalCode, |
| 93 | + onClick: stringToPythonCodeDownload, |
| 94 | + }); |
| 95 | + |
| 96 | + orderedActions.push({ |
| 97 | + label: "Copy YAML", |
| 98 | + icon: "Clipboard", |
| 99 | + onClick: handleCopyYaml, |
| 100 | + }); |
| 101 | + |
| 102 | + orderedActions.push(...actions); |
| 103 | + |
| 104 | + orderedActions.push({ |
| 105 | + label: |
| 106 | + confirmDelete && hasTwoStepDeletion |
| 107 | + ? "Confirm Delete. This action cannot be undone." |
| 108 | + : "Delete Component", |
| 109 | + content: ( |
| 110 | + <InlineStack gap="2" blockAlign="center"> |
| 111 | + <Icon name="Trash" /> |
| 112 | + {confirmDelete && hasTwoStepDeletion && ( |
| 113 | + <Paragraph size="xs" className="text-white"> |
| 114 | + Confirm Delete |
| 115 | + </Paragraph> |
| 116 | + )} |
| 117 | + </InlineStack> |
| 118 | + ), |
| 119 | + destructive: true, |
| 120 | + hidden: !onDelete || readOnly, |
| 121 | + onClick: handleDelete, |
| 122 | + }); |
| 123 | + |
| 124 | + return <ActionBlock actions={orderedActions} className={className} />; |
| 125 | +}; |
| 126 | + |
| 127 | +export default TaskActions; |
0 commit comments