import type { ReactNode } from "react";
export type Route = "home" | "docs" | "console";
const companyLogo = new URL("../../web/public/brand-logo.svg", import.meta.url).href;
export function navigate(route: Route) {
window.location.hash = route === "home" ? "/" : `/${route}`;
}
export function currentRoute(): Route {
const path = window.location.hash.replace(/^#/, "");
if (path.startsWith("/docs")) return "docs";
if (path.startsWith("/console")) return "console";
return "home";
}
export function Brand({ compact = false }: { compact?: boolean }) {
return (
);
}
export type IconName =
| "home" | "apps" | "users" | "car" | "shield" | "book" | "key"
| "terminal" | "activity" | "settings" | "plus" | "search" | "close"
| "check" | "clock" | "copy" | "logout" | "arrow" | "menu";
const paths: Record = {
home: <>>,
apps: <>>,
users: <>>,
car: <>>,
shield: <>>,
book: <>>,
key: <>>,
terminal: <>>,
activity: ,
settings: <>>,
plus: ,
search: <>>,
close: ,
check: ,
clock: <>>,
copy: <>>,
logout: <>>,
arrow: ,
menu:
};
export function Icon({ name, size = 20 }: { name: IconName; size?: number }) {
return ;
}
export function Status({ value, label }: { value: string; label?: string }) {
const normalized = value === "enabled" || value === "success" || value === "available" ? "enabled" : value === "disabled" || value === "failed" ? "disabled" : value;
const text = label || (value === "enabled" ? "启用" : value === "disabled" ? "已停用" : value === "success" ? "成功" : value);
return {text};
}
export function PageHeader({ title, description, action }: { title: string; description?: string; action?: ReactNode }) {
return {title}
{description &&
{description}
}
{action};
}
export function EmptyState({ title, body, action }: { title: string; body: string; action?: ReactNode }) {
return ;
}
export function dateInput(value?: string) {
return value ? value.slice(0, 10) : "";
}
export function rfcDate(value: string) {
return `${value}T00:00:00+08:00`;
}
export function formatDate(value?: string) {
if (!value) return "长期有效";
return new Intl.DateTimeFormat("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit" }).format(new Date(value));
}
export function formatTime(value?: string) {
if (!value) return "从未登录";
return new Intl.DateTimeFormat("zh-CN", { dateStyle: "short", timeStyle: "short" }).format(new Date(value));
}
export function errorMessage(reason: unknown) {
return reason instanceof Error ? reason.message : "操作失败,请稍后重试";
}