From 4708d5419524c2e1b2e71669cebdf84d2fd0800f Mon Sep 17 00:00:00 2001 From: davidmonterocrespo24 Date: Thu, 23 Apr 2026 05:22:35 +0200 Subject: [PATCH] fix(examples): re-create board when loading an Arduino example after an analog one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Loading an analog-only example removes every board. The single-board branch of loadExample then called setBoardType, which only maps over existing entries in boards[] and silently did nothing when the array was empty — components rendered but no board. Fall back to addBoard + setActiveBoardId when there are no boards. --- frontend/src/utils/loadExample.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/frontend/src/utils/loadExample.ts b/frontend/src/utils/loadExample.ts index 6856c706..1119bc1e 100644 --- a/frontend/src/utils/loadExample.ts +++ b/frontend/src/utils/loadExample.ts @@ -6,7 +6,7 @@ import type { ExampleProject } from '../data/examples'; import type { BoardKind } from '../types/board'; import { useEditorStore } from '../store/useEditorStore'; -import { useSimulatorStore } from '../store/useSimulatorStore'; +import { useSimulatorStore, DEFAULT_BOARD_POSITION } from '../store/useSimulatorStore'; import { useVfsStore } from '../store/useVfsStore'; import { isBoardComponent } from './boardPinMapping'; import { getInstalledLibraries, installLibrary } from '../services/libraryService'; @@ -155,7 +155,19 @@ export async function loadExample( currentIds.forEach((id) => removeBoard(id)); } else { const targetBoard = example.boardType || 'arduino-uno'; - setBoardType(targetBoard); + // If boards[] is empty (e.g. a previous analog example removed every + // board), setBoardType can't work — it only maps over existing entries. + // Add a fresh board instead. + if (useSimulatorStore.getState().boards.length === 0) { + const newId = addBoard( + targetBoard as BoardKind, + DEFAULT_BOARD_POSITION.x, + DEFAULT_BOARD_POSITION.y, + ); + setActiveBoardId(newId); + } else { + setBoardType(targetBoard); + } } useEditorStore.getState().setCode(example.code);