Topbar.tsx 2.57 KB
import { useLocation, useNavigate } from 'react-router-dom';
import { Moon, Sun, Plus, ChevronRight, PanelLeft } from 'lucide-react';
import { navItems } from './nav';
import { useTheme } from '@/lib/theme';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';

export function Topbar({
  onExpand,
  collapsed,
}: {
  onExpand: () => void;
  collapsed: boolean;
}) {
  const { pathname } = useLocation();
  const navigate = useNavigate();
  const { theme, toggle } = useTheme();

  const current =
    navItems.find((n) => (n.end ? pathname === n.to : pathname.startsWith(n.to))) ??
    navItems[0];

  return (
    <header className="sticky top-0 z-10 flex h-16 items-center justify-between gap-4 border-b border-border/70 bg-background/70 px-6 glass dark:shadow-[0_1px_0_0_rgba(255,255,255,0.05),0_16px_44px_-22px_rgba(0,0,0,0.7)]">
      <div className="flex min-w-0 items-center gap-3">
        {collapsed && (
          <button
            onClick={onExpand}
            className="flex h-9 w-9 items-center justify-center rounded-lg text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
            title="展开侧栏"
          >
            <PanelLeft className="h-5 w-5" />
          </button>
        )}
        <div className="min-w-0 animate-fade-in">
          <div className="flex items-center gap-1.5 text-[11px] font-medium uppercase tracking-wider text-muted-foreground">
            <span>CodeRio Studio</span>
            <ChevronRight className="h-3 w-3" />
            <span className="text-foreground/70">{current.label}</span>
          </div>
          <h1 className="truncate text-lg font-semibold leading-tight tracking-tight">
            {current.label}
          </h1>
        </div>
      </div>

      <div className="flex items-center gap-2">
        <button
          onClick={toggle}
          className="flex h-9 w-9 items-center justify-center rounded-lg border border-border text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
          title={theme === 'dark' ? '切换到浅色' : '切换到深色'}
        >
          {theme === 'dark' ? (
            <Sun className="h-[18px] w-[18px]" />
          ) : (
            <Moon className="h-[18px] w-[18px]" />
          )}
        </button>
        <Button
          className={cn(
            'brand-gradient shadow-lg shadow-primary/25 transition-transform hover:scale-[1.02]',
          )}
          onClick={() => navigate('/')}
        >
          <Plus className="h-4 w-4" />
          新建生成
        </Button>
      </div>
    </header>
  );
}