Sidebar.tsx 4.42 KB
import { NavLink } from 'react-router-dom';
import {
  PanelLeftClose,
  PanelLeft,
  Zap,
  type LucideIcon,
} from 'lucide-react';
import { navItems, brandIcon } from './nav';
import { cn } from '@/lib/utils';
import { useConnection } from '@/lib/connection';

function ConnDot() {
  const { connected } = useConnection();
  return (
    <span className="flex items-center gap-2 text-xs">
      <span
        className={cn(
          'relative inline-flex h-2 w-2 rounded-full',
          connected ? 'bg-emerald-500 text-emerald-500' : 'bg-rose-500 text-rose-500',
        )}
      >
        {connected && (
          <span className="pulse-ring absolute inset-0 rounded-full" />
        )}
      </span>
      <span className="text-muted-foreground">
        {connected ? '服务在线' : '连接断开'}
      </span>
    </span>
  );
}

export function Sidebar({
  collapsed,
  onToggle,
}: {
  collapsed: boolean;
  onToggle: () => void;
}) {
  return (
    <aside
      className={cn(
        'relative z-20 flex shrink-0 flex-col border-r border-border/70 bg-card/60 glass transition-[width] duration-300 ease-in-out dark:shadow-[1px_0_0_0_rgba(255,255,255,0.05),16px_0_44px_-22px_rgba(0,0,0,0.7)]',
        collapsed ? 'w-[76px]' : 'w-64',
      )}
    >
      {/* Brand */}
      <div
        className={cn(
          'flex h-16 items-center gap-3 border-b border-border/70 px-5',
          collapsed && 'justify-center px-0',
        )}
      >
        <div className="brand-gradient flex h-9 w-9 shrink-0 items-center justify-center rounded-xl shadow-lg shadow-primary/20">
          {(() => {
            const Icon: LucideIcon = brandIcon;
            return <Icon className="h-5 w-5 text-white" />;
          })()}
        </div>
        {!collapsed && (
          <div className="min-w-0 animate-fade-in">
            <div className="truncate text-[15px] font-semibold tracking-tight">
              CodeRio <span className="text-primary">Studio</span>
            </div>
            <div className="truncate text-[11px] text-muted-foreground">
              设计转代码指挥中心
            </div>
          </div>
        )}
      </div>

      {/* Nav */}
      <nav className="flex-1 space-y-1 p-3">
        {navItems.map((item) => (
          <NavLink
            key={item.to}
            to={item.to}
            end={item.end}
            title={collapsed ? item.label : undefined}
            className={({ isActive }) =>
              cn(
                'group relative flex items-center gap-3 rounded-xl px-3 py-2.5 text-sm font-medium transition-all',
                collapsed && 'justify-center px-0',
                isActive
                  ? 'bg-primary/10 text-foreground'
                  : 'text-muted-foreground hover:bg-accent hover:text-foreground',
              )
            }
          >
            {({ isActive }) => (
              <>
                {isActive && (
                  <span className="absolute left-0 top-1/2 h-5 w-1 -translate-y-1/2 rounded-r-full bg-primary" />
                )}
                <item.icon
                  className={cn(
                    'h-[18px] w-[18px] shrink-0 transition-colors',
                    isActive && 'text-primary',
                  )}
                />
                {!collapsed && <span className="truncate">{item.label}</span>}
              </>
            )}
          </NavLink>
        ))}
      </nav>

      {/* Footer */}
      <div className="space-y-3 border-t border-border/70 p-3">
        {!collapsed && (
          <div className="flex items-center justify-between px-2">
            <ConnDot />
            <span className="flex items-center gap-1 text-[10px] text-muted-foreground">
              <Zap className="h-3 w-3 text-primary" /> v0.1
            </span>
          </div>
        )}
        {collapsed && (
          <div className="flex justify-center">
            <ConnDot />
          </div>
        )}
        <button
          onClick={onToggle}
          className={cn(
            'flex w-full items-center gap-2 rounded-lg px-3 py-2 text-xs font-medium text-muted-foreground transition-colors hover:bg-accent hover:text-foreground',
            collapsed && 'justify-center px-0',
          )}
        >
          {collapsed ? (
            <PanelLeft className="h-4 w-4" />
          ) : (
            <>
              <PanelLeftClose className="h-4 w-4" />
              收起侧栏
            </>
          )}
        </button>
      </div>
    </aside>
  );
}