# dan-lore/tfg-planner architecture

> Architecture diagram of the GitHub repository dan-lore/tfg-planner, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dan-lore/tfg-planner
- Repository: https://github.com/dan-lore/tfg-planner

## Overview

1. **Overall shape**
   - This is a TypeScript/Vite React web app for planning factory/production “schemes” for TerraFirmaGreg-Modern.
   - The runtime app is separate from an offline data-generation toolchain that builds the recipe packs consumed by the UI.

2. **Application shell**
   - `src/main.tsx` boots the app.
   - `src/app/*` defines the top-level layout and route split between the home page and the editor page.
   - The UI is deployed as a static site, including a GitHub Pages build path variant.

3. **Primary user workflow**
   - The user selects a pack/version, opens an editor, places machines, connects ports, sets target output rates, and saves/loads `.tfgp` scheme files.
   - That workflow is the central data path through the repository: pack selection → scheme editing → flow calculation → validation/visualization → persistence/export.

4. **Editor subsystem**
   - `src/editor`, `src/canvas`, and `src/components` make up the editing surface.
   - The canvas layer handles node rendering, edges, port menus, selection, drag interactions, obstacles, and route planning.
   - The editor layer provides toolbar, inspector, sidebar, issue panels, and action handling around the canvas.

5. **Scheme model and validation**
   - `src/schema` defines the `.tfgp` schema and validation rules.
   - `src/lib/read-tfgp-file.ts`, `editor-persist.ts`, and related helpers support import/export and browser persistence.
   - `src/scheme-check` performs structural checks such as wiring issues and cycles, turning invalid schemes into user-visible diagnostics.

6. **Flow calculation engine**
   - `src/calculator` is the mathematical core: flow rates, machine counts, energy, buffers, port resolution, constraints, and result metrics.
   - The calculator turns a scheme graph plus pack recipe data into computed throughput and required machine quantities.
   - This layer is heavily unit-tested and appears designed as a pure domain engine, separate from UI concerns.

7. **Background execution boundary**
   - `src/workers/flow-worker.ts` and `src/stores/flow-compute-runtime.ts` indicate that expensive flow solving runs off the main UI thread.
   - The worker boundary keeps interactive editing responsive while recalculations happen asynchronously.
   - Stores mediate between the UI and the worker-produced results.

8. **Pack data runtime**
   - `public/data/packs/<version>/` contains the versioned pack artifacts the app loads at runtime, including recipe shards, manifests, metadata, and language bundles.
   - `src/data`, `src/stores/pack-store.ts`, and `src/lib/pack-*` manage pack selection, loading, caching, and version mismatch handling.
   - This pack layer is the main runtime dependency for recipe lookup, names, and machine metadata.

9. **Language and lexicon support**
   - `src/data/pack-lang-loader.ts`, `src/data/lang-cache.ts`, and `src/lib/product-lexicon/*` resolve in-game names, tags, and localized labels.
   - The app supports RU/EN UI language behavior and pack-specific language assets, which affect recipe search, picker display, and labels in the editor.

10. **Offline parser/build pipeline**
   - `tools/parser` is a separate Node/TypeScript toolchain that extracts modpack data from snapshots, KubeJS scripts, and related sources.
   - It normalizes recipes, enriches metadata, builds language bundles, validates output, and generates the sharded pack format used by the web app.
   - This pipeline is maintenance-only and not part of the browser runtime, but it is essential to the data architecture.

11. **Static assets and examples**
   - `examples/` and `src/lib/fixtures` contain sample schemes and routing fixtures used for testing and documentation.
   - `data/packs` and `public/data/packs` reflect the runtime-vs-build-time split: generated content is checked into the web app for direct static hosting.

12. **Quality and enforcement boundaries**
   - Tests are spread across calculator, canvas, parser, schema, and store layers, showing a strong separation of domain logic from UI.
   - CI, Semgrep, dependency-cruiser, and knip are used to keep the architecture bounded and detect unwanted coupling.
   - The docs under `docs/` act as the project’s architectural spec for editor behavior, parser pipeline, and data formats.

