-
Notifications
You must be signed in to change notification settings - Fork 947
/
Copy pathWorkflowActions.tsx
205 lines (193 loc) · 5.9 KB
/
WorkflowActions.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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<string, { data: WorkflowApiResponse }>(
"/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 (
<Dialog>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button size="icon" variant="outline">
<DotsHorizontalIcon className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem
onSelect={() => {
if (!workflow) {
return;
}
const clonedWorkflow = convert({
...workflow,
title: `Copy of ${workflow.title}`,
});
createWorkflowMutation.mutate(clonedWorkflow);
}}
className="p-2"
>
<CopyIcon className="mr-2 h-4 w-4" />
Clone Workflow
</DropdownMenuItem>
<DropdownMenuSub>
<DropdownMenuSubTrigger>
<DownloadIcon className="mr-2 h-4 w-4" />
Export as...
</DropdownMenuSubTrigger>
<DropdownMenuPortal>
<DropdownMenuSubContent>
<DropdownMenuItem
onSelect={() => {
handleExport("yaml");
}}
>
YAML
</DropdownMenuItem>
<DropdownMenuItem
onSelect={() => {
handleExport("json");
}}
>
JSON
</DropdownMenuItem>
</DropdownMenuSubContent>
</DropdownMenuPortal>
</DropdownMenuSub>
<DialogTrigger>
<DropdownMenuItem className="p-2">
<GarbageIcon className="mr-2 h-4 w-4 text-destructive" />
Delete Workflow
</DropdownMenuItem>
</DialogTrigger>
</DropdownMenuContent>
</DropdownMenu>
<DialogContent onCloseAutoFocus={(e) => e.preventDefault()}>
<DialogHeader>
<DialogTitle>Are you sure?</DialogTitle>
<DialogDescription>This workflow will be deleted.</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose asChild>
<Button variant="secondary">Cancel</Button>
</DialogClose>
<Button
variant="destructive"
onClick={() => {
deleteWorkflowMutation.mutate(id);
}}
disabled={deleteWorkflowMutation.isPending}
>
{deleteWorkflowMutation.isPending && (
<ReloadIcon className="mr-2 h-4 w-4 animate-spin" />
)}
Delete
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
export { WorkflowActions };