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/ui/ServerSettingsModal.tsx from agus1111/Perps-Terminal: direct link, hf CLI and curl.
- Browser
- Download file 10.6 kB
-
https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/components/ui/ServerSettingsModal.tsx
- Command line
-
hf download hf://spaces/agus1111/Perps-Terminal/frontend/src/components/ui/ServerSettingsModal.tsx
-
curl -L -o ServerSettingsModal.tsx https://huggingface.co/spaces/agus1111/Perps-Terminal/resolve/main/frontend/src/components/ui/ServerSettingsModal.tsx
10.6 kB
| import { useState, useEffect } from 'react'; | |
| import { useQueryClient } from '@tanstack/react-query'; | |
| import { Wifi, Check, AlertCircle, RefreshCw, Globe, Smartphone, KeyRound, Eye, EyeOff } from 'lucide-react'; | |
| import { Modal } from './Modal'; | |
| import { Button } from './Button'; | |
| import { useToast } from './Toast'; | |
| import { getServerUrl, setServerUrl, getApiToken, setApiToken, DEFAULT_CLOUD_URL } from '../../api/client'; | |
| import axios from 'axios'; | |
| interface ServerSettingsModalProps { | |
| isOpen: boolean; | |
| onClose: () => void; | |
| } | |
| export function ServerSettingsModal({ isOpen, onClose }: ServerSettingsModalProps) { | |
| const queryClient = useQueryClient(); | |
| const [url, setUrl] = useState(''); | |
| const [apiTokenInput, setApiTokenInput] = useState(''); | |
| const [showToken, setShowToken] = useState(false); | |
| const [isTesting, setIsTesting] = useState(false); | |
| const [testResult, setTestResult] = useState<{ ok: boolean; message: string } | null>(null); | |
| const { toast } = useToast(); | |
| useEffect(() => { | |
| if (isOpen) { | |
| setUrl(getServerUrl()); | |
| setApiTokenInput(getApiToken()); | |
| setShowToken(false); | |
| setTestResult(null); | |
| } | |
| }, [isOpen]); | |
| const handleTestConnection = async (targetUrl?: string) => { | |
| const testTarget = (targetUrl ?? url).trim().replace(/\/+$/, ''); | |
| const token = apiTokenInput.trim(); | |
| setIsTesting(true); | |
| setTestResult(null); | |
| try { | |
| const healthUrl = testTarget ? `${testTarget}/health` : '/health'; | |
| const res = await axios.get(healthUrl, { timeout: 5000 }); | |
| if (res.status !== 200) { | |
| setTestResult({ ok: false, message: `Server merespons kode status: ${res.status}` }); | |
| return; | |
| } | |
| try { | |
| const statusUrl = testTarget ? `${testTarget}/api/auth/status` : '/api/auth/status'; | |
| const authStatusRes = await axios.get<{ auth_configured?: boolean }>(statusUrl, { timeout: 5000 }); | |
| if (authStatusRes.data?.auth_configured === false) { | |
| setTestResult({ | |
| ok: false, | |
| message: 'Server online, tetapi PERPS_API_TOKEN belum diset di backend (route terlindungi akan fail-closed 503).', | |
| }); | |
| return; | |
| } | |
| } catch { | |
| // Older backend without /api/auth/status still passes health check | |
| } | |
| if (token) { | |
| try { | |
| const verifyUrl = testTarget ? `${testTarget}/api/auth/verify` : '/api/auth/verify'; | |
| await axios.post(verifyUrl, null, { | |
| timeout: 5000, | |
| headers: { Authorization: `Bearer ${token}` }, | |
| }); | |
| setTestResult({ ok: true, message: 'Terhubung & Terautentikasi! Backend online dan API Token valid.' }); | |
| return; | |
| } catch (authErr: any) { | |
| const st = authErr?.response?.status; | |
| setTestResult({ | |
| ok: false, | |
| message: st === 401 | |
| ? 'Server online, tetapi API Token salah / tidak cocok (401 Unauthorized).' | |
| : st === 503 | |
| ? 'Server online, namun PERPS_API_TOKEN belum dikonfigurasi di backend (503).' | |
| : `Server online, verifikasi token gagal: ${authErr.message}`, | |
| }); | |
| return; | |
| } | |
| } | |
| setTestResult({ | |
| ok: true, | |
| message: 'Server online! Masukkan API Bearer Token agar fitur Pairs, Jurnal, dan Eksekusi aktif.', | |
| }); | |
| } catch (err: any) { | |
| if (testTarget) { | |
| try { | |
| const fallbackRes = await axios.get(testTarget, { timeout: 5000 }); | |
| if (fallbackRes.status === 200) { | |
| setTestResult({ ok: true, message: 'Terhubung ke server!' }); | |
| return; | |
| } | |
| } catch { | |
| // ignore | |
| } | |
| } | |
| setTestResult({ | |
| ok: false, | |
| message: err.message?.includes('Network Error') | |
| ? 'Gagal terhubung. Pastikan backend aktif dan URL dapat diakses.' | |
| : `Koneksi gagal: ${err.message}`, | |
| }); | |
| } finally { | |
| setIsTesting(false); | |
| } | |
| }; | |
| const handleSave = () => { | |
| const finalUrl = url.trim().replace(/\/+$/, ''); | |
| setServerUrl(finalUrl); | |
| setApiToken(apiTokenInput); | |
| queryClient.invalidateQueries(); | |
| toast('Pengaturan server & API token berhasil disimpan', 'success'); | |
| onClose(); | |
| }; | |
| const applyPreset = (presetUrl: string) => { | |
| setUrl(presetUrl); | |
| setTestResult(null); | |
| handleTestConnection(presetUrl); | |
| }; | |
| return ( | |
| <Modal isOpen={isOpen} onClose={onClose} title="Pengaturan Server Backend" size="md"> | |
| <div className="space-y-4 text-xs sm:text-sm"> | |
| <p className="text-text-secondary leading-relaxed text-xs"> | |
| Aplikasi APK dapat terhubung ke server cloud 24/7 (gratis) ataupun server lokal (seperti Termux di HP atau PC melalui Wi-Fi). | |
| </p> | |
| {/* Input Server URL */} | |
| <div className="space-y-1.5"> | |
| <label className="block text-xs font-semibold text-text-primary"> | |
| Backend API / WebSocket URL | |
| </label> | |
| <div className="relative"> | |
| <input | |
| type="text" | |
| value={url} | |
| onChange={(e) => { | |
| setUrl(e.target.value); | |
| setTestResult(null); | |
| }} | |
| placeholder="https://... atau http://127.0.0.1:8000" | |
| className="w-full bg-bg-elevated border border-border rounded-lg px-3 py-2 text-xs font-mono text-text-primary focus:outline-none focus:border-accent-blue focus:ring-1 focus:ring-accent-blue" | |
| /> | |
| </div> | |
| </div> | |
| {/* Input API Bearer Token */} | |
| <div className="space-y-1.5"> | |
| <label className="flex items-center gap-1.5 text-xs font-semibold text-text-primary"> | |
| <KeyRound size={13} className="text-amber-400" /> | |
| <span>API Bearer Token (PERPS_API_TOKEN)</span> | |
| </label> | |
| <div className="relative"> | |
| <input | |
| type={showToken ? 'text' : 'password'} | |
| value={apiTokenInput} | |
| onChange={(e) => { | |
| setApiTokenInput(e.target.value); | |
| setTestResult(null); | |
| }} | |
| placeholder="Tempel PERPS_API_TOKEN dari server backend" | |
| className="w-full bg-bg-elevated border border-border rounded-lg pl-3 pr-9 py-2 text-xs font-mono text-text-primary focus:outline-none focus:border-amber-400 focus:ring-1 focus:ring-amber-400" | |
| /> | |
| <button | |
| type="button" | |
| onClick={() => setShowToken((prev) => !prev)} | |
| className="absolute right-2.5 top-1/2 -translate-y-1/2 text-text-muted hover:text-text-primary" | |
| title={showToken ? 'Sembunyikan token' : 'Tampilkan token'} | |
| > | |
| {showToken ? <EyeOff size={14} /> : <Eye size={14} />} | |
| </button> | |
| </div> | |
| <p className="text-[11px] text-text-muted"> | |
| Wajib untuk operasi Pair Watchlist, Jurnal Trade, dan Eksekusi Order Bybit. Disimpan lokal di perangkat Anda. | |
| </p> | |
| </div> | |
| {/* Quick Presets */} | |
| <div className="space-y-1.5"> | |
| <span className="text-[11px] font-medium text-text-muted block">Pilihan Preset Cepat:</span> | |
| <div className="grid grid-cols-1 sm:grid-cols-2 gap-2"> | |
| <button | |
| type="button" | |
| onClick={() => applyPreset(DEFAULT_CLOUD_URL)} | |
| className="flex items-center gap-2 p-2 rounded-lg border border-border/80 bg-bg-elevated/60 hover:bg-bg-elevated hover:border-purple-500/50 text-left transition-all group" | |
| > | |
| <Globe size={14} className="text-purple-400 group-hover:scale-110 transition-transform shrink-0" /> | |
| <div className="overflow-hidden"> | |
| <span className="font-semibold text-text-primary text-[11px] block">Cloud 24/7 (Hugging Face)</span> | |
| <span className="text-[10px] text-text-muted font-mono truncate block">Online gratis nonstop</span> | |
| </div> | |
| </button> | |
| <button | |
| type="button" | |
| onClick={() => applyPreset('http://127.0.0.1:8000')} | |
| className="flex items-center gap-2 p-2 rounded-lg border border-border/80 bg-bg-elevated/60 hover:bg-bg-elevated hover:border-emerald-500/50 text-left transition-all group" | |
| > | |
| <Smartphone size={14} className="text-emerald-400 group-hover:scale-110 transition-transform shrink-0" /> | |
| <div className="overflow-hidden"> | |
| <span className="font-semibold text-text-primary text-[11px] block">Lokal HP (Termux)</span> | |
| <span className="text-[10px] text-text-muted font-mono truncate block">http://127.0.0.1:8000</span> | |
| </div> | |
| </button> | |
| </div> | |
| </div> | |
| {/* Test Result Feedback */} | |
| {testResult && ( | |
| <div | |
| className={`p-3 rounded-lg border flex items-start gap-2.5 ${ | |
| testResult.ok | |
| ? 'bg-emerald-500/10 border-emerald-500/30 text-emerald-300' | |
| : 'bg-red-500/10 border-red-500/30 text-red-300' | |
| }`} | |
| > | |
| {testResult.ok ? ( | |
| <Check size={16} className="text-emerald-400 shrink-0 mt-0.5" /> | |
| ) : ( | |
| <AlertCircle size={16} className="text-red-400 shrink-0 mt-0.5" /> | |
| )} | |
| <div className="text-xs">{testResult.message}</div> | |
| </div> | |
| )} | |
| {/* Action Buttons */} | |
| <div className="flex items-center justify-between pt-2 border-t border-border"> | |
| <Button | |
| type="button" | |
| variant="ghost" | |
| size="sm" | |
| onClick={() => handleTestConnection()} | |
| disabled={isTesting} | |
| className="text-xs flex items-center gap-1.5" | |
| > | |
| {isTesting ? <RefreshCw size={13} className="animate-spin" /> : <Wifi size={13} />} | |
| <span>{isTesting ? 'Menguji...' : 'Uji Koneksi'}</span> | |
| </Button> | |
| <div className="flex items-center gap-2"> | |
| <Button type="button" variant="ghost" size="sm" onClick={onClose} className="text-xs"> | |
| Batal | |
| </Button> | |
| <Button | |
| type="button" | |
| variant="primary" | |
| size="sm" | |
| onClick={handleSave} | |
| className="text-xs bg-accent-blue hover:bg-accent-blue/90" | |
| > | |
| Simpan & Terapkan | |
| </Button> | |
| </div> | |
| </div> | |
| </div> | |
| </Modal> | |
| ); | |
| } | |