import { ActionIcon, Avatar, Badge, Box, Combobox, Group, Loader, Paper, ScrollArea, Stack, Text, Textarea, Tooltip, useCombobox, } from "@mantine/core"; import { IconSend, IconTrash } from "@tabler/icons-react"; import { notifications } from "@mantine/notifications"; import { KeyboardEvent, ReactNode, useCallback, useEffect, useMemo, useRef, useState, } from "react"; import * as api from "../api"; import type { CardMessage, User } from "../types"; import { tagColor } from "./colors"; import { formatDateTimeShort } from "./format"; interface Props { cardId: string; users: User[]; currentUserId?: string; onMessagesChange?: (messages: CardMessage[]) => void; // When set, the panel scrolls the matching message into view and flashes a // brief highlight (~2s). Used by notification click → open card. highlightMessageId?: string; } // Window for considering a peer "actively typing" after its last event. const TYPING_LIFETIME_MS = 4000; // Minimum gap between successive typing pings emitted while the user types. const TYPING_THROTTLE_MS = 1500; interface MentionMatch { start: number; // index of '@' in the textarea value query: string; // text after '@', lowercased } function detectMention(value: string, cursor: number): MentionMatch | null { // Look backwards from cursor for an '@' that starts a word. for (let i = cursor - 1; i >= 0 && cursor - i <= 64; i--) { const ch = value[i]; if (ch === "@") { // Valid start: beginning of string or whitespace before. if (i === 0 || /\s/.test(value[i - 1])) { const q = value.slice(i + 1, cursor); if (/^[a-z0-9_.-]*$/i.test(q)) { return { start: i, query: q.toLowerCase() }; } } return null; } if (/\s/.test(ch)) return null; } return null; } const mentionRegex = /(^|\s)(@[a-z0-9][a-z0-9_.-]{0,63})/gi; function renderBody(body: string, knownUsers: Map): ReactNode { const out: ReactNode[] = []; let last = 0; let key = 0; for (const m of body.matchAll(mentionRegex)) { const handle = m[2].slice(1).toLowerCase(); const idx = (m.index ?? 0) + m[1].length; if (idx > last) out.push(body.slice(last, idx)); const user = knownUsers.get(handle); if (user) { out.push( @{user.username} , ); } else { out.push(`@${handle}`); } last = idx + m[2].length; } if (last < body.length) out.push(body.slice(last)); return out; } export function CardChatPanel({ cardId, users, currentUserId, onMessagesChange, highlightMessageId }: Props) { const [messages, setMessages] = useState([]); const [loading, setLoading] = useState(true); const [body, setBody] = useState(""); const [sending, setSending] = useState(false); const [typingUsers, setTypingUsers] = useState>({}); const [mention, setMention] = useState(null); const viewportRef = useRef(null); const wsRef = useRef(null); const textareaRef = useRef(null); const lastTypingEmitRef = useRef(0); const usersById = useMemo(() => new Map(users.map((u) => [u.id, u])), [users]); const usersByUsername = useMemo(() => new Map(users.map((u) => [u.username.toLowerCase(), u])), [users]); const reload = useCallback(async () => { try { const ms = await api.listCardMessages(cardId); setMessages(ms); onMessagesChange?.(ms); } catch (e) { notifications.show({ color: "red", message: (e as Error).message }); } finally { setLoading(false); } }, [cardId, onMessagesChange]); useEffect(() => { reload(); }, [reload]); // Open one WebSocket per cardId for realtime chat + typing. useEffect(() => { const ws = new WebSocket(api.cardChatWSURL(cardId)); wsRef.current = ws; ws.onmessage = (ev) => { try { const data = JSON.parse(ev.data) as | { type: "message.created"; message: CardMessage } | { type: "typing"; user_id: string } | { type: "error"; error: string }; if (data.type === "message.created" && data.message) { setMessages((prev) => { if (prev.some((m) => m.id === data.message!.id)) return prev; const next = [...prev, data.message!]; onMessagesChange?.(next); return next; }); } else if (data.type === "typing" && data.user_id) { setTypingUsers((prev) => ({ ...prev, [data.user_id]: Date.now() })); } else if (data.type === "error") { notifications.show({ color: "red", message: data.error }); } } catch { // ignore malformed } }; ws.onerror = () => { // browser will report; we keep the panel functional via REST fallback }; return () => { ws.close(); wsRef.current = null; }; }, [cardId, onMessagesChange]); // Sweep stale typing entries. useEffect(() => { const t = setInterval(() => { const now = Date.now(); setTypingUsers((prev) => { const next: Record = {}; for (const [k, v] of Object.entries(prev)) { if (now - v < TYPING_LIFETIME_MS) next[k] = v; } return next; }); }, 1000); return () => clearInterval(t); }, []); useEffect(() => { if (viewportRef.current) { viewportRef.current.scrollTo({ top: viewportRef.current.scrollHeight, behavior: "smooth" }); } }, [messages.length]); // Scroll to + briefly pulse the message that triggered an incoming // notification. Runs whenever the highlight id changes AND the message // is present in the list (it may arrive asynchronously after WS sync). const [pulse, setPulse] = useState(null); useEffect(() => { if (!highlightMessageId) return; if (!messages.some((m) => m.id === highlightMessageId)) return; const el = document.querySelector(`[data-msg-id="${highlightMessageId}"]`); if (el && el instanceof HTMLElement) { el.scrollIntoView({ behavior: "smooth", block: "center" }); } setPulse(highlightMessageId); const t = setTimeout(() => setPulse(null), 2200); return () => clearTimeout(t); }, [highlightMessageId, messages]); const sendTypingPing = () => { const ws = wsRef.current; if (!ws || ws.readyState !== WebSocket.OPEN) return; const now = Date.now(); if (now - lastTypingEmitRef.current < TYPING_THROTTLE_MS) return; lastTypingEmitRef.current = now; ws.send(JSON.stringify({ type: "typing" })); }; const combobox = useCombobox({ onDropdownClose: () => setMention(null), }); const mentionCandidates = useMemo(() => { if (!mention) return [] as User[]; return users .filter((u) => u.username.toLowerCase().startsWith(mention.query)) .slice(0, 8); }, [users, mention]); useEffect(() => { if (mention && mentionCandidates.length > 0) { combobox.openDropdown(); combobox.selectFirstOption(); } else { combobox.closeDropdown(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [mention?.query, mentionCandidates.length]); const insertMention = (username: string) => { if (!mention) return; const before = body.slice(0, mention.start); const after = body.slice(mention.start + 1 + mention.query.length); const inserted = `@${username} `; const next = before + inserted + after; setBody(next); setMention(null); // Restore caret right after the inserted mention. requestAnimationFrame(() => { const el = textareaRef.current; if (!el) return; const pos = (before + inserted).length; el.focus(); el.setSelectionRange(pos, pos); }); }; const send = async () => { const text = body.trim(); if (!text || sending) return; setSending(true); const ws = wsRef.current; try { if (ws && ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ type: "send", body: text })); // Optimistic clear; server will broadcast the persisted message. setBody(""); } else { const m = await api.createCardMessage(cardId, text); setMessages((prev) => [...prev, m]); onMessagesChange?.([...messages, m]); setBody(""); } } catch (e) { notifications.show({ color: "red", message: (e as Error).message }); } finally { setSending(false); } }; const remove = async (mid: string) => { try { await api.deleteCardMessage(cardId, mid); const next = messages.filter((m) => m.id !== mid); setMessages(next); onMessagesChange?.(next); } catch (e) { notifications.show({ color: "red", message: (e as Error).message }); } }; const onChange = (e: React.ChangeEvent) => { setBody(e.currentTarget.value); sendTypingPing(); const cursor = e.currentTarget.selectionStart ?? e.currentTarget.value.length; setMention(detectMention(e.currentTarget.value, cursor)); }; const onKeyDown = (e: KeyboardEvent) => { if (mention && mentionCandidates.length > 0 && (e.key === "Enter" || e.key === "Tab")) { e.preventDefault(); const sel = combobox.getSelectedOptionIndex(); const pick = mentionCandidates[Math.max(0, sel)]; if (pick) insertMention(pick.username); return; } if (mention && e.key === "Escape") { setMention(null); return; } if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); send(); } }; const typingNames = Object.keys(typingUsers) .filter((uid) => uid !== currentUserId) .map((uid) => { const u = usersById.get(uid); return u?.display_name || u?.username || "alguien"; }); return ( {loading ? ( ) : messages.length === 0 ? ( Sin mensajes aun. Escribe el primero. ) : ( {messages.map((m) => { const author = m.author_id ? usersById.get(m.author_id) : null; const isMe = m.author_id && m.author_id === currentUserId; const label = author ? author.display_name || author.username : "Anonimo"; const highlighted = pulse === m.id; return ( {label.slice(0, 2).toUpperCase()} {label} {formatDateTimeShort(m.created_at)} {isMe && ( remove(m.id)}> )} {renderBody(m.body, usersByUsername)} ); })} )} {typingNames.length > 0 && ( {typingNames.length === 1 ? `${typingNames[0]} esta escribiendo...` : `${typingNames.slice(0, 2).join(", ")}${typingNames.length > 2 ? "..." : ""} estan escribiendo...`} )} insertMention(value)} position="top-start" withinPortal={false} >