"use client"; import { useEffect, useState } from "react"; import { Check, Copy, ExternalLink } from "lucide-react"; import { usePathname } from "next/navigation"; export default function CodeExample({ framework }) { const pathname = usePathname() || ""; const isEn = pathname === "/en" || pathname.startsWith("/en/"); const examplesUrl = framework?.exemplos; 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) { setSample( isEn ? `// Full examples: ${examplesUrl || "see repository"}\n` : `// Exemplos completos: ${examplesUrl || "ver repositório"}\n` ); return; } // Se locale for en, busca primeiro em /exemplos/en/ let url = exemploPath.startsWith("/") ? exemploPath : `/${exemploPath}`; if (isEn && url.startsWith("/exemplos/") && !url.startsWith("/exemplos/en/")) { url = url.replace("/exemplos/", "/exemplos/en/"); } let cancelled = false; setLoading(true); fetch(url) .then((res) => { if (!res.ok) { // Fallback para o arquivo original se não achar a versão traduzida const fallbackUrl = exemploPath.startsWith("/") ? exemploPath : `/${exemploPath}`; if (fallbackUrl !== url) { return fetch(fallbackUrl).then((r2) => { if (!r2.ok) throw new Error(`HTTP ${r2.status}`); return r2.text(); }); } throw new Error(`HTTP ${res.status}`); } return res.text(); }) .then((text) => { if (cancelled) return; if (text && text.trim().length > 0) { setSample(text); } else { setSample( isEn ? "// The example file is empty. Refer to the official repository.\n" : "// O arquivo de exemplo está vazio. Consulte o repositório oficial.\n" ); setError(isEn ? "empty file" : "arquivo vazio"); } }) .catch((err) => { if (cancelled) return; setSample( isEn ? "// Could not load example.\n// Refer to official repository for complete examples.\n" : "// Não foi possível carregar o exemplo.\n// Consulte o repositório oficial para exemplos completos.\n" ); setError(err.message || (isEn ? "load failure" : "falha ao carregar")); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [framework?.exemplo, framework?.exemplos, isEn]); const lines = (sample || "").replace(/\n$/, "").split("\n"); async function handleCopy() { try { await navigator.clipboard.writeText(sample); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch {} } return (

{isEn ? "Code example" : "Exemplo de código"}

{isEn ? `Starter snippet in ${framework?.linguagem}. Adjust pins and configuration for your board.` : `Trecho de partida em ${framework?.linguagem}. Ajuste pinos e configuração à sua placa.`}

{examplesUrl && ( {isEn ? "More examples" : "Mais exemplos"} )}
{framework?.nome} {loading ? (isEn ? "Loading example…" : "Carregando exemplo…") : framework?.linguagem}
					
						{lines.map((line, idx) => (
							
								{idx + 1}
								{line || " "}
							
						))}
					
				
{error && (

{isEn ? `Could not load local example (${error}). Use “More examples” to view official code.` : `Não foi possível carregar o exemplo local (${error}). Use “Mais exemplos” para ver o código oficial.`}

)}
); }