578 lines
20 KiB
TypeScript
578 lines
20 KiB
TypeScript
import { Bell, ChevronDown, ListTree, User } from "lucide-react";
|
||
import {
|
||
useLayoutEffect,
|
||
useRef,
|
||
useState,
|
||
useSyncExternalStore,
|
||
type CSSProperties,
|
||
} from "react";
|
||
import { useLocation, useNavigate } from "react-router-dom";
|
||
|
||
import { Button } from "@/components/ui/button";
|
||
import {
|
||
DropdownMenu,
|
||
DropdownMenuContent,
|
||
DropdownMenuItem,
|
||
DropdownMenuTrigger,
|
||
} from "@/components/ui/dropdown-menu";
|
||
import "@/components/page-sdebar.css";
|
||
import {
|
||
CompactShortcutButtons,
|
||
LayoutModeSwitcher,
|
||
ThemeSwitcher,
|
||
} from "@/components/workspace/appearance-controls";
|
||
import { NotificationDialog } from "@/components/workspace/notification-dialog";
|
||
import { getStoredAuth } from "@/core/auth";
|
||
import { useLiveAccount } from "@/core/auth/use-live-account";
|
||
import { AppBrandImage } from "@/core/branding/app-brand-image";
|
||
import {
|
||
getHeaderTitlePreview,
|
||
subscribeHeaderTitlePreview,
|
||
} from "@/core/branding/header-title-preview";
|
||
import {
|
||
getLightRedNavigationPreview,
|
||
subscribeLightRedNavigationPreview,
|
||
} from "@/core/branding/light-red-navigation-preview";
|
||
import { isWujieEnv } from "@/core/embed/wujie";
|
||
import { isLightRedTopNavEnabled, readRuntimeEnv } from "@/env";
|
||
import { useUnreadCount } from "@/core/notifications";
|
||
import { useMyPosition } from "@/core/positions";
|
||
import {
|
||
getActiveThirdLevelItems,
|
||
menuItemContainsPath,
|
||
menuItemMatchesPath,
|
||
type MenuItem,
|
||
} from "@/core/page-layout/sidebar-menu";
|
||
import { useSidebarMenuItems } from "@/core/page-layout/use-sidebar-menu";
|
||
import { useDisplayDesensitizer } from "@/core/sensitive-words/use-display-desensitizer";
|
||
import { resolveAppIdentityAssetUrl } from "@/core/system-settings/api";
|
||
import { useAppIdentitySettings } from "@/core/system-settings/hooks";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
import { useTheme } from "../contexts/ThemeContext";
|
||
import {
|
||
calculateHeaderTitleFit,
|
||
HEADER_TITLE_MAX_FONT_SIZE,
|
||
HEADER_TITLE_MAX_LETTER_SPACING,
|
||
resolveHeaderTitleFit,
|
||
type HeaderTitleFit,
|
||
} from "./header-title-fit";
|
||
|
||
const iconButtonClass = (isDark: boolean, isPaleLightTheme: boolean, isCustomTheme: boolean) =>
|
||
cn(
|
||
"size-8 shrink-0 rounded-sm",
|
||
isCustomTheme
|
||
? "text-[var(--navigation-foreground)] hover:bg-white/20 hover:text-[var(--navigation-foreground)] active:bg-white/30"
|
||
: isDark
|
||
? "text-slate-400 hover:bg-slate-800/50 hover:text-white"
|
||
: isPaleLightTheme
|
||
? "text-[var(--foreground)] hover:bg-white/55 hover:text-[var(--foreground)] active:bg-white/75"
|
||
: "text-white hover:bg-[var(--brand-950)] active:bg-[#8a0618]",
|
||
);
|
||
|
||
type HeaderProps = {
|
||
collapsed?: boolean;
|
||
};
|
||
|
||
const Header = ({ collapsed }: HeaderProps) => {
|
||
const { theme, baseMode } = useTheme();
|
||
const isDark = baseMode === "dark";
|
||
const isCustomTheme = theme.startsWith("custom-theme-");
|
||
const isPaleLightTheme = theme === "iceblue" || theme === "lightyellow";
|
||
const headerTone = isCustomTheme
|
||
? "banner-custom"
|
||
: isDark
|
||
? "banner-dark"
|
||
: isPaleLightTheme
|
||
? "content"
|
||
: "banner-light";
|
||
const headerTextClass = isCustomTheme
|
||
? "text-[var(--navigation-foreground)]"
|
||
: isPaleLightTheme
|
||
? "text-[var(--foreground)]"
|
||
: "text-white";
|
||
const isLightRedTopNav = isLightRedTopNavEnabled(theme);
|
||
const topNavLogoPosition =
|
||
readRuntimeEnv("VITE_DARK_LOGO_POSITION") === "left" ? "left" : "center";
|
||
const isTopNavLogoOnLeft = topNavLogoPosition === "left";
|
||
const { data: appIdentity } = useAppIdentitySettings();
|
||
const headerTitlePreview = useSyncExternalStore(
|
||
subscribeHeaderTitlePreview,
|
||
getHeaderTitlePreview,
|
||
getHeaderTitlePreview,
|
||
);
|
||
const lightRedNavigationPreview = useSyncExternalStore(
|
||
subscribeLightRedNavigationPreview,
|
||
getLightRedNavigationPreview,
|
||
getLightRedNavigationPreview,
|
||
);
|
||
const systemName = appIdentity?.system_name.trim() || "CM业务中心";
|
||
const headerTitleFontMode =
|
||
headerTitlePreview?.mode ??
|
||
(appIdentity?.header_title_font_mode === "fixed" ? "fixed" : "adaptive");
|
||
const headerTitleFontSize =
|
||
headerTitlePreview?.fontSize ??
|
||
appIdentity?.header_title_font_size ??
|
||
HEADER_TITLE_MAX_FONT_SIZE;
|
||
const savedLightRedHeaderBackgroundUrl = resolveAppIdentityAssetUrl(
|
||
appIdentity?.light_red_header_background_url,
|
||
);
|
||
const savedLightRedNavActiveUrl = resolveAppIdentityAssetUrl(
|
||
appIdentity?.light_red_nav_active_url,
|
||
);
|
||
const lightRedHeaderBackgroundUrl = lightRedNavigationPreview
|
||
? lightRedNavigationPreview.headerBackgroundUrl
|
||
: savedLightRedHeaderBackgroundUrl;
|
||
const lightRedNavActiveUrl = lightRedNavigationPreview
|
||
? lightRedNavigationPreview.navActiveUrl
|
||
: savedLightRedNavActiveUrl;
|
||
const lightRedArtworkStyle = {
|
||
"--light-red-top-nav-background-image": lightRedHeaderBackgroundUrl
|
||
? `url("${lightRedHeaderBackgroundUrl}")`
|
||
: undefined,
|
||
"--light-red-nav-active-background-image": lightRedNavActiveUrl
|
||
? `url("${lightRedNavActiveUrl}")`
|
||
: undefined,
|
||
} as CSSProperties;
|
||
const [notificationOpen, setNotificationOpen] = useState(false);
|
||
const { data: unreadCount = 0 } = useUnreadCount();
|
||
const navigate = useNavigate();
|
||
const { pathname } = useLocation();
|
||
const liveAccount = useLiveAccount();
|
||
const auth = getStoredAuth();
|
||
const isAdmin = !auth || auth.system_role === "admin";
|
||
const visibleMenuItems = useSidebarMenuItems(!!isAdmin);
|
||
const desensitizeText = useDisplayDesensitizer();
|
||
const displaySystemName = desensitizeText(systemName);
|
||
const centerTitleRef = useRef<HTMLDivElement>(null);
|
||
const centerTitleMeasureRef = useRef<HTMLSpanElement>(null);
|
||
const [centerTitleFit, setCenterTitleFit] = useState<HeaderTitleFit>({
|
||
fontSize: HEADER_TITLE_MAX_FONT_SIZE,
|
||
letterSpacing: HEADER_TITLE_MAX_LETTER_SPACING,
|
||
lines: 1,
|
||
});
|
||
const renderedCenterTitleFit = resolveHeaderTitleFit({
|
||
mode: headerTitleFontMode,
|
||
fixedFontSize: headerTitleFontSize,
|
||
adaptiveFit: centerTitleFit,
|
||
});
|
||
const { data: myPosition } = useMyPosition();
|
||
const positionName = myPosition?.name?.trim() || "";
|
||
|
||
const displayName = liveAccount.displayName;
|
||
// 深色与可选浅红色皮肤共用同一套导航结构;两者只替换背景与选中态配色。
|
||
// 侧栏收起只影响左侧导航,不应让顶栏回退到浅色的图标布局。
|
||
const showTopNav = (isDark || isLightRedTopNav) && !isWujieEnv();
|
||
// 浅红主题继续使用原浅色侧栏承载三级菜单,避免与顶部横栏重复。
|
||
const showThirdSubNav = isDark && !isWujieEnv();
|
||
// 浅色(红/蓝)顶栏:一级导航正常横排进顶栏,不做深色那种围绕基座的特殊处理。
|
||
// 与 collapsed 解耦(折叠只影响侧边栏,一级导航始终可见);wujie 嵌入下隐藏,
|
||
// 此时一级导航回落到侧边栏(见 page-sidebar.tsx)。
|
||
const showLightTopNav = !isDark && !isLightRedTopNav && !isWujieEnv();
|
||
|
||
useLayoutEffect(() => {
|
||
if (!showTopNav || headerTitleFontMode === "fixed") {
|
||
return;
|
||
}
|
||
|
||
const title = centerTitleRef.current;
|
||
const measure = centerTitleMeasureRef.current;
|
||
if (!title || !measure) {
|
||
return;
|
||
}
|
||
|
||
let disposed = false;
|
||
|
||
const updateTitleFit = () => {
|
||
if (disposed) {
|
||
return;
|
||
}
|
||
|
||
const titleStyle = window.getComputedStyle(title);
|
||
const horizontalPadding =
|
||
Number.parseFloat(titleStyle.paddingLeft) +
|
||
Number.parseFloat(titleStyle.paddingRight);
|
||
const logo = title.querySelector<HTMLElement>(".header-center-title__logo");
|
||
const logoWidth = logo?.getBoundingClientRect().width ?? 0;
|
||
const gap = logoWidth > 0 ? Number.parseFloat(titleStyle.columnGap) || 0 : 0;
|
||
const availableWidth = Math.max(
|
||
1,
|
||
title.clientWidth - horizontalPadding - logoWidth - gap,
|
||
);
|
||
const nextFit = calculateHeaderTitleFit({
|
||
availableWidth,
|
||
naturalWidth: measure.getBoundingClientRect().width,
|
||
characterCount: Array.from(displaySystemName).length,
|
||
});
|
||
|
||
setCenterTitleFit((currentFit) =>
|
||
currentFit.fontSize === nextFit.fontSize &&
|
||
currentFit.letterSpacing === nextFit.letterSpacing &&
|
||
currentFit.lines === nextFit.lines
|
||
? currentFit
|
||
: nextFit,
|
||
);
|
||
};
|
||
|
||
updateTitleFit();
|
||
|
||
const resizeObserver = new ResizeObserver(updateTitleFit);
|
||
resizeObserver.observe(title);
|
||
|
||
void document.fonts?.ready.then(updateTitleFit);
|
||
|
||
return () => {
|
||
disposed = true;
|
||
resizeObserver.disconnect();
|
||
};
|
||
}, [
|
||
displaySystemName,
|
||
headerTitleFontMode,
|
||
showTopNav,
|
||
topNavLogoPosition,
|
||
]);
|
||
|
||
const hasActiveChild = (item: MenuItem): boolean => menuItemContainsPath(item, pathname);
|
||
|
||
const findFirstRoutePath = (items?: MenuItem[]): string | null => {
|
||
if (!items?.length) {
|
||
return null;
|
||
}
|
||
|
||
for (const child of items) {
|
||
if (child.path) {
|
||
return child.path;
|
||
}
|
||
const nestedPath = findFirstRoutePath(child.children);
|
||
if (nestedPath) {
|
||
return nestedPath;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
};
|
||
|
||
const thirdSubNavItems = getActiveThirdLevelItems(visibleMenuItems, pathname);
|
||
|
||
// 深色顶栏围绕中间标题分成左右两组。直接按菜单管理后的一级顺序均分,确保被升为
|
||
// 一级或重新排序的节点仍会显示;默认 5 项时仍保持左 3 / 右 2 的原布局。
|
||
const topMenuSplitIndex = Math.ceil(visibleMenuItems.length / 2);
|
||
const leftMenuItems = visibleMenuItems.slice(0, topMenuSplitIndex);
|
||
const rightMenuItems = visibleMenuItems.slice(topMenuSplitIndex);
|
||
|
||
const handleNavItemClick = (item: MenuItem) => {
|
||
const path = item.path ?? findFirstRoutePath(item.children);
|
||
if (path) {
|
||
navigate(path);
|
||
}
|
||
};
|
||
|
||
const renderTopItem = (item: MenuItem) => {
|
||
const isSelected = item.path || item.aliasPaths?.length
|
||
? menuItemMatchesPath(item, pathname)
|
||
: hasActiveChild(item);
|
||
return (
|
||
<button
|
||
key={item.id}
|
||
type="button"
|
||
className={`top-submenu-item ${isSelected ? "top-submenu-item--active" : ""}`}
|
||
onClick={() => handleNavItemClick(item)}
|
||
>
|
||
{item.label}
|
||
</button>
|
||
);
|
||
};
|
||
|
||
const renderLightTopItem = (item: MenuItem) => {
|
||
const isSelected = item.path || item.aliasPaths?.length
|
||
? menuItemMatchesPath(item, pathname)
|
||
: hasActiveChild(item);
|
||
return (
|
||
<button
|
||
key={item.id}
|
||
type="button"
|
||
className={`light-topnav-item ${isSelected ? "light-topnav-item--active" : ""}`}
|
||
onClick={() => handleNavItemClick(item)}
|
||
>
|
||
{item.label}
|
||
</button>
|
||
);
|
||
};
|
||
|
||
// 右侧操作区(换肤/通知/用户)—— 深浅两套布局共用
|
||
const actionCluster = (
|
||
<>
|
||
<CompactShortcutButtons tone={headerTone} />
|
||
<ThemeSwitcher tone={headerTone} />
|
||
|
||
<LayoutModeSwitcher tone={headerTone} />
|
||
|
||
<Button
|
||
type="button"
|
||
variant="ghost"
|
||
size="icon-sm"
|
||
onClick={() => setNotificationOpen(true)}
|
||
className={cn(iconButtonClass(isDark, isPaleLightTheme, isCustomTheme), "relative")}
|
||
title="通知"
|
||
>
|
||
<Bell className="size-4" />
|
||
{unreadCount > 0 && (
|
||
<span
|
||
className={cn(
|
||
"absolute top-1 right-1 flex h-4 min-w-4 items-center justify-center rounded-sm px-1 text-[10px] leading-none font-medium",
|
||
isDark
|
||
? "bg-cyan-500 text-white"
|
||
: "bg-white tabular-nums text-[var(--brand-900)]",
|
||
)}
|
||
>
|
||
{unreadCount > 99 ? "99+" : unreadCount}
|
||
</span>
|
||
)}
|
||
</Button>
|
||
|
||
<div
|
||
className={cn(
|
||
"mx-1 h-8 w-px",
|
||
isCustomTheme
|
||
? "bg-[var(--navigation-foreground)]/25"
|
||
: isDark
|
||
? "bg-blue-800/30"
|
||
: isPaleLightTheme
|
||
? "bg-[var(--brand-900)]/25"
|
||
: "bg-white/30",
|
||
)}
|
||
aria-hidden
|
||
/>
|
||
|
||
<DropdownMenu>
|
||
<DropdownMenuTrigger asChild>
|
||
<Button
|
||
type="button"
|
||
variant="ghost"
|
||
className={cn(
|
||
"h-8 gap-2 rounded-sm px-2",
|
||
isCustomTheme
|
||
? "text-[var(--navigation-foreground)] hover:bg-white/20 active:bg-white/30"
|
||
: isDark
|
||
? "text-slate-200 hover:bg-slate-800/50"
|
||
: isPaleLightTheme
|
||
? "text-[var(--foreground)] hover:bg-white/55 active:bg-white/75"
|
||
: "text-white hover:bg-[var(--brand-950)] active:bg-[#8a0618]",
|
||
)}
|
||
>
|
||
<div
|
||
className={cn(
|
||
"flex size-8 shrink-0 items-center justify-center rounded-sm",
|
||
isCustomTheme
|
||
? "bg-white/25"
|
||
: isDark
|
||
? "bg-gradient-to-br from-blue-500 to-cyan-400"
|
||
: "bg-white",
|
||
)}
|
||
>
|
||
<User
|
||
className={cn(
|
||
"size-4",
|
||
isDark && !isCustomTheme ? "text-white" : "text-[var(--brand-900)]",
|
||
)}
|
||
/>
|
||
</div>
|
||
<div className="hidden text-left sm:block">
|
||
<p className={cn("text-sm leading-tight font-medium", headerTextClass)}>
|
||
{displayName}
|
||
</p>
|
||
{positionName && (
|
||
<p className={cn("text-[11px] leading-tight", isCustomTheme ? "text-[var(--navigation-foreground)]/75" : isPaleLightTheme ? "text-[var(--muted-foreground)]" : "text-white/70")}>
|
||
{positionName}
|
||
</p>
|
||
)}
|
||
</div>
|
||
<ChevronDown
|
||
className={cn(
|
||
"size-4 shrink-0",
|
||
isCustomTheme ? "text-[var(--navigation-foreground)]/80" : isDark ? "text-slate-500" : isPaleLightTheme ? "text-[var(--muted-foreground)]" : "text-white/80",
|
||
)}
|
||
/>
|
||
</Button>
|
||
</DropdownMenuTrigger>
|
||
<DropdownMenuContent align="end" sideOffset={8} className="min-w-48">
|
||
<DropdownMenuItem onClick={() => navigate("/page/strategy/admin/menu-management")}>
|
||
<ListTree className="size-4" />
|
||
{isAdmin ? "默认菜单管理" : "我的菜单"}
|
||
</DropdownMenuItem>
|
||
</DropdownMenuContent>
|
||
</DropdownMenu>
|
||
</>
|
||
);
|
||
|
||
return (
|
||
<>
|
||
<NotificationDialog
|
||
open={notificationOpen}
|
||
onOpenChange={setNotificationOpen}
|
||
/>
|
||
<header
|
||
className={cn(
|
||
"strategy-app-header theme-transition relative z-[45] flex h-16 items-center justify-between px-4",
|
||
isLightRedTopNav && "strategy-app-header--light-red-top-nav",
|
||
)}
|
||
style={isLightRedTopNav ? lightRedArtworkStyle : undefined}
|
||
>
|
||
{showTopNav ? (
|
||
<>
|
||
{/* 左分区(flex-1):菜单靠右贴近中间基座 */}
|
||
<div
|
||
className={cn(
|
||
"flex h-full min-w-0 flex-1 items-center",
|
||
isTopNavLogoOnLeft ? "justify-between" : "justify-end",
|
||
)}
|
||
>
|
||
{isTopNavLogoOnLeft && (
|
||
<div
|
||
className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-blue-950/55"
|
||
title="系统 Logo"
|
||
>
|
||
<AppBrandImage
|
||
theme={theme}
|
||
className="h-[35px] w-[35px] object-contain"
|
||
/>
|
||
</div>
|
||
)}
|
||
<nav
|
||
className="top-submenu-bar top-submenu-bar--group"
|
||
aria-label="左侧菜单"
|
||
>
|
||
<div className="top-submenu-list">
|
||
{leftMenuItems.map(renderTopItem)}
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
|
||
{/* 中:系统 logo + 名称,落在背景图发光基座上。 */}
|
||
<div ref={centerTitleRef} className="header-center-title">
|
||
{topNavLogoPosition === "center" && (
|
||
<AppBrandImage
|
||
theme={theme}
|
||
className="header-center-title__logo"
|
||
/>
|
||
)}
|
||
<span
|
||
className="header-center-title__label"
|
||
data-lines={renderedCenterTitleFit.lines}
|
||
style={{
|
||
fontSize: `${renderedCenterTitleFit.fontSize}px`,
|
||
letterSpacing: `${renderedCenterTitleFit.letterSpacing}px`,
|
||
}}
|
||
>
|
||
{displaySystemName}
|
||
</span>
|
||
<span
|
||
ref={centerTitleMeasureRef}
|
||
className="header-center-title__measure"
|
||
aria-hidden="true"
|
||
>
|
||
{displaySystemName}
|
||
</span>
|
||
</div>
|
||
|
||
{/* 右分区(flex-1):菜单靠左贴近中间基座,操作区靠最右 */}
|
||
<div className="flex h-full min-w-0 flex-1 items-center justify-between">
|
||
<nav
|
||
className="top-submenu-bar top-submenu-bar--group"
|
||
aria-label="右侧菜单"
|
||
>
|
||
<div className="top-submenu-list">
|
||
{rightMenuItems.map(renderTopItem)}
|
||
</div>
|
||
</nav>
|
||
<div className="flex shrink-0 items-center gap-2">
|
||
{actionCluster}
|
||
</div>
|
||
</div>
|
||
</>
|
||
) : showLightTopNav ? (
|
||
<>
|
||
{/* 浅色顶栏:logo + 一级导航横排(白字落在 banner 上) */}
|
||
<div className="flex h-full min-w-0 flex-1 items-center">
|
||
<div className="flex flex-shrink-0 items-center gap-2 pr-1">
|
||
<div className="flex h-10 w-10 flex-shrink-0 items-center justify-center rounded-lg bg-white/20">
|
||
<AppBrandImage
|
||
theme={theme}
|
||
className="h-[35px] w-[35px] object-contain"
|
||
/>
|
||
</div>
|
||
<span className={cn("whitespace-nowrap text-lg font-semibold", headerTextClass)}>
|
||
{displaySystemName}
|
||
</span>
|
||
</div>
|
||
<nav className="light-topnav" aria-label="一级菜单">
|
||
{visibleMenuItems.map(renderLightTopItem)}
|
||
</nav>
|
||
</div>
|
||
|
||
<div className="flex shrink-0 items-center gap-2 pl-2">
|
||
{actionCluster}
|
||
</div>
|
||
</>
|
||
) : (
|
||
<>
|
||
<div className="flex h-full min-w-0 flex-1 items-center gap-3">
|
||
<div
|
||
className={cn(
|
||
"flex h-8 w-8 flex-shrink-0 items-center justify-center rounded-lg",
|
||
isCustomTheme
|
||
? "bg-white/25"
|
||
: isDark
|
||
? "bg-gradient-to-br from-blue-500 to-cyan-400"
|
||
: isPaleLightTheme
|
||
? "bg-white/55"
|
||
: "bg-white/20",
|
||
)}
|
||
>
|
||
<AppBrandImage
|
||
theme={theme}
|
||
className="h-7 w-7 object-contain"
|
||
/>
|
||
</div>
|
||
{!collapsed && (
|
||
<span className={cn("text-sm font-semibold", headerTextClass)}>
|
||
{displaySystemName}
|
||
</span>
|
||
)}
|
||
</div>
|
||
|
||
<div className="flex shrink-0 items-center gap-2">
|
||
{actionCluster}
|
||
</div>
|
||
</>
|
||
)}
|
||
</header>
|
||
{/* 三级菜单横条仅深色用;浅色三级已改到侧边栏右侧列(见 page-sidebar.tsx) */}
|
||
{showThirdSubNav && thirdSubNavItems.length ? (
|
||
<nav className="third-submenu-bar" aria-label="三级菜单">
|
||
<div className="third-submenu-list">
|
||
{thirdSubNavItems.map((item) => {
|
||
const isSelected = item.path || item.aliasPaths?.length
|
||
? menuItemMatchesPath(item, pathname)
|
||
: hasActiveChild(item);
|
||
|
||
return (
|
||
<button
|
||
key={item.id}
|
||
type="button"
|
||
className={`third-submenu-item ${isSelected ? "third-submenu-item--active" : ""}`}
|
||
onClick={() => handleNavItemClick(item)}
|
||
>
|
||
{item.label}
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
</nav>
|
||
) : null}
|
||
</>
|
||
);
|
||
};
|
||
|
||
export default Header;
|