'use client' import { useEffect, useState } from 'react' import { Code, Copy, CheckCircle, ExternalLink, HelpCircle } from 'lucide-react' export default function CodeExample({ framework }) { const examplesUrl = framework?.exemplos; const color = framework?.cor || '#6B7280'; const [sample, setSample] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [copied, setCopied] = useState(false); useEffect(() => { setSample(''); setError(null); setLoading(false); if (!framework) return; const exemploPath = framework.exemplo; if (!exemploPath) { const msg = `// Exemplos completos: ${examplesUrl || 'Ver repositório'}\n// Abra o link acima para ver exemplos e código completo.\n`; setSample(msg); return; } const url = exemploPath.startsWith('/') ? exemploPath : `/${exemploPath}`; let cancelled = false; setLoading(true); fetch(url) .then((res) => { if (!res.ok) throw new Error(`HTTP ${res.status}`); return res.text(); }) .then((text) => { if (cancelled) return; if (text && text.trim().length > 0) { setSample(text); } else { setSample(`// Arquivo de exemplo está vazio. Consulte o repositório oficial.\n`); setError('Arquivo vazio'); } }) .catch((err) => { if (cancelled) return; setSample(`// Não foi possível carregar o exemplo localmente.\n// Consulte o link do GitHub acima para exemplos funcionais.\n`); setError(err.message || 'Falha ao carregar exemplo'); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [framework?.exemplo, framework?.exemplos]); const lines = (sample || '').split('\n'); async function handleCopy() { try { await navigator.clipboard.writeText(sample); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch {} } return (

Demonstração de Código

Trecho de inicialização rápida. Copie e personalize em seu projeto.

{framework?.linguagem || 'Código'} {examplesUrl && ( Mais Exemplos )}
{framework?.nome} example
{loading ? 'Buscando exemplo...' : (framework?.exemplo ? 'Local Source' : 'External GitHub')}
						{lines.map((line, idx) => (
							
{idx + 1} {line === '' ? '\u00A0' : line}
))}
{error && (

Nota: Não foi possível carregar o arquivo de exemplo local ({error}). Use o botão "Mais Exemplos" para ver os códigos oficiais.

)}

Dica de Desenvolvimento: Personalize o trecho de inicialização com as pinagens e configurações corretas correspondentes à série do seu microcontrolador (ex: ESP32-S3, ESP32-C6). A documentação detalhada da API pode ser acessada na barra superior do framework.

); }