| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1 | import "@xyflow/react/dist/style.css"; |
| 2 | import { |
| 3 | ReactFlow, |
| 4 | Background, |
| 5 | Controls, |
| 6 | Connection, |
| 7 | BackgroundVariant, |
| 8 | Edge, |
| 9 | useReactFlow, |
| 10 | Panel, |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 11 | useStoreApi, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 12 | } from "@xyflow/react"; |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 13 | import { useStateStore, AppState, useZoom } from "@/lib/state"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 14 | import { useShallow } from "zustand/react/shallow"; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 15 | import { useCallback, useEffect, useMemo } from "react"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 16 | import { NodeGatewayHttps } from "@/components/node-gateway-https"; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 17 | import { NodeApp } from "@/components/node-app"; |
| 18 | import { NodeVolume } from "./node-volume"; |
| 19 | import { NodePostgreSQL } from "./node-postgresql"; |
| 20 | import { NodeMongoDB } from "./node-mongodb"; |
| 21 | import { NodeGithub } from "./node-github"; |
| 22 | import { Actions } from "./actions"; |
| 23 | import { NodeGatewayTCP } from "./node-gateway-tcp"; |
| 24 | import { NodeNetwork } from "./node-network"; |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 25 | import { AppNode } from "config"; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 26 | |
| 27 | const selector = (state: AppState) => ({ |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 28 | nodes: state.nodes, |
| 29 | edges: state.edges, |
| 30 | onNodesChange: state.onNodesChange, |
| 31 | onEdgesChange: state.onEdgesChange, |
| 32 | onConnect: state.onConnect, |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 33 | }); |
| 34 | |
| gio | 3d0bf03 | 2025-06-05 06:57:26 +0000 | [diff] [blame] | 35 | export function Canvas({ className }: { className?: string }) { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 36 | const { nodes, edges, onNodesChange, onEdgesChange, onConnect } = useStateStore(useShallow(selector)); |
| 37 | const store = useStateStore(); |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 38 | const instance = useReactFlow(); |
| 39 | const flow = useStoreApi(); |
| gio | 359a685 | 2025-05-14 03:38:24 +0000 | [diff] [blame] | 40 | const viewport = useZoom(); |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 41 | useEffect(() => { |
| 42 | store.setViewport({ |
| 43 | width: flow.getState().width, |
| 44 | height: flow.getState().height, |
| 45 | transformX: flow.getState().transform[0], |
| 46 | transformY: flow.getState().transform[1], |
| 47 | transformZoom: flow.getState().transform[2], |
| 48 | }); |
| 49 | }, [store, flow]); |
| gio | 359a685 | 2025-05-14 03:38:24 +0000 | [diff] [blame] | 50 | useEffect(() => { |
| 51 | instance.setViewport(viewport); |
| 52 | }, [viewport, instance]); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 53 | const nodeTypes = useMemo( |
| 54 | () => ({ |
| 55 | network: NodeNetwork, |
| 56 | app: NodeApp, |
| 57 | "gateway-https": NodeGatewayHttps, |
| 58 | "gateway-tcp": NodeGatewayTCP, |
| 59 | volume: NodeVolume, |
| 60 | postgresql: NodePostgreSQL, |
| 61 | mongodb: NodeMongoDB, |
| 62 | github: NodeGithub, |
| 63 | }), |
| 64 | [], |
| 65 | ); |
| 66 | const isValidConnection = useCallback( |
| 67 | (c: Edge | Connection) => { |
| 68 | if (c.source === c.target) { |
| 69 | return false; |
| 70 | } |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 71 | const sn = instance.getNode(c.source)! as AppNode; |
| 72 | const tn = instance.getNode(c.target)! as AppNode; |
| gio | 6914832 | 2025-06-19 23:16:12 +0400 | [diff] [blame] | 73 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 74 | if (sn.type === "github") { |
| 75 | return c.targetHandle === "repository"; |
| 76 | } |
| 77 | if (sn.type === "app") { |
| 78 | if (c.sourceHandle === "ports" && (!sn.data.ports || sn.data.ports.length === 0)) { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | if (tn.type === "gateway-https") { |
| 83 | if (c.targetHandle === "https" && tn.data.https !== undefined) { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | if (sn.type === "volume") { |
| 88 | if (c.targetHandle !== "volume") { |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | if (tn.type === "network") { |
| 94 | if (c.sourceHandle !== "subdomain") { |
| 95 | return false; |
| 96 | } |
| 97 | if (sn.type !== "gateway-https" && sn.type !== "gateway-tcp") { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | return true; |
| 102 | }, |
| gio | af8db83 | 2025-05-13 14:43:05 +0000 | [diff] [blame] | 103 | [instance], |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 104 | ); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 105 | return ( |
| gio | 3d0bf03 | 2025-06-05 06:57:26 +0000 | [diff] [blame] | 106 | <div style={{ width: "100%", height: "100%" }} className={className}> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 107 | <ReactFlow |
| 108 | nodeTypes={nodeTypes} |
| 109 | nodes={nodes} |
| 110 | edges={edges} |
| 111 | onNodesChange={onNodesChange} |
| 112 | onEdgesChange={onEdgesChange} |
| 113 | onConnect={onConnect} |
| 114 | isValidConnection={isValidConnection} |
| 115 | fitView |
| 116 | proOptions={{ hideAttribution: true }} |
| 117 | > |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 118 | <Controls showInteractive={false} /> |
| 119 | <Background |
| 120 | variant={store.mode === "deploy" ? BackgroundVariant.Dots : BackgroundVariant.Lines} |
| 121 | gap={12} |
| 122 | size={1} |
| 123 | /> |
| gio | 3d0bf03 | 2025-06-05 06:57:26 +0000 | [diff] [blame] | 124 | <Panel position="top-right"> |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 125 | <Actions /> |
| 126 | </Panel> |
| 127 | </ReactFlow> |
| 128 | </div> |
| 129 | ); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | export default Canvas; |