"use client"; // PEP-228: new messages of the open conversation drawn in place. The page's chat scope // (`chat:`) brings them ready for this viewer (lib/chat-live.ts); what belongs to this // conversation and needs no buttons is added to the end of the feed at once — no server // re-render. Questions, offers, other threads go to LiveRefresh (the island leaves `m` to it). import { useEffect, useRef, useState, type ReactNode } from "react"; import { Bot, MessagesSquare, User as UserIcon } from "lucide-react"; import { PRODUCT_NAME } from "@peppa/shared/i18n"; import { AgentBadge } from "@/components/AgentBadge"; import { Attachments } from "@/components/Attachments"; import { ChatScroll } from "@/components/ChatScroll"; import { useT } from "@/components/I18n"; import { ago, linkify } from "@/lib/format"; import { useLiveIsland } from "@/lib/live"; import { cn } from "@/lib/utils"; import { chatParts, type ChatView, type LiveChatMessage } from "@/lib/chat-live-types"; import { markChatReadAction } from "@/app/(app)/chats/actions"; export function ChatLiveFeed({ chatId, lastId, view, userId, compact, showThread, agentName, typing, empty, children, }: { chatId: number; /** The last message the server drew: anything newer is ours to draw. */ lastId: number; view: ChatView; userId: number; /** Events as compact lines (threads and «События агентов»). */ compact: boolean; showThread: boolean; agentName: string | null; /** «Аналитик печатает…» while the analyst has not answered yet (PEP-110); hidden once an answer is drawn. */ typing: ReactNode; /** The server drew no messages (its children are the empty state). */ empty: boolean; children: ReactNode; }) { const t = useT(); const [added, setAdded] = useState([]); const [answered, setAnswered] = useState(false); const read = useRef | null>(null); const scope = `chat:${chatId}`; const thread = view.kind === "thread" ? view.key : view.kind === "discussion" ? null : undefined; // A server re-render drew them itself. const shown = added.filter((m) => m.id > lastId); useEffect(() => setAnswered(false), [lastId]); const markRead = () => { if (thread === undefined || read.current) return; read.current = setTimeout(() => { read.current = null; if (document.visibilityState === "visible") void markChatReadAction(chatId, thread).catch(() => {}); }, 1000); }; useEffect(() => () => { if (read.current) clearTimeout(read.current); }, []); useLiveIsland((e) => { if (e.scope !== scope) return []; const { applied, add } = chatParts(e, view); const fresh = add.filter((m) => m.id > lastId); if (fresh.length) { setAdded((was) => { const have = new Set(was.map((m) => m.id)); return [...was, ...fresh.filter((m) => !have.has(m.id))].sort((a, b) => a.id - b.id); }); if (view.kind === "discussion" && fresh.some((m) => m.authorKind !== "user")) setAnswered(true); markRead(); } return applied; }); const groups: Array<{ events: LiveChatMessage[] } | { m: LiveChatMessage }> = []; for (const m of shown) { const event = compact && (m.kind === "event" || m.kind === "system"); const last = groups.at(-1); if (event && last && "events" in last) last.events.push(m); else groups.push(event ? { events: [m] } : { m }); } return ( <> {!(empty && shown.length) && children} {shown.length > 0 && (
    {groups.map((g) => "events" in g ? (
  1. {g.events.map((m) => (
    {m.authorKind === "agent" && (m.agent ?? agentName) && {m.agent ?? agentName}:} {m.main.split("\n")[0]} {m.link && ( {t("открыть")} )} {m.repeats > 0 && ×{m.repeats + 1}} {ago(new Date(m.at), t)}
    ))}
  2. ) : (
  3. ), )}
)} {!answered && typing}
); } /** The client twin of the page's MessageItem for a message without buttons. */ function LiveMessage({ m, mine, showThread, agentName }: { m: LiveChatMessage; mine: boolean; showThread: boolean; agentName: string | null }) { const t = useT(); const person = m.authorKind === "user" && m.kind === "user"; const agentLabel = m.authorKind === "agent" ? (m.agent ?? agentName) : null; return (
{person ? : agentLabel ? null : m.authorKind === "agent" ? : } {agentLabel ? ( ) : ( {person ? (mine ? t("вы") : (m.login ?? "?")) : m.authorKind === "agent" ? t("агент") : PRODUCT_NAME} )} {m.via === "telegram" && · Telegram} {showThread && m.thread && ( 🧵 {m.thread.label} )} {m.repeats > 0 && ( ×{m.repeats + 1} )} {ago(new Date(m.at), t)}

{linkify(m.main)}

{m.more && (
{t("Подробнее")}

{linkify(m.more)}

)} {m.link && ( {m.link.startsWith("/") ? t("Открыть") : m.link} )}
); }