102 lines
5.4 KiB
TypeScript
102 lines
5.4 KiB
TypeScript
import Link from "next/link";
|
||
import { Search } from "lucide-react";
|
||
import { listUserSessions, RUNNER_FILTERS, type RunnerFilter, type SessionView } from "@peppa/core";
|
||
import { AutoRefresh } from "@/components/AutoRefresh";
|
||
import { Empty, Flash, PageHeader } from "@/components/common";
|
||
import { RunnerDetails, RunnerRow } from "@/components/RunnerConsole";
|
||
import { Input } from "@/components/ui/input";
|
||
import { Workspace } from "@/components/Workspace";
|
||
import { requireUser } from "@/lib/auth";
|
||
import { db } from "@/lib/db";
|
||
import { inbox, sessionEvents } from "@/lib/queries";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
const FILTERS: Array<{ key: RunnerFilter | undefined; label: string }> = [
|
||
{ key: undefined, label: "все" },
|
||
{ key: "waiting", label: "ждут тебя" },
|
||
{ key: "working", label: "работают" },
|
||
{ key: "stopped", label: "упали" },
|
||
{ key: "finished", label: "готово" },
|
||
];
|
||
const inFilter = (s: SessionView, f: RunnerFilter) => (RUNNER_FILTERS[f] as string[]).includes(s.status);
|
||
const matches = (s: SessionView, q: string) =>
|
||
[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 { 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] = await Promise.all([listUserSessions(db, user.id), inbox(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)} back={back} closeHref={url(base)} chosen={!!id} />
|
||
) : null;
|
||
|
||
return (
|
||
<Workspace aside={aside} asideWideOnly={!id}>
|
||
<AutoRefresh />
|
||
<Flash error={error} />
|
||
<PageHeader title="Раннеры">Агенты в 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="Фильтр раннеров">
|
||
{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>
|
||
{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" defaultValue={rawQ ?? ""} placeholder="Задача, проект, tmux…" aria-label="Поиск раннеров" className="h-8 pl-8" />
|
||
</form>
|
||
</div>
|
||
{shown.length === 0 ? (
|
||
<Empty>{q || filter ? "Таких раннеров нет." : "Раннеров пока не было: перенеси задачу в «В работе» на доске проекта."}</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>
|
||
);
|
||
}
|