| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | import '@xyflow/react/dist/style.css'; |
| 2 | import { ReactFlow, Background, Controls, Connection, BackgroundVariant, Edge, useReactFlow, Panel } from '@xyflow/react'; |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 3 | import { useStateStore, AppState, AppNode, useEnv } from '@/lib/state'; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 4 | import { useShallow } from "zustand/react/shallow"; |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 5 | import { useCallback, useEffect, useMemo } from 'react'; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 6 | import { NodeGatewayHttps } from "@/components/node-gateway-https"; |
| 7 | import { NodeApp } from '@/components/node-app'; |
| 8 | import { NodeVolume } from './node-volume'; |
| 9 | import { NodePostgreSQL } from './node-postgresql'; |
| 10 | import { NodeMongoDB } from './node-mongodb'; |
| 11 | import { NodeGithub } from './node-github'; |
| 12 | import { Actions } from './actions'; |
| 13 | import { NodeGatewayTCP } from './node-gateway-tcp'; |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 14 | import { NodeNetwork } from './node-network'; |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 15 | |
| 16 | const selector = (state: AppState) => ({ |
| 17 | nodes: state.nodes, |
| 18 | edges: state.edges, |
| 19 | onNodesChange: state.onNodesChange, |
| 20 | onEdgesChange: state.onEdgesChange, |
| 21 | onConnect: state.onConnect, |
| 22 | }); |
| 23 | |
| 24 | export function Canvas() { |
| 25 | const { nodes, edges, onNodesChange, onEdgesChange, onConnect } = useStateStore( |
| 26 | useShallow(selector), |
| 27 | ); |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 28 | const store = useStateStore(); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 29 | const flow = useReactFlow(); |
| 30 | const nodeTypes = useMemo(() => ({ |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 31 | "network": NodeNetwork, |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 32 | "app": NodeApp, |
| 33 | "gateway-https": NodeGatewayHttps, |
| 34 | "gateway-tcp": NodeGatewayTCP, |
| 35 | "volume": NodeVolume, |
| 36 | "postgresql": NodePostgreSQL, |
| 37 | "mongodb": NodeMongoDB, |
| 38 | "github": NodeGithub, |
| 39 | }), []); |
| 40 | const isValidConnection = useCallback((c: Edge | Connection) => { |
| 41 | if (c.source === c.target) { |
| 42 | return false; |
| 43 | } |
| 44 | const sn = flow.getNode(c.source)! as AppNode; |
| 45 | const tn = flow.getNode(c.target)! as AppNode; |
| 46 | if (sn.type === "github") { |
| 47 | return c.targetHandle === "repository"; |
| 48 | } |
| 49 | if (sn.type === "app") { |
| 50 | if (c.sourceHandle === "ports" && (!sn.data.ports || sn.data.ports.length === 0)) { |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | if (tn.type === "gateway-https") { |
| 55 | if (c.targetHandle === "https" && tn.data.https !== undefined) { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | if (sn.type === "volume") { |
| 60 | if (c.targetHandle !== "volume") { |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 65 | if (tn.type === "network") { |
| 66 | if (c.sourceHandle !== "subdomain") { |
| 67 | return false; |
| 68 | } |
| 69 | if (sn.type !== "gateway-https" && sn.type !== "gateway-tcp") { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 73 | return true; |
| 74 | }, [flow]); |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 75 | const env = useEnv(); |
| 76 | useEffect(() => { |
| 77 | const networkNodes: AppNode[] = env.networks.map((n) => ({ |
| 78 | id: n.domain, |
| 79 | type: "network", |
| 80 | position: { |
| 81 | x: 0, |
| 82 | y: 0, |
| 83 | }, |
| 84 | isConnectable: true, |
| 85 | data: { |
| 86 | domain: n.domain, |
| 87 | label: n.domain, |
| 88 | envVars: [], |
| 89 | ports: [], |
| gio | da70865 | 2025-04-30 14:57:38 +0400 | [diff] [blame] | 90 | state: "success", // TODO(gio): monitor network health |
| gio | aba9a96 | 2025-04-25 14:19:40 +0000 | [diff] [blame] | 91 | }, |
| 92 | })); |
| 93 | const prevNodes = store.nodes; |
| 94 | const newNodes = networkNodes.concat(prevNodes.filter((n) => n.type !== "network")); |
| 95 | // TODO(gio): actually compare |
| 96 | if (prevNodes.length !== newNodes.length) { |
| 97 | store.setNodes(newNodes); |
| 98 | } |
| 99 | }, [env, store]); |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 100 | return ( |
| 101 | <div style={{ width: '100%', height: '100%' }}> |
| 102 | <ReactFlow |
| 103 | nodeTypes={nodeTypes} |
| 104 | nodes={nodes} |
| 105 | edges={edges} |
| 106 | onNodesChange={onNodesChange} |
| 107 | onEdgesChange={onEdgesChange} |
| 108 | onConnect={onConnect} |
| 109 | isValidConnection={isValidConnection} |
| 110 | fitView |
| 111 | proOptions={{ hideAttribution: true }} |
| 112 | > |
| 113 | <Controls /> |
| 114 | <Background variant={BackgroundVariant.Dots} gap={12} size={1} /> |
| 115 | <Panel position="bottom-right"> |
| 116 | <Actions /> |
| 117 | </Panel> |
| 118 | </ReactFlow> |
| 119 | </div> |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | export default Canvas; |