agenthub/frontend/components/TopBar.tsx

174 lines
5.4 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

"use client";
import {
Bot,
Building2,
Cable,
LayoutDashboard,
ListChecks,
MessagesSquare,
Moon,
Plus,
Settings,
Sun,
Users,
} from "lucide-react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { useEffect, useState } from "react";
import { DispatchModal } from "./DispatchModal";
import { useLive } from "./LiveProvider";
import { Dot } from "./ui";
import { getApiKey, keyShapeWarning, onApiKeyChange, setApiKey } from "@/lib/api";
import { setTheme, useTheme } from "@/lib/theme";
import { Field, Input, Modal, PixelButton, useConfirm } from "./ui";
const NAV = [
{ href: "/", label: "总览", icon: LayoutDashboard },
{ href: "/office", label: "办公室", icon: Building2 },
{ href: "/workbench", label: "工作台", icon: MessagesSquare },
{ href: "/agents", label: "Agents", icon: Bot },
{ href: "/tasks", label: "任务", icon: ListChecks },
{ href: "/teams", label: "团队", icon: Users },
{ href: "/connect", label: "接入", icon: Cable },
{ href: "/settings", label: "设置", icon: Settings },
];
const SOCKET_LABEL: Record<string, string> = {
open: "实时",
connecting: "连接中",
closed: "已断线",
};
export function TopBar() {
const pathname = usePathname();
const { status } = useLive();
const theme = useTheme();
const confirm = useConfirm();
const [editing, setEditing] = useState(false);
const [dispatching, setDispatching] = useState(false);
const [key, setKey] = useState("");
useEffect(() => {
setKey(getApiKey());
return onApiKeyChange(() => setKey(getApiKey()));
}, []);
const active = (href: string) =>
href === "/" ? pathname === "/" : pathname === href || pathname.startsWith(href + "/");
return (
<header className="pixel-panel flex flex-wrap items-center gap-2 px-3 py-2">
<Link href="/" className="flex items-center gap-2 pr-2">
<span className="text-[16px]">🕹️</span>
<span className="pixel-title text-[13px] tracking-[0.12em]">AgentHub</span>
</Link>
<nav className="flex flex-wrap items-center gap-1">
{NAV.map((item) => (
<Link
key={item.href}
href={item.href}
className={"pixel-nav" + (active(item.href) ? " pixel-nav--on" : "")}
>
<item.icon size={13} />
{item.label}
</Link>
))}
</nav>
<div className="ml-auto flex items-center gap-2">
<PixelButton size="sm" variant="primary" onClick={() => setDispatching(true)}>
<Plus size={12} />
派活
</PixelButton>
<PixelButton
size="sm"
onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
aria-label={theme === "dark" ? "切换到亮色模式" : "切换到暗色模式"}
aria-pressed={theme === "light"}
>
{theme === "dark" ? <Sun size={12} /> : <Moon size={12} />}
{theme === "dark" ? "亮色" : "暗色"}
</PixelButton>
<span className="pixel-badge">
<Dot tone={status === "open" ? "green" : status === "connecting" ? "yellow" : "red"} />
{SOCKET_LABEL[status] ?? status}
</span>
<PixelButton size="sm" onClick={() => setEditing(true)}>
{key ? "API Key 已配置" : "配置 API Key"}
</PixelButton>
{key && (
<PixelButton
size="sm"
variant="danger"
onClick={async () => {
const ok = await confirm({
title: "清除本机登录?",
confirmLabel: "清除",
danger: true,
});
if (!ok) return;
setApiKey("");
setKey("");
}}
>
退出登录
</PixelButton>
)}
</div>
{dispatching && <DispatchModal onClose={() => setDispatching(false)} />}
{editing && (
<Modal
title="连接 Hub"
onClose={() => setEditing(false)}
footer={
<>
<PixelButton onClick={() => setEditing(false)}>取消</PixelButton>
{getApiKey() && (
<PixelButton
variant="danger"
onClick={async () => {
const ok = await confirm({
title: "清除本机登录?",
confirmLabel: "清除",
danger: true,
});
if (!ok) return;
setApiKey("");
setKey("");
setEditing(false);
}}
>
清除登录
</PixelButton>
)}
<PixelButton
variant="primary"
onClick={() => {
setApiKey(key);
setEditing(false);
}}
>
保存
</PixelButton>
</>
}
>
<div className="grid gap-3">
<Field label="Hub URL">
<Input value={process.env.NEXT_PUBLIC_HUB_URL ?? ""} readOnly />
</Field>
<Field label="Console API Key" hint={keyShapeWarning(key) || undefined}>
<Input
value={key}
placeholder="ah_..."
onChange={(event) => setKey(event.target.value)}
/>
</Field>
</div>
</Modal>
)}
</header>
);
}