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