| gio | 1dc800a | 2025-04-24 17:15:43 +0000 | [diff] [blame] | 1 | import { AppNode, nodeLabel, useEnv, useMessages, useProjectId, useStateStore } from "@/lib/state"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 2 | import { Button } from "./ui/button"; |
| 3 | import { useCallback, useEffect, useState } from "react"; |
| 4 | import { generateDodoConfig } from "@/lib/config"; |
| 5 | import { useNodes, useReactFlow } from "@xyflow/react"; |
| 6 | import { useToast } from "@/hooks/use-toast"; |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 7 | import { |
| 8 | DropdownMenuGroup, |
| 9 | DropdownMenuItem, |
| 10 | DropdownMenu, |
| 11 | DropdownMenuContent, |
| 12 | DropdownMenuTrigger, |
| 13 | } from "./ui/dropdown-menu"; |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 14 | import { LoaderCircle, Menu } from "lucide-react"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 15 | |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame] | 16 | function toNodeType(t: string): string { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 17 | if (t === "ingress") { |
| 18 | return "gateway-https"; |
| 19 | } else if (t === "service") { |
| 20 | return "app"; |
| 21 | } else { |
| 22 | return t; |
| 23 | } |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame] | 24 | } |
| 25 | |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 26 | export function Actions() { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 27 | const { toast } = useToast(); |
| 28 | const store = useStateStore(); |
| 29 | const projectId = useProjectId(); |
| 30 | const nodes = useNodes<AppNode>(); |
| 31 | const env = useEnv(); |
| 32 | const messages = useMessages(); |
| 33 | const instance = useReactFlow(); |
| 34 | const [ok, setOk] = useState(false); |
| 35 | const [loading, setLoading] = useState(false); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 36 | const [reloading, setReloading] = useState(false); |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 37 | const info = useCallback( |
| 38 | (title: string, description?: string, duration?: number) => { |
| 39 | return toast({ |
| 40 | title, |
| 41 | description, |
| 42 | duration: duration ?? 2000, |
| 43 | }); |
| 44 | }, |
| 45 | [toast], |
| 46 | ); |
| 47 | const error = useCallback( |
| 48 | (title: string, description?: string, duration?: number) => { |
| 49 | return toast({ |
| 50 | variant: "destructive", |
| 51 | title, |
| 52 | description, |
| 53 | duration: duration ?? 5000, |
| 54 | }); |
| 55 | }, |
| 56 | [toast], |
| 57 | ); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 58 | useEffect(() => { |
| 59 | setOk(!messages.some((m) => m.type === "FATAL")); |
| 60 | }, [messages, setOk]); |
| 61 | const monitor = useCallback(async () => { |
| 62 | const m = async function () { |
| 63 | const resp = await fetch(`/api/project/${projectId}/status`, { |
| 64 | method: "GET", |
| 65 | headers: { |
| 66 | "Content-Type": "application/json", |
| 67 | }, |
| 68 | }); |
| 69 | if (resp.status !== 200) { |
| 70 | return; |
| 71 | } |
| 72 | const data: { type: string; name: string; status: string }[] = await resp.json(); |
| 73 | console.log(data); |
| 74 | for (const n of nodes) { |
| 75 | if (n.type === "network") { |
| 76 | continue; |
| 77 | } |
| 78 | const d = data.find((d) => n.type === toNodeType(d.type) && nodeLabel(n) === d.name); |
| 79 | if (d !== undefined) { |
| 80 | store.updateNodeData(n.id, { |
| 81 | state: d?.status, |
| 82 | }); |
| 83 | } |
| 84 | } |
| 85 | if (data.find((d) => d.status !== "success" && d.status != "failure") !== undefined) { |
| 86 | setTimeout(m, 1000); |
| 87 | } |
| 88 | }; |
| 89 | setTimeout(m, 100); |
| 90 | }, [projectId, nodes, store]); |
| 91 | const deploy = useCallback(async () => { |
| 92 | if (projectId == null) { |
| 93 | return; |
| 94 | } |
| 95 | setLoading(true); |
| 96 | try { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 97 | const config = generateDodoConfig(projectId, nodes, env); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 98 | if (config == null) { |
| 99 | throw new Error("MUST NOT REACH!"); |
| 100 | } |
| 101 | const resp = await fetch(`/api/project/${projectId}/deploy`, { |
| 102 | method: "POST", |
| 103 | headers: { |
| 104 | "Content-Type": "application/json", |
| 105 | }, |
| 106 | body: JSON.stringify({ |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 107 | state: instance.toObject(), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 108 | config, |
| 109 | }), |
| 110 | }); |
| 111 | if (resp.ok) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 112 | store.setMode("deploy"); |
| 113 | info("Deployment succeeded"); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 114 | monitor(); |
| 115 | } else { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 116 | error("Deployment failed", await resp.text()); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 117 | } |
| gio | 6223714 | 2025-05-19 10:39:40 +0000 | [diff] [blame] | 118 | store.refreshEnv(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 119 | } catch (e) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 120 | error("Deployment failed", e instanceof Error ? e.message : undefined); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 121 | } finally { |
| 122 | setLoading(false); |
| 123 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 124 | }, [projectId, instance, nodes, env, setLoading, info, error, monitor, store]); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 125 | const save = useCallback(async () => { |
| 126 | if (projectId == null) { |
| 127 | return; |
| 128 | } |
| 129 | const resp = await fetch(`/api/project/${projectId}/saved`, { |
| 130 | method: "POST", |
| 131 | headers: { |
| 132 | "Content-Type": "application/json", |
| 133 | }, |
| 134 | body: JSON.stringify(instance.toObject()), |
| 135 | }); |
| 136 | if (resp.ok) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 137 | info("Save succeeded"); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 138 | } else { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 139 | error("Save failed", await resp.text()); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 140 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 141 | }, [projectId, instance, info, error]); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 142 | const restoreSaved = useCallback(async () => { |
| 143 | if (projectId == null) { |
| 144 | return; |
| 145 | } |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 146 | const resp = await fetch(`/api/project/${projectId}/saved/${store.mode === "deploy" ? "deploy" : "draft"}`, { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 147 | method: "GET", |
| 148 | }); |
| 149 | const inst = await resp.json(); |
| 150 | const { x = 0, y = 0, zoom = 1 } = inst.viewport; |
| 151 | store.setNodes(inst.nodes || []); |
| 152 | store.setEdges(inst.edges || []); |
| 153 | instance.setViewport({ x, y, zoom }); |
| 154 | }, [projectId, instance, store]); |
| 155 | const clear = useCallback(() => { |
| 156 | store.setEdges([]); |
| gio | 6d8b71c | 2025-05-19 12:57:35 +0000 | [diff] [blame] | 157 | store.setNodes(store.nodes.filter((n) => n.type === "network")); |
| 158 | }, [store]); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 159 | const edit = useCallback(async () => { |
| 160 | store.setMode("edit"); |
| 161 | }, [store]); |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 162 | // TODO(gio): refresh projects |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 163 | const deleteProject = useCallback(async () => { |
| 164 | if (projectId == null) { |
| 165 | return; |
| 166 | } |
| gio | 3304672 | 2025-05-16 14:49:55 +0000 | [diff] [blame] | 167 | if (!confirm("Are you sure you want to delete this project? This action cannot be undone.")) { |
| 168 | return; |
| 169 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 170 | const resp = await fetch(`/api/project/${projectId}`, { |
| 171 | method: "DELETE", |
| 172 | }); |
| 173 | if (resp.ok) { |
| 174 | clear(); |
| 175 | store.setProject(undefined); |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 176 | info("Project deleted"); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 177 | } else { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 178 | error("Failed to delete project", await resp.text()); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 179 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 180 | }, [store, clear, projectId, info, error]); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 181 | const reload = useCallback(async () => { |
| 182 | if (projectId == null) { |
| 183 | return; |
| 184 | } |
| 185 | setReloading(true); |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 186 | const { dismiss } = info("Reloading services", "This may take a while...", Infinity); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 187 | try { |
| 188 | const resp = await fetch(`/api/project/${projectId}/reload`, { |
| 189 | method: "POST", |
| 190 | headers: { |
| 191 | "Content-Type": "application/json", |
| 192 | }, |
| 193 | }); |
| 194 | if (resp.ok) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 195 | dismiss(); |
| 196 | info("Reloaded services successfully"); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 197 | } else { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 198 | dismiss(); |
| 199 | error("Reload failed", await resp.text()); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 200 | } |
| 201 | } catch (e) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 202 | dismiss(); |
| 203 | error("Reload failed", e instanceof Error ? e.message : undefined); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 204 | } finally { |
| 205 | setReloading(false); |
| 206 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 207 | }, [projectId, info, error]); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 208 | const removeDeployment = useCallback(async () => { |
| 209 | if (projectId == null) { |
| 210 | return; |
| 211 | } |
| 212 | if (!confirm("Are you sure you want to remove this deployment? This action cannot be undone.")) { |
| 213 | return; |
| 214 | } |
| 215 | setReloading(true); |
| 216 | try { |
| 217 | const resp = await fetch(`/api/project/${projectId}/remove-deployment`, { |
| 218 | method: "POST", |
| 219 | headers: { |
| 220 | "Content-Type": "application/json", |
| 221 | }, |
| 222 | }); |
| 223 | if (resp.ok) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 224 | info("Deployment removed successfully"); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 225 | store.setMode("edit"); |
| 226 | } else { |
| 227 | const errorData = await resp.json(); |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 228 | error("Failed to remove deployment", errorData.error || "Unknown error"); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 229 | } |
| 230 | } catch (e) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 231 | error("Failed to remove deployment", e instanceof Error ? e.message : undefined); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 232 | } finally { |
| gio | b1c5c45 | 2025-05-21 04:16:54 +0000 | [diff] [blame^] | 233 | store.refreshEnv(); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 234 | setReloading(false); |
| 235 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 236 | }, [projectId, info, error, store]); |
| 237 | const [deployProps, setDeployProps] = useState<{ loading?: boolean; disabled?: boolean }>({ |
| 238 | loading: false, |
| 239 | disabled: false, |
| 240 | }); |
| 241 | const [reloadProps, setReloadProps] = useState<{ loading?: boolean; disabled?: boolean }>({ |
| 242 | loading: false, |
| 243 | disabled: false, |
| 244 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 245 | useEffect(() => { |
| 246 | if (loading) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 247 | setDeployProps({ loading: true, disabled: true }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 248 | } else if (ok) { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 249 | setDeployProps({ disabled: false }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 250 | } else { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 251 | setDeployProps({ disabled: true }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 252 | } |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 253 | |
| 254 | if (reloading) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 255 | setReloadProps({ loading: true, disabled: true }); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 256 | } else { |
| 257 | setReloadProps({ disabled: projectId === undefined }); |
| 258 | } |
| 259 | }, [ok, loading, reloading, projectId]); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 260 | if (store.mode === "deploy") { |
| 261 | return ( |
| 262 | <div className="flex flex-row gap-1 items-center"> |
| 263 | <Button onClick={edit} {...reloadProps}> |
| 264 | Edit |
| 265 | </Button> |
| 266 | <DropdownMenu> |
| 267 | <DropdownMenuTrigger> |
| 268 | <Menu className="rounded-md bg-gray-200 opacity-50" /> |
| 269 | </DropdownMenuTrigger> |
| 270 | <DropdownMenuContent className="w-56"> |
| 271 | <DropdownMenuGroup> |
| 272 | <DropdownMenuItem |
| 273 | onClick={reload} |
| 274 | className="cursor-pointer hover:bg-gray-200" |
| 275 | {...reloadProps} |
| 276 | > |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 277 | {reloadProps.loading ? ( |
| 278 | <> |
| 279 | <LoaderCircle className="animate-spin" /> |
| 280 | Reloading... |
| 281 | </> |
| 282 | ) : ( |
| 283 | "Reload services" |
| 284 | )} |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 285 | </DropdownMenuItem> |
| 286 | <DropdownMenuItem |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 287 | onClick={removeDeployment} |
| 288 | disabled={projectId === undefined} |
| 289 | className="cursor-pointer hover:bg-gray-200" |
| 290 | > |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 291 | Remove deployment |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 292 | </DropdownMenuItem> |
| 293 | <DropdownMenuItem |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 294 | onClick={deleteProject} |
| 295 | disabled={projectId === undefined} |
| 296 | className="cursor-pointer hover:bg-gray-200" |
| 297 | > |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 298 | Delete project |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 299 | </DropdownMenuItem> |
| 300 | </DropdownMenuGroup> |
| 301 | </DropdownMenuContent> |
| 302 | </DropdownMenu> |
| 303 | </div> |
| 304 | ); |
| 305 | } else { |
| 306 | return ( |
| 307 | <div className="flex flex-row gap-1 items-center"> |
| 308 | <Button onClick={deploy} {...deployProps}> |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 309 | {deployProps.loading ? ( |
| 310 | <> |
| 311 | <LoaderCircle className="animate-spin" /> |
| 312 | Deploying... |
| 313 | </> |
| 314 | ) : ( |
| 315 | "Deploy" |
| 316 | )} |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 317 | </Button> |
| 318 | <Button onClick={save}>Save</Button> |
| 319 | <DropdownMenu> |
| 320 | <DropdownMenuTrigger> |
| 321 | <Menu /> |
| 322 | </DropdownMenuTrigger> |
| 323 | <DropdownMenuContent className="w-56"> |
| 324 | <DropdownMenuGroup> |
| 325 | <DropdownMenuItem |
| 326 | onClick={restoreSaved} |
| 327 | disabled={projectId === undefined} |
| 328 | className="cursor-pointer hover:bg-gray-200" |
| 329 | > |
| 330 | Restore |
| 331 | </DropdownMenuItem> |
| 332 | <DropdownMenuItem onClick={clear} className="cursor-pointer hover:bg-gray-200"> |
| 333 | Clear |
| 334 | </DropdownMenuItem> |
| 335 | <DropdownMenuItem |
| 336 | onClick={deleteProject} |
| 337 | disabled={projectId === undefined} |
| 338 | className="cursor-pointer hover:bg-gray-200" |
| 339 | > |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame] | 340 | Delete project |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 341 | </DropdownMenuItem> |
| 342 | </DropdownMenuGroup> |
| 343 | </DropdownMenuContent> |
| 344 | </DropdownMenu> |
| 345 | </div> |
| 346 | ); |
| 347 | } |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 348 | } |