329 lines
18 KiB
TypeScript
329 lines
18 KiB
TypeScript
"use client";
|
||
|
||
import { FormEvent, useEffect, useRef, useState } from "react";
|
||
import { Hash, SendHorizontal, UserRound } from "lucide-react";
|
||
|
||
/* ──────────────────────────────────────────────────────────────────────────
|
||
* WebChatDemo — interactive desktop chat workspace for the chat subpage. Four
|
||
* project channels, each with a fixed conversation. Click a channel to open it
|
||
* (its unread badge clears). When you send a message, the team's conversation
|
||
* simply CONTINUES — the next scripted message replies to the thread, not to
|
||
* your line — so it feels like a live chat you dropped into. Own messages use
|
||
* the site's secondary colour (teal). The mobile app is a "coming soon" below.
|
||
* ────────────────────────────────────────────────────────────────────────── */
|
||
|
||
const OWN_BG = "#0D6E6E";
|
||
const OWN_BORDER = "#0A5A5A";
|
||
const OTHER_BG = "#FFFFFF";
|
||
const OTHER_BORDER = "#E9E7E5";
|
||
const SENDER = "#C4931E";
|
||
|
||
type Author = "team" | "self" | "assistant";
|
||
type Message = { id: string; sender: string; text: string; time: string; author: Author };
|
||
type ScriptMsg = { sender: string; text: string; author: Author };
|
||
type Channel = { id: string; name: string; meta: string; unread: number; seed: Message[]; followups: ScriptMsg[] };
|
||
|
||
// Fixed per-channel conversations (identical on every refresh). `followups`
|
||
// continue the thread after each user message — they never address the user.
|
||
const CHANNELS: Channel[] = [
|
||
{
|
||
id: "heizung",
|
||
name: "Heizungsprojekt Schulz",
|
||
meta: "4 Mitglieder · 2 online",
|
||
unread: 0,
|
||
seed: [
|
||
{ id: "h1", sender: "Murat Kaya", text: "Foto vom Heizungskeller hochgeladen.", time: "09:12", author: "team" },
|
||
{ id: "h2", sender: "Lea Brandt", text: "Aufmaß ist fertig, schicke gleich die Unterlagen.", time: "09:15", author: "team" },
|
||
{ id: "h3", sender: "Du", text: "Super, dann können wir morgen mit der Installation anfangen. 👍", time: "09:17", author: "self" },
|
||
{ id: "h4", sender: "Murat Kaya", text: "Material ist bestellt, Lieferung Donnerstag.", time: "09:20", author: "team" },
|
||
{ id: "h5", sender: "Daniel Reiter", text: "Hinweis: Für morgen fehlt noch eine verbindliche Terminbestätigung vom Kunden.", time: "09:21", author: "assistant" },
|
||
],
|
||
followups: [
|
||
{ sender: "Lea Brandt", text: "Der Kunde meldet sich sonst eigentlich immer recht zuverlässig zurück.", author: "team" },
|
||
{ sender: "Murat Kaya", text: "Werkzeug und Material für morgen liegen jedenfalls schon bereit.", author: "team" },
|
||
{ sender: "Tom Berger", text: "Wetter soll morgen auch passen – trocken und mild.", author: "team" },
|
||
{ sender: "Sara Möller", text: "Dann steht dem Start ja nichts mehr im Weg. 👍", author: "team" },
|
||
{ sender: "Murat Kaya", text: "Bin gespannt, wird ein schönes Projekt.", author: "team" },
|
||
],
|
||
},
|
||
{
|
||
id: "bad",
|
||
name: "Bad-Renovierung Fischer",
|
||
meta: "3 Mitglieder · 1 online",
|
||
unread: 3,
|
||
seed: [
|
||
{ id: "b1", sender: "Lea Brandt", text: "Fliesen sind heute früh angekommen, alles vollständig.", time: "08:40", author: "team" },
|
||
{ id: "b2", sender: "Tom Berger", text: "Alte Wanne ist raus, der Estrich muss aber noch trocknen.", time: "08:52", author: "team" },
|
||
{ id: "b3", sender: "Du", text: "Top. Ab wann kann der Fliesenleger ran?", time: "09:03", author: "self" },
|
||
{ id: "b4", sender: "Tom Berger", text: "Ab Donnerstag, dann ist der Estrich durch.", time: "09:05", author: "team" },
|
||
{ id: "b5", sender: "Daniel Reiter", text: "Erinnerung: Die Sonderanfertigung für die Dusche muss bis Mittwoch bestellt sein.", time: "09:08", author: "assistant" },
|
||
],
|
||
followups: [
|
||
{ sender: "Tom Berger", text: "Der Kunde war heute kurz da und ist echt zufrieden mit dem Fortschritt.", author: "team" },
|
||
{ sender: "Lea Brandt", text: "Die Armaturen sind übrigens auch schon im Lager angekommen.", author: "team" },
|
||
{ sender: "Murat Kaya", text: "Fugenfarbe wird Anthrazit, da hat sich der Kunde festgelegt.", author: "team" },
|
||
{ sender: "Sara Möller", text: "Dann liegen wir ja voll im Zeitplan – sauber.", author: "team" },
|
||
{ sender: "Tom Berger", text: "👍 Läuft rund hier.", author: "team" },
|
||
],
|
||
},
|
||
{
|
||
id: "team",
|
||
name: "Team Allgemein",
|
||
meta: "12 Mitglieder · 5 online",
|
||
unread: 0,
|
||
seed: [
|
||
{ id: "t1", sender: "Sara Möller", text: "Morgen um 8 kurzes Teammeeting zur Wochenplanung.", time: "07:30", author: "team" },
|
||
{ id: "t2", sender: "Jonas Weiß", text: "Bin ab 10 auf der Baustelle Weber, danach im Büro.", time: "07:45", author: "team" },
|
||
{ id: "t3", sender: "Du", text: "Passt. Die Kaffeekasse ist übrigens wieder leer 😅", time: "07:50", author: "self" },
|
||
{ id: "t4", sender: "Lea Brandt", text: "😄 Ich kümmere mich drum, bring gleich was mit.", time: "07:52", author: "team" },
|
||
],
|
||
followups: [
|
||
{ sender: "Jonas Weiß", text: "Danke Lea! 🙌", author: "team" },
|
||
{ sender: "Sara Möller", text: "Kurze Info fürs Team: Der neue Transporter ist ab Freitag da.", author: "team" },
|
||
{ sender: "Murat Kaya", text: "Oh nice, wurde auch Zeit. 😄", author: "team" },
|
||
{ sender: "Sara Möller", text: "Details und Schlüsselübergabe kommen noch per Mail.", author: "team" },
|
||
{ sender: "Tom Berger", text: "Top, danke fürs Update! 👍", author: "team" },
|
||
],
|
||
},
|
||
{
|
||
id: "wartung",
|
||
name: "Wartung & Service",
|
||
meta: "5 Mitglieder · 3 online",
|
||
unread: 1,
|
||
seed: [
|
||
{ id: "w1", sender: "Murat Kaya", text: "Notdienst-Anruf: Heizung bei Familie Krause ausgefallen.", time: "11:02", author: "team" },
|
||
{ id: "w2", sender: "Du", text: "Wer kann heute noch hin?", time: "11:04", author: "self" },
|
||
{ id: "w3", sender: "Tom Berger", text: "Ich fahre direkt nach dem Mittag raus.", time: "11:06", author: "team" },
|
||
{ id: "w4", sender: "Daniel Reiter", text: "Neuer Wartungsvertrag Bäckerei Nowak startet am 01.08. – Ersttermin einplanen.", time: "11:10", author: "assistant" },
|
||
],
|
||
followups: [
|
||
{ sender: "Sara Möller", text: "Für die Bäckerei Nowak wäre Montagvormittag ein guter Ersttermin, oder?", author: "team" },
|
||
{ sender: "Lea Brandt", text: "Klingt gut, der Kunde ist da erfahrungsgemäß flexibel.", author: "team" },
|
||
{ sender: "Tom Berger", text: "Bei Familie Krause war's vorhin übrigens nur die Umwälzpumpe – läuft wieder.", author: "team" },
|
||
{ sender: "Murat Kaya", text: "Ah, gut zu wissen. Die war ja schon länger auffällig.", author: "team" },
|
||
{ sender: "Sara Möller", text: "Dann haben wir für heute ja alles im Griff. 👍", author: "team" },
|
||
],
|
||
},
|
||
];
|
||
|
||
// Neutral team continuations once a channel's scripted follow-ups run out.
|
||
const GENERIC: ScriptMsg[] = [
|
||
{ sender: "Lea Brandt", text: "Alles klar, machen wir so.", author: "team" },
|
||
{ sender: "Murat Kaya", text: "👍", author: "team" },
|
||
{ sender: "Tom Berger", text: "Passt für mich.", author: "team" },
|
||
{ sender: "Sara Möller", text: "Notiert, danke!", author: "team" },
|
||
{ sender: "Daniel Reiter", text: "Ich halte alles im Projekt fest.", author: "assistant" },
|
||
];
|
||
|
||
function nowTime(): string {
|
||
return new Intl.DateTimeFormat("de-DE", { hour: "2-digit", minute: "2-digit" }).format(new Date());
|
||
}
|
||
function newId(): string {
|
||
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return crypto.randomUUID();
|
||
return `web-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
||
}
|
||
|
||
export default function WebChatDemo() {
|
||
const [activeId, setActiveId] = useState<string>(CHANNELS[0].id);
|
||
const [threads, setThreads] = useState<Record<string, Message[]>>(() =>
|
||
Object.fromEntries(CHANNELS.map((c) => [c.id, c.seed]))
|
||
);
|
||
const [unread, setUnread] = useState<Record<string, number>>(() =>
|
||
Object.fromEntries(CHANNELS.map((c) => [c.id, c.unread]))
|
||
);
|
||
const [draft, setDraft] = useState("");
|
||
const [replyingIn, setReplyingIn] = useState<string | null>(null);
|
||
const scrollRef = useRef<HTMLDivElement>(null);
|
||
// how many follow-ups a channel has already revealed
|
||
const followIdx = useRef<Record<string, number>>({});
|
||
|
||
const active = CHANNELS.find((c) => c.id === activeId) ?? CHANNELS[0];
|
||
const messages = threads[activeId] ?? [];
|
||
const busy = replyingIn === activeId;
|
||
|
||
useEffect(() => {
|
||
const el = scrollRef.current;
|
||
if (el) el.scrollTop = el.scrollHeight;
|
||
}, [messages, activeId, replyingIn]);
|
||
|
||
function selectChannel(id: string) {
|
||
setActiveId(id);
|
||
setUnread((u) => (u[id] ? { ...u, [id]: 0 } : u));
|
||
setDraft("");
|
||
}
|
||
|
||
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
|
||
event.preventDefault();
|
||
const trimmed = draft.trim();
|
||
if (!trimmed || busy) return;
|
||
|
||
const channelId = activeId;
|
||
const channel = CHANNELS.find((c) => c.id === channelId) ?? CHANNELS[0];
|
||
const userMsg: Message = { id: newId(), sender: "Du", text: trimmed, time: nowTime(), author: "self" };
|
||
setThreads((t) => ({ ...t, [channelId]: [...(t[channelId] ?? []), userMsg] }));
|
||
setDraft("");
|
||
setReplyingIn(channelId);
|
||
|
||
// The conversation just continues — reveal the next scripted line (which
|
||
// answers the previous thread, not the user's message).
|
||
const i = followIdx.current[channelId] ?? 0;
|
||
const next =
|
||
channel.followups[i] ?? GENERIC[(i - channel.followups.length) % GENERIC.length];
|
||
followIdx.current[channelId] = i + 1;
|
||
|
||
await new Promise((r) => setTimeout(r, 750));
|
||
|
||
setThreads((t) => ({
|
||
...t,
|
||
[channelId]: [...(t[channelId] ?? []), { id: newId(), sender: next.sender, text: next.text, time: nowTime(), author: next.author }],
|
||
}));
|
||
setReplyingIn((prev) => (prev === channelId ? null : prev));
|
||
}
|
||
|
||
return (
|
||
<section className="section-full" style={{ background: "var(--surface)", paddingBlock: 80 }}>
|
||
{/* Desktop chat workspace */}
|
||
<div
|
||
className="mx-auto w-full overflow-hidden flex"
|
||
style={{
|
||
maxWidth: 940,
|
||
height: 520,
|
||
background: "#FFFFFF",
|
||
border: "1px solid var(--border-light)",
|
||
borderRadius: 16,
|
||
boxShadow: "0 24px 60px -24px rgba(15,23,42,0.28)",
|
||
}}
|
||
>
|
||
{/* Sidebar — clickable channels */}
|
||
<aside
|
||
className="hidden md:flex flex-col shrink-0"
|
||
style={{ width: 240, background: "var(--container-bg)", borderRight: "1px solid var(--border-light)" }}
|
||
>
|
||
<div className="flex items-center gap-2.5 px-4 shrink-0" style={{ height: 60, borderBottom: "1px solid var(--border-light)" }}>
|
||
<span
|
||
className="shrink-0 overflow-hidden"
|
||
style={{
|
||
width: 36,
|
||
height: 31, // flat-top hexagon (2:√3), matches the honeycomb cells
|
||
clipPath: "polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%)",
|
||
}}
|
||
>
|
||
<img src="/geerkens-logo.png" alt="Geerkens" className="w-full h-full object-cover" draggable={false} />
|
||
</span>
|
||
<span className="flex flex-col min-w-0">
|
||
<span className="text-[13px] font-bold truncate" style={{ color: "var(--text-primary)" }}>Geerkens GmbH</span>
|
||
<span className="text-[11px]" style={{ color: "var(--text-tertiary)" }}>Workspace</span>
|
||
</span>
|
||
</div>
|
||
<div className="flex flex-col gap-0.5 p-2 overflow-hidden">
|
||
<span className="px-2 pt-2 pb-1 text-[10px] font-bold uppercase tracking-widest" style={{ color: "var(--text-tertiary)" }}>Projekte</span>
|
||
{CHANNELS.map((c) => {
|
||
const isActive = c.id === activeId;
|
||
const count = unread[c.id] ?? 0;
|
||
return (
|
||
<button
|
||
key={c.id}
|
||
type="button"
|
||
onClick={() => selectChannel(c.id)}
|
||
className="flex items-center gap-2 rounded-lg px-2.5 py-2 text-[13px] text-left transition-colors"
|
||
style={{
|
||
background: isActive ? "var(--accent-yellow-light)" : "transparent",
|
||
color: isActive ? "var(--accent-yellow-dark)" : "var(--text-secondary)",
|
||
fontWeight: isActive ? 700 : 500,
|
||
}}
|
||
onMouseEnter={(e) => { if (!isActive) e.currentTarget.style.background = "rgba(0,0,0,0.035)"; }}
|
||
onMouseLeave={(e) => { if (!isActive) e.currentTarget.style.background = "transparent"; }}
|
||
>
|
||
<Hash size={14} strokeWidth={2.4} className="shrink-0" />
|
||
<span className="truncate">{c.name}</span>
|
||
{count > 0 && (
|
||
<span className="ml-auto grid place-items-center rounded-full text-[10px] font-bold text-white" style={{ minWidth: 18, height: 18, padding: "0 5px", background: "var(--accent-teal)" }}>
|
||
{count}
|
||
</span>
|
||
)}
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
</aside>
|
||
|
||
{/* Main thread */}
|
||
<div className="flex flex-col flex-1 min-w-0">
|
||
{/* Channel header */}
|
||
<div className="flex items-center gap-2.5 px-5 shrink-0" style={{ height: 60, borderBottom: "1px solid var(--border-light)" }}>
|
||
<Hash size={18} strokeWidth={2.4} style={{ color: "var(--text-muted)" }} />
|
||
<span className="flex flex-col min-w-0">
|
||
<span className="text-[15px] font-bold truncate" style={{ color: "var(--text-primary)" }}>{active.name}</span>
|
||
<span className="text-[11.5px]" style={{ color: "var(--text-secondary)" }}>{active.meta}</span>
|
||
</span>
|
||
</div>
|
||
|
||
{/* Messages */}
|
||
<div ref={scrollRef} className="chat-scroll-hidden flex flex-col gap-2.5 flex-1 overflow-y-auto" style={{ background: "hsl(30 17% 96.5%)", padding: "16px 18px" }}>
|
||
{messages.map((m) => (
|
||
<div key={m.id} className={`flex items-end gap-2.5 ${m.author === "self" ? "justify-end" : "justify-start"}`}>
|
||
{m.author !== "self" && (
|
||
<span className="grid place-items-center rounded-full shrink-0" style={{ width: 30, height: 30, background: "#E8DDCE" }}>
|
||
<UserRound size={14} color="#7F6D58" strokeWidth={2.3} />
|
||
</span>
|
||
)}
|
||
<div
|
||
className="text-[14px] leading-snug max-w-[70%]"
|
||
style={{
|
||
backgroundColor: m.author === "self" ? OWN_BG : OTHER_BG,
|
||
borderRadius: m.author === "self" ? "16px 16px 4px 16px" : "16px 16px 16px 4px",
|
||
padding: "9px 13px",
|
||
color: m.author === "self" ? "#FFFFFF" : "var(--text-primary)",
|
||
boxShadow: "0 1px 2px rgba(0,0,0,0.06)",
|
||
border: `1px solid ${m.author === "self" ? OWN_BORDER : OTHER_BORDER}`,
|
||
}}
|
||
>
|
||
{m.author !== "self" && (
|
||
<p className="mb-0.5 text-[12px] font-semibold" style={{ color: SENDER }}>{m.sender}</p>
|
||
)}
|
||
{m.text}
|
||
<p className="mt-1 text-[10.5px]" style={{ color: m.author === "self" ? "rgba(255,255,255,0.82)" : "#7A726C" }}>{m.time}</p>
|
||
</div>
|
||
</div>
|
||
))}
|
||
{busy && (
|
||
<div className="flex items-end gap-2.5 justify-start">
|
||
<span className="grid place-items-center rounded-full shrink-0" style={{ width: 30, height: 30, background: "#E8DDCE" }}>
|
||
<UserRound size={14} color="#7F6D58" strokeWidth={2.3} />
|
||
</span>
|
||
<div className="flex items-center gap-1" style={{ background: OTHER_BG, borderRadius: "16px 16px 16px 4px", padding: "12px 14px", border: `1px solid ${OTHER_BORDER}` }}>
|
||
{[0, 1, 2].map((i) => (
|
||
<span key={i} className="rounded-full" style={{ width: 6, height: 6, background: "#B8AC94", animation: `wc-typing 1s ${i * 0.15}s infinite` }} />
|
||
))}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* Input */}
|
||
<form onSubmit={(e) => void handleSubmit(e)} className="flex items-center gap-2.5 shrink-0" style={{ padding: "12px 16px", borderTop: "1px solid var(--border-light)" }}>
|
||
<input
|
||
value={draft}
|
||
onChange={(e) => setDraft(e.target.value)}
|
||
placeholder={`Nachricht an #${active.name} …`}
|
||
className="flex-1 min-w-0 rounded-full text-[14px]"
|
||
style={{ background: "var(--container-bg)", height: 42, padding: "0 18px", color: "#111827", outline: "none", border: "1px solid var(--border-light)" }}
|
||
/>
|
||
<button
|
||
type="submit"
|
||
disabled={!draft.trim() || busy}
|
||
aria-label="Senden"
|
||
className="flex items-center justify-center rounded-full shrink-0 transition-opacity"
|
||
style={{ width: 42, height: 42, background: !draft.trim() || busy ? "#DCCFB8" : "var(--accent-yellow)", opacity: !draft.trim() || busy ? 0.8 : 1 }}
|
||
>
|
||
<SendHorizontal size={17} color="#1A1A1A" />
|
||
</button>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
|
||
<style>{`@keyframes wc-typing{0%,60%,100%{transform:translateY(0);opacity:0.5}30%{transform:translateY(-4px);opacity:1}}@media(prefers-reduced-motion:reduce){[style*="wc-typing"]{animation:none!important}}`}</style>
|
||
</section>
|
||
);
|
||
}
|