| 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 | } |
| 118 | } catch (e) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 119 | error("Deployment failed", e instanceof Error ? e.message : undefined); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 120 | } finally { |
| 121 | setLoading(false); |
| 122 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 123 | }, [projectId, instance, nodes, env, setLoading, info, error, monitor, store]); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 124 | const save = useCallback(async () => { |
| 125 | if (projectId == null) { |
| 126 | return; |
| 127 | } |
| 128 | const resp = await fetch(`/api/project/${projectId}/saved`, { |
| 129 | method: "POST", |
| 130 | headers: { |
| 131 | "Content-Type": "application/json", |
| 132 | }, |
| 133 | body: JSON.stringify(instance.toObject()), |
| 134 | }); |
| 135 | if (resp.ok) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 136 | info("Save succeeded"); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 137 | } else { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 138 | error("Save failed", await resp.text()); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 139 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 140 | }, [projectId, instance, info, error]); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 141 | const restoreSaved = useCallback(async () => { |
| 142 | if (projectId == null) { |
| 143 | return; |
| 144 | } |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 145 | const resp = await fetch(`/api/project/${projectId}/saved/${store.mode === "deploy" ? "deploy" : "draft"}`, { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 146 | method: "GET", |
| 147 | }); |
| 148 | const inst = await resp.json(); |
| 149 | const { x = 0, y = 0, zoom = 1 } = inst.viewport; |
| 150 | store.setNodes(inst.nodes || []); |
| 151 | store.setEdges(inst.edges || []); |
| 152 | instance.setViewport({ x, y, zoom }); |
| 153 | }, [projectId, instance, store]); |
| 154 | const clear = useCallback(() => { |
| 155 | store.setEdges([]); |
| 156 | store.setNodes([]); |
| 157 | instance.setViewport({ x: 0, y: 0, zoom: 1 }); |
| 158 | }, [store, instance]); |
| 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 | } |
| 167 | const resp = await fetch(`/api/project/${projectId}`, { |
| 168 | method: "DELETE", |
| 169 | }); |
| 170 | if (resp.ok) { |
| 171 | clear(); |
| 172 | store.setProject(undefined); |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 173 | info("Project deleted"); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 174 | } else { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 175 | error("Failed to delete project", await resp.text()); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 176 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 177 | }, [store, clear, projectId, info, error]); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 178 | const reload = useCallback(async () => { |
| 179 | if (projectId == null) { |
| 180 | return; |
| 181 | } |
| 182 | setReloading(true); |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 183 | const { dismiss } = info("Reloading services", "This may take a while...", Infinity); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 184 | try { |
| 185 | const resp = await fetch(`/api/project/${projectId}/reload`, { |
| 186 | method: "POST", |
| 187 | headers: { |
| 188 | "Content-Type": "application/json", |
| 189 | }, |
| 190 | }); |
| 191 | if (resp.ok) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 192 | dismiss(); |
| 193 | info("Reloaded services successfully"); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 194 | } else { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 195 | dismiss(); |
| 196 | error("Reload failed", await resp.text()); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 197 | } |
| 198 | } catch (e) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 199 | dismiss(); |
| 200 | error("Reload failed", e instanceof Error ? e.message : undefined); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 201 | } finally { |
| 202 | setReloading(false); |
| 203 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 204 | }, [projectId, info, error]); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 205 | const removeDeployment = useCallback(async () => { |
| 206 | if (projectId == null) { |
| 207 | return; |
| 208 | } |
| 209 | if (!confirm("Are you sure you want to remove this deployment? This action cannot be undone.")) { |
| 210 | return; |
| 211 | } |
| 212 | setReloading(true); |
| 213 | try { |
| 214 | const resp = await fetch(`/api/project/${projectId}/remove-deployment`, { |
| 215 | method: "POST", |
| 216 | headers: { |
| 217 | "Content-Type": "application/json", |
| 218 | }, |
| 219 | }); |
| 220 | if (resp.ok) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 221 | info("Deployment removed successfully"); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 222 | store.setMode("edit"); |
| 223 | } else { |
| 224 | const errorData = await resp.json(); |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 225 | error("Failed to remove deployment", errorData.error || "Unknown error"); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 226 | } |
| 227 | } catch (e) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 228 | error("Failed to remove deployment", e instanceof Error ? e.message : undefined); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 229 | } finally { |
| 230 | setReloading(false); |
| 231 | } |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 232 | }, [projectId, info, error, store]); |
| 233 | const [deployProps, setDeployProps] = useState<{ loading?: boolean; disabled?: boolean }>({ |
| 234 | loading: false, |
| 235 | disabled: false, |
| 236 | }); |
| 237 | const [reloadProps, setReloadProps] = useState<{ loading?: boolean; disabled?: boolean }>({ |
| 238 | loading: false, |
| 239 | disabled: false, |
| 240 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 241 | useEffect(() => { |
| 242 | if (loading) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 243 | setDeployProps({ loading: true, disabled: true }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 244 | } else if (ok) { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 245 | setDeployProps({ disabled: false }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 246 | } else { |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 247 | setDeployProps({ disabled: true }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 248 | } |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 249 | |
| 250 | if (reloading) { |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 251 | setReloadProps({ loading: true, disabled: true }); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 252 | } else { |
| 253 | setReloadProps({ disabled: projectId === undefined }); |
| 254 | } |
| 255 | }, [ok, loading, reloading, projectId]); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 256 | if (store.mode === "deploy") { |
| 257 | return ( |
| 258 | <div className="flex flex-row gap-1 items-center"> |
| 259 | <Button onClick={edit} {...reloadProps}> |
| 260 | Edit |
| 261 | </Button> |
| 262 | <DropdownMenu> |
| 263 | <DropdownMenuTrigger> |
| 264 | <Menu className="rounded-md bg-gray-200 opacity-50" /> |
| 265 | </DropdownMenuTrigger> |
| 266 | <DropdownMenuContent className="w-56"> |
| 267 | <DropdownMenuGroup> |
| 268 | <DropdownMenuItem |
| 269 | onClick={reload} |
| 270 | className="cursor-pointer hover:bg-gray-200" |
| 271 | {...reloadProps} |
| 272 | > |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 273 | {reloadProps.loading ? ( |
| 274 | <> |
| 275 | <LoaderCircle className="animate-spin" /> |
| 276 | Reloading... |
| 277 | </> |
| 278 | ) : ( |
| 279 | "Reload services" |
| 280 | )} |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 281 | </DropdownMenuItem> |
| 282 | <DropdownMenuItem |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 283 | onClick={removeDeployment} |
| 284 | disabled={projectId === undefined} |
| 285 | className="cursor-pointer hover:bg-gray-200" |
| 286 | > |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 287 | Remove deployment |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 288 | </DropdownMenuItem> |
| 289 | <DropdownMenuItem |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 290 | onClick={deleteProject} |
| 291 | disabled={projectId === undefined} |
| 292 | className="cursor-pointer hover:bg-gray-200" |
| 293 | > |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 294 | Delete project |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 295 | </DropdownMenuItem> |
| 296 | </DropdownMenuGroup> |
| 297 | </DropdownMenuContent> |
| 298 | </DropdownMenu> |
| 299 | </div> |
| 300 | ); |
| 301 | } else { |
| 302 | return ( |
| 303 | <div className="flex flex-row gap-1 items-center"> |
| 304 | <Button onClick={deploy} {...deployProps}> |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 305 | {deployProps.loading ? ( |
| 306 | <> |
| 307 | <LoaderCircle className="animate-spin" /> |
| 308 | Deploying... |
| 309 | </> |
| 310 | ) : ( |
| 311 | "Deploy" |
| 312 | )} |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 313 | </Button> |
| 314 | <Button onClick={save}>Save</Button> |
| 315 | <DropdownMenu> |
| 316 | <DropdownMenuTrigger> |
| 317 | <Menu /> |
| 318 | </DropdownMenuTrigger> |
| 319 | <DropdownMenuContent className="w-56"> |
| 320 | <DropdownMenuGroup> |
| 321 | <DropdownMenuItem |
| 322 | onClick={restoreSaved} |
| 323 | disabled={projectId === undefined} |
| 324 | className="cursor-pointer hover:bg-gray-200" |
| 325 | > |
| 326 | Restore |
| 327 | </DropdownMenuItem> |
| 328 | <DropdownMenuItem onClick={clear} className="cursor-pointer hover:bg-gray-200"> |
| 329 | Clear |
| 330 | </DropdownMenuItem> |
| 331 | <DropdownMenuItem |
| 332 | onClick={deleteProject} |
| 333 | disabled={projectId === undefined} |
| 334 | className="cursor-pointer hover:bg-gray-200" |
| 335 | > |
| gio | e2b955a | 2025-05-15 15:41:05 +0000 | [diff] [blame^] | 336 | Delete project |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 337 | </DropdownMenuItem> |
| 338 | </DropdownMenuGroup> |
| 339 | </DropdownMenuContent> |
| 340 | </DropdownMenu> |
| 341 | </div> |
| 342 | ); |
| 343 | } |
| gio | f8acc61 | 2025-04-26 08:20:55 +0400 | [diff] [blame] | 344 | } |