Files

295 lines
14 KiB
TypeScript

"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 (
<span
contentEditable="plaintext-only"
suppressContentEditableWarning
className="block text-[17px] font-bold leading-snug outline-none rounded cursor-text focus:ring-2 focus:ring-[var(--accent-yellow)] focus:px-1 focus:-mx-1"
style={{ color: "var(--text-primary)" }}
>
Ihre neue Wärmepumpe wartet auf Sie
</span>
);
case "image":
return (
<div
className="w-full rounded-lg flex flex-col items-center justify-center gap-1.5"
style={{ height: 120, background: "var(--container-bg)", border: "1px dashed var(--border-light)" }}
>
<ImageIcon size={22} style={{ color: "var(--text-muted)" }} />
<span className="text-[10px] font-medium" style={{ color: "var(--text-muted)" }}>Bild hierher ziehen</span>
</div>
);
case "text":
return (
<p
contentEditable="plaintext-only"
suppressContentEditableWarning
className="text-[13px] leading-relaxed outline-none rounded cursor-text focus:ring-2 focus:ring-[var(--accent-yellow)]"
style={{ color: "var(--text-secondary)" }}
>
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.
</p>
);
case "button":
return (
<div
className="inline-flex items-center justify-center text-[13px] font-semibold rounded-lg"
style={{ background: "var(--accent-yellow)", color: "var(--text-primary)", padding: "10px 20px" }}
>
Jetzt Angebot ansehen
</div>
);
case "products":
return (
<div className="grid grid-cols-2 gap-2.5">
{[
{ 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) => (
<div key={p.name} className="flex flex-col gap-1.5 rounded-lg p-2.5" style={{ background: "var(--container-bg)", border: "1px solid var(--border-light)" }}>
<div className="w-full rounded grid place-items-center" style={{ height: 44, background: "#FFFFFF", border: "1px dashed var(--border-light)" }}>
<ImageIcon size={14} style={{ color: "var(--text-muted)" }} />
</div>
<span className="text-[11px] font-semibold" style={{ color: "var(--text-primary)" }}>{p.name}-Paket</span>
<span className="text-[10px]" style={{ color: "var(--text-tertiary)" }}>{p.price}</span>
</div>
))}
</div>
);
case "divider":
return <div style={{ height: 1, background: "var(--border-light)" }} />;
}
}
export default function EmailBuilder({ hideHeader = false }: { hideHeader?: boolean }) {
const [blocks, setBlocks] = useState<Block[]>(INITIAL);
const [dragId, setDragId] = useState<string | null>(null);
const [resetKey, setResetKey] = useState(0); // bump to remount blocks → clears edited contentEditable text
const idRef = useRef(100);
const blockRefs = useRef<Record<string, HTMLDivElement | null>>({});
const hint = useCanvasHint<HTMLDivElement>();
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 (
<section className="section-full" style={{ background: "var(--surface)", paddingBlock: hideHeader ? 40 : 80 }}>
<div className="mx-auto flex flex-col gap-6" style={{ maxWidth: "var(--content-width)" }}>
{!hideHeader && (
<div className="flex flex-col items-center gap-4 text-center">
<span className="text-[12px] font-semibold tracking-widest chamfer-badge px-2 py-0.5" style={{ background: "var(--accent-yellow-light)", color: "var(--accent-yellow-dark)" }}>
E-MAIL-BAUKASTEN
</span>
<h2 className="text-[32px] md:text-[42px] font-bold leading-[1.15]" style={{ color: "var(--text-primary)" }}>Kampagnen selbst gebaut.</h2>
</div>
)}
{/* Builder on a dotted "canvas" — same treatment as the workflow editor
& kanban. Reset sits in the corner; the popup replaces the caption. */}
<div
ref={hint.ref}
className="relative rounded-2xl"
style={{
background: "#FAFBFD",
border: "1px solid var(--border-light)",
backgroundImage: "radial-gradient(circle, #E5E7EB 1px, transparent 1px)",
backgroundSize: "24px 24px",
padding: "32px 24px",
}}
>
{/* Reset — top-right corner of the canvas */}
<button
onClick={reset}
className="absolute top-3 right-3 z-[40] inline-flex items-center gap-1.5 text-[12.5px] font-semibold rounded-lg px-2.5 py-1.5 transition-colors hover:bg-[var(--bg-muted)]"
style={{ color: "var(--text-secondary)", background: "#FFFFFF", border: "1px solid var(--border-light)" }}
>
<RotateCcw size={13} /> Zurücksetzen
</button>
<div className="flex flex-col lg:flex-row gap-8 lg:gap-10 justify-center items-center lg:items-start">
{/* Palette */}
<div className="w-full lg:w-[230px] min-w-0 grid grid-cols-3 lg:grid-cols-1 gap-2.5">
{PALETTE.map(({ type, label, icon: Icon }) => (
<button
key={type}
onClick={() => addBlock(type)}
className="group flex items-center gap-2.5 rounded-lg text-left bg-white border border-[var(--border-light)] shadow-sm transition-colors duration-150 hover:border-[var(--accent-yellow)] hover:shadow-md"
style={{ padding: "9px 11px" }}
>
{/* Icon sits neutral at rest; brand amber only appears on hover
(an amber tile on every row read as generic/vibecoded). */}
<span
className="grid place-items-center rounded-md shrink-0 transition-colors duration-150 bg-[var(--container-bg)] text-[var(--text-secondary)] group-hover:bg-[var(--accent-yellow-light)] group-hover:text-[var(--accent-yellow-dark)]"
style={{ width: 28, height: 28 }}
>
<Icon size={15} />
</span>
<span className="text-[13px] font-semibold min-w-0 truncate" style={{ color: "var(--text-primary)" }}>{label}</span>
<Plus
size={14}
strokeWidth={2.5}
className="ml-auto shrink-0 hidden lg:block opacity-0 -translate-x-1 transition-all duration-150 group-hover:opacity-100 group-hover:translate-x-0 text-[var(--accent-yellow-dark)]"
/>
</button>
))}
</div>
{/* Email preview */}
<div
className="w-full max-w-[440px] min-w-0 overflow-hidden rounded-2xl"
style={{ background: "#FFFFFF", border: "1px solid var(--border-light)", boxShadow: "0 30px 60px -30px rgba(15,23,42,0.3)" }}
>
{/* Mail header */}
<div className="flex items-center gap-3 px-5 py-3.5" style={{ borderBottom: "1px solid var(--border-light)", background: "var(--container-bg)" }}>
<span className="grid place-items-center rounded-full text-[13px] font-bold shrink-0" style={{ width: 34, height: 34, background: "var(--accent-yellow)", color: "var(--text-primary)" }}>G</span>
<div className="flex flex-col min-w-0">
<span className="text-[13px] font-semibold truncate" style={{ color: "var(--text-primary)" }}>Geerkens Sanitär &amp; Heizung GmbH</span>
<span className="text-[11.5px] truncate" style={{ color: "var(--text-tertiary)" }}>an Sie · buero@geerkens.de</span>
</div>
</div>
{/* Body blocks */}
<div className="flex flex-col gap-4 p-5" style={{ touchAction: dragId ? "none" : undefined }}>
{blocks.length === 0 && (
<p className="text-[13px] text-center py-8" style={{ color: "var(--text-muted)" }}>
Fügen Sie links Blöcke hinzu …
</p>
)}
{blocks.map((b) => {
const isDragging = dragId === b.id;
return (
<div
key={`${resetKey}-${b.id}`}
ref={(el) => {
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,
}}
>
<BlockPreview type={b.type} />
{/* Drag handle */}
<span
onPointerDown={() => { 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"
>
<GripVertical size={13} style={{ color: "var(--text-muted)" }} />
</span>
{/* Remove */}
<button
onClick={() => removeBlock(b.id)}
className="absolute right-0 top-0 translate-x-1/2 -translate-y-1/2 grid place-items-center rounded-full opacity-0 group-hover:opacity-100 transition hover:text-[#EF4444] hover:border-[#EF4444]"
style={{ width: 24, height: 24, background: "#fff", border: "1px solid var(--border-light)", color: "var(--text-muted)" }}
aria-label="Block entfernen"
>
<Trash2 size={13} />
</button>
</div>
);
})}
</div>
</div>
</div>
{/* Centered onboarding hint (dismisses on first block add/drag or via X) */}
<CanvasHint show={hint.show} onDismiss={hint.dismiss} title="E-Mail bauen" subtitle="Klick links auf einen Block, um ihn hinzuzufügen" />
</div>
</div>
</section>
);
}