File size: 10,670 Bytes
3ce30ae
7843862
712f4ea
80df99b
690a7ba
9b6da67
3ce30ae
 
 
 
 
712f4ea
69a84ff
 
 
 
7843862
 
712f4ea
 
 
a45d518
c132c62
a45d518
7843862
 
712f4ea
 
69a84ff
712f4ea
 
 
bbf8ecc
712f4ea
 
80df99b
a45d518
712f4ea
 
 
bbf8ecc
712f4ea
a45d518
bbf8ecc
a45d518
712f4ea
 
bbf8ecc
 
 
a45d518
bbf8ecc
 
712f4ea
 
bbf8ecc
 
 
 
 
a45d518
712f4ea
 
 
 
69a84ff
 
 
 
 
712f4ea
 
 
a45d518
69a84ff
712f4ea
 
 
 
 
 
 
 
 
 
 
 
b9d2e2b
 
 
 
c132c62
b9d2e2b
 
 
 
690a7ba
69a84ff
690a7ba
 
c132c62
 
 
 
 
 
 
 
690a7ba
 
 
 
 
 
 
80df99b
 
 
 
a45d518
690a7ba
 
 
 
 
712f4ea
690a7ba
a45d518
 
 
b9d2e2b
 
a45d518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
690a7ba
 
c132c62
690a7ba
 
 
a45d518
b9d2e2b
690a7ba
 
712f4ea
690a7ba
c132c62
b9d2e2b
c132c62
b9d2e2b
 
 
c132c62
b9d2e2b
 
 
 
 
 
c132c62
b9d2e2b
7843862
 
 
 
 
 
 
 
 
 
 
 
b9d2e2b
 
 
c132c62
b9d2e2b
 
 
 
 
 
299d1d7
 
 
 
 
 
 
 
 
 
 
 
 
b9d2e2b
 
 
 
690a7ba
9b6da67
 
 
 
c132c62
690a7ba
 
c132c62
 
690a7ba
 
 
 
 
 
 
 
 
 
a45d518
 
690a7ba
 
 
 
a45d518
690a7ba
 
a45d518
690a7ba
 
 
a45d518
690a7ba
a45d518
690a7ba
 
69a84ff
712f4ea
690a7ba
712f4ea
69a84ff
 
 
 
 
 
 
 
 
 
c132c62
 
3ce30ae
 
 
 
 
 
 
690a7ba
712f4ea
 
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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>
      )}
    </>
  );
}