feat: persist multi-board projects + add auto-save
The project save/load pipeline only persisted a single `board_type`, so
multi-board workspaces silently lost every board except the active one
on save, and wires referencing the dropped boards' IDs orphaned to the
canvas corner on reload. An audit of the production backup found 74/306
projects (24%) with at least one orphaned wire and 174/301 non-trivial
projects whose code was still the default Blink template — strong signal
that users save once and never re-save.
Backend
- Add `boards_json` column on `projects` with idempotent ALTER TABLE in
the lifespan migration list.
- New `FileGroup` schema + `file_groups` array on
ProjectCreate/Update/Response. Legacy `files`/`code` kept for back-compat.
- `project_files.py` now uses `{pid}/{groupId}/{filename}` subdirs via
`read_groups`/`write_groups`. Legacy flat layouts are auto-promoted on
read; legacy single-list `files` only updates the active group, leaving
other boards' files intact.
- `_persist_files_from_body` honors file_groups → files → code priority.
Frontend
- `useSimulatorStore.addBoard` accepts an optional `explicitId` so
saved board IDs can be restored verbatim (wires reference IDs literally).
- New `loadProjectState({boards, fileGroups, components, wires,
activeBoardId})` action: tears down current boards, recreates from the
payload, restores file groups atomically, recalculates wire positions
on the next frame, and refreshes the Interconnect.
- `useEditorStore.replaceFileGroups` for atomic multi-group restore.
- `SaveProjectModal` and `ProjectByIdPage`/`ProjectPage` now go through
`buildSavePayload` / `buildLoadPayload` (handles pre-backfill projects
by synthesising a default board from `board_type`).
Auto-save (#useAutoSaveProject hook)
- 2.5s debounced silent PUT triggered ONLY when an authenticated user
has a `currentProject` with a UUID. State hash detects real changes
vs. UI-only churn; baseline is reset on project load so the just-loaded
state isn't immediately re-saved.
- `beforeunload` flush via `fetch keepalive: true` (supports PUT +
credentials, survives unload).
- Compact status indicator in `AppHeader` (idle/dirty/saving/saved/error).
Backfill script (one-off, idempotent)
- `backend/scripts/backfill_boards_2026_05.py` populates `boards_json`
for legacy projects. Heuristic per project, based on which board IDs
the wires reference:
Case A — wires only ref 'arduino-uno' but board_type ≠ uno:
rename id→board_type and rewrite wire endpoints.
Case B — single-board normal: keep verbatim.
Case C — multi-board: recreate one board per distinct ref, infer
kind by stripping trailing -N suffix.
Also moves any flat files into the active board's group subdir.
Stdlib-only, runs from host or `docker exec`.
Docker
- `Dockerfile.standalone` now copies `backend/scripts/` into the image
so the backfill is callable via `docker exec velxio-app python
/app/scripts/backfill_boards_2026_05.py --apply`.
Verified locally on the restored production backup (363 projects):
33 Case A, 316 Case B, 14 Case C, 135 wire endpoints renamed, 0 orphans.
Re-running the script after apply skips all 363 (idempotent).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 23:43:33 +07:00
|
|
|
/**
|
|
|
|
|
* Shared helpers for serialising the simulator + editor state into a project
|
|
|
|
|
* save payload, and for hashing that state to detect dirty changes.
|
|
|
|
|
*
|
|
|
|
|
* Used by SaveProjectModal (manual save) and useAutoSaveProject (silent
|
|
|
|
|
* background updates).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type { ProjectSaveData } from '../services/projectService';
|
|
|
|
|
import type { BoardInstance } from '../types/board';
|
|
|
|
|
import type { Wire } from '../types/wire';
|
|
|
|
|
import { useEditorStore } from '../store/useEditorStore';
|
|
|
|
|
import { useSimulatorStore } from '../store/useSimulatorStore';
|
|
|
|
|
|
|
|
|
|
/** Strip BoardInstance down to JSON-safe fields (drop runtime state). */
|
|
|
|
|
function serialisableBoard(b: BoardInstance) {
|
|
|
|
|
return {
|
|
|
|
|
id: b.id,
|
|
|
|
|
boardKind: b.boardKind,
|
|
|
|
|
x: b.x,
|
|
|
|
|
y: b.y,
|
|
|
|
|
activeFileGroupId: b.activeFileGroupId,
|
|
|
|
|
languageMode: b.languageMode,
|
|
|
|
|
serialBaudRate: b.serialBaudRate,
|
|
|
|
|
compiledProgram: b.compiledProgram,
|
2026-05-18 12:38:51 +07:00
|
|
|
// ESP32 board options + uploaded SPIFFS files. Optional, only present
|
|
|
|
|
// after the user has opened Board Options... at least once. Both ride
|
|
|
|
|
// inside boards_json so there's no DB migration.
|
|
|
|
|
boardOptions: b.boardOptions,
|
|
|
|
|
spiffsFiles: b.spiffsFiles,
|
feat: persist multi-board projects + add auto-save
The project save/load pipeline only persisted a single `board_type`, so
multi-board workspaces silently lost every board except the active one
on save, and wires referencing the dropped boards' IDs orphaned to the
canvas corner on reload. An audit of the production backup found 74/306
projects (24%) with at least one orphaned wire and 174/301 non-trivial
projects whose code was still the default Blink template — strong signal
that users save once and never re-save.
Backend
- Add `boards_json` column on `projects` with idempotent ALTER TABLE in
the lifespan migration list.
- New `FileGroup` schema + `file_groups` array on
ProjectCreate/Update/Response. Legacy `files`/`code` kept for back-compat.
- `project_files.py` now uses `{pid}/{groupId}/{filename}` subdirs via
`read_groups`/`write_groups`. Legacy flat layouts are auto-promoted on
read; legacy single-list `files` only updates the active group, leaving
other boards' files intact.
- `_persist_files_from_body` honors file_groups → files → code priority.
Frontend
- `useSimulatorStore.addBoard` accepts an optional `explicitId` so
saved board IDs can be restored verbatim (wires reference IDs literally).
- New `loadProjectState({boards, fileGroups, components, wires,
activeBoardId})` action: tears down current boards, recreates from the
payload, restores file groups atomically, recalculates wire positions
on the next frame, and refreshes the Interconnect.
- `useEditorStore.replaceFileGroups` for atomic multi-group restore.
- `SaveProjectModal` and `ProjectByIdPage`/`ProjectPage` now go through
`buildSavePayload` / `buildLoadPayload` (handles pre-backfill projects
by synthesising a default board from `board_type`).
Auto-save (#useAutoSaveProject hook)
- 2.5s debounced silent PUT triggered ONLY when an authenticated user
has a `currentProject` with a UUID. State hash detects real changes
vs. UI-only churn; baseline is reset on project load so the just-loaded
state isn't immediately re-saved.
- `beforeunload` flush via `fetch keepalive: true` (supports PUT +
credentials, survives unload).
- Compact status indicator in `AppHeader` (idle/dirty/saving/saved/error).
Backfill script (one-off, idempotent)
- `backend/scripts/backfill_boards_2026_05.py` populates `boards_json`
for legacy projects. Heuristic per project, based on which board IDs
the wires reference:
Case A — wires only ref 'arduino-uno' but board_type ≠ uno:
rename id→board_type and rewrite wire endpoints.
Case B — single-board normal: keep verbatim.
Case C — multi-board: recreate one board per distinct ref, infer
kind by stripping trailing -N suffix.
Also moves any flat files into the active board's group subdir.
Stdlib-only, runs from host or `docker exec`.
Docker
- `Dockerfile.standalone` now copies `backend/scripts/` into the image
so the backfill is callable via `docker exec velxio-app python
/app/scripts/backfill_boards_2026_05.py --apply`.
Verified locally on the restored production backup (363 projects):
33 Case A, 316 Case B, 14 Case C, 135 wire endpoints renamed, 0 orphans.
Re-running the script after apply skips all 363 (idempotent).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 23:43:33 +07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface SnapshotInputs {
|
|
|
|
|
name?: string;
|
|
|
|
|
description?: string;
|
|
|
|
|
isPublic?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build the payload sent to POST/PUT /api/projects. Includes everything the
|
|
|
|
|
* server cares about: boards_json, file_groups, components, wires, and the
|
|
|
|
|
* legacy `files` / `code` fields for backwards compat.
|
|
|
|
|
*/
|
|
|
|
|
export function buildSavePayload(meta: SnapshotInputs = {}): ProjectSaveData {
|
|
|
|
|
const sim = useSimulatorStore.getState();
|
|
|
|
|
const editor = useEditorStore.getState();
|
|
|
|
|
|
|
|
|
|
const activeBoard =
|
|
|
|
|
sim.boards.find((b) => b.id === sim.activeBoardId) ?? sim.boards[0];
|
|
|
|
|
const boardKind = activeBoard?.boardKind ?? 'arduino-uno';
|
|
|
|
|
|
|
|
|
|
const activeGroupId = activeBoard?.activeFileGroupId ?? '';
|
|
|
|
|
const activeFiles =
|
|
|
|
|
(editor.fileGroups[activeGroupId]?.length
|
|
|
|
|
? editor.fileGroups[activeGroupId]
|
|
|
|
|
: editor.files) ?? editor.files;
|
|
|
|
|
|
|
|
|
|
// Legacy `code` field — primary .ino content of the active board.
|
|
|
|
|
const code =
|
|
|
|
|
activeFiles.find((f) => f.name.endsWith('.ino'))?.content ??
|
|
|
|
|
activeFiles[0]?.content ??
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
const fileGroups = sim.boards.map((b) => ({
|
|
|
|
|
groupId: b.activeFileGroupId,
|
|
|
|
|
files: (editor.fileGroups[b.activeFileGroupId] ?? []).map((f) => ({
|
|
|
|
|
name: f.name,
|
|
|
|
|
content: f.content,
|
|
|
|
|
})),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
name: meta.name ?? '',
|
|
|
|
|
description: meta.description,
|
|
|
|
|
is_public: meta.isPublic ?? true,
|
|
|
|
|
board_type: boardKind,
|
|
|
|
|
files: activeFiles.map((f) => ({ name: f.name, content: f.content })),
|
|
|
|
|
file_groups: fileGroups,
|
|
|
|
|
code,
|
|
|
|
|
components_json: JSON.stringify(sim.components),
|
|
|
|
|
wires_json: JSON.stringify(sim.wires),
|
|
|
|
|
boards_json: JSON.stringify(sim.boards.map(serialisableBoard)),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Compact deterministic fingerprint of the parts of the workspace that the
|
|
|
|
|
* server persists. Used for dirty-checking by the auto-save hook.
|
|
|
|
|
*
|
|
|
|
|
* Excludes meta fields (name/description/isPublic) — those only change via
|
|
|
|
|
* the manual save modal.
|
|
|
|
|
*/
|
|
|
|
|
export function computeProjectStateHash(): string {
|
|
|
|
|
const sim = useSimulatorStore.getState();
|
|
|
|
|
const editor = useEditorStore.getState();
|
|
|
|
|
|
|
|
|
|
// Group file contents only for groups referenced by an existing board, in
|
|
|
|
|
// a stable order (sorted by groupId).
|
|
|
|
|
const referencedGroups = Array.from(
|
|
|
|
|
new Set(sim.boards.map((b) => b.activeFileGroupId)),
|
|
|
|
|
).sort();
|
|
|
|
|
const groupsForHash = referencedGroups.map((gid) => ({
|
|
|
|
|
g: gid,
|
|
|
|
|
f: (editor.fileGroups[gid] ?? []).map((f) => [f.name, f.content]),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Wires/components: serialise as-is. Object key order is stable in JSON.stringify
|
|
|
|
|
// when the keys are insertion-ordered (which Zustand state respects).
|
|
|
|
|
const wiresHash = (sim.wires as Wire[]).map((w) => ({
|
|
|
|
|
id: w.id,
|
|
|
|
|
s: [w.start.componentId, w.start.pinName],
|
|
|
|
|
e: [w.end.componentId, w.end.pinName],
|
|
|
|
|
c: w.color,
|
|
|
|
|
sig: w.signalType,
|
|
|
|
|
wp: w.waypoints,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
|
boards: sim.boards.map(serialisableBoard),
|
|
|
|
|
activeId: sim.activeBoardId,
|
|
|
|
|
components: sim.components,
|
|
|
|
|
wires: wiresHash,
|
|
|
|
|
groups: groupsForHash,
|
|
|
|
|
};
|
|
|
|
|
return JSON.stringify(payload);
|
|
|
|
|
}
|