377 lines
17 KiB
TypeScript
377 lines
17 KiB
TypeScript
"use client";
|
||
|
||
import { useEffect, useRef, useState } from "react";
|
||
import CanvasHint, { useCanvasHint } from "@/components/ui/CanvasHint";
|
||
|
||
/* ──────────────────────────────────────────────────────────────────────────
|
||
* KanbanBoard — compact, interactive product centrepiece for the
|
||
* projektmanagement page. Tight columns (subtle light panels), small white
|
||
* cards, draggable between columns (mouse + touch).
|
||
*
|
||
* Card look modelled on a real PM/CRM board (Pipedrive/Linear-style): a tag +
|
||
* priority-flag row, a prominent title, and a footer with overlapping assignee
|
||
* avatars and a single prioritised status chip. All in the HiveGrid amber
|
||
* palette with inline SVG icons — no external icon/theme deps.
|
||
* ────────────────────────────────────────────────────────────────────────── */
|
||
|
||
type Priority = "high" | "medium" | "low";
|
||
|
||
type Card = {
|
||
id: string;
|
||
title: string;
|
||
tag: string;
|
||
tagColor: string;
|
||
who: string;
|
||
initials: string;
|
||
priority: Priority;
|
||
/** second, overlapping avatar (e.g. the assigned monteur) */
|
||
monteur?: string;
|
||
monteurInitials?: string;
|
||
/** status chip, prioritised: overdue › due soon › task count › none */
|
||
overdueDays?: number;
|
||
dueDays?: number;
|
||
taskCount?: number;
|
||
};
|
||
type Column = { id: string; title: string; accent: string; cards: Card[] };
|
||
|
||
// Subtle "drag me" wiggle for the top todo card — mostly at rest, a quick nudge
|
||
// every few seconds. Stops after the first drag; disabled for reduced motion.
|
||
const NUDGE_CSS = `
|
||
@keyframes kanban-nudge{0%,78%,100%{transform:rotate(0deg)}83%{transform:rotate(-2.6deg)}88%{transform:rotate(2.4deg)}93%{transform:rotate(-1.4deg)}97%{transform:rotate(0.6deg)}}
|
||
.kanban-nudge{animation:kanban-nudge 3.4s ease-in-out infinite;transform-origin:60% 65%}
|
||
@media (prefers-reduced-motion:reduce){.kanban-nudge{animation:none}}
|
||
`;
|
||
|
||
const INITIAL: Column[] = [
|
||
{
|
||
id: "todo",
|
||
title: "Zu erledigen",
|
||
accent: "#93C5FD",
|
||
cards: [
|
||
{ id: "c1", title: "Angebot Sanitär — Müller", tag: "Angebot", tagColor: "#3B82F6", who: "Lena Krause", initials: "LK", priority: "high", dueDays: 1 },
|
||
{ id: "c2", title: "Baustelle Weber vorbereiten", tag: "Baustelle", tagColor: "#E8A317", who: "Tom Berger", initials: "TB", monteur: "Stefan Müller", monteurInitials: "SM", priority: "medium", taskCount: 3 },
|
||
{ id: "c3", title: "Material bestellen — Berger", tag: "Material", tagColor: "#F97316", who: "Sara Möller", initials: "SM", priority: "low", overdueDays: 2 },
|
||
],
|
||
},
|
||
{
|
||
id: "doing",
|
||
title: "In Arbeit",
|
||
accent: "#F1B729",
|
||
cards: [
|
||
{ id: "c4", title: "Heizungstausch — Schmidt", tag: "Heizung", tagColor: "#E8A317", who: "Tom Berger", initials: "TB", monteur: "Jonas Weiß", monteurInitials: "JW", priority: "high", dueDays: 0 },
|
||
{ id: "c5", title: "Bad-Renovierung — Fischer", tag: "Sanitär", tagColor: "#0D6E6E", who: "Lena Krause", initials: "LK", priority: "medium", taskCount: 5 },
|
||
],
|
||
},
|
||
{
|
||
id: "done",
|
||
title: "Erledigt",
|
||
accent: "#22C55E",
|
||
cards: [
|
||
{ id: "c6", title: "Wartung Anlage Nord", tag: "Wartung", tagColor: "#22C55E", who: "Sara Möller", initials: "SM", priority: "low" },
|
||
{ id: "c7", title: "Abnahme — Solarcarport", tag: "Abnahme", tagColor: "#22C55E", who: "Jonas Weiß", initials: "JW", priority: "low" },
|
||
],
|
||
},
|
||
];
|
||
|
||
/* ─── card sub-parts ──────────────────────────────────────────────────── */
|
||
|
||
function PriorityFlag({ priority }: { priority: Priority }) {
|
||
const color = priority === "high" ? "#EF4444" : priority === "medium" ? "#F1B729" : "#CBD5E1";
|
||
const tip = priority === "high" ? "Hohe Priorität" : priority === "medium" ? "Mittlere Priorität" : "Niedrige Priorität";
|
||
return (
|
||
<span title={tip} className="flex-none" style={{ lineHeight: 0 }}>
|
||
<svg width="13" height="13" viewBox="0 0 24 24" fill={color} stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||
<path d="M4 15s1-1 4-1 5 2 8 2 4-1 4-1V3s-1 1-4 1-5-2-8-2-4 1-4 1z" />
|
||
<line x1="4" y1="22" x2="4" y2="15" />
|
||
</svg>
|
||
</span>
|
||
);
|
||
}
|
||
|
||
function Avatar({ initials, title, variant = "who", overlap = false }: { initials: string; title: string; variant?: "who" | "monteur"; overlap?: boolean }) {
|
||
const bg = variant === "monteur" ? "#FFEDD5" : "#E5E7EB";
|
||
const fg = variant === "monteur" ? "#C2410C" : "#475569";
|
||
return (
|
||
<span
|
||
title={title}
|
||
className="grid place-items-center rounded-full flex-none text-[9px] font-bold"
|
||
style={{ width: 20, height: 20, background: bg, color: fg, boxShadow: "0 0 0 2px #FFFFFF", marginLeft: overlap ? -6 : 0 }}
|
||
>
|
||
{initials}
|
||
</span>
|
||
);
|
||
}
|
||
|
||
function StatusChip({ card }: { card: Card }) {
|
||
const base = "inline-flex items-center gap-1 rounded text-[10px] font-semibold px-1.5 py-0.5";
|
||
if (card.overdueDays && card.overdueDays > 0) {
|
||
return (
|
||
<span className={base} style={{ background: "#FEE2E2", color: "#B91C1C" }} title="Überfällig">
|
||
<svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z" /><line x1="12" y1="9" x2="12" y2="13" /><line x1="12" y1="17" x2="12.01" y2="17" /></svg>
|
||
{card.overdueDays} T überfällig
|
||
</span>
|
||
);
|
||
}
|
||
if (card.dueDays !== undefined && card.dueDays <= 3) {
|
||
const label = card.dueDays === 0 ? "Termin Heute" : card.dueDays === 1 ? "Termin Morgen" : `Termin in ${card.dueDays} T`;
|
||
return (
|
||
<span className={base} style={{ background: "#DBEAFE", color: "#1D4ED8" }} title="Anstehender Termin">
|
||
<svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="4" width="18" height="18" rx="2" ry="2" /><line x1="16" y1="2" x2="16" y2="6" /><line x1="8" y1="2" x2="8" y2="6" /><line x1="3" y1="10" x2="21" y2="10" /></svg>
|
||
{label}
|
||
</span>
|
||
);
|
||
}
|
||
if (card.taskCount && card.taskCount > 0) {
|
||
return (
|
||
<span className={base} style={{ background: "#F1F5F9", color: "#64748B" }} title={`${card.taskCount} offene Aufgaben`}>
|
||
<svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><polyline points="9 11 12 14 22 4" /><path d="M21 12v7a2 2 0 01-2 2H5a2 2 0 01-2-2V5a2 2 0 012-2h11" /></svg>
|
||
{card.taskCount}
|
||
</span>
|
||
);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
function CardBody({ card }: { card: Card }) {
|
||
const hasFooter = !!card.initials || !!card.monteurInitials || card.overdueDays || card.dueDays !== undefined || card.taskCount;
|
||
return (
|
||
<div className="flex flex-col gap-2">
|
||
{/* Row 1: tag (left) · priority flag (right) */}
|
||
<div className="flex items-start justify-between gap-2">
|
||
<span
|
||
className="inline-flex text-[10px] font-bold uppercase tracking-wider px-1.5 py-0.5 rounded self-start"
|
||
style={{ background: `${card.tagColor}1f`, color: card.tagColor, border: `1px solid ${card.tagColor}33` }}
|
||
>
|
||
{card.tag}
|
||
</span>
|
||
<PriorityFlag priority={card.priority} />
|
||
</div>
|
||
|
||
{/* Title */}
|
||
<span className="text-[13.5px] font-semibold leading-snug" style={{ color: "var(--text-primary)" }}>
|
||
{card.title}
|
||
</span>
|
||
|
||
{/* Footer: avatars + single status chip */}
|
||
{hasFooter && (
|
||
<div className="flex items-center justify-between gap-2 pt-2 mt-0.5" style={{ borderTop: "1px solid var(--border-light)" }}>
|
||
<span className="flex items-center min-h-[20px]">
|
||
<Avatar initials={card.initials} title={card.who} />
|
||
{card.monteurInitials && (
|
||
<Avatar initials={card.monteurInitials} title={`Monteur: ${card.monteur ?? ""}`} variant="monteur" overlap />
|
||
)}
|
||
</span>
|
||
<StatusChip card={card} />
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export default function KanbanBoard({ hideHeader = false }: { hideHeader?: boolean }) {
|
||
const [columns, setColumns] = useState<Column[]>(INITIAL);
|
||
const [dragging, setDragging] = useState<
|
||
{ cardId: string; fromCol: string; offX: number; offY: number; w: number } | null
|
||
>(null);
|
||
const [pos, setPos] = useState({ x: 0, y: 0 });
|
||
const [overCol, setOverCol] = useState<string | null>(null);
|
||
const [hinted, setHinted] = useState(false); // true once the user drags anything → stop the nudge
|
||
|
||
const posRef = useRef({ x: 0, y: 0 });
|
||
const colRefs = useRef<Record<string, HTMLDivElement | null>>({});
|
||
|
||
// Centered onboarding hint — appears when the board scrolls into view,
|
||
// dismissed on the first drag or via the X.
|
||
const hint = useCanvasHint<HTMLDivElement>();
|
||
|
||
const draggedCard = dragging
|
||
? columns.find((c) => c.id === dragging.fromCol)?.cards.find((k) => k.id === dragging.cardId) ?? null
|
||
: null;
|
||
|
||
const colUnderPointer = (x: number, y: number): string | null => {
|
||
for (const [id, el] of Object.entries(colRefs.current)) {
|
||
if (!el) continue;
|
||
const r = el.getBoundingClientRect();
|
||
if (x >= r.left && x <= r.right && y >= r.top && y <= r.bottom) return id;
|
||
}
|
||
return null;
|
||
};
|
||
|
||
useEffect(() => {
|
||
if (!dragging) return;
|
||
|
||
// Stop text from being selected anywhere on the page while dragging.
|
||
document.body.style.setProperty("user-select", "none");
|
||
document.body.style.setProperty("-webkit-user-select", "none");
|
||
|
||
const onMove = (e: PointerEvent) => {
|
||
const x = e.clientX;
|
||
const y = e.clientY;
|
||
posRef.current = { x, y };
|
||
setPos({ x, y });
|
||
setOverCol(colUnderPointer(x, y));
|
||
};
|
||
const onUp = () => {
|
||
const target = colUnderPointer(posRef.current.x, posRef.current.y);
|
||
if (target && target !== dragging.fromCol) {
|
||
setColumns((cols) => {
|
||
const card = cols.find((c) => c.id === dragging.fromCol)?.cards.find((k) => k.id === dragging.cardId);
|
||
if (!card) return cols;
|
||
return cols.map((c) => {
|
||
if (c.id === dragging.fromCol) return { ...c, cards: c.cards.filter((k) => k.id !== dragging.cardId) };
|
||
if (c.id === target) return { ...c, cards: [...c.cards, card] };
|
||
return c;
|
||
});
|
||
});
|
||
}
|
||
setDragging(null);
|
||
setOverCol(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");
|
||
};
|
||
}, [dragging]);
|
||
|
||
const startDrag = (e: React.PointerEvent, colId: string, card: Card) => {
|
||
if (e.button !== 0 && e.pointerType === "mouse") return;
|
||
setHinted(true);
|
||
hint.dismiss();
|
||
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
|
||
posRef.current = { x: e.clientX, y: e.clientY };
|
||
setPos({ x: e.clientX, y: e.clientY });
|
||
setOverCol(colId);
|
||
setDragging({ cardId: card.id, fromCol: colId, offX: e.clientX - rect.left, offY: e.clientY - rect.top, w: rect.width });
|
||
};
|
||
|
||
return (
|
||
<section
|
||
className="section-full"
|
||
style={{ background: "var(--surface)", paddingBlock: hideHeader ? 40 : 80, userSelect: dragging ? "none" : undefined }}
|
||
>
|
||
<style dangerouslySetInnerHTML={{ __html: NUDGE_CSS }} />
|
||
<div className="mx-auto flex flex-col gap-5" style={{ maxWidth: 880 }}>
|
||
{!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)" }}
|
||
>
|
||
KANBAN-BOARD
|
||
</span>
|
||
<h2 className="text-[32px] md:text-[42px] font-bold leading-[1.15]" style={{ color: "var(--text-primary)" }}>
|
||
Projekte im Blick.
|
||
</h2>
|
||
</div>
|
||
)}
|
||
|
||
{/* Board on a dotted "canvas" — same treatment as the workflow editor.
|
||
The onboarding popup lives inside it and replaces the old 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: 16,
|
||
}}
|
||
>
|
||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3 items-start" style={{ touchAction: dragging ? "none" : undefined }}>
|
||
{columns.map((col) => {
|
||
const isOver = dragging != null && overCol === col.id;
|
||
return (
|
||
<div
|
||
key={col.id}
|
||
ref={(el) => {
|
||
colRefs.current[col.id] = el;
|
||
}}
|
||
className="flex flex-col gap-2.5 rounded-xl transition-colors"
|
||
style={{
|
||
minHeight: 210,
|
||
padding: 10,
|
||
// a clearly-outlined but soft lane so each column is visibly
|
||
// separated (dots still shimmer through the light fill);
|
||
// amber dashed drop-zone while a card is dragged over it
|
||
background: isOver ? "rgba(241,183,41,0.08)" : "rgba(255,255,255,0.55)",
|
||
border: isOver ? "1px dashed var(--accent-yellow)" : "1px solid #CBD5E1",
|
||
}}
|
||
>
|
||
{/* Column header */}
|
||
<div className="flex items-center gap-2 px-0.5 pb-0.5">
|
||
<span className="inline-block" style={{ width: 7, height: 7, borderRadius: 2, background: col.accent }} />
|
||
<span className="text-[11px] font-bold uppercase tracking-wider" style={{ color: "var(--text-secondary)" }}>
|
||
{col.title}
|
||
</span>
|
||
<span
|
||
className="ml-auto grid place-items-center text-[10px] font-bold rounded-full"
|
||
style={{ minWidth: 18, height: 18, padding: "0 5px", background: "var(--border-light)", color: "var(--text-secondary)" }}
|
||
>
|
||
{col.cards.length}
|
||
</span>
|
||
</div>
|
||
|
||
{/* Cards */}
|
||
{col.cards.map((card, idx) => {
|
||
const isDragging = dragging?.cardId === card.id;
|
||
const nudge = col.id === "todo" && idx === 0 && !hinted && !isDragging;
|
||
return (
|
||
<div
|
||
key={card.id}
|
||
onPointerDown={(e) => startDrag(e, col.id, card)}
|
||
className={`shadow-sm hover:shadow-md transition-shadow ${nudge ? "select-none kanban-nudge" : "select-none"}`}
|
||
style={{
|
||
background: "#FFFFFF",
|
||
border: "1px solid var(--border-light)",
|
||
borderRadius: 10,
|
||
padding: "11px 12px",
|
||
cursor: isDragging ? "grabbing" : "grab",
|
||
opacity: isDragging ? 0.4 : 1,
|
||
touchAction: "none",
|
||
}}
|
||
>
|
||
<CardBody card={card} />
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
|
||
{/* Centered onboarding hint (dismisses on first drag or via X) */}
|
||
<CanvasHint show={hint.show} onDismiss={hint.dismiss} title="Karten verschieben" subtitle="Zieh eine Karte in eine andere Spalte" />
|
||
</div>
|
||
</div>
|
||
|
||
{/* Floating drag ghost */}
|
||
{dragging && draggedCard && (
|
||
<div
|
||
className="fixed pointer-events-none z-[9999]"
|
||
style={{ left: pos.x - dragging.offX, top: pos.y - dragging.offY, width: dragging.w, transform: "rotate(2deg)" }}
|
||
>
|
||
<div
|
||
style={{
|
||
background: "#FFFFFF",
|
||
border: "1px solid var(--accent-yellow)",
|
||
borderRadius: 10,
|
||
padding: "11px 12px",
|
||
boxShadow: "0 22px 44px -14px rgba(15,23,42,0.4)",
|
||
}}
|
||
>
|
||
<CardBody card={draggedCard} />
|
||
</div>
|
||
</div>
|
||
)}
|
||
</section>
|
||
);
|
||
}
|