Spaces:
Running

FlowWeb / src /features /templates /TemplatePreview.jsx
danylokhodus's picture
Claude Opus 5.5
feat: new landing, template system, auto-layout, Free/Plus plans; remove AI and hide habits
0dcbaee
Raw History Blame Contribute Delete
4.08 kB
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 (
<div style={{ height }} className="w-full flex items-center justify-center text-primary/30 font-accent-note text-2xl">
∅
</div>
);
}
return (
<svg viewBox={layout.viewBox} preserveAspectRatio="xMidYMid meet" style={{ height }} className="w-full" aria-hidden="true">
{layout.lines.map((l, i) => (
<motion.path
key={l.key}
d={`M ${l.x1} ${l.y1} C ${l.x1} ${(l.y1 + l.y2) / 2}, ${l.x2} ${(l.y1 + l.y2) / 2}, ${l.x2} ${l.y2}`}
fill="none"
stroke="currentColor"
strokeOpacity={l.sticker ? 0.25 : 0.55}
strokeWidth={layout.stroke}
strokeDasharray={l.sticker ? '18 14' : undefined}
className="text-primary"
initial={{ pathLength: 0 }}
animate={{ pathLength: 1 }}
transition={{ duration: 0.6, delay: 0.05 * i, ease: 'easeOut' }}
/>
))}
{layout.boxes.map((b, i) => {
const fill = NODE_FILL[b.color] || (b.type === 'comment' ? NODE_FILL.yellow : 'var(--color-surface-container-lowest, #fff)');
const common = {
initial: { opacity: 0, scale: 0.6 },
animate: { opacity: 1, scale: 1 },
transition: { delay: 0.04 * i, type: 'spring', stiffness: 260, damping: 20 },
style: { transformOrigin: `${b.x + b.w / 2}px ${b.y + b.h / 2}px`, transformBox: 'view-box' },
};
if (b.type === 'condition') {
const cx = b.x + b.w / 2;
const cy = b.y + b.h / 2;
return (
<motion.polygon
key={b.id}
{...common}
points={`${cx},${b.y} ${b.x + b.w},${cy} ${cx},${b.y + b.h} ${b.x},${cy}`}
fill={fill}
stroke="currentColor"
strokeWidth={layout.stroke}
className="text-primary"
/>
);
}
return (
<motion.rect
key={b.id}
{...common}
x={b.x}
y={b.y}
width={b.w}
height={b.h}
rx={b.type === 'comment' ? 6 : 28}
fill={fill}
stroke="currentColor"
strokeWidth={layout.stroke}
className="text-primary"
/>
);
})}
</svg>
);
};
export default TemplatePreview;