peppa/apps/web/components/Sidebar.tsx
2026-11-25 12:58:03 +00:00

132 lines
9.0 KiB
TypeScript
Raw 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.

"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { useEffect, useState } from "react";
import { useLive } from "@/lib/live";
import { Activity, Bot, PlugZap, Contact, FolderKanban, Gauge, GitBranch, Megaphone, House, KeyRound, MessagesSquare, ScrollText, LifeBuoy, LogOut, MessageCircleWarning, Search, Server, Settings, ShieldCheck, Sparkles, SquareTerminal, Users, type LucideIcon } from "lucide-react";
import { Brand } from "@/components/Brand";
import { ThemeToggle } from "@/components/ThemeToggle";
import { LanguageSwitcher } from "@/components/I18n";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { useT } from "@/components/I18n";
import { m } from "@peppa/shared/i18n";
interface Item {
href: string;
label: string;
icon: LucideIcon;
match: (p: string) => boolean;
count?: "questions" | "waitingRunners" | "chats" | "feedback";
/** Superadmin only (PEP-68, PEP-67; others get 404 on the URL too); desktop panel only — on phones it is linked from Settings. */
admin?: boolean;
/** Not in the phone tab bar (six tabs fit; PEP-62 added «Чаты»). */
desktopOnly?: boolean;
/** PEP-119: only in «Инженер» mode (terminals and other tools of a developer). */
engineer?: boolean;
}
const ITEMS: Item[] = [
{ href: "/", label: m("Главная"), icon: House, match: (p) => p === "/", count: "questions" },
{ href: "/chats", label: m("Чаты"), icon: MessagesSquare, match: (p) => p.startsWith("/chats"), count: "chats" },
{ href: "/projects", label: m("Проекты"), icon: FolderKanban, match: (p) => p.startsWith("/projects") || p.startsWith("/p/") || p.startsWith("/t/") },
// glossary-ignore: PEP-257, терминалы агентов — пункт только в виде «Инженер»
{ href: "/sessions", label: m("Сессии"), icon: SquareTerminal, match: (p) => p.startsWith("/sessions"), desktopOnly: true, engineer: true },
{ href: "/runners", label: m("Работа агентов"), icon: Bot, match: (p) => p.startsWith("/runners"), count: "waitingRunners", engineer: true },
{ href: "/connections", label: m("Подключения"), icon: PlugZap, match: (p) => p.startsWith("/connections"), desktopOnly: true },
{ href: "/team", label: m("Команда"), icon: Contact, match: (p) => p.startsWith("/team"), desktopOnly: true },
{ href: "/welcome", label: m("О FORGE"), icon: Sparkles, match: (p) => p.startsWith("/welcome"), desktopOnly: true },
{ href: "/help", label: m("Помощь"), icon: LifeBuoy, match: (p) => p.startsWith("/help") },
{ href: "/dashboard", label: m("Дашборд"), icon: Gauge, match: (p) => p.startsWith("/dashboard"), admin: true },
{ href: "/subscriptions", label: m("Подписки"), icon: KeyRound, match: (p) => p.startsWith("/subscriptions"), admin: true },
{ href: "/secrets", label: m("Секреты"), icon: ShieldCheck, match: (p) => p.startsWith("/secrets"), admin: true },
{ href: "/gitea", label: m("Gitea"), icon: GitBranch, match: (p) => p.startsWith("/gitea"), admin: true },
{ href: "/observability", label: m("Observability"), icon: Activity, match: (p) => p.startsWith("/observability"), admin: true },
{ href: "/hosts", label: m("Хосты"), icon: Server, match: (p) => p.startsWith("/hosts"), admin: true },
{ href: "/journal", label: m("Журнал"), icon: ScrollText, match: (p) => p.startsWith("/journal"), admin: true },
{ href: "/platform", label: m("Платформа"), icon: Megaphone, match: (p) => p.startsWith("/platform"), admin: true },
{ href: "/feedback", label: m("Обращения"), icon: MessageCircleWarning, match: (p) => p.startsWith("/feedback"), count: "feedback", admin: true },
{ href: "/users", label: m("Пользователи"), icon: Users, match: (p) => p.startsWith("/users"), admin: true },
{ href: "/settings", label: m("Настройки"), icon: Settings, match: (p) => p.startsWith("/settings") },
];
const COUNT_TITLE = { questions: m("{n} вопросов ждут вас"), waitingRunners: m("{n} агентов ждут вас"), chats: m("{n} новых сообщений в обсуждениях"), feedback: m("{n} обращений ждут решения") };
/** Left panel on desktop; on phones a top bar plus a bottom tab bar (PEP-38): sections with «ждут вас» counters, theme, user. */
export function Sidebar({ login, avatarUrl, counts: initialCounts, superadmin = false, engineer = false }: { login: string; avatarUrl: string | null; counts: Record<"questions" | "waitingRunners" | "chats" | "feedback", number>; superadmin?: boolean; engineer?: boolean }) {
const path = usePathname();
const t = useT();
// PEP-225: the «ждут вас» counters follow the live channel (`attention`) without a re-render.
const [live, setLive] = useState<Partial<typeof initialCounts>>({});
useEffect(() => setLive({}), [initialCounts]);
useLive("attention", (e) => {
if (e.type !== "data") return;
const d = e.data as { questions?: number; waitingRunners?: number; chats?: number };
setLive({ questions: d.questions, waitingRunners: d.waitingRunners, chats: d.chats });
});
const counts = { ...initialCounts, ...Object.fromEntries(Object.entries(live).filter(([, v]) => typeof v === "number")) };
const items = ITEMS.filter((i) => (!i.admin || superadmin) && (!i.engineer || engineer));
// The phone tab bar holds what is left after the desktop-only and admin sections: 5 or 6 tabs.
const phoneTabs = items.filter((i) => !i.admin && !i.desktopOnly).length;
return (
<header className="sticky top-0 z-30 flex h-14 items-center gap-2 border-b bg-sidebar px-3 md:h-dvh md:flex-col md:items-stretch md:gap-1 md:border-r md:border-b-0 md:px-3 md:py-4">
<Link href="/" className="flex shrink-0 items-center gap-2 rounded-md px-1 text-foreground hover:no-underline md:mb-4 md:px-2">
<Brand />
</Link>
{/* PEP-52: the palette listens for clicks on [data-open-palette]. */}
<button
type="button"
data-open-palette
title={t("Поиск и команды (⌘K)")}
className="ml-auto flex items-center gap-2 rounded-md border bg-background/60 px-2 py-1.5 text-sm text-muted-foreground transition-colors hover:text-foreground md:mb-2 md:ml-0"
>
<Search className="size-4" />
<span className="hidden md:inline">{t("Поиск")}</span>
<kbd className="ml-auto hidden rounded border bg-muted px-1 font-mono text-[10px] md:inline">⌘K</kbd>
</button>
<nav
aria-label={t("Разделы")}
style={{ gridTemplateColumns: `repeat(${phoneTabs}, minmax(0, 1fr))` }}
className="fixed inset-x-0 bottom-0 z-30 grid border-t bg-sidebar/95 pb-[env(safe-area-inset-bottom)] backdrop-blur md:static md:flex md:flex-col md:gap-0.5 md:border-0 md:bg-transparent md:pb-0 md:backdrop-blur-none"
>
{items.map((i) => {
const active = i.match(path);
const n = i.count ? counts[i.count] : 0;
return (
<Link
key={i.href}
href={i.href}
aria-current={active ? "page" : undefined}
className={cn(
(i.admin || i.desktopOnly) && "max-md:hidden",
"relative flex flex-col items-center gap-0.5 px-1 pt-2 pb-1.5 text-[10px] text-muted-foreground transition-colors hover:text-foreground hover:no-underline md:flex-row md:gap-2 md:rounded-md md:px-2.5 md:py-1.5 md:text-sm md:hover:bg-accent",
active && "font-medium text-foreground md:bg-accent md:before:absolute md:before:inset-y-1.5 md:before:-left-3 md:before:w-0.5 md:before:rounded-full md:before:bg-primary",
)}
>
<i.icon className={cn("size-5 md:size-4", active && "text-primary")} />
<span>{t(i.label)}</span>
{n > 0 && (
<span title={t(COUNT_TITLE[i.count!], { n })} className="absolute top-1 left-[calc(50%+6px)] min-w-4 rounded-full bg-primary px-1 text-center font-mono text-[10px] font-semibold leading-4 text-primary-foreground tabular-nums md:static md:ml-auto md:min-w-5 md:px-1.5 md:text-[11px] md:leading-5">
{n}
</span>
)}
</Link>
);
})}
</nav>
<div className="flex shrink-0 items-center gap-0.5 md:mt-auto md:gap-1 md:ml-0 md:flex-wrap md:border-t md:pt-3">
{/* PEP-136: the language is one click away on every page, not only in «Настройки». */}
<div className="max-md:[&_button]:px-1 md:mb-1 md:basis-full"><LanguageSwitcher /></div>
{avatarUrl && <img src={avatarUrl} alt="" width={24} height={24} className="hidden size-6 rounded-full md:block" />}
<span className="hidden min-w-0 flex-1 truncate text-sm md:block" title={login}>{login}</span>
<ThemeToggle />
<form action="/logout" method="post">
<Button variant="ghost" size="icon-sm" aria-label={t("Выйти")} title={t("Выйти")}>
<LogOut />
</Button>
</form>
</div>
</header>
);
}