118 lines
4.0 KiB
TypeScript

import React, { useEffect, useState } from "react";
import { Loader2 } from "lucide-react";
import { Sidebar } from "./Sidebar";
import { Logo } from "./Logo";
import { UserBootstrapPanel } from "./UserBootstrapPanel";
import { useUser } from "../context/UserContext";
import { apiRequest } from "../services/api/client";
interface LayoutProps {
children: React.ReactNode;
}
type HealthState = {
status: "loading" | "online" | "offline";
service?: string;
};
export function Layout({ children }: LayoutProps) {
const [isCollapsed, setIsCollapsed] = useState(false);
const [health, setHealth] = useState<HealthState>({ status: "loading" });
const { currentUser, loading } = useUser();
useEffect(() => {
let cancelled = false;
const loadHealth = async () => {
try {
const result = await apiRequest<{ status: string; service: string }>("/api/health");
if (!cancelled) {
setHealth({
status: result.status === "ok" ? "online" : "offline",
service: result.service,
});
}
} catch (error) {
if (!cancelled) {
console.error("Failed to load API health", error);
setHealth({ status: "offline" });
}
}
};
loadHealth();
const timer = window.setInterval(loadHealth, 15000);
return () => {
cancelled = true;
window.clearInterval(timer);
};
}, []);
return (
<div className="flex h-screen bg-[#0a0a0a] text-gray-100 font-sans">
<Sidebar isCollapsed={isCollapsed} onToggle={() => setIsCollapsed(!isCollapsed)} />
<main className="flex-1 flex flex-col overflow-hidden">
<header className="h-16 border-b border-[#2a2b2f] bg-[#151619] flex items-center justify-between px-8">
<div className="flex items-center gap-4">
<Logo size="sm" />
<h2 className="text-sm font-medium text-gray-400 uppercase tracking-widest">DroboticFlow Platform</h2>
</div>
<div className="flex items-center gap-4">
<div
className={[
"flex items-center gap-2 px-3 py-1 rounded-full border",
health.status === "online"
? "bg-green-500/10 border-green-500/20"
: health.status === "offline"
? "bg-red-500/10 border-red-500/20"
: "bg-gray-500/10 border-gray-500/20",
].join(" ")}
>
<div
className={[
"w-2 h-2 rounded-full",
health.status === "online"
? "bg-green-500 animate-pulse"
: health.status === "offline"
? "bg-red-500"
: "bg-gray-500 animate-pulse",
].join(" ")}
/>
<span
className={[
"text-[10px] font-mono uppercase tracking-wider",
health.status === "online"
? "text-green-500"
: health.status === "offline"
? "text-red-400"
: "text-gray-400",
].join(" ")}
>
{health.status === "online"
? `${health.service || "api"}: online`
: health.status === "offline"
? "api: offline"
: "api: checking"}
</span>
</div>
</div>
</header>
<div className="flex-1 overflow-auto p-8">
{loading ? (
<div className="flex h-full items-center justify-center">
<div className="flex items-center gap-3 rounded-2xl border border-[#2a2b2f] bg-[#151619] px-6 py-4 text-sm text-gray-400">
<Loader2 className="w-4 h-4 animate-spin text-blue-500" />
正在加载真实用户上下文...
</div>
</div>
) : currentUser ? (
children
) : (
<UserBootstrapPanel />
)}
</div>
</main>
</div>
);
}