1
0
Fork 0
polaris-task-force/src/components/frontend/interiors/InteriorGraphTab.tsx

161 lines
5 KiB
TypeScript

"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 (
<div className="flex h-full w-full flex-col justify-between border border-white/15 bg-black/70 px-3 py-2 text-white">
<div className="flex items-center justify-between gap-2">
<span className="truncate font-mono text-[9px] tracking-widest text-white/50 uppercase">
Room // {room.key}
</span>
<span
className={cn("size-2 shrink-0 rounded-full", ROOM_STATE_DOT[state])}
title={ROOM_STATE_LABEL[state]}
/>
</div>
<div className="min-w-0">
<p className="truncate text-sm font-medium">{room.name}</p>
<p className="font-mono text-[10px] tracking-widest text-white/50 uppercase">
{state === "sealed" ? "Sealed" : `Level ${room.runtime.level} / ${room.maxLevel}`}
</p>
</div>
<Handle type="target" position={Position.Left} className="opacity-0" />
<Handle type="source" position={Position.Right} className="opacity-0" />
</div>
);
}
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<string>();
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 (
<div className="relative h-full w-full" style={{ backgroundColor: "#05070a" }}>
<ReactFlow
nodes={nodes}
edges={edges}
nodeTypes={nodeTypes}
fitView
fitViewOptions={{ padding: 0.2, maxZoom: 1 }}
minZoom={0.2}
nodesDraggable={false}
nodesConnectable={false}
elementsSelectable={false}
edgesFocusable={false}
proOptions={{ hideAttribution: false }}
onNodeClick={(_, node) => onSelect(node.id)}
>
<Background
variant={BackgroundVariant.Lines}
gap={46}
size={1}
color="rgba(255,255,255,0.055)"
/>
<Controls showInteractive={false} />
</ReactFlow>
</div>
);
}
export function InteriorGraphTab({ rooms, onSelect }: InteriorGraphTabProps) {
return (
<div className="h-[min(70dvh,720px)] min-h-[420px] w-full overflow-hidden rounded-none border border-white/15">
<ReactFlowProvider>
<GraphInner rooms={rooms} onSelect={onSelect} />
</ReactFlowProvider>
</div>
);
}