2026-03-04 08:25:52 +07:00
|
|
|
/**
|
|
|
|
|
* Examples Page Component
|
|
|
|
|
*
|
|
|
|
|
* Displays the examples gallery
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
|
|
import { ExamplesGallery } from '../components/examples/ExamplesGallery';
|
|
|
|
|
import { useEditorStore } from '../store/useEditorStore';
|
|
|
|
|
import { useSimulatorStore } from '../store/useSimulatorStore';
|
|
|
|
|
import type { ExampleProject } from '../data/examples';
|
|
|
|
|
|
|
|
|
|
export const ExamplesPage: React.FC = () => {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const { setCode } = useEditorStore();
|
|
|
|
|
const { setComponents, setWires } = useSimulatorStore();
|
|
|
|
|
|
|
|
|
|
const handleLoadExample = (example: ExampleProject) => {
|
|
|
|
|
console.log('Loading example:', example.title);
|
|
|
|
|
|
|
|
|
|
// Load the code into the editor
|
|
|
|
|
setCode(example.code);
|
|
|
|
|
|
2026-03-04 23:36:33 +07:00
|
|
|
// Filter out Arduino component from examples (it's rendered separately in SimulatorCanvas)
|
|
|
|
|
const componentsWithoutArduino = example.components.filter(
|
|
|
|
|
(comp) => !comp.type.includes('arduino')
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-04 08:25:52 +07:00
|
|
|
// Load components into the simulator
|
|
|
|
|
// Convert component type to metadataId (e.g., 'wokwi-led' -> 'led')
|
|
|
|
|
setComponents(
|
2026-03-04 23:36:33 +07:00
|
|
|
componentsWithoutArduino.map((comp) => ({
|
2026-03-04 08:25:52 +07:00
|
|
|
id: comp.id,
|
|
|
|
|
metadataId: comp.type.replace('wokwi-', ''),
|
|
|
|
|
x: comp.x,
|
|
|
|
|
y: comp.y,
|
|
|
|
|
properties: comp.properties,
|
|
|
|
|
}))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Load wires (need to convert to full wire format with positions)
|
|
|
|
|
// For now, just set empty wires - wire positions will be calculated when components are loaded
|
|
|
|
|
const wiresWithPositions = example.wires.map((wire) => ({
|
|
|
|
|
id: wire.id,
|
|
|
|
|
start: {
|
|
|
|
|
componentId: wire.start.componentId,
|
|
|
|
|
pinName: wire.start.pinName,
|
|
|
|
|
x: 0, // Will be calculated by SimulatorCanvas
|
|
|
|
|
y: 0,
|
|
|
|
|
},
|
|
|
|
|
end: {
|
|
|
|
|
componentId: wire.end.componentId,
|
|
|
|
|
pinName: wire.end.pinName,
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
},
|
|
|
|
|
color: wire.color,
|
|
|
|
|
controlPoints: [],
|
|
|
|
|
isValid: true,
|
|
|
|
|
signalType: 'digital' as const,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
setWires(wiresWithPositions);
|
|
|
|
|
|
|
|
|
|
// Navigate to the editor
|
|
|
|
|
navigate('/');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return <ExamplesGallery onLoadExample={handleLoadExample} />;
|
|
|
|
|
};
|