import React, { useMemo } from 'react'; import { motion } from 'framer-motion'; import { layoutFresh, getNodeSize } from '../canvas/layout'; const NODE_FILL = { blue: '#dbeafe', green: '#dcfce7', pink: '#fce7f3', purple: '#f3e8ff', orange: '#ffedd5', yellow: '#fef9c3', }; // Miniature, non-interactive drawing of a template's graph. const TemplatePreview = ({ content, height = 140 }) => { const layout = useMemo(() => { const nodes = content?.nodes || []; const edges = content?.edges || []; if (nodes.length === 0) return null; const placed = nodes.some((n) => !n.position) ? layoutFresh(nodes, edges) : nodes; let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity; const boxes = placed.map((n) => { const { width, height: h } = getNodeSize(n); minX = Math.min(minX, n.position.x); minY = Math.min(minY, n.position.y); maxX = Math.max(maxX, n.position.x + width); maxY = Math.max(maxY, n.position.y + h); return { id: n.id, type: n.type, color: n.data?.color, x: n.position.x, y: n.position.y, w: width, h }; }); const byId = new Map(boxes.map((b) => [b.id, b])); const lines = edges .map((e) => [byId.get(e.source), byId.get(e.target)]) .filter(([a, b]) => a && b) .map(([a, b], i) => ({ key: i, x1: a.x + a.w / 2, y1: a.y + a.h, x2: b.x + b.w / 2, y2: b.y, sticker: a.type === 'comment' || b.type === 'comment' })); const pad = 40; // Keep strokes legible whatever the size of the graph const stroke = Math.max(6, Math.max(maxX - minX, maxY - minY) / 70); return { boxes, lines, stroke, viewBox: `${minX - pad} ${minY - pad} ${maxX - minX + pad * 2} ${maxY - minY + pad * 2}` }; }, [content]); if (!layout) { return (