Main entry points: [`src/calculator`](https://github.com/dan-lore/tfg-planner/tree/master/src/calculator) (Calculator), [`src/stores/pack-store.ts`](https://github.com/dan-lore/tfg-planner/blob/master/src/stores/pack-store.ts) (Pack store), [`src/app/routes.tsx`](https://github.com/dan-lore/tfg-planner/blob/master/src/app/routes.tsx) (Routes), [`src/pages/EditorPage.tsx`](https://github.com/dan-lore/tfg-planner/blob/master/src/pages/EditorPage.tsx) (Editor page)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Web app"]
  node_main["Entry<br/>Vite bootstrap<br/>[main.tsx]"]
  node_layout["Shell<br/>app layout<br/>[AppLayout.tsx]"]
  node_routes["Routes<br/>route config<br/>[routes.tsx]"]
  node_home["Home<br/>landing page<br/>[HomePage.tsx]"]
  node_editorpage["Editor page<br/>[EditorPage.tsx]"]
  node_editorui["Editor UI<br/>editor surface"]
  node_canvas["Canvas<br/>diagram editor"]
  node_persist["Persistence<br/>save/load<br/>[editor-persist.ts]"]
  node_flowrt["Flow runtime<br/>worker runtime<br/>[flow-worker.ts]"]
  node_store["Stores<br/>state bridge"]
end

subgraph group_domain["Domain core"]
  node_issues["Scheme checks<br/>diagnostics"]
  node_schema["Scheme schema<br/>tfgp model"]
  node_calc["Calculator<br/>flow engine"]
end

subgraph group_data["Pack data"]
  node_packstore["Pack store<br/>pack loader<br/>[pack-store.ts]"]
  node_packdata[("Pack data<br/>versioned assets<br/>[0.12.8]")]
  node_lang["Lang layer<br/>name resolution"]
end

subgraph group_tooling["Offline tooling"]
  node_parser["Parser build<br/>offline pipeline"]
  node_docs["Docs & CI<br/>quality gates<br/>[architecture.md]"]
end

node_main -->|"mounts"| node_layout
node_layout -->|"hosts"| node_routes
node_routes -->|"home"| node_home
node_routes -->|"editor"| node_editorpage
node_editorpage -->|"contains"| node_editorui
node_editorui -->|"drives"| node_canvas
node_editorui -->|"state"| node_store
node_canvas -->|"requests"| node_calc
node_canvas -->|"checks"| node_issues
node_store -->|"dispatches"| node_flowrt
node_flowrt -->|"runs"| node_calc
node_calc -->|"consumes"| node_schema
node_issues -->|"validates"| node_schema
node_persist -->|"serializes"| node_schema
node_packstore -->|"loads"| node_packdata
node_lang -->|"resolves"| node_packdata
node_home -->|"selects"| node_packstore
node_editorpage -->|"uses"| node_packstore
node_calc -->|"looks up"| node_packstore
node_parser -->|"generates"| node_packdata
node_parser -->|"emits"| node_lang
node_docs -.->|"specifies"| node_parser
node_docs -.->|"specifies"| node_calc

click node_main "https://github.com/dan-lore/tfg-planner/blob/master/src/main.tsx"
click node_layout "https://github.com/dan-lore/tfg-planner/blob/master/src/app/AppLayout.tsx"
click node_routes "https://github.com/dan-lore/tfg-planner/blob/master/src/app/routes.tsx"
click node_home "https://github.com/dan-lore/tfg-planner/blob/master/src/pages/HomePage.tsx"
click node_editorpage "https://github.com/dan-lore/tfg-planner/blob/master/src/pages/EditorPage.tsx"
click node_editorui "https://github.com/dan-lore/tfg-planner/tree/master/src/editor"
click node_canvas "https://github.com/dan-lore/tfg-planner/tree/master/src/canvas"
click node_persist "https://github.com/dan-lore/tfg-planner/blob/master/src/lib/editor-persist.ts"
click node_issues "https://github.com/dan-lore/tfg-planner/tree/master/src/scheme-check"
click node_schema "https://github.com/dan-lore/tfg-planner/tree/master/src/schema"
click node_calc "https://github.com/dan-lore/tfg-planner/tree/master/src/calculator"
click node_flowrt "https://github.com/dan-lore/tfg-planner/blob/master/src/workers/flow-worker.ts"
click node_packstore "https://github.com/dan-lore/tfg-planner/blob/master/src/stores/pack-store.ts"
click node_packdata "https://github.com/dan-lore/tfg-planner/blob/master/public/data/packs/0.12.8"
click node_lang "https://github.com/dan-lore/tfg-planner/blob/master/src/data/pack-lang-loader.ts"
click node_store "https://github.com/dan-lore/tfg-planner/tree/master/src/stores"
click node_parser "https://github.com/dan-lore/tfg-planner/tree/master/tools/parser/src"
click node_docs "https://github.com/dan-lore/tfg-planner/blob/master/docs/architecture.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_layout,node_routes,node_home,node_editorpage,node_editorui,node_canvas,node_persist,node_flowrt,node_store toneBlue
class node_issues,node_schema,node_calc toneAmber
class node_packstore,node_packdata,node_lang toneMint
class node_parser,node_docs toneRose
```

## Components

### Web app

Browser runtime for editing schemes and computing flows.

- [Entry](https://github.com/dan-lore/tfg-planner/blob/master/src/main.tsx): Vite bootstrap · Bootstraps the React app. · `src/main.tsx`
- [Shell](https://github.com/dan-lore/tfg-planner/blob/master/src/app/AppLayout.tsx): app layout · Top-level layout and route split. · `src/app/AppLayout.tsx`
- [Routes](https://github.com/dan-lore/tfg-planner/blob/master/src/app/routes.tsx): route config · Home/editor route wiring. · `src/app/routes.tsx`
- [Home](https://github.com/dan-lore/tfg-planner/blob/master/src/pages/HomePage.tsx): landing page · Pack selection and entry point. · `src/pages/HomePage.tsx`
- [Editor page](https://github.com/dan-lore/tfg-planner/blob/master/src/pages/EditorPage.tsx): editor page · Hosts the interactive scheme editor. · `src/pages/EditorPage.tsx`
- [Editor UI](https://github.com/dan-lore/tfg-planner/tree/master/src/editor): editor surface · Toolbar, sidebar, inspector, issues, and actions around the canvas. · `src/editor`
- [Canvas](https://github.com/dan-lore/tfg-planner/tree/master/src/canvas): diagram editor · Machine nodes, edges, selection, routing, and drag interactions. · `src/canvas`
- [Persistence](https://github.com/dan-lore/tfg-planner/blob/master/src/lib/editor-persist.ts): save/load · Import/export and browser persistence for .tfgp schemes. · `src/lib/editor-persist.ts`
- [Flow runtime](https://github.com/dan-lore/tfg-planner/blob/master/src/workers/flow-worker.ts): worker runtime · Asynchronous compute boundary between UI and solver. · `src/workers/flow-worker.ts`
- [Stores](https://github.com/dan-lore/tfg-planner/tree/master/src/stores): state bridge · Editor, scheme, flow, and runtime state coordination. · `src/stores`

### Domain core

Pure scheme, validation, and calculation logic.

- [Scheme checks](https://github.com/dan-lore/tfg-planner/tree/master/src/scheme-check): diagnostics · Structural scheme checks that surface wiring and cycle problems. · `src/scheme-check`
- [Scheme schema](https://github.com/dan-lore/tfg-planner/tree/master/src/schema): tfgp model · Schema and validation rules for .tfgp files. · `src/schema`
- [Calculator](https://github.com/dan-lore/tfg-planner/tree/master/src/calculator): flow engine · Pure flow, machine count, energy, buffer, and metric solving. · `src/calculator`

### Pack data

Versioned pack artifacts and language metadata used at runtime.

- [Pack store](https://github.com/dan-lore/tfg-planner/blob/master/src/stores/pack-store.ts): pack loader · Pack selection, caching, and version mismatch handling. · `src/stores/pack-store.ts`
- [Pack data](https://github.com/dan-lore/tfg-planner/blob/master/public/data/packs/0.12.8): versioned assets · Static recipe, manifest, metadata, and language artifacts. · `public/data/packs/0.12.8`
- [Lang layer](https://github.com/dan-lore/tfg-planner/blob/master/src/data/pack-lang-loader.ts): name resolution · Pack language loading and product lexicon resolution. · `src/data/pack-lang-loader.ts`

### Offline tooling

Parser and build pipeline for generating pack artifacts.

- [Parser build](https://github.com/dan-lore/tfg-planner/tree/master/tools/parser/src): offline pipeline · Node toolchain that extracts, normalizes, validates, and builds pack artifacts. · `tools/parser/src`
- [Docs & CI](https://github.com/dan-lore/tfg-planner/blob/master/docs/architecture.md): quality gates · Architecture docs, workflow checks, and repo enforcement. · `docs/architecture.md`

## Connections

- Entry → Shell: mounts · App entry renders the shell.
- Shell → Routes: hosts · Shell delegates page selection to routes.
- Routes → Home: home · Route to the pack selection landing page.
- Routes → Editor page: editor · Route to the interactive editor page.
- Editor page → Editor UI: contains · Editor page composes the editing controls.
- Editor UI → Canvas: drives · Actions and panels operate on the canvas surface.
- Editor UI → Stores: state · Editor UI reads and updates shared state.
- Canvas → Calculator: requests · Scheme edits trigger flow recomputation.
- Canvas → Scheme checks: checks · Canvas changes feed structural validation.
- Stores → Flow runtime: dispatches · Stores send compute jobs to the worker boundary.
- Flow runtime → Calculator: runs · Worker executes the solver off the main thread.
- Calculator → Scheme schema: consumes · Calculator operates on validated scheme structures.
- Scheme checks → Scheme schema: validates · Scheme checker enforces schema and structural rules.
- Persistence → Scheme schema: serializes · Save/load logic reads and writes .tfgp schemes.
- Pack store → Pack data: loads · Runtime store selects versioned pack artifacts.
- Lang layer → Pack data: resolves · Language layer reads pack language assets and labels.
- Home → Pack store: selects · Home page chooses an active pack/version.
- Editor page → Pack store: uses · Editor depends on the active pack for recipes and machines.
- Calculator → Pack store: looks up · Flow solving depends on pack recipe and machine metadata.
- Parser build → Pack data: generates · Offline pipeline builds the pack artifacts consumed by the app.
- Parser build → Lang layer: emits · Pipeline produces localized language bundles and lexicons.
- Docs & CI → Parser build: specifies · Docs and CI constrain the parser pipeline and architecture.
- Docs & CI → Calculator: specifies · Docs and checks encode calculator and editor boundaries.
