/** * ExampleLoaderPage — loads an example by ID from the URL and redirects to the editor. * * Route: /examples/:exampleId * Example: /examples/blink-led */ import React, { useEffect, useState } from 'react'; import { useParams, useNavigate, Link } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { exampleProjects } from '../data/examples'; import { loadExample, type LibraryInstallProgress } from '../utils/loadExample'; import { AppHeader } from '../components/layout/AppHeader'; import { useLocalizedHref } from '../i18n/useLocalizedNavigate'; export const ExampleLoaderPage: React.FC = () => { const { t } = useTranslation(); const localize = useLocalizedHref(); const { exampleId } = useParams<{ exampleId: string }>(); const navigate = useNavigate(); const [error, setError] = useState(false); const [installing, setInstalling] = useState(null); useEffect(() => { if (!exampleId) { setError(true); return; } const example = exampleProjects.find((e) => e.id === exampleId); if (!example) { setError(true); return; } let cancelled = false; (async () => { await loadExample(example, setInstalling); if (!cancelled) navigate(localize('/editor'), { replace: true }); })(); return () => { cancelled = true; }; }, [exampleId, navigate]); if (error) { return (
404
{t('examples.notFound', { id: exampleId })}
{t('examples.browseAll')}
); } return (
{t('examples.loadingExample')}
{installing && (
{t('examples.installing', { done: installing.done + 1, total: installing.total })}
{installing.current}
)}
); };