"use client";
import { useEffect, useRef, useState } from "react";
import { Type, Image as ImageIcon, Heading1, MousePointerClick, LayoutGrid, Minus, Trash2, GripVertical, RotateCcw, Plus } from "lucide-react";
import CanvasHint, { useCanvasHint } from "@/components/ui/CanvasHint";
/* ──────────────────────────────────────────────────────────────────────────
* EmailBuilder — interactive centrepiece for the email-marketing page. Click a
* block in the palette to add it to the live e-mail preview; drag blocks to
* reorder, hover to remove. Brand-styled, not a 1:1 copy of the real editor.
* ────────────────────────────────────────────────────────────────────────── */
type BlockType = "subject" | "image" | "text" | "button" | "products" | "divider";
const PALETTE: { type: BlockType; label: string; icon: typeof Type }[] = [
{ type: "subject", label: "Überschrift", icon: Heading1 },
{ type: "image", label: "Bild", icon: ImageIcon },
{ type: "text", label: "Text", icon: Type },
{ type: "button", label: "CTA-Button", icon: MousePointerClick },
{ type: "products", label: "Produkte", icon: LayoutGrid },
{ type: "divider", label: "Trenner", icon: Minus },
];
type Block = { id: string; type: BlockType };
const INITIAL: Block[] = [
{ id: "b1", type: "subject" },
{ id: "b2", type: "image" },
{ id: "b3", type: "text" },
{ id: "b4", type: "button" },
];
function BlockPreview({ type }: { type: BlockType }) {
switch (type) {
case "subject":
return (
Ihre neue Wärmepumpe wartet auf Sie
);
case "image":
return (
Bild hierher ziehen
);
case "text":
return (
Hallo Herr Müller, danke für Ihr Interesse an der Wärmepumpe. Wir haben Ihnen
ein persönliches Angebot zusammengestellt. Schauen Sie gern rein.
);
case "button":
return (
Jetzt Angebot ansehen
);
case "products":
return (
{[
{ name: "Heizung", price: "ab € 3.900" },
{ name: "Sanitär", price: "ab € 1.490" },
{ name: "Wartung", price: "ab € 149" },
{ name: "Solar", price: "ab € 6.200" },
].map((p) => (
))}
);
case "divider":
return ;
}
}
export default function EmailBuilder({ hideHeader = false }: { hideHeader?: boolean }) {
const [blocks, setBlocks] = useState(INITIAL);
const [dragId, setDragId] = useState(null);
const [resetKey, setResetKey] = useState(0); // bump to remount blocks → clears edited contentEditable text
const idRef = useRef(100);
const blockRefs = useRef>({});
const hint = useCanvasHint();
const addBlock = (type: BlockType) => {
hint.dismiss();
idRef.current += 1;
setBlocks((bs) => [...bs, { id: `b${idRef.current}`, type }]);
};
const removeBlock = (id: string) => setBlocks((bs) => bs.filter((b) => b.id !== id));
const reset = () => {
setDragId(null);
setBlocks(INITIAL);
setResetKey((k) => k + 1);
};
useEffect(() => {
if (!dragId) return;
document.body.style.setProperty("user-select", "none");
document.body.style.setProperty("-webkit-user-select", "none");
const onMove = (e: PointerEvent) => {
const y = e.clientY;
const targetId = blocks.find((b) => {
const el = blockRefs.current[b.id];
if (!el) return false;
const r = el.getBoundingClientRect();
return y >= r.top && y <= r.bottom;
})?.id;
if (!targetId || targetId === dragId) return;
setBlocks((bs) => {
const from = bs.findIndex((b) => b.id === dragId);
const to = bs.findIndex((b) => b.id === targetId);
if (from === -1 || to === -1) return bs;
const arr = [...bs];
const [moved] = arr.splice(from, 1);
arr.splice(to, 0, moved);
return arr;
});
};
const onUp = () => setDragId(null);
window.addEventListener("pointermove", onMove, { passive: true });
window.addEventListener("pointerup", onUp);
window.addEventListener("pointercancel", onUp);
return () => {
window.removeEventListener("pointermove", onMove);
window.removeEventListener("pointerup", onUp);
window.removeEventListener("pointercancel", onUp);
document.body.style.removeProperty("user-select");
document.body.style.removeProperty("-webkit-user-select");
};
}, [dragId, blocks]);
return (
{!hideHeader && (
E-MAIL-BAUKASTEN
Kampagnen selbst gebaut.
)}
{/* Builder on a dotted "canvas" — same treatment as the workflow editor
& kanban. Reset sits in the corner; the popup replaces the caption. */}
{/* Reset — top-right corner of the canvas */}
{/* Palette */}
{PALETTE.map(({ type, label, icon: Icon }) => (
))}
{/* Email preview */}
{/* Mail header */}
G
Geerkens Sanitär & Heizung GmbH
an Sie · buero@geerkens.de
{/* Body blocks */}
{blocks.length === 0 && (
Fügen Sie links Blöcke hinzu …
)}
{blocks.map((b) => {
const isDragging = dragId === b.id;
return (
{
blockRefs.current[b.id] = el;
}}
className="group relative rounded-lg transition-shadow"
style={{
padding: 8,
margin: -8,
background: isDragging ? "var(--container-bg)" : "transparent",
boxShadow: isDragging ? "0 12px 30px -12px rgba(15,23,42,0.25)" : "none",
cursor: isDragging ? "grabbing" : undefined,
}}
>
{/* Drag handle */}
{ hint.dismiss(); setDragId(b.id); }}
className="absolute left-0 top-1/2 -translate-y-1/2 -translate-x-1/2 grid place-items-center rounded-md opacity-0 group-hover:opacity-100 transition-opacity"
style={{ width: 22, height: 22, background: "#fff", border: "1px solid var(--border-light)", cursor: "grab", touchAction: "none" }}
aria-label="Verschieben"
>
{/* Remove */}
);
})}
{/* Centered onboarding hint (dismisses on first block add/drag or via X) */}
);
}