Spaces:
Sleeping
Sleeping
Naufal Arhab
fix(audit): resolve multi-pass backend calculations, exchange guards, AI/UI state, and chart bugs
80df99b Download frontend/src/components/layout/Header.tsx from agus1111/Perps-Terminal: direct link, hf CLI and curl.
- Browser
- Download file 10.7 kB
-
https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/components/layout/Header.tsx
- Command line
-
hf download hf://spaces/agus1111/Perps-Terminal/frontend/src/components/layout/Header.tsx
-
curl -L -o Header.tsx https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/components/layout/Header.tsx
10.7 kB
| import { useState, lazy, Suspense } from 'react'; | |
| import { Zap, Wifi, RefreshCw, Clock, Bot, Settings, Menu, Activity, BookOpen, BarChart2 } from 'lucide-react'; | |
| import type { WSStatus } from '../../hooks/useWebSocket'; | |
| import { resolveWsUrl } from '../../api/client'; | |
| import { useAISettings } from '../../hooks/useAISettings'; | |
| import { GlobalRegimeIndicator } from './GlobalRegimeIndicator'; | |
| // Lazy-load settings modal | |
| const GlobalAISettingsModal = lazy(() => | |
| import('../ui/GlobalAISettingsModal').then((m) => ({ default: m.GlobalAISettingsModal })) | |
| ); | |
| const ServerSettingsModal = lazy(() => | |
| import('../ui/ServerSettingsModal').then((m) => ({ default: m.ServerSettingsModal })) | |
| ); | |
| export type AppTab = 'signals' | 'chart' | 'ai' | 'journal'; | |
| interface HeaderProps { | |
| wsStatus: WSStatus; | |
| lastUpdated: Date | null; | |
| onOpenMobileDrawer?: () => void; | |
| onOpenAISettings?: () => void; | |
| activePairCount?: number; | |
| activeTab?: AppTab; | |
| onTabChange?: (tab: AppTab) => void; | |
| } | |
| function WSIndicator({ status, onClick }: { status: WSStatus; onClick?: () => void }) { | |
| const config = { | |
| connected: { | |
| color: 'text-signal-long', | |
| bg: 'border-signal-long/20 bg-signal-long-bg', | |
| dot: 'bg-signal-long animate-pulse', | |
| label: 'Live', | |
| tooltip: resolveWsUrl() ? 'Koneksi real-time aktif (WebSocket Live)' : 'Koneksi real-time aktif (HTTP Fast Polling)', | |
| icon: <Wifi size={12} />, | |
| }, | |
| connecting: { | |
| color: 'text-accent-yellow', | |
| bg: 'border-accent-yellow/20 bg-yellow-500/10', | |
| dot: 'bg-accent-yellow animate-pulse', | |
| label: 'Sync...', | |
| tooltip: 'Menghubungkan ke server pasar...', | |
| icon: <RefreshCw size={12} className="animate-spin" />, | |
| }, | |
| disconnected: { | |
| color: 'text-amber-400', | |
| bg: 'border-amber-500/25 bg-amber-500/10', | |
| dot: 'bg-amber-400 animate-pulse', | |
| label: 'Sync', | |
| tooltip: 'Koneksi terputus. Mencoba rekoneksi otomatis...', | |
| icon: <RefreshCw size={12} className="animate-spin" />, | |
| }, | |
| error: { | |
| color: 'text-signal-short', | |
| bg: 'border-signal-short/20 bg-signal-short-bg', | |
| dot: 'bg-signal-short', | |
| label: 'Offline', | |
| tooltip: 'Koneksi ke backend server terputus', | |
| icon: <RefreshCw size={12} />, | |
| }, | |
| }[status]; | |
| return ( | |
| <button | |
| type="button" | |
| onClick={onClick} | |
| title={`${config.tooltip} (Klik untuk pengaturan server)`} | |
| className={`flex items-center gap-1.5 px-2 sm:px-3 py-1 sm:py-1.5 rounded-full border text-[11px] font-medium shrink-0 cursor-pointer hover:opacity-90 active:scale-95 transition-all ${config.bg} ${config.color}`} | |
| > | |
| <span className={`w-1.5 h-1.5 rounded-full ${config.dot}`} /> | |
| {config.icon} | |
| <span className="hidden xs:inline">{config.label}</span> | |
| </button> | |
| ); | |
| } | |
| function formatTime(date: Date): string { | |
| return date.toLocaleTimeString('en-US', { | |
| hour: '2-digit', | |
| minute: '2-digit', | |
| second: '2-digit', | |
| hour12: false, | |
| }); | |
| } | |
| export function Header({ | |
| wsStatus, | |
| lastUpdated, | |
| onOpenMobileDrawer, | |
| onOpenAISettings, | |
| activePairCount = 0, | |
| activeTab = 'signals', | |
| onTabChange, | |
| }: HeaderProps) { | |
| const [isAISettingsOpen, setIsAISettingsOpen] = useState(false); | |
| const [isServerSettingsOpen, setIsServerSettingsOpen] = useState(false); | |
| const { settings, isConfigured } = useAISettings(); | |
| const handleOpenSettings = () => { | |
| if (onOpenAISettings) { | |
| onOpenAISettings(); | |
| } else { | |
| setIsAISettingsOpen(true); | |
| } | |
| }; | |
| const providerLabel = | |
| settings.provider === 'vikey' | |
| ? 'Vikey' | |
| : settings.provider === 'gemini' | |
| ? 'Gemini' | |
| : settings.provider === 'openai' | |
| ? 'OpenAI' | |
| : settings.provider === 'openrouter' | |
| ? 'OpenRouter' | |
| : settings.provider === 'groq' | |
| ? 'Groq' | |
| : 'Custom'; | |
| const shortModel = settings.model.includes('/') | |
| ? settings.model.split('/')[1] | |
| : settings.model; | |
| return ( | |
| <> | |
| <header className="h-14 flex items-center justify-between px-3 sm:px-5 border-b border-border bg-bg-card shrink-0 gap-2"> | |
| {/* Left: Mobile Menu + Logo */} | |
| <div className="flex items-center gap-2 sm:gap-2.5"> | |
| {/* Mobile Pairs Drawer Button (Only shown on Live Signals tab) */} | |
| {onOpenMobileDrawer && activeTab === 'signals' && ( | |
| <button | |
| type="button" | |
| onClick={onOpenMobileDrawer} | |
| className="md:hidden p-2 rounded-lg bg-bg-elevated/80 border border-border text-text-secondary hover:text-text-primary hover:border-accent-blue/40 flex items-center gap-1.5 transition-all shrink-0 active:scale-95" | |
| title="Buka Daftar Trading Pairs" | |
| > | |
| <Menu size={16} className="text-accent-blue" /> | |
| {activePairCount > 0 && ( | |
| <span className="text-[10px] font-mono font-bold bg-accent-blue/20 text-accent-blue px-1 rounded"> | |
| {activePairCount} | |
| </span> | |
| )} | |
| </button> | |
| )} | |
| <div className="w-8 h-8 rounded-lg bg-accent-blue/20 border border-accent-blue/30 flex items-center justify-center shrink-0"> | |
| <Zap size={16} className="text-accent-blue" /> | |
| </div> | |
| <div> | |
| <h1 className="text-sm font-bold text-text-primary font-mono tracking-tight leading-none"> | |
| PerpsAI | |
| </h1> | |
| <p className="text-[9px] sm:text-[10px] text-text-muted leading-none mt-0.5"> | |
| Signal & AI Terminal | |
| </p> | |
| </div> | |
| </div> | |
| {/* Center: Main View Navigation Switcher (Desktop/Tablet — Mobile uses BottomNav) */} | |
| {onTabChange && ( | |
| <div className="hidden md:flex items-center bg-bg-elevated/90 p-1 rounded-xl border border-border shrink-0 shadow-inner"> | |
| <button | |
| type="button" | |
| onClick={() => onTabChange('signals')} | |
| className={`flex items-center gap-1.5 px-3.5 py-1.5 rounded-lg text-xs font-semibold transition-all ${ | |
| activeTab === 'signals' | |
| ? 'bg-accent-blue text-white shadow-sm shadow-accent-blue/30' | |
| : 'text-text-secondary hover:text-text-primary' | |
| }`} | |
| > | |
| <Activity size={13} /> | |
| <span>Live Sinyal</span> | |
| </button> | |
| <button | |
| type="button" | |
| onClick={() => onTabChange('chart')} | |
| className={`flex items-center gap-1.5 px-3.5 py-1.5 rounded-lg text-xs font-semibold transition-all ${ | |
| activeTab === 'chart' | |
| ? 'bg-sky-600 text-white shadow-sm shadow-sky-600/30 font-bold' | |
| : 'text-text-secondary hover:text-sky-300' | |
| }`} | |
| > | |
| <BarChart2 size={13} /> | |
| <span>Live Chart</span> | |
| </button> | |
| <button | |
| type="button" | |
| onClick={() => onTabChange('ai')} | |
| className={`flex items-center gap-1.5 px-3.5 py-1.5 rounded-lg text-xs font-semibold transition-all ${ | |
| activeTab === 'ai' | |
| ? 'bg-purple-600 text-white shadow-sm shadow-purple-600/30' | |
| : 'text-text-secondary hover:text-purple-300' | |
| }`} | |
| > | |
| <Bot size={13} /> | |
| <span>Damian</span> | |
| </button> | |
| <button | |
| type="button" | |
| onClick={() => onTabChange('journal')} | |
| className={`flex items-center gap-1.5 px-3.5 py-1.5 rounded-lg text-xs font-semibold transition-all ${ | |
| activeTab === 'journal' | |
| ? 'bg-emerald-600 text-white shadow-sm shadow-emerald-600/30' | |
| : 'text-text-secondary hover:text-emerald-300' | |
| }`} | |
| > | |
| <BookOpen size={13} /> | |
| <span>Jurnal Trade</span> | |
| </button> | |
| </div> | |
| )} | |
| {/* Right side */} | |
| <div className="flex items-center gap-1.5 sm:gap-2.5"> | |
| {/* Global Market Regime Parameter based strictly on added pairs */} | |
| <GlobalRegimeIndicator /> | |
| {/* Universal AI Settings Trigger (Desktop/Tablet — Mobile accesses via BottomNav) */} | |
| <button | |
| type="button" | |
| onClick={handleOpenSettings} | |
| className={`hidden sm:flex items-center gap-1.5 sm:gap-2 px-2.5 sm:px-3 py-1 sm:py-1.5 rounded-full border text-xs transition-all shadow-sm cursor-pointer shrink-0 ${ | |
| isConfigured | |
| ? 'bg-purple-500/10 hover:bg-purple-500/20 border-purple-500/30 hover:border-purple-400 text-purple-200' | |
| : 'bg-amber-500/10 hover:bg-amber-500/20 border-amber-500/30 text-amber-200 animate-pulse' | |
| }`} | |
| title="Klik untuk konfigurasi model AI dan API Key Universal" | |
| > | |
| <Bot size={13} className={isConfigured ? 'text-purple-400' : 'text-amber-400'} /> | |
| <span className="font-semibold text-[11px]"> | |
| {isConfigured ? ( | |
| <> | |
| <span className="text-purple-300 font-mono hidden xs:inline">{providerLabel}:</span>{' '} | |
| <span className="text-text-primary truncate max-w-[70px] xs:max-w-[100px] sm:max-w-[120px] inline-block align-bottom font-mono"> | |
| {shortModel} | |
| </span> | |
| </> | |
| ) : ( | |
| <span className="font-mono">Atur AI</span> | |
| )} | |
| </span> | |
| <Settings size={11} className="text-text-muted hover:text-text-primary hidden sm:inline" /> | |
| </button> | |
| {lastUpdated && ( | |
| <div className="hidden lg:flex items-center gap-1.5 text-[11px] text-text-muted"> | |
| <Clock size={11} /> | |
| <span>{formatTime(lastUpdated)}</span> | |
| </div> | |
| )} | |
| <WSIndicator status={wsStatus} onClick={() => setIsServerSettingsOpen(true)} /> | |
| </div> | |
| </header> | |
| {/* Server Settings Modal */} | |
| {isServerSettingsOpen && ( | |
| <Suspense fallback={null}> | |
| <ServerSettingsModal | |
| isOpen={isServerSettingsOpen} | |
| onClose={() => setIsServerSettingsOpen(false)} | |
| /> | |
| </Suspense> | |
| )} | |
| {/* Universal AI Settings Modal Fallback if not controlled by parent */} | |
| {!onOpenAISettings && isAISettingsOpen && ( | |
| <Suspense fallback={null}> | |
| <GlobalAISettingsModal | |
| isOpen={isAISettingsOpen} | |
| onClose={() => setIsAISettingsOpen(false)} | |
| /> | |
| </Suspense> | |
| )} | |
| </> | |
| ); | |
| } | |