peppa/apps/web/components/ChatThreadList.tsx
peppa-bot e8247924c6 PEP-207: чаты — понятно, что от меня нужно, и это можно сделать на месте
Агент не меняет статус принятой задачи (причина противоречия в PEP-204);
«Посмотреть результат» / «Принять» / «Поправить» в сообщении «готово»;
переписка сверху вниз с полем ввода внизу; строка состояния вместо служебных
сообщений; компактные события; очистка «Всех событий»; выбор проекта списком;
«Задача описана. Начать работу?».

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 00:30:02 +05:00

163 lines
7.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Link from "next/link";
import { Search, X } from "lucide-react";
import type { ThreadList, ThreadRow } from "@peppa/core";
import { AgentBadge } from "@/components/AgentBadge";
import { ago, StatusBadge } from "@/components/common";
import { Hidden } from "@/components/cards";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { getT } from "@/lib/i18n";
import { words } from "@/lib/auth";
import { cn } from "@/lib/utils";
const COUNT = "rounded-full px-1.5 font-mono text-[11px] leading-4 font-semibold tabular-nums";
/**
* PEP-155: the conversations of a project chat — the chat with the analyst and the agents' events
* on top, then story threads in groups «Ждут вас», «В работе», «Готово» (folded), with a search.
*/
export async function ChatThreadList({
base,
list,
active,
discussion,
feed,
raw,
}: {
/** `/chats/<KEY>` */
base: string;
list: ThreadList;
/** The open thread's key, if a thread is open. */
active: string | null;
discussion: boolean;
feed: boolean;
/** The `thread` parameter as it is in the URL: the search keeps the open conversation. */
raw: string | null;
}) {
const t = await getT();
const w = await words();
const q = list.q;
const href = (thread: string | null) => {
const p = new URLSearchParams();
if (thread) p.set("thread", thread);
if (q) p.set("q", q);
return p.size ? `${base}?${p}` : base;
};
const total = list.waiting.length + list.working.length + list.done.length;
const rows = (items: ThreadRow[]) => items.map((r) => <Row key={r.key} r={r} href={href(r.key)} active={active === r.key} />);
return (
<nav aria-label={t("Треды")} className="flex w-full min-w-0 flex-col gap-0.5 rounded-lg border bg-card/50 p-1.5 text-sm">
<form action={base} method="get" role="search" className="mb-1 flex items-center gap-1">
{/* On a phone the answer to a search is the list, not the open conversation. */}
<Hidden fields={{ list: 1, ...(raw ? { thread: raw } : {}) }} />
<Input type="search" name="q" defaultValue={q ?? ""} maxLength={100} placeholder={t("Найти в переписке…")} aria-label={t("Поиск по переписке")} className="h-8 min-w-0 flex-1 text-sm" />
<Button size="sm" variant="outline" aria-label={t("Найти")} title={t("Найти")} className="px-2">
<Search />
</Button>
{q && (
<Link href={raw ? `${base}?thread=${encodeURIComponent(raw)}&list=1` : `${base}?list=1`} aria-label={t("Сбросить поиск")} title={t("Сбросить поиск")} className="grid size-8 shrink-0 place-items-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground">
<X className="size-4" />
</Link>
)}
</form>
<Plain href={href(null)} active={discussion} title={t("💬 Чат с аналитиком")} meta={t("вопросы и новые задачи")} />
<Plain href={href("feed")} active={feed} title={t("События агентов")} meta={t("журнал: что делали агенты")} />
{q && total === 0 ? (
<p className="m-0 px-2 py-3 text-muted-foreground">{t("Ничего не нашлось. Попробуйте другое слово.")}</p>
) : total === 0 ? (
<p className="m-0 px-2 py-1.5 text-xs text-muted-foreground">{t("Переписки по {tasks} пока нет.", { tasks: w.task("datPl") })}</p>
) : (
<>
{(!q || list.waiting.length > 0) && (
<section aria-label={t("Ждут вас")} data-group="waiting" className="flex flex-col gap-0.5">
<GroupTitle count={list.waiting.length} tone={list.waiting.length ? "bg-primary text-primary-foreground" : undefined}>
{t("Ждут вас")}
</GroupTitle>
{list.waiting.length ? rows(list.waiting) : <p className="m-0 px-2 py-1 text-xs text-muted-foreground">{t("Сейчас от вас ничего не нужно.")}</p>}
</section>
)}
{list.working.length > 0 && (
<section aria-label={t("В работе")} data-group="working" className="flex flex-col gap-0.5">
<GroupTitle count={list.working.length}>{t("В работе")}</GroupTitle>
{rows(list.working)}
</section>
)}
{list.done.length > 0 && (
<details data-group="done" open={!!q || list.done.some((r) => r.key === active)} className="group/done flex flex-col">
<summary className="flex cursor-pointer list-none items-center gap-1.5 rounded-md px-2 pt-2 pb-1 text-[11px] tracking-wide text-muted-foreground uppercase hover:text-foreground [&::-webkit-details-marker]:hidden">
<span aria-hidden className="transition-transform group-open/done:rotate-90">▸</span>
{t("Готово")}
<span className={cn(COUNT, "bg-muted text-muted-foreground")}>{list.done.length}</span>
</summary>
<div className="flex flex-col gap-0.5">{rows(list.done)}</div>
</details>
)}
</>
)}
</nav>
);
}
function GroupTitle({ children, count, tone = "bg-muted text-muted-foreground" }: { children: React.ReactNode; count: number; tone?: string }) {
return (
<h2 className="m-0 flex items-center gap-1.5 px-2 pt-2 pb-1 text-[11px] font-normal tracking-wide text-muted-foreground uppercase">
{children}
{count > 0 && <span className={cn(COUNT, tone)}>{count}</span>}
</h2>
);
}
function Plain({ href, active, title, meta }: { href: string; active: boolean; title: string; meta: string }) {
return (
<Link href={href} aria-current={active ? "page" : undefined} className={cn("flex flex-col rounded-md px-2 py-1.5 text-foreground hover:bg-accent hover:no-underline", active && "bg-accent font-medium")}>
<span className="truncate">{title}</span>
<span className="text-[11px] font-normal text-muted-foreground">{meta}</span>
</Link>
);
}
async function Row({ r, href, active }: { r: ThreadRow; href: string; active: boolean }) {
const t = await getT();
const title = r.taskKey ? r.title : t.text(r.title);
return (
<Link
href={href}
data-thread={r.key}
aria-current={active ? "page" : undefined}
className={cn("flex flex-col gap-1 rounded-md px-2 py-2 text-foreground hover:bg-accent hover:no-underline", active && "bg-accent")}
>
<span className="flex items-baseline gap-1.5">
{r.taskKey && <span className="shrink-0 font-mono text-xs text-muted-foreground">{r.taskKey}</span>}
<span className={cn("min-w-0 flex-1 truncate", (active || r.unread > 0) && "font-medium")} title={title}>
{title}
</span>
{r.open > 0 && (
<span className={cn(COUNT, "self-center bg-primary text-primary-foreground")} title={t("ждут ответа")}>
?
</span>
)}
{r.unread > 0 && (
<span className={cn(COUNT, "self-center bg-info text-background")} title={t("Новых сообщений: {n}", { n: r.unread })}>
{r.unread > 99 ? "99+" : r.unread}
</span>
)}
</span>
{(r.status || r.agentName || r.lastAt) && (
<span className="flex min-w-0 flex-wrap items-center gap-x-2 gap-y-1">
{r.status && <StatusBadge status={r.status} />}
{r.agentName && <AgentBadge name={r.agentName} />}
{r.lastAt && <span className="ml-auto shrink-0 font-mono text-[11px] text-muted-foreground">{ago(r.lastAt, t)}</span>}
</span>
)}
{r.last && (
<span className="truncate text-xs text-muted-foreground">
{r.last.mine && t("вы") + ": "}
{r.last.system ? t.text(r.last.text) : r.last.text}
</span>
)}
</Link>
);
}