| 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"; |
| 7 | |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame] | 8 | function toNodeType(t: string): string { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 9 | if (t === "ingress") { |
| 10 | return "gateway-https"; |
| 11 | } else if (t === "service") { |
| 12 | return "app"; |
| 13 | } else { |
| 14 | return t; |
| 15 | } |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame] | 16 | } |
| 17 | |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 18 | export function Actions() { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 19 | const { toast } = useToast(); |
| 20 | const store = useStateStore(); |
| 21 | const projectId = useProjectId(); |
| 22 | const nodes = useNodes<AppNode>(); |
| 23 | const env = useEnv(); |
| 24 | const messages = useMessages(); |
| 25 | const instance = useReactFlow(); |
| 26 | const [ok, setOk] = useState(false); |
| 27 | const [loading, setLoading] = useState(false); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 28 | const [reloading, setReloading] = useState(false); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 29 | useEffect(() => { |
| 30 | setOk(!messages.some((m) => m.type === "FATAL")); |
| 31 | }, [messages, setOk]); |
| 32 | const monitor = useCallback(async () => { |
| 33 | const m = async function () { |
| 34 | const resp = await fetch(`/api/project/${projectId}/status`, { |
| 35 | method: "GET", |
| 36 | headers: { |
| 37 | "Content-Type": "application/json", |
| 38 | }, |
| 39 | }); |
| 40 | if (resp.status !== 200) { |
| 41 | return; |
| 42 | } |
| 43 | const data: { type: string; name: string; status: string }[] = await resp.json(); |
| 44 | console.log(data); |
| 45 | for (const n of nodes) { |
| 46 | if (n.type === "network") { |
| 47 | continue; |
| 48 | } |
| 49 | const d = data.find((d) => n.type === toNodeType(d.type) && nodeLabel(n) === d.name); |
| 50 | if (d !== undefined) { |
| 51 | store.updateNodeData(n.id, { |
| 52 | state: d?.status, |
| 53 | }); |
| 54 | } |
| 55 | } |
| 56 | if (data.find((d) => d.status !== "success" && d.status != "failure") !== undefined) { |
| 57 | setTimeout(m, 1000); |
| 58 | } |
| 59 | }; |
| 60 | setTimeout(m, 100); |
| 61 | }, [projectId, nodes, store]); |
| 62 | const deploy = useCallback(async () => { |
| 63 | if (projectId == null) { |
| 64 | return; |
| 65 | } |
| 66 | setLoading(true); |
| 67 | try { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 68 | const config = generateDodoConfig(projectId, nodes, env); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 69 | if (config == null) { |
| 70 | throw new Error("MUST NOT REACH!"); |
| 71 | } |
| 72 | const resp = await fetch(`/api/project/${projectId}/deploy`, { |
| 73 | method: "POST", |
| 74 | headers: { |
| 75 | "Content-Type": "application/json", |
| 76 | }, |
| 77 | body: JSON.stringify({ |
| 78 | state: JSON.stringify(instance.toObject()), |
| 79 | config, |
| 80 | }), |
| 81 | }); |
| 82 | if (resp.ok) { |
| 83 | toast({ |
| 84 | title: "Deployment succeeded", |
| 85 | }); |
| 86 | monitor(); |
| 87 | } else { |
| 88 | toast({ |
| 89 | variant: "destructive", |
| 90 | title: "Deployment failed", |
| 91 | description: await resp.text(), |
| 92 | }); |
| 93 | } |
| 94 | } catch (e) { |
| 95 | console.log(e); |
| 96 | toast({ |
| 97 | variant: "destructive", |
| 98 | title: "Deployment failed", |
| 99 | }); |
| 100 | } finally { |
| 101 | setLoading(false); |
| 102 | } |
| 103 | }, [projectId, instance, nodes, env, setLoading, toast, monitor]); |
| 104 | const save = useCallback(async () => { |
| 105 | if (projectId == null) { |
| 106 | return; |
| 107 | } |
| 108 | const resp = await fetch(`/api/project/${projectId}/saved`, { |
| 109 | method: "POST", |
| 110 | headers: { |
| 111 | "Content-Type": "application/json", |
| 112 | }, |
| 113 | body: JSON.stringify(instance.toObject()), |
| 114 | }); |
| 115 | if (resp.ok) { |
| 116 | toast({ |
| 117 | title: "Save succeeded", |
| 118 | }); |
| 119 | } else { |
| 120 | toast({ |
| 121 | variant: "destructive", |
| 122 | title: "Save failed", |
| 123 | description: await resp.text(), |
| 124 | }); |
| 125 | } |
| 126 | }, [projectId, instance, toast]); |
| 127 | const restoreSaved = useCallback(async () => { |
| 128 | if (projectId == null) { |
| 129 | return; |
| 130 | } |
| 131 | const resp = await fetch(`/api/project/${projectId}/saved`, { |
| 132 | method: "GET", |
| 133 | }); |
| 134 | const inst = await resp.json(); |
| 135 | const { x = 0, y = 0, zoom = 1 } = inst.viewport; |
| 136 | store.setNodes(inst.nodes || []); |
| 137 | store.setEdges(inst.edges || []); |
| 138 | instance.setViewport({ x, y, zoom }); |
| 139 | }, [projectId, instance, store]); |
| 140 | const clear = useCallback(() => { |
| 141 | store.setEdges([]); |
| 142 | store.setNodes([]); |
| 143 | instance.setViewport({ x: 0, y: 0, zoom: 1 }); |
| 144 | }, [store, instance]); |
| 145 | // TODO(gio): Update store |
| 146 | const deleteProject = useCallback(async () => { |
| 147 | if (projectId == null) { |
| 148 | return; |
| 149 | } |
| 150 | const resp = await fetch(`/api/project/${projectId}`, { |
| 151 | method: "DELETE", |
| 152 | }); |
| 153 | if (resp.ok) { |
| 154 | clear(); |
| 155 | store.setProject(undefined); |
| 156 | toast({ |
| 157 | title: "Save succeeded", |
| 158 | }); |
| 159 | } else { |
| 160 | toast({ |
| 161 | variant: "destructive", |
| 162 | title: "Save failed", |
| 163 | description: await resp.text(), |
| 164 | }); |
| 165 | } |
| 166 | }, [store, clear, projectId, toast]); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 167 | const reload = useCallback(async () => { |
| 168 | if (projectId == null) { |
| 169 | return; |
| 170 | } |
| 171 | setReloading(true); |
| 172 | try { |
| 173 | const resp = await fetch(`/api/project/${projectId}/reload`, { |
| 174 | method: "POST", |
| 175 | headers: { |
| 176 | "Content-Type": "application/json", |
| 177 | }, |
| 178 | }); |
| 179 | if (resp.ok) { |
| 180 | toast({ |
| 181 | title: "Reload triggered successfully", |
| 182 | }); |
| 183 | } else { |
| 184 | toast({ |
| 185 | variant: "destructive", |
| 186 | title: "Reload failed", |
| 187 | description: await resp.text(), |
| 188 | }); |
| 189 | } |
| 190 | } catch (e) { |
| 191 | console.log(e); |
| 192 | toast({ |
| 193 | variant: "destructive", |
| 194 | title: "Reload failed", |
| 195 | }); |
| 196 | } finally { |
| 197 | setReloading(false); |
| 198 | } |
| 199 | }, [projectId, toast]); |
| 200 | const [deployProps, setDeployProps] = useState({}); |
| 201 | const [reloadProps, setReloadProps] = useState({}); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 202 | useEffect(() => { |
| 203 | if (loading) { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 204 | setDeployProps({ loading: true }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 205 | } else if (ok) { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 206 | setDeployProps({ disabled: false }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 207 | } else { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 208 | setDeployProps({ disabled: true }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 209 | } |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 210 | |
| 211 | if (reloading) { |
| 212 | setReloadProps({ loading: true }); |
| 213 | } else { |
| 214 | setReloadProps({ disabled: projectId === undefined }); |
| 215 | } |
| 216 | }, [ok, loading, reloading, projectId]); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 217 | return ( |
| 218 | <> |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 219 | <Button onClick={deploy} {...deployProps}> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 220 | Deploy |
| 221 | </Button> |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 222 | <Button onClick={reload} {...reloadProps}> |
| 223 | Reload |
| 224 | </Button> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 225 | <Button onClick={save}>Save</Button> |
| 226 | <Button onClick={restoreSaved}>Restore</Button> |
| 227 | <Button onClick={clear} variant="destructive"> |
| 228 | Clear |
| 229 | </Button> |
| 230 | <Button onClick={deleteProject} variant="destructive" disabled={projectId === undefined}> |
| 231 | Delete |
| 232 | </Button> |
| 233 | </> |
| 234 | ); |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 235 | } |