peppa/apps/web/app/(app)/runners/page.tsx
Асель 9052f3245f Merge main into task/pep-98: PEP-112…124 texts through the dictionary
Main's new wording (PEP-123 «Продакт» words) kept and wrapped in t()/m();
wording(mode, locale) gives English words for {task}/{runner} placeholders;
glossary check (pnpm lint) passes; English for all new keys (en/merge-*.ts).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:04:39 +05:00

118 lines
6.3 KiB
TypeScript

import Link from "next/link";
import { Search, SquareTerminal } from "lucide-react";
import { listUserProjects, runnerStories, listUserSessions, RUNNER_FILTERS, sessionSteps, type RunnerFilter, type SessionView } from "@peppa/core";
import { LiveRefresh } from "@/components/LiveRefresh";
import { Empty, Flash, PageHeader } from "@/components/common";
import { RunnerDetails, RunnerRow } from "@/components/RunnerConsole";
import { Input } from "@/components/ui/input";
import { NewRunnerForm } from "@/components/Runners";
import { SettingsDialog } from "@/components/SettingsDialog";
import { Workspace } from "@/components/Workspace";
import { requireUser, subscriptionNamesVisible } from "@/lib/auth";
import { db } from "@/lib/db";
import { inbox, sessionEvents } from "@/lib/queries";
import { cn } from "@/lib/utils";
import { getT } from "@/lib/i18n";
import { m } from "@peppa/shared/i18n";
const FILTERS: Array<{ key: RunnerFilter | undefined; label: string }> = [
{ key: undefined, label: m("все") },
{ key: "waiting", label: m("ждут вас") },
{ key: "working", label: m("работают") },
{ key: "stopped", label: m("упали") },
{ key: "finished", label: m("готово") },
];
const inFilter = (s: SessionView, f: RunnerFilter) => (RUNNER_FILTERS[f] as string[]).includes(s.status);
const matches = (s: SessionView, q: string) =>
[s.name, s.taskKey, s.taskTitle, s.projectKey, s.tmuxName, `#${s.id}`].some((v) => v?.toLowerCase().includes(q));
type Params = { error?: string; f?: string; q?: string; id?: string };
export default async function Runners({ searchParams }: { searchParams: Promise<Params> }) {
const user = await requireUser();
const t = await getT();
const { error, f, q: rawQ, id } = await searchParams;
const filter = f && f in RUNNER_FILTERS ? (f as RunnerFilter) : undefined;
const q = rawQ?.trim().toLowerCase() ?? "";
const [all, open, projects] = await Promise.all([listUserSessions(db, user.id), inbox(user.id), listUserProjects(db, user.id)]);
const asking = new Map<number, typeof open>();
for (const x of open) if (x.sessionId) asking.set(x.sessionId, [...(asking.get(x.sessionId) ?? []), x]);
const needsPm = (s: SessionView) => s.status === "waiting_input" || asking.has(s.id);
const found = q ? all.filter((s) => matches(s, q)) : all;
const count = (k: RunnerFilter | undefined) => (k ? found.filter((s) => inFilter(s, k)).length : found.length);
// Who needs the PM first, then live runners, then finished ones; newest first inside each group.
const rank = (s: SessionView) => (needsPm(s) ? 0 : ["done", "failed"].includes(s.status) ? 2 : 1);
const shown = (filter ? found.filter((s) => inFilter(s, filter)) : found).sort((a, b) => rank(a) - rank(b) || b.id - a.id);
const url = (p: { f?: string; q?: string; id?: number | string }) => {
const sp = new URLSearchParams();
if (p.f) sp.set("f", p.f);
if (p.q) sp.set("q", p.q);
if (p.id) sp.set("id", String(p.id));
const s = sp.toString();
return s ? `/runners?${s}` : "/runners";
};
const base = { f: filter, q: rawQ?.trim() || undefined };
const selected = (id && all.find((s) => s.id === Number(id))) || shown[0];
const back = url({ ...base, id: selected?.id });
const aside = selected ? (
<RunnerDetails s={selected} questions={asking.get(selected.id) ?? []} events={await sessionEvents(selected.id)} steps={await sessionSteps(db, selected.id)} stories={await runnerStories(db, selected.id)} back={back} closeHref={url(base)} chosen={!!id} names={await subscriptionNamesVisible()} />
) : null;
return (
<Workspace aside={aside} asideWideOnly={!id}>
<LiveRefresh scope="runners" />
<Flash error={error} />
<PageHeader
title={t("Работа агентов")}
actions={
projects.length > 0 && (
<SettingsDialog title={t("Новый терминал")} trigger={<><SquareTerminal />{t("Новый терминал")}</>}>
<NewRunnerForm projects={projects} back="/runners" />
</SettingsDialog>
)
}
>
{t("Агенты в tmux по стори ваших и общих проектов и свободные терминалы: кто ждёт вас, кто работает, что готово.")}
</PageHeader>
<div className="mb-3 flex flex-col gap-3 lg:flex-row lg:items-center">
<nav className="flex flex-wrap gap-1.5" aria-label={t("Фильтр раннеров")}>
{FILTERS.map((x) => {
const n = count(x.key);
const active = x.key === filter;
return (
<Link
key={x.label}
href={url({ ...base, f: x.key })}
aria-current={active ? "page" : undefined}
className={cn(
"flex items-center gap-1.5 rounded-md border bg-card px-2.5 py-1 text-sm text-muted-foreground hover:border-muted-foreground hover:text-foreground hover:no-underline",
active && "border-primary/60 bg-primary/10 text-foreground",
)}
>
<span className={cn("font-mono font-semibold tabular-nums", x.key === "waiting" && n > 0 ? "text-primary" : "text-foreground")}>{n}</span>
{t(x.label)}
</Link>
);
})}
</nav>
<form action="/runners" className="relative lg:ml-auto lg:w-64" role="search">
{filter && <input type="hidden" name="f" value={filter} />}
<Search className="pointer-events-none absolute top-1/2 left-2.5 size-4 -translate-y-1/2 text-muted-foreground" />
<Input type="search" name="q" data-hotkey-search defaultValue={rawQ ?? ""} placeholder={t("Стори, проект, tmux…")} aria-label={t("Поиск раннеров")} className="h-8 pl-8" />
</form>
</div>
{shown.length === 0 ? (
<Empty>{q || filter ? t("Таких раннеров нет.") : t("Раннеров пока не было: перенесите стори в «В работе» на доске проекта.")}</Empty>
) : (
<ul className="m-0 flex list-none flex-col gap-1 rounded-lg border bg-card/50 p-1.5">
{shown.map((s) => (
<RunnerRow key={s.id} s={s} href={url({ ...base, id: s.id })} selected={s.id === selected?.id && (id ? true : "wide")} needsPm={needsPm(s)} />
))}
</ul>
)}
</Workspace>
);
}