makearmy-app/components/utilities/SVGNestPanel.tsx

92 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

"use client";
import { useEffect, useRef, useState } from "react";
/**
2025-10-15 20:53:06 -04:00
* SVGNest panel same-origin iframe wrapper (clean, frameless).
2025-10-15 20:58:01 -04:00
* Hides the legacy pages heading/tagline and the "Back to Main" link.
*/
export default function SVGNestPanel() {
2025-10-15 20:45:56 -04:00
const iframeRef = useRef<HTMLIFrameElement | null>(null);
const [ready, setReady] = useState(false);
const [err, setErr] = useState<string | null>(null);
useEffect(() => {
2025-10-15 20:45:56 -04:00
const el = iframeRef.current;
if (!el) return;
2025-10-15 20:53:06 -04:00
const onLoad = () => {
setReady(true);
2025-10-15 20:58:01 -04:00
// Tidy up the embedded vendor page (no edits to files on disk)
2025-10-15 20:53:06 -04:00
try {
const doc = el.contentDocument;
if (!doc) return;
2025-10-15 20:58:01 -04:00
// 1) Hide the first H1 (their big title)
2025-10-15 20:53:06 -04:00
const h1 = doc.querySelector("h1");
if (h1) (h1 as HTMLElement).style.display = "none";
2025-10-15 20:58:01 -04:00
// 2) Hide the tagline "automatically nests parts and exports svg"
2025-10-15 20:53:06 -04:00
const all = Array.from(doc.querySelectorAll<HTMLElement>("body *"));
for (const n of all) {
const t = (n.textContent || "").trim().toLowerCase();
if (t.includes("automatically nests parts") && t.includes("exports svg")) {
n.style.display = "none";
}
}
2025-10-15 20:58:01 -04:00
// 3) Hide "Back to Main" link(s)
const anchors = Array.from(doc.querySelectorAll<HTMLAnchorElement>("a"));
for (const a of anchors) {
const text = (a.textContent || "").trim().toLowerCase();
const href = a.getAttribute("href") || "";
const isBackText = text === "back to main" || text === "← back to main";
const isRootHref =
href === "/" ||
/^https?:\/\/[^/]+\/?$/.test(href); // absolute site root
if (isBackText || isRootHref) {
(a as HTMLElement).style.display = "none";
}
}
// 4) Tighten top spacing a touch
2025-10-15 20:53:06 -04:00
(doc.body as HTMLElement).style.marginTop = "4px";
} catch {
2025-10-15 20:58:01 -04:00
// same-origin, so this *should* succeed; ignore if not
2025-10-15 20:53:06 -04:00
}
};
2025-10-15 20:45:56 -04:00
const onError = () => setErr("Failed to load /svgnest/index.html");
el.addEventListener("load", onLoad);
el.addEventListener("error", onError);
return () => {
el.removeEventListener("load", onLoad);
el.removeEventListener("error", onError);
};
}, []);
return (
<div className="w-full">
2025-10-15 20:45:56 -04:00
{!ready && !err && (
<div className="mb-2 text-sm text-zinc-400">Loading SVGnest</div>
)}
2025-10-15 20:45:56 -04:00
{err && (
<div className="mb-2 rounded border border-rose-600/40 bg-rose-600/10 p-3 text-sm">
Couldnt load SVGnest: {err}
</div>
)}
2025-10-15 20:45:56 -04:00
2025-10-15 20:53:06 -04:00
{/* Frameless iframe (no border, no wrapper frame) */}
2025-10-15 20:45:56 -04:00
<iframe
ref={iframeRef}
src="/svgnest/index.html"
title="SVGNest"
2025-10-15 20:53:06 -04:00
className="w-full"
style={{ height: "72vh", background: "transparent", border: "none" }}
2025-10-15 20:45:56 -04:00
/>
</div>
);
}