blob: 5e9315bc06df642265fa1828e56b7b6753e9a0fc [file] [log] [blame]
gio5f2f1002025-03-20 18:38:48 +04001import { Category, defaultCategories } from "./categories";
2import { CreateValidators, Validator } from "./config";
gioa71316d2025-05-24 09:41:36 +04003import { GitHubService, GitHubServiceImpl, GitHubRepository } from "./github";
gioc31bf142025-06-16 07:48:20 +00004import type { Edge, OnConnect, OnEdgesChange, OnNodesChange, Viewport as ReactFlowViewport } from "@xyflow/react";
gioaf8db832025-05-13 14:43:05 +00005import {
6 addEdge,
7 applyEdgeChanges,
8 applyNodeChanges,
9 Connection,
10 EdgeChange,
11 useNodes,
12 XYPosition,
13} from "@xyflow/react";
giod0026612025-05-08 13:00:36 +000014import type { DeepPartial } from "react-hook-form";
15import { v4 as uuidv4 } from "uuid";
giod0026612025-05-08 13:00:36 +000016import { create } from "zustand";
gio74c6f752025-07-05 04:10:58 +000017import { AppNode, Env, NodeType, VolumeNode, GatewayTCPData, envSchema, AgentAccess } from "config";
gio5f2f1002025-03-20 18:38:48 +040018
19export function nodeLabel(n: AppNode): string {
gio48fde052025-05-14 09:48:08 +000020 try {
21 switch (n.type) {
22 case "network":
23 return n.data.domain;
24 case "app":
25 return n.data.label || "Service";
26 case "github":
27 return n.data.repository?.fullName || "Github";
28 case "gateway-https": {
gio29050d62025-05-16 04:49:26 +000029 if (n.data && n.data.subdomain) {
30 return `${n.data.subdomain}`;
gio48fde052025-05-14 09:48:08 +000031 } else {
32 return "HTTPS Gateway";
33 }
giod0026612025-05-08 13:00:36 +000034 }
gio48fde052025-05-14 09:48:08 +000035 case "gateway-tcp": {
gio29050d62025-05-16 04:49:26 +000036 if (n.data && n.data.subdomain) {
37 return `${n.data.subdomain}`;
gio48fde052025-05-14 09:48:08 +000038 } else {
39 return "TCP Gateway";
40 }
giod0026612025-05-08 13:00:36 +000041 }
gio48fde052025-05-14 09:48:08 +000042 case "mongodb":
43 return n.data.label || "MongoDB";
44 case "postgresql":
45 return n.data.label || "PostgreSQL";
46 case "volume":
47 return n.data.label || "Volume";
48 case undefined:
gio69148322025-06-19 23:16:12 +040049 throw new Error(`nodeLabel: Node type is undefined. Node ID: ${n.id}, Data: ${JSON.stringify(n.data)}`);
giod0026612025-05-08 13:00:36 +000050 }
gio48fde052025-05-14 09:48:08 +000051 } catch (e) {
52 console.error("opaa", e);
53 } finally {
54 console.log("done");
giod0026612025-05-08 13:00:36 +000055 }
gioa1efbad2025-05-21 07:16:45 +000056 return "Unknown Node";
gio5f2f1002025-03-20 18:38:48 +040057}
58
gio8fad76a2025-05-22 14:01:23 +000059export function nodeLabelFull(n: AppNode): string {
60 if (n.type === "gateway-https") {
61 return `https://${n.data.subdomain}.${n.data.network}`;
62 } else {
63 return nodeLabel(n);
64 }
65}
66
gio5f2f1002025-03-20 18:38:48 +040067export function nodeIsConnectable(n: AppNode, handle: string): boolean {
giod0026612025-05-08 13:00:36 +000068 switch (n.type) {
69 case "network":
70 return true;
71 case "app":
72 if (handle === "ports") {
73 return n.data !== undefined && n.data.ports !== undefined && n.data.ports.length > 0;
74 } else if (handle === "repository") {
75 if (!n.data || !n.data.repository || !n.data.repository.id) {
76 return true;
77 }
78 return false;
79 }
80 return false;
81 case "github":
82 if (n.data.repository?.id !== undefined) {
83 return true;
84 }
85 return false;
86 case "gateway-https":
87 if (handle === "subdomain") {
88 return n.data.network === undefined;
89 }
90 return n.data === undefined || n.data.https === undefined;
91 case "gateway-tcp":
92 if (handle === "subdomain") {
93 return n.data.network === undefined;
94 }
95 return true;
96 case "mongodb":
97 return true;
98 case "postgresql":
99 return true;
100 case "volume":
101 if (n.data === undefined || n.data.type === undefined) {
102 return false;
103 }
104 if (n.data.type === "ReadWriteOnce" || n.data.type === "ReadWriteOncePod") {
105 return n.data.attachedTo === undefined || n.data.attachedTo.length === 0;
106 }
107 return true;
108 case undefined:
gio69148322025-06-19 23:16:12 +0400109 throw new Error(
110 `nodeIsConnectable: Node type is undefined. Node ID: ${n.id}, Handle: ${handle}, Data: ${JSON.stringify(n.data)}`,
111 );
giod0026612025-05-08 13:00:36 +0000112 }
gio5f2f1002025-03-20 18:38:48 +0400113}
114
giob41ecae2025-04-24 08:46:50 +0000115export function nodeEnvVarNamePort(n: AppNode, portName: string): string {
giod0026612025-05-08 13:00:36 +0000116 return `DODO_SERVICE_${n.data.label.toUpperCase()}_ADDRESS_${portName.toUpperCase()}`;
giob41ecae2025-04-24 08:46:50 +0000117}
118
gio5f2f1002025-03-20 18:38:48 +0400119export function nodeEnvVarNames(n: AppNode): string[] {
giod0026612025-05-08 13:00:36 +0000120 switch (n.type) {
121 case "app":
122 return [
123 `DODO_SERVICE_${n.data.label.toUpperCase()}_ADDRESS`,
124 ...(n.data.ports || []).map((p) => nodeEnvVarNamePort(n, p.name)),
125 ];
126 case "github":
127 return [];
128 case "gateway-https":
129 return [];
130 case "gateway-tcp":
131 return [];
132 case "mongodb":
133 return [`DODO_MONGODB_${n.data.label.toUpperCase()}_URL`];
134 case "postgresql":
135 return [`DODO_POSTGRESQL_${n.data.label.toUpperCase()}_URL`];
136 case "volume":
137 return [`DODO_VOLUME_${n.data.label.toUpperCase()}_PATH`];
138 case undefined:
139 throw new Error("MUST NOT REACH");
140 default:
141 throw new Error("MUST NOT REACH");
142 }
gio5f2f1002025-03-20 18:38:48 +0400143}
144
gio5f2f1002025-03-20 18:38:48 +0400145export type MessageType = "INFO" | "WARNING" | "FATAL";
146
147export type Message = {
giod0026612025-05-08 13:00:36 +0000148 id: string;
149 type: MessageType;
150 nodeId?: string;
151 message: string;
152 onHighlight?: (state: AppState) => void;
153 onLooseHighlight?: (state: AppState) => void;
154 onClick?: (state: AppState) => void;
gio5f2f1002025-03-20 18:38:48 +0400155};
156
gio7f98e772025-05-07 11:00:14 +0000157const defaultEnv: Env = {
gioa71316d2025-05-24 09:41:36 +0400158 deployKeyPublic: undefined,
159 instanceId: undefined,
giod0026612025-05-08 13:00:36 +0000160 networks: [],
161 integrations: {
162 github: false,
gio69148322025-06-19 23:16:12 +0400163 gemini: false,
gio69ff7592025-07-03 06:27:21 +0000164 anthropic: false,
giod0026612025-05-08 13:00:36 +0000165 },
gio3a921b82025-05-10 07:36:09 +0000166 services: [],
gio3ed59592025-05-14 16:51:09 +0000167 user: {
168 id: "",
169 username: "",
170 },
giob77cb932025-05-19 09:37:14 +0000171 access: [],
gio7f98e772025-05-07 11:00:14 +0000172};
173
gio5f2f1002025-03-20 18:38:48 +0400174export type Project = {
giod0026612025-05-08 13:00:36 +0000175 id: string;
176 name: string;
177};
gio5f2f1002025-03-20 18:38:48 +0400178
gio7f98e772025-05-07 11:00:14 +0000179export type IntegrationsConfig = {
giod0026612025-05-08 13:00:36 +0000180 github: boolean;
gio7f98e772025-05-07 11:00:14 +0000181};
182
183type NodeUpdate<T extends NodeType> = DeepPartial<Extract<AppNode, { type: T }>>;
184type NodeDataUpdate<T extends NodeType> = DeepPartial<Extract<AppNode, { type: T }>["data"]>;
185
gioaf8db832025-05-13 14:43:05 +0000186type Viewport = {
187 transformX: number;
188 transformY: number;
189 transformZoom: number;
190 width: number;
191 height: number;
192};
193
gio918780d2025-05-22 08:24:41 +0000194let refreshEnvIntervalId: number | null = null;
195
gio5f2f1002025-03-20 18:38:48 +0400196export type AppState = {
giod0026612025-05-08 13:00:36 +0000197 projectId: string | undefined;
gio818da4e2025-05-12 14:45:35 +0000198 mode: "edit" | "deploy";
giod0026612025-05-08 13:00:36 +0000199 projects: Project[];
200 nodes: AppNode[];
201 edges: Edge[];
gio359a6852025-05-14 03:38:24 +0000202 zoom: ReactFlowViewport;
giod0026612025-05-08 13:00:36 +0000203 categories: Category[];
204 messages: Message[];
205 env: Env;
gioaf8db832025-05-13 14:43:05 +0000206 viewport: Viewport;
207 setViewport: (viewport: Viewport) => void;
giod0026612025-05-08 13:00:36 +0000208 githubService: GitHubService | null;
gioa71316d2025-05-24 09:41:36 +0400209 githubRepositories: GitHubRepository[];
210 githubRepositoriesLoading: boolean;
211 githubRepositoriesError: string | null;
gio8a5f12f2025-07-05 07:02:31 +0000212 stateEventSource: EventSource | null;
giod0026612025-05-08 13:00:36 +0000213 setHighlightCategory: (name: string, active: boolean) => void;
214 onNodesChange: OnNodesChange<AppNode>;
215 onEdgesChange: OnEdgesChange;
216 onConnect: OnConnect;
gioaf8db832025-05-13 14:43:05 +0000217 addNode: (node: Omit<AppNode, "position">) => void;
giod0026612025-05-08 13:00:36 +0000218 setNodes: (nodes: AppNode[]) => void;
219 setEdges: (edges: Edge[]) => void;
gio818da4e2025-05-12 14:45:35 +0000220 setProject: (projectId: string | undefined) => Promise<void>;
221 setMode: (mode: "edit" | "deploy") => void;
giod0026612025-05-08 13:00:36 +0000222 updateNode: <T extends NodeType>(id: string, node: NodeUpdate<T>) => void;
223 updateNodeData: <T extends NodeType>(id: string, data: NodeDataUpdate<T>) => void;
224 replaceEdge: (c: Connection, id?: string) => void;
225 refreshEnv: () => Promise<void>;
gioa71316d2025-05-24 09:41:36 +0400226 fetchGithubRepositories: () => Promise<void>;
gio5f2f1002025-03-20 18:38:48 +0400227};
228
229const projectIdSelector = (state: AppState) => state.projectId;
230const categoriesSelector = (state: AppState) => state.categories;
231const messagesSelector = (state: AppState) => state.messages;
232const envSelector = (state: AppState) => state.env;
gio359a6852025-05-14 03:38:24 +0000233const zoomSelector = (state: AppState) => state.zoom;
gioa71316d2025-05-24 09:41:36 +0400234const githubRepositoriesSelector = (state: AppState) => state.githubRepositories;
235const githubRepositoriesLoadingSelector = (state: AppState) => state.githubRepositoriesLoading;
236const githubRepositoriesErrorSelector = (state: AppState) => state.githubRepositoriesError;
gioaf8db832025-05-13 14:43:05 +0000237
gio359a6852025-05-14 03:38:24 +0000238export function useZoom(): ReactFlowViewport {
239 return useStateStore(zoomSelector);
gioaf8db832025-05-13 14:43:05 +0000240}
gio5f2f1002025-03-20 18:38:48 +0400241
242export function useProjectId(): string | undefined {
giod0026612025-05-08 13:00:36 +0000243 return useStateStore(projectIdSelector);
gio5f2f1002025-03-20 18:38:48 +0400244}
245
giob45b1862025-05-20 11:42:20 +0000246export function useSetProject(): (projectId: string | undefined) => void {
247 return useStateStore((state) => state.setProject);
248}
249
gio5f2f1002025-03-20 18:38:48 +0400250export function useCategories(): Category[] {
giod0026612025-05-08 13:00:36 +0000251 return useStateStore(categoriesSelector);
gio5f2f1002025-03-20 18:38:48 +0400252}
253
254export function useMessages(): Message[] {
giod0026612025-05-08 13:00:36 +0000255 return useStateStore(messagesSelector);
gio5f2f1002025-03-20 18:38:48 +0400256}
257
258export function useNodeMessages(id: string): Message[] {
giod0026612025-05-08 13:00:36 +0000259 return useMessages().filter((m) => m.nodeId === id);
gio5f2f1002025-03-20 18:38:48 +0400260}
261
262export function useNodeLabel(id: string): string {
giod0026612025-05-08 13:00:36 +0000263 return nodeLabel(useNodes<AppNode>().find((n) => n.id === id)!);
gio5f2f1002025-03-20 18:38:48 +0400264}
265
266export function useNodePortName(id: string, portId: string): string {
giod0026612025-05-08 13:00:36 +0000267 return (useNodes<AppNode>().find((n) => n.id === id)!.data.ports || []).find((p) => p.id === portId)!.name;
gio5f2f1002025-03-20 18:38:48 +0400268}
269
gio5f2f1002025-03-20 18:38:48 +0400270export function useEnv(): Env {
giod0026612025-05-08 13:00:36 +0000271 return useStateStore(envSelector);
gio7f98e772025-05-07 11:00:14 +0000272}
273
gio74c6f752025-07-05 04:10:58 +0000274export function useAgents(): AgentAccess[] {
giocc5ce582025-06-25 07:45:21 +0400275 return useStateStore(envSelector).access.filter(
gio74c6f752025-07-05 04:10:58 +0000276 (acc): acc is AgentAccess => acc.type === "https" && acc.agentName != null,
giocc5ce582025-06-25 07:45:21 +0400277 );
278}
279
gio74c6f752025-07-05 04:10:58 +0000280export function useLeadAgent(): AgentAccess | undefined {
281 const agents = useAgents();
282 return agents.find((a) => a.agentName === "dodo") || agents[0];
283}
284
gio69148322025-06-19 23:16:12 +0400285export function useGithubService(): boolean {
286 return useStateStore(envSelector).integrations.github;
287}
288
289export function useGeminiService(): boolean {
290 return useStateStore(envSelector).integrations.gemini;
gio5f2f1002025-03-20 18:38:48 +0400291}
292
gio69ff7592025-07-03 06:27:21 +0000293export function useAnthropicService(): boolean {
294 return useStateStore(envSelector).integrations.anthropic;
295}
296
gioa71316d2025-05-24 09:41:36 +0400297export function useGithubRepositories(): GitHubRepository[] {
298 return useStateStore(githubRepositoriesSelector);
299}
300
301export function useGithubRepositoriesLoading(): boolean {
302 return useStateStore(githubRepositoriesLoadingSelector);
303}
304
305export function useGithubRepositoriesError(): string | null {
306 return useStateStore(githubRepositoriesErrorSelector);
307}
308
309export function useFetchGithubRepositories(): () => Promise<void> {
310 return useStateStore((state) => state.fetchGithubRepositories);
311}
312
gio3ec94242025-05-16 12:46:57 +0000313export function useMode(): "edit" | "deploy" {
314 return useStateStore((state) => state.mode);
315}
316
gio5f2f1002025-03-20 18:38:48 +0400317const v: Validator = CreateValidators();
318
gioaf8db832025-05-13 14:43:05 +0000319function getRandomPosition({ width, height, transformX, transformY, transformZoom }: Viewport): XYPosition {
320 const zoomMultiplier = 1 / transformZoom;
321 const realWidth = width * zoomMultiplier;
322 const realHeight = height * zoomMultiplier;
323 const paddingMultiplier = 0.8;
324 const ret = {
325 x: -transformX * zoomMultiplier + Math.random() * realWidth * paddingMultiplier,
326 y: -transformY * zoomMultiplier + Math.random() * realHeight * paddingMultiplier,
327 };
328 return ret;
329}
330
gio3d0bf032025-06-05 06:57:26 +0000331export const useStateStore = create<AppState>((setOg, get): AppState => {
332 const set = (state: Partial<AppState>) => {
333 setOg(state);
334 };
giod0026612025-05-08 13:00:36 +0000335 const setN = (nodes: AppNode[]) => {
gio34193052025-07-03 03:55:11 +0000336 const env = get().env;
337 console.log("---env", env);
gio4b9b58a2025-05-12 11:46:08 +0000338 set({
giod0026612025-05-08 13:00:36 +0000339 nodes,
gio34193052025-07-03 03:55:11 +0000340 messages: v(nodes, env),
gio4b9b58a2025-05-12 11:46:08 +0000341 });
342 };
343
gio918780d2025-05-22 08:24:41 +0000344 const startRefreshEnvInterval = () => {
345 if (refreshEnvIntervalId) {
346 clearInterval(refreshEnvIntervalId);
347 }
348 if (get().projectId && typeof document !== "undefined" && document.visibilityState === "visible") {
349 console.log("Starting refreshEnv interval for project:", get().projectId);
350 refreshEnvIntervalId = setInterval(async () => {
351 if (get().projectId && typeof document !== "undefined" && document.visibilityState === "visible") {
352 console.log("Interval: Calling refreshEnv for project:", get().projectId);
353 await get().refreshEnv();
354 } else if (refreshEnvIntervalId) {
355 console.log(
356 "Interval: Conditions not met (project removed or tab hidden), stopping interval from inside.",
357 );
358 clearInterval(refreshEnvIntervalId);
359 refreshEnvIntervalId = null;
360 }
361 }, 5000) as unknown as number;
362 } else {
363 console.log(
364 "Not starting refreshEnv interval. Project ID:",
365 get().projectId,
366 "Visibility:",
367 typeof document !== "undefined" ? document.visibilityState : "SSR",
368 );
369 }
370 };
371
372 const stopRefreshEnvInterval = () => {
373 if (refreshEnvIntervalId) {
374 console.log("Stopping refreshEnv interval for project:", get().projectId);
375 clearInterval(refreshEnvIntervalId);
376 refreshEnvIntervalId = null;
377 }
378 };
379
380 if (typeof document !== "undefined") {
381 document.addEventListener("visibilitychange", () => {
382 if (document.visibilityState === "visible") {
383 console.log("Tab became visible, attempting to start refreshEnv interval.");
384 startRefreshEnvInterval();
385 } else {
386 console.log("Tab became hidden, stopping refreshEnv interval.");
387 stopRefreshEnvInterval();
388 }
389 });
390 }
391
gio48fde052025-05-14 09:48:08 +0000392 const injectNetworkNodes = () => {
393 const newNetworks = get().env.networks.filter(
394 (x) => !get().nodes.some((n) => n.type === "network" && n.data.domain === x.domain),
395 );
396 newNetworks.forEach((n) => {
397 get().addNode({
398 id: n.domain,
399 type: "network",
400 connectable: true,
401 data: {
402 domain: n.domain,
403 label: n.domain,
404 envVars: [],
405 ports: [],
406 state: "success", // TODO(gio): monitor network health
407 },
408 });
409 console.log("added network", n.domain);
410 });
411 };
412
giod0026612025-05-08 13:00:36 +0000413 function updateNodeData<T extends NodeType>(id: string, data: NodeDataUpdate<T>): void {
414 setN(
415 get().nodes.map((n) => {
416 if (n.id === id) {
417 return {
418 ...n,
419 data: {
420 ...n.data,
421 ...data,
422 },
423 } as Extract<AppNode, { type: T }>;
424 }
425 return n;
426 }),
427 );
428 }
gio7f98e772025-05-07 11:00:14 +0000429
giod0026612025-05-08 13:00:36 +0000430 function updateNode<T extends NodeType>(id: string, node: NodeUpdate<T>): void {
431 setN(
432 get().nodes.map((n) => {
433 if (n.id === id) {
434 return {
435 ...n,
436 ...node,
437 } as Extract<AppNode, { type: T }>;
438 }
439 return n;
440 }),
441 );
442 }
gio7f98e772025-05-07 11:00:14 +0000443
giod0026612025-05-08 13:00:36 +0000444 function onConnect(c: Connection) {
445 const { nodes, edges } = get();
446 set({
447 edges: addEdge(c, edges),
448 });
449 const sn = nodes.filter((n) => n.id === c.source)[0]!;
450 const tn = nodes.filter((n) => n.id === c.target)[0]!;
451 if (tn.type === "network") {
452 if (sn.type === "gateway-https") {
453 updateNodeData<"gateway-https">(sn.id, {
454 network: tn.data.domain,
455 });
456 } else if (sn.type === "gateway-tcp") {
457 updateNodeData<"gateway-tcp">(sn.id, {
458 network: tn.data.domain,
459 });
460 }
461 }
462 if (tn.type === "app") {
463 if (c.sourceHandle === "env_var" && c.targetHandle === "env_var") {
464 const sourceEnvVars = nodeEnvVarNames(sn);
465 if (sourceEnvVars.length === 0) {
gio69148322025-06-19 23:16:12 +0400466 throw new Error(
467 `onConnect (env_var): Source node ${sn.id} (type: ${sn.type}) has no env vars to connect from.`,
468 );
giod0026612025-05-08 13:00:36 +0000469 }
470 const id = uuidv4();
471 if (sourceEnvVars.length === 1) {
472 updateNode<"app">(c.target, {
473 ...tn,
474 data: {
475 ...tn.data,
476 envVars: [
477 ...(tn.data.envVars || []),
478 {
479 id: id,
480 source: c.source,
481 name: sourceEnvVars[0],
482 isEditting: false,
483 },
484 ],
485 },
486 });
487 } else {
488 updateNode<"app">(c.target, {
489 ...tn,
490 data: {
491 ...tn.data,
492 envVars: [
493 ...(tn.data.envVars || []),
494 {
495 id: id,
496 source: c.source,
497 },
498 ],
499 },
500 });
501 }
502 }
503 if (c.sourceHandle === "ports" && c.targetHandle === "env_var") {
504 const sourcePorts = sn.data.ports || [];
505 const id = uuidv4();
506 if (sourcePorts.length === 1) {
507 updateNode<"app">(c.target, {
508 ...tn,
509 data: {
510 ...tn.data,
511 envVars: [
512 ...(tn.data.envVars || []),
513 {
514 id: id,
515 source: c.source,
516 name: nodeEnvVarNamePort(sn, sourcePorts[0].name),
517 portId: sourcePorts[0].id,
518 isEditting: false,
519 },
520 ],
521 },
522 });
523 }
524 }
gio3d0bf032025-06-05 06:57:26 +0000525 if (c.targetHandle === "repository") {
526 const sourceNode = nodes.find((n) => n.id === c.source);
527 if (sourceNode && sourceNode.type === "github" && sourceNode.data.repository) {
528 updateNodeData<"app">(tn.id, {
529 repository: {
530 id: sourceNode.data.repository.id,
531 repoNodeId: c.source,
532 },
533 });
534 }
535 }
giod0026612025-05-08 13:00:36 +0000536 }
537 if (c.sourceHandle === "volume") {
538 updateNodeData<"volume">(c.source, {
539 attachedTo: ((sn as VolumeNode).data.attachedTo || []).concat(c.source),
540 });
541 }
giod0026612025-05-08 13:00:36 +0000542 if (c.targetHandle === "https") {
543 if ((sn.data.ports || []).length === 1) {
544 updateNodeData<"gateway-https">(c.target, {
545 https: {
546 serviceId: c.source,
547 portId: sn.data.ports![0].id,
548 },
549 });
550 } else {
551 updateNodeData<"gateway-https">(c.target, {
552 https: {
553 serviceId: c.source,
554 portId: "", // TODO(gio)
555 },
556 });
557 }
558 }
559 if (c.targetHandle === "tcp") {
560 const td = tn.data as GatewayTCPData;
561 if ((sn.data.ports || []).length === 1) {
562 updateNodeData<"gateway-tcp">(c.target, {
563 exposed: (td.exposed || []).concat({
564 serviceId: c.source,
565 portId: sn.data.ports![0].id,
566 }),
567 });
568 } else {
569 updateNodeData<"gateway-tcp">(c.target, {
570 selected: {
571 serviceId: c.source,
572 portId: undefined,
573 },
574 });
575 }
576 }
577 if (sn.type === "app") {
578 if (c.sourceHandle === "ports") {
579 updateNodeData<"app">(sn.id, {
580 isChoosingPortToConnect: true,
581 });
582 }
583 }
giod0026612025-05-08 13:00:36 +0000584 }
gioa71316d2025-05-24 09:41:36 +0400585
586 const fetchGithubRepositories = async () => {
587 const { githubService, projectId } = get();
588 if (!githubService || !projectId) {
589 set({
590 githubRepositories: [],
591 githubRepositoriesError: "GitHub service or Project ID not available.",
592 githubRepositoriesLoading: false,
593 });
594 return;
595 }
596
597 set({ githubRepositoriesLoading: true, githubRepositoriesError: null });
598 try {
599 const repos = await githubService.getRepositories();
600 set({ githubRepositories: repos, githubRepositoriesLoading: false });
601 } catch (error) {
602 console.error("Failed to fetch GitHub repositories in store:", error);
603 const errorMessage = error instanceof Error ? error.message : "Unknown error fetching repositories";
604 set({ githubRepositories: [], githubRepositoriesError: errorMessage, githubRepositoriesLoading: false });
605 }
606 };
607
gio8a5f12f2025-07-05 07:02:31 +0000608 const disconnectFromStateStream = () => {
609 const { stateEventSource } = get();
610 if (stateEventSource) {
611 stateEventSource.close();
612 set({ stateEventSource: null });
613 }
614 };
615
616 const connectToStateStream = (projectId: string, mode: "deploy" | "edit") => {
617 disconnectFromStateStream();
618
619 const eventSource = new EventSource(
620 `/api/project/${projectId}/state/stream/${mode === "edit" ? "draft" : "deploy"}`,
621 );
622 set({ stateEventSource: eventSource });
623
624 eventSource.onmessage = (event) => {
625 const inst = JSON.parse(event.data);
626 setN(inst.nodes);
627 set({ edges: inst.edges });
628 injectNetworkNodes();
629 if (
630 get().zoom.x !== inst.viewport.x ||
631 get().zoom.y !== inst.viewport.y ||
632 get().zoom.zoom !== inst.viewport.zoom
633 ) {
634 set({ zoom: inst.viewport });
635 }
636 };
637
638 eventSource.onerror = (err) => {
639 console.error("EventSource failed:", err);
640 eventSource.close();
641 set({ stateEventSource: null });
642 };
643 };
644
giod0026612025-05-08 13:00:36 +0000645 return {
646 projectId: undefined,
gio818da4e2025-05-12 14:45:35 +0000647 mode: "edit",
giod0026612025-05-08 13:00:36 +0000648 projects: [],
649 nodes: [],
650 edges: [],
651 categories: defaultCategories,
gio34193052025-07-03 03:55:11 +0000652 messages: [],
giod0026612025-05-08 13:00:36 +0000653 env: defaultEnv,
gioaf8db832025-05-13 14:43:05 +0000654 viewport: {
655 transformX: 0,
656 transformY: 0,
657 transformZoom: 1,
658 width: 800,
659 height: 600,
660 },
gio359a6852025-05-14 03:38:24 +0000661 zoom: {
662 x: 0,
663 y: 0,
664 zoom: 1,
665 },
giod0026612025-05-08 13:00:36 +0000666 githubService: null,
gioa71316d2025-05-24 09:41:36 +0400667 githubRepositories: [],
668 githubRepositoriesLoading: false,
669 githubRepositoriesError: null,
gio8a5f12f2025-07-05 07:02:31 +0000670 stateEventSource: null,
gioaf8db832025-05-13 14:43:05 +0000671 setViewport: (viewport) => {
672 const { viewport: vp } = get();
673 if (
674 viewport.transformX !== vp.transformX ||
675 viewport.transformY !== vp.transformY ||
676 viewport.transformZoom !== vp.transformZoom ||
677 viewport.width !== vp.width ||
678 viewport.height !== vp.height
679 ) {
680 set({ viewport });
681 }
682 },
giod0026612025-05-08 13:00:36 +0000683 setHighlightCategory: (name, active) => {
684 set({
685 categories: get().categories.map((c) => {
686 if (c.title.toLowerCase() !== name.toLowerCase()) {
687 return c;
688 } else {
689 return {
690 ...c,
691 active,
692 };
693 }
694 }),
695 });
696 },
697 onNodesChange: (changes) => {
698 const nodes = applyNodeChanges(changes, get().nodes);
699 setN(nodes);
700 },
701 onEdgesChange: (changes) => {
702 set({
703 edges: applyEdgeChanges(changes, get().edges),
704 });
705 },
gioaf8db832025-05-13 14:43:05 +0000706 addNode: (node) => {
707 const { viewport, nodes } = get();
708 setN(
709 nodes.concat({
710 ...node,
711 position: getRandomPosition(viewport),
gioa1efbad2025-05-21 07:16:45 +0000712 } as AppNode),
gioaf8db832025-05-13 14:43:05 +0000713 );
714 },
giod0026612025-05-08 13:00:36 +0000715 setNodes: (nodes) => {
716 setN(nodes);
717 },
718 setEdges: (edges) => {
719 set({ edges });
720 },
721 replaceEdge: (c, id) => {
722 let change: EdgeChange;
723 if (id === undefined) {
724 change = {
725 type: "add",
726 item: {
727 id: uuidv4(),
728 ...c,
729 },
730 };
731 onConnect(c);
732 } else {
733 change = {
734 type: "replace",
735 id,
736 item: {
737 id,
738 ...c,
739 },
740 };
741 }
742 set({
743 edges: applyEdgeChanges([change], get().edges),
744 });
745 },
746 updateNode,
747 updateNodeData,
748 onConnect,
749 refreshEnv: async () => {
750 const projectId = get().projectId;
751 let env: Env = defaultEnv;
giod0026612025-05-08 13:00:36 +0000752 try {
753 if (projectId) {
754 const response = await fetch(`/api/project/${projectId}/env`);
755 if (response.ok) {
756 const data = await response.json();
757 const result = envSchema.safeParse(data);
758 if (result.success) {
759 env = result.data;
760 } else {
761 console.error("Invalid env data:", result.error);
762 }
763 }
764 }
765 } catch (error) {
766 console.error("Failed to fetch integrations:", error);
767 } finally {
gioa71316d2025-05-24 09:41:36 +0400768 const oldEnv = get().env;
769 const oldGithubIntegrationStatus = oldEnv.integrations.github;
770 if (JSON.stringify(oldEnv) !== JSON.stringify(env)) {
gio4b9b58a2025-05-12 11:46:08 +0000771 set({ env });
gio48fde052025-05-14 09:48:08 +0000772 injectNetworkNodes();
gioa71316d2025-05-24 09:41:36 +0400773 let ghService = null;
gio4b9b58a2025-05-12 11:46:08 +0000774 if (env.integrations.github) {
gioa71316d2025-05-24 09:41:36 +0400775 ghService = new GitHubServiceImpl(projectId!);
776 }
777 if (get().githubService !== ghService || (ghService && !get().githubService)) {
778 set({ githubService: ghService });
779 }
780 if (
781 ghService &&
782 (oldGithubIntegrationStatus !== env.integrations.github || !oldEnv.integrations.github)
783 ) {
784 get().fetchGithubRepositories();
785 }
786 if (!env.integrations.github) {
787 set({
788 githubRepositories: [],
789 githubRepositoriesError: null,
790 githubRepositoriesLoading: false,
791 });
gio4b9b58a2025-05-12 11:46:08 +0000792 }
giod0026612025-05-08 13:00:36 +0000793 }
794 }
795 },
gio818da4e2025-05-12 14:45:35 +0000796 setMode: (mode) => {
gio8a5f12f2025-07-05 07:02:31 +0000797 disconnectFromStateStream();
gio818da4e2025-05-12 14:45:35 +0000798 set({ mode });
gio8a5f12f2025-07-05 07:02:31 +0000799 const projectId = get().projectId;
800 if (projectId) {
801 connectToStateStream(projectId, mode);
802 }
gio818da4e2025-05-12 14:45:35 +0000803 },
804 setProject: async (projectId) => {
gio918780d2025-05-22 08:24:41 +0000805 const currentProjectId = get().projectId;
806 if (projectId === currentProjectId) {
gio359a6852025-05-14 03:38:24 +0000807 return;
808 }
gio918780d2025-05-22 08:24:41 +0000809 stopRefreshEnvInterval();
gio8a5f12f2025-07-05 07:02:31 +0000810 disconnectFromStateStream();
giod0026612025-05-08 13:00:36 +0000811 set({
812 projectId,
gioa71316d2025-05-24 09:41:36 +0400813 githubRepositories: [],
814 githubRepositoriesLoading: false,
815 githubRepositoriesError: null,
giod0026612025-05-08 13:00:36 +0000816 });
817 if (projectId) {
gio818da4e2025-05-12 14:45:35 +0000818 await get().refreshEnv();
gioa71316d2025-05-24 09:41:36 +0400819 if (get().env.instanceId) {
gio818da4e2025-05-12 14:45:35 +0000820 set({ mode: "deploy" });
821 } else {
822 set({ mode: "edit" });
823 }
gio8a5f12f2025-07-05 07:02:31 +0000824 const mode = get().mode;
825 connectToStateStream(projectId, mode);
gio918780d2025-05-22 08:24:41 +0000826 startRefreshEnvInterval();
gio4b9b58a2025-05-12 11:46:08 +0000827 } else {
828 set({
829 nodes: [],
830 edges: [],
gio918780d2025-05-22 08:24:41 +0000831 env: defaultEnv,
832 githubService: null,
gioa71316d2025-05-24 09:41:36 +0400833 githubRepositories: [],
834 githubRepositoriesLoading: false,
835 githubRepositoriesError: null,
gio4b9b58a2025-05-12 11:46:08 +0000836 });
giod0026612025-05-08 13:00:36 +0000837 }
838 },
gioa71316d2025-05-24 09:41:36 +0400839 fetchGithubRepositories: fetchGithubRepositories,
giod0026612025-05-08 13:00:36 +0000840 };
gio5f2f1002025-03-20 18:38:48 +0400841});