Spaces:
Running
Running
File size: 7,613 Bytes
6f5f15e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | import { useState } from 'react';
import {
ChevronDown,
ChevronUp,
ExternalLink,
Plug,
Radio,
User as UserIcon,
Check,
X,
} from 'lucide-react';
import type { AppConfigView, DashboardState } from '../../../shared/api-types';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { ThemeToggle } from './ThemeToggle';
import type { Theme } from '@/lib/theme';
import { cn } from '@/lib/utils';
export function TopBar({
config,
state,
user,
onChangeUser,
connected,
mockMode,
connectOpen,
onToggleConnect,
theme,
onToggleTheme,
}: {
config: AppConfigView;
state: DashboardState | null;
user: string;
onChangeUser: (nextUser: string) => void;
connected: boolean;
mockMode: boolean;
connectOpen: boolean;
onToggleConnect: () => void;
theme: Theme;
onToggleTheme: () => void;
}) {
const [editingUser, setEditingUser] = useState(false);
const [draftUser, setDraftUser] = useState(user);
const pushCount = state?.push.length ?? 0;
const pollCount = state?.pollers.filter((p) => p.status !== 'gone').length ?? 0;
const callerCount = state?.callers.length ?? 0;
const showUserSwitcher = config.mcpHostConfigured || user !== 'default';
const commitUser = () => {
const trimmed = draftUser.trim();
if (trimmed) onChangeUser(trimmed);
setEditingUser(false);
};
return (
<header className="sticky top-0 z-30 border-b border-line bg-bg/90 backdrop-blur-md">
<div className="mx-auto flex max-w-[1440px] flex-wrap items-center justify-between gap-2 px-4 py-2.5">
{/* Left: brand + version + mode + user */}
<div className="flex flex-wrap items-center gap-2">
<div className="flex items-center gap-2">
<span className="relative flex h-6 w-6 items-center justify-center rounded-lg border border-line-strong bg-panel">
<Radio className="h-3.5 w-3.5 text-push" />
<span
className={cn(
'absolute -right-0.5 -top-0.5 h-2 w-2 rounded-full ring-2 ring-bg',
mockMode ? 'bg-event' : connected ? 'bg-push' : 'bg-danger animate-pulse',
)}
/>
</span>
<span className="text-sm font-semibold tracking-tight text-fg">mcp-webcam</span>
<Badge variant="muted" className="font-mono text-[10px]">
v{config.version}
</Badge>
</div>
<Badge variant={config.mode === 'http' ? 'push' : 'call'} className="font-mono text-[10px]">
{config.mode === 'http'
? `HTTP 路 ${config.modernProtocol}${config.legacyFallback ? ' + 2025' : ''}`
: 'STDIO'}
</Badge>
{mockMode && (
<Badge variant="event" className="font-mono text-[10px]">
MOCK MODE
</Badge>
)}
{showUserSwitcher && (
<div className="flex items-center">
{editingUser ? (
<form
className="flex items-center gap-1"
onSubmit={(e) => {
e.preventDefault();
commitUser();
}}
>
<Input
value={draftUser}
onChange={(e) => setDraftUser(e.target.value)}
className="h-6.5 w-32 font-mono text-[11px]"
placeholder="room-id"
autoFocus
onKeyDown={(e) => {
if (e.key === 'Escape') {
setDraftUser(user);
setEditingUser(false);
}
}}
/>
<Button type="submit" variant="ghost" size="icon-sm" className="h-6.5 w-6.5" aria-label="Apply user">
<Check className="h-3 w-3 text-push" />
</Button>
<Button
type="button"
variant="ghost"
size="icon-sm"
className="h-6.5 w-6.5"
aria-label="Cancel"
onClick={() => {
setDraftUser(user);
setEditingUser(false);
}}
>
<X className="h-3 w-3" />
</Button>
</form>
) : (
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={() => {
setDraftUser(user);
setEditingUser(true);
}}
className="inline-flex items-center gap-1 rounded-md border border-line bg-panel px-2 py-1 font-mono text-[11px] text-fg transition-colors hover:border-line-strong cursor-pointer"
>
<UserIcon className="h-3 w-3 text-muted" />
<span>{user}</span>
</button>
</TooltipTrigger>
<TooltipContent>Click to switch user / room (?user=)</TooltipContent>
</Tooltip>
)}
</div>
)}
</div>
{/* Right: live counters + connect + theme + github */}
<div className="flex items-center gap-2">
<div className="hidden items-center gap-2 rounded-lg border border-line bg-panel px-2.5 py-1 text-[11px] tabular-nums md:flex">
<span className="flex items-center gap-1.5 text-muted">
<span className="h-1.5 w-1.5 rounded-full bg-push" />
<strong className="font-medium text-fg">{pushCount}</strong> push
</span>
<span className="text-line-strong">路</span>
<span className="flex items-center gap-1.5 text-muted">
<span className="h-1.5 w-1.5 rounded-full bg-poll" />
<strong className="font-medium text-fg">{pollCount}</strong> poll
</span>
<span className="text-line-strong">路</span>
<span className="flex items-center gap-1.5 text-muted">
<span className="h-1.5 w-1.5 rounded-full bg-call" />
<strong className="font-medium text-fg">{callerCount}</strong> callers
</span>
</div>
<Button
variant={connectOpen ? 'default' : 'secondary'}
size="sm"
onClick={onToggleConnect}
className="gap-1.5"
>
<Plug className="h-3.5 w-3.5" />
<span>Connect</span>
{connectOpen ? <ChevronUp className="h-3 w-3" /> : <ChevronDown className="h-3 w-3" />}
</Button>
<ThemeToggle theme={theme} onToggle={onToggleTheme} />
<Tooltip>
<TooltipTrigger asChild>
<Button variant="ghost" size="icon-sm" asChild>
<a
href="https://github.com/evalstate/mcp-webcam"
target="_blank"
rel="noreferrer"
aria-label="GitHub repository"
>
<ExternalLink className="h-3.5 w-3.5" />
</a>
</Button>
</TooltipTrigger>
<TooltipContent>GitHub 路 evalstate/mcp-webcam</TooltipContent>
</Tooltip>
</div>
</div>
</header>
);
}
|