"use client";
import { useMemo } from "react";
import {
Background,
BackgroundVariant,
Controls,
Handle,
MarkerType,
Position,
ReactFlow,
ReactFlowProvider,
type Edge,
type Node,
type NodeProps,
} from "@xyflow/react";
import "@xyflow/react/dist/style.css";
import { cn } from "@/lib/utils";
import { ROOM_STATE_DOT, ROOM_STATE_LABEL, roomState, type InteriorRoomView } from "./types";
/**
* Read-only room graph. Node setup mirrors FrontendTechTreeCanvas: a provider
* wrapper, fixed node boxes, and every interaction that implies editing
* (drag, connect, select) disabled. Positions come from authored graphX/graphY
* or the hotspot center, scaled from the 0-100 authoring space to pixels.
*/
const NODE_WIDTH = 200;
const NODE_HEIGHT = 76;
const SCALE_X = 12;
const SCALE_Y = 8;
function RoomNode({ data }: NodeProps) {
const { room } = data as { room: InteriorRoomView };
const state = roomState(room);
return (
Room // {room.key}
{room.name}
{state === "sealed" ? "Sealed" : `Level ${room.runtime.level} / ${room.maxLevel}`}
);
}
const nodeTypes = { roomNode: RoomNode };
/** Producer -> consumer edges where an enabled resource output feeds an enabled input. */
function buildEdges(rooms: InteriorRoomView[]): Edge[] {
const edges: Edge[] = [];
const seen = new Set();
const stations = rooms.filter((room) => room.station?.enabled);
for (const consumer of stations) {
for (const recipe of consumer.station!.recipes) {
if (!recipe.enabled) continue;
for (const input of recipe.inputs) {
for (const producer of stations) {
if (producer.key === consumer.key) continue;
const feeds = producer.station!.recipes.some(
(producerRecipe) =>
producerRecipe.enabled &&
producerRecipe.outputs.some(
(output) => output.kind === "resource" && output.id === input.resourceId,
),
);
if (!feeds) continue;
const id = `feed-${producer.key}-${consumer.key}`;
if (seen.has(id)) continue;
seen.add(id);
edges.push({
id,
source: producer.key,
target: consumer.key,
type: "smoothstep",
style: { stroke: "rgba(34,211,238,0.55)", strokeWidth: 1.5 },
markerEnd: { type: MarkerType.ArrowClosed, color: "rgba(34,211,238,0.75)" },
});
}
}
}
}
return edges;
}
function buildNodes(rooms: InteriorRoomView[]): Node[] {
return rooms.map((room) => ({
id: room.key,
type: "roomNode",
position: {
x: room.graphPosition.x * SCALE_X - NODE_WIDTH / 2,
y: room.graphPosition.y * SCALE_Y - NODE_HEIGHT / 2,
},
style: { width: NODE_WIDTH, height: NODE_HEIGHT },
draggable: false,
selectable: false,
deletable: false,
data: { room },
}));
}
interface InteriorGraphTabProps {
rooms: InteriorRoomView[];
onSelect: (roomKey: string) => void;
}
function GraphInner({ rooms, onSelect }: InteriorGraphTabProps) {
const nodes = useMemo(() => buildNodes(rooms), [rooms]);
const edges = useMemo(() => buildEdges(rooms), [rooms]);
return (
onSelect(node.id)}
>
);
}
export function InteriorGraphTab({ rooms, onSelect }: InteriorGraphTabProps) {
return (
);
}