eust-w f1d71164ae 🐛 Render assistant Markdown and validated topology diagrams
Replace plain-text replies with react-markdown and GFM tables, lists,
code and safe links. Recognize complete topology fences, JSON fences and
standalone graph data in source order without promoting ordinary JSON.
Render escaped SVG elements with cycle-safe layouts, zoom, node details
and accessible data tables; show incomplete graphs as recoverable errors.

Preserve finish_reason=length, prevent truncated tool calls from executing,
and give concise topology replies a 4096-token budget. Tighten the prompt
to return bounded, closed diagrams rather than an exhaustive node dump.

Add parser, model-completion and isolated browser regression coverage.
Fix existing frontend type declarations exposed by the React types now
provided with react-markdown, without changing workflow execution.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-04 21:19:41 +08:00

339 lines
13 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.

import React, { useEffect, useMemo, useState } from "react";
import { NavLink, useLocation } from "react-router-dom";
import { Logo } from "./Logo";
import { useUser } from "../context/UserContext";
import {
LayoutDashboard,
Bot,
FolderKanban,
FileCode,
Cpu,
Activity,
ClipboardCheck,
ClipboardList,
Settings,
Users,
ChevronLeft,
ChevronRight,
User,
X,
Mail,
Shield,
Database,
Cloudy,
CloudUpload,
DatabaseZap,
BarChart3,
LogOut,
Loader2,
} from "lucide-react";
import { cn } from "../lib/utils";
import { getActionErrorMessage } from "../lib/action-errors.js";
import { getUserRoleLabel } from "../lib/user-roles";
import { preloadRoute, scheduleIdleRoutePreload } from "../lib/route-preload.js";
const navItems = [
{
icon: LayoutDashboard,
label: "仪表盘",
path: "/",
roles: ["admin", "pipeline_engineer", "data_operator", "qa_reviewer", "compliance_auditor"],
},
{
icon: Bot,
label: "运维助手",
path: "/assistant",
roles: ["admin", "pipeline_engineer", "data_operator", "qa_reviewer", "compliance_auditor"],
},
{
icon: FolderKanban,
label: "项目管理",
path: "/projects",
roles: ["admin", "pipeline_engineer", "data_operator"],
},
{
icon: BarChart3,
label: "数据运营",
path: "/data-operations",
roles: ["admin", "pipeline_engineer", "data_operator", "qa_reviewer", "compliance_auditor"],
},
{
icon: FileCode,
label: "工作流模板",
path: "/templates",
roles: ["admin", "pipeline_engineer"],
},
{ icon: Cpu, label: "算子管理", path: "/operators", roles: ["admin", "pipeline_engineer"] },
{ icon: Database, label: "业务数据库", path: "/database", roles: ["admin"] },
{ icon: Cloudy, label: "对象存储", path: "/object-storage", roles: ["admin"] },
{
icon: CloudUpload,
label: "drosync 同步",
path: "/object-sync",
roles: ["admin", "pipeline_engineer", "data_operator", "compliance_auditor"],
},
{
icon: DatabaseZap,
label: "数据资产",
path: "/data-assets",
roles: ["admin", "pipeline_engineer", "data_operator", "qa_reviewer", "compliance_auditor"],
},
{
icon: Activity,
label: "执行监控",
path: "/monitoring",
roles: ["admin", "pipeline_engineer", "data_operator", "qa_reviewer", "compliance_auditor"],
},
{ icon: ClipboardCheck, label: "任务审核", path: "/audit", roles: ["admin", "qa_reviewer"] },
{ icon: ClipboardList, label: "抽检任务", path: "/sampling", roles: ["admin", "qa_reviewer"] },
{ icon: Users, label: "用户管理", path: "/users", roles: ["admin"] },
{ icon: Settings, label: "系统配置", path: "/settings", roles: ["admin"] },
] as const;
interface SidebarProps {
isCollapsed: boolean;
onToggle: () => void;
}
type ProfileDraft = {
name: string;
email: string;
};
export function Sidebar({ isCollapsed, onToggle }: SidebarProps) {
const [isProfileModalOpen, setIsProfileModalOpen] = useState(false);
const [profileDraft, setProfileDraft] = useState<ProfileDraft | null>(null);
const [profileError, setProfileError] = useState("");
const [isSavingProfile, setIsSavingProfile] = useState(false);
const { currentUser, role, name, email, updateCurrentUser, logout } = useUser();
useEffect(() => {
if (!isProfileModalOpen || !currentUser) {
return;
}
setProfileError("");
setProfileDraft({
name: currentUser.name,
email: currentUser.email,
});
}, [currentUser, isProfileModalOpen]);
const filteredNavItems = useMemo(() => {
if (!role) {
return [];
}
return navItems.filter((item) => item.roles.some((allowed) => allowed === role));
}, [role]);
const { pathname } = useLocation();
// Warm up the remaining navigable pages in the background once the current
// page has settled; hover/focus below covers anything not fetched yet.
useEffect(() => {
const paths = filteredNavItems.map((item) => item.path).filter((path) => path !== pathname);
return scheduleIdleRoutePreload(paths, preloadRoute);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filteredNavItems]);
const handleSaveProfile = async (event: React.FormEvent) => {
event.preventDefault();
if (!profileDraft) {
return;
}
setIsSavingProfile(true);
setProfileError("");
try {
await updateCurrentUser({
name: profileDraft.name,
email: profileDraft.email,
});
setIsProfileModalOpen(false);
} catch (error) {
console.error("Failed to update current user", error);
setProfileError(getActionErrorMessage(error, "保存当前用户失败"));
} finally {
setIsSavingProfile(false);
}
};
const handleLogout = () => {
logout();
setIsProfileModalOpen(false);
};
const avatarLabel = currentUser?.name?.slice(0, 2).toUpperCase() || "??";
return (
<aside
className={cn(
"relative flex h-screen shrink-0 flex-col border-r border-[#2a2b2f] bg-[#151619] transition-all duration-300",
isCollapsed ? "w-20" : "w-64",
)}
>
<button
onClick={onToggle}
className="absolute -right-3 top-20 z-50 hidden h-6 w-6 items-center justify-center rounded-full border border-[#3a3b3f] bg-[#2a2b2f] text-gray-400 hover:text-white md:flex"
>
{isCollapsed ? <ChevronRight className="w-4 h-4" /> : <ChevronLeft className="w-4 h-4" />}
</button>
<div className={cn("p-6 border-b border-[#2a2b2f]", isCollapsed && "px-4")}>
<Logo showText={!isCollapsed} />
</div>
<nav className="flex-1 p-4 space-y-1 overflow-y-auto overflow-x-hidden">
{filteredNavItems.map((item) => (
<NavLink
key={item.path}
to={item.path}
title={isCollapsed ? item.label : undefined}
onMouseEnter={() => void preloadRoute(item.path)}
onFocus={() => void preloadRoute(item.path)}
onTouchStart={() => void preloadRoute(item.path)}
className={({ isActive }) =>
cn(
"flex items-center gap-3 px-3 py-2 rounded-md text-sm font-medium transition-colors",
isActive ? "bg-blue-600/10 text-blue-400" : "text-gray-400 hover:text-white hover:bg-white/5",
isCollapsed && "justify-center px-0",
)
}
>
<item.icon className="w-4 h-4 shrink-0" />
{!isCollapsed && <span className="truncate">{item.label}</span>}
</NavLink>
))}
</nav>
<div className={cn("p-4 border-t border-[#2a2b2f]", isCollapsed && "px-2")}>
<button
onClick={() => setIsProfileModalOpen(true)}
className={cn(
"flex items-center gap-3 px-3 py-2 w-full rounded-md hover:bg-white/5 transition-colors group text-left",
isCollapsed && "px-0 justify-center",
)}
>
<div className="w-8 h-8 rounded-full bg-blue-600/20 border border-blue-500/30 flex items-center justify-center text-xs text-blue-400 font-bold shrink-0 group-hover:border-blue-500/50 transition-colors">
{avatarLabel}
</div>
{!isCollapsed && (
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-white truncate group-hover:text-blue-400 transition-colors">
{name || "未选择用户"}
</p>
<p className="text-xs text-gray-500 truncate">{email || "请选择或创建真实用户"}</p>
</div>
)}
</button>
</div>
{isProfileModalOpen && (
<div className="fixed inset-0 z-[200] flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm">
<div className="bg-[#151619] border border-[#2a2b2f] rounded-2xl w-full max-w-md shadow-2xl overflow-hidden">
<div className="px-6 py-4 border-b border-[#2a2b2f] flex items-center justify-between bg-[#1a1b1e]">
<h2 className="text-xl font-bold text-white flex items-center gap-2">
<User className="w-5 h-5 text-blue-500" />
当前用户
</h2>
<button
onClick={() => setIsProfileModalOpen(false)}
className="text-gray-500 hover:text-white transition-colors"
>
<X className="w-6 h-6" />
</button>
</div>
<form onSubmit={handleSaveProfile}>
<div className="p-6 space-y-4">
{currentUser && profileDraft ? (
<>
<div className="flex justify-center pb-4">
<div className="w-20 h-20 rounded-full bg-blue-600/20 border-2 border-blue-500/30 flex items-center justify-center text-2xl text-blue-400 font-bold">
{avatarLabel}
</div>
</div>
<div className="space-y-1.5">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
<User className="w-3 h-3" /> 用户名
</label>
<input
type="text"
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
value={profileDraft.name}
onChange={(event) =>
setProfileDraft((previous) =>
previous ? { ...previous, name: event.target.value } : previous,
)
}
/>
</div>
<div className="space-y-1.5">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
<Mail className="w-3 h-3" /> 邮箱地址
</label>
<input
type="email"
className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm focus:outline-none focus:border-blue-500 text-white"
value={profileDraft.email}
onChange={(event) =>
setProfileDraft((previous) =>
previous ? { ...previous, email: event.target.value } : previous,
)
}
/>
</div>
<div className="space-y-1.5">
<label className="text-xs font-medium text-gray-400 uppercase tracking-wider flex items-center gap-1.5">
<Shield className="w-3 h-3" /> 角色
</label>
<div className="w-full bg-[#0a0a0a] border border-[#2a2b2f] rounded-md px-4 py-2 text-sm text-gray-300">
{getUserRoleLabel(currentUser.role)}
</div>
</div>
{profileError && (
<div className="rounded-md border border-red-500/30 bg-red-500/10 px-3 py-2 text-sm text-red-300">
{profileError}
</div>
)}
</>
) : (
<div className="rounded-lg border border-dashed border-[#2a2b2f] bg-[#0a0a0a] px-4 py-6 text-sm text-gray-500">
当前没有选中的用户。请先在初始化页创建用户,或从已有用户列表中选择一个用户。
</div>
)}
</div>
<div className="p-6 border-t border-[#2a2b2f] flex items-center justify-between bg-[#0a0a0a]/50">
<button
type="button"
onClick={handleLogout}
className="flex items-center gap-2 px-4 py-2 text-sm font-medium text-red-500 hover:bg-red-500/10 rounded-md transition-colors"
>
<LogOut className="w-4 h-4" />
退出当前用户
</button>
<div className="flex gap-3">
<button
type="button"
onClick={() => setIsProfileModalOpen(false)}
className="px-4 py-2 text-sm font-medium text-gray-400 hover:text-white transition-colors"
>
取消
</button>
<button
type="submit"
disabled={!profileDraft || isSavingProfile}
className="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-all disabled:opacity-50 disabled:cursor-not-allowed inline-flex items-center gap-2"
>
{isSavingProfile && <Loader2 className="w-4 h-4 animate-spin" />}
保存
</button>
</div>
</div>
</form>
</div>
</div>
)}
</aside>
);
}