Skip to content

Commit 1c6cd65

Browse files
committed
Make layout configurable
1 parent 531347e commit 1c6cd65

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

src/Graph.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { useState, useEffect, useRef, useCallback } from "react";
33
import type { TechTree, TechNode, TechNodeId } from "./TechTree.js";
44
import mermaid from "mermaid";
55

6-
function mermaidGraph(tree: TechTree): string {
7-
let mermaid = "graph LR\n";
6+
function mermaidGraph(tree: TechTree, topDown: boolean): string {
7+
let mermaid = `graph ${topDown ? "TD" : "LR"}\n`;
88

99
// Check if any nodes have external dependencies
1010
const nodeIds = new Set(tree.nodes.map((node) => node.id));
@@ -53,6 +53,7 @@ export function Graph(props: GraphProps) {
5353
const [position, setPosition] = useState({ x: 0, y: 0 });
5454
const [isDragging, setIsDragging] = useState(false);
5555
const [dragStart, setDragStart] = useState({ x: 0, y: 0 });
56+
const [topDown, setTopDown] = useState(false);
5657
const [graphId] = useState(
5758
() => `graph-${Math.random().toString(36).substr(2, 9)}`,
5859
);
@@ -124,7 +125,7 @@ export function Graph(props: GraphProps) {
124125

125126
const renderGraph = async () => {
126127
try {
127-
const mermaidSyntax = mermaidGraph(props.tree);
128+
const mermaidSyntax = mermaidGraph(props.tree, topDown);
128129
const { svg } = await mermaid.render(graphId, mermaidSyntax);
129130

130131
if (containerRef.current) {
@@ -201,6 +202,7 @@ export function Graph(props: GraphProps) {
201202
handleNodeClick,
202203
props.selectedNodeId,
203204
highlightSelectedNode,
205+
topDown,
204206
]);
205207

206208
// Update highlighting when selected node changes
@@ -257,6 +259,10 @@ export function Graph(props: GraphProps) {
257259
setPosition({ x: 0, y: 0 });
258260
}, []);
259261

262+
const toggleTopDown = useCallback(() => {
263+
setTopDown(!topDown);
264+
}, [topDown]);
265+
260266
return (
261267
<div className="relative w-full h-full overflow-hidden bg-gray-50">
262268
{/* Controls */}
@@ -282,6 +288,13 @@ export function Graph(props: GraphProps) {
282288
>
283289
Reset
284290
</button>
291+
<button
292+
onClick={toggleTopDown}
293+
className="px-3 py-2 bg-white border rounded shadow hover:bg-gray-50"
294+
title="Toggle Layout"
295+
>
296+
{topDown ? "TD" : "LR"}
297+
</button>
285298
</div>
286299

287300
{/* Graph Container */}

0 commit comments

Comments
 (0)