Perps-Terminal / frontend /src /components /layout /MobileDrawer.tsx
Naufal Arhab
fix(audit): resolve multi-pass backend calculations, exchange guards, AI/UI state, and chart bugs
80df99b
Raw History Blame Contribute Delete
7.5 kB
import { useState } from 'react';
import { X, Plus, BarChart3, Search, Layers } from 'lucide-react';
import { PairItem } from '../pairs/PairItem';
import { Skeleton } from '../ui/Spinner';
import { Button } from '../ui/Button';
import type { Pair, Signal } from '../../types';
interface MobileDrawerProps {
isOpen: boolean;
onClose: () => void;
pairs: Pair[];
signals: Signal[];
isLoading: boolean;
selectedSymbol: string | null;
onSelect: (symbol: string | null) => void;
onOpenAddModal: () => void;
}
export function MobileDrawer({
isOpen,
onClose,
pairs,
signals,
isLoading,
selectedSymbol,
onSelect,
onOpenAddModal,
}: MobileDrawerProps) {
const [searchQuery, setSearchQuery] = useState('');
if (!isOpen) return null;
const signalMap = new Map(signals.map((s) => [s.symbol, s]));
const activePairCount = pairs.filter((p) => p.is_active).length;
const filteredPairs = pairs.filter((p) =>
p.symbol.toLowerCase().includes(searchQuery.toLowerCase().trim())
);
const activePairs = [...filteredPairs.filter((p) => p.is_active)].sort((a, b) =>
a.symbol.localeCompare(b.symbol)
);
const inactivePairs = filteredPairs.filter((p) => !p.is_active);
const handleSelect = (symbol: string | null) => {
onSelect(symbol);
onClose();
};
return (
<div className="fixed inset-0 z-50 md:hidden flex">
{/* Backdrop */}
<div
className="fixed inset-0 bg-black/80 backdrop-blur-sm transition-opacity"
onClick={onClose}
/>
{/* Slide-over panel */}
<div className="relative w-4/5 max-w-xs h-full bg-bg-card border-r border-border shadow-2xl flex flex-col z-10 animate-fade-in">
{/* Header */}
<div className="p-4 border-b border-border space-y-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<BarChart3 size={16} className="text-accent-blue" />
<span className="text-sm font-bold text-text-primary">
Trading Pairs
</span>
{activePairCount > 0 && (
<span className="text-[10px] font-mono bg-accent-blue/15 text-accent-blue border border-accent-blue/30 px-1.5 py-0.5 rounded font-semibold">
{activePairCount} Aktif
</span>
)}
</div>
<button
onClick={onClose}
className="p-1 rounded-lg text-text-secondary hover:text-text-primary hover:bg-bg-elevated transition-colors"
>
<X size={18} />
</button>
</div>
{/* Quick All Pairs filter button */}
{selectedSymbol && (
<button
onClick={() => handleSelect(null)}
className="w-full flex items-center justify-center gap-1.5 py-1.5 px-3 rounded-lg bg-accent-blue/10 border border-accent-blue/30 text-xs font-semibold text-accent-blue hover:bg-accent-blue/20 transition-colors"
>
<Layers size={13} />
<span>Tampilkan Semua Pair (Reset Filter)</span>
</button>
)}
{/* Search box */}
<div className="relative">
<Search
size={14}
className="absolute left-2.5 top-1/2 -translate-y-1/2 text-text-muted pointer-events-none"
/>
<input
type="text"
placeholder="Cari pair (e.g. BTC, ETH)..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
className="input-field w-full pl-8 pr-3 py-1.5 text-xs font-mono"
/>
{searchQuery && (
<button
onClick={() => setSearchQuery('')}
className="absolute right-2 top-1/2 -translate-y-1/2 text-text-muted hover:text-text-primary"
>
<X size={12} />
</button>
)}
</div>
{/* Add Pair Button */}
<Button
variant="primary"
size="sm"
onClick={() => {
onClose();
onOpenAddModal();
}}
icon={<Plus size={14} />}
className="w-full justify-center text-xs h-8"
>
Tambah Pair Baru
</Button>
</div>
{/* Pair List */}
<div className="flex-1 overflow-y-auto py-2 custom-scrollbar">
{isLoading ? (
<div className="space-y-2 px-3">
{[...Array(5)].map((_, i) => (
<div key={i} className="space-y-1.5 py-2">
<div className="flex items-center gap-2">
<Skeleton className="w-2 h-2 rounded-full" />
<Skeleton className="h-4 flex-1" />
</div>
</div>
))}
</div>
) : filteredPairs.length === 0 ? (
<div className="p-4 text-center">
<p className="text-xs text-text-muted">
{searchQuery ? `Tidak ada pair "${searchQuery}"` : 'Belum ada pair ditambahkan'}
</p>
</div>
) : (
<div className="space-y-1 px-2">
{activePairs.map((pair) => (
<PairItem
key={pair.id}
pair={pair}
signal={signalMap.get(pair.symbol)}
isSelected={selectedSymbol === pair.symbol}
onSelect={(sym) => handleSelect(sym)}
/>
))}
{inactivePairs.length > 0 && (
<>
<div className="px-3 pt-3 pb-1">
<p className="text-[10px] uppercase tracking-widest text-text-muted font-medium">
Nonaktif
</p>
</div>
{inactivePairs.map((pair) => (
<PairItem
key={pair.id}
pair={pair}
signal={signalMap.get(pair.symbol)}
isSelected={selectedSymbol === pair.symbol}
onSelect={(sym) => handleSelect(sym)}
/>
))}
</>
)}
</div>
)}
</div>
{/* Footer Statistics */}
<div className="p-3 border-t border-border bg-bg-primary/50 shrink-0">
<div className="grid grid-cols-3 gap-2 text-center">
{(['LONG', 'SHORT', 'NEUTRAL'] as const).map((dir) => {
const activeSymbolSet = new Set(pairs.filter((p) => p.is_active).map((p) => p.symbol));
const count = signals.filter(
(s) => activeSymbolSet.has(s.symbol) && s.direction === dir
).length;
return (
<div key={dir}>
<p
className={`text-xs font-bold font-mono ${
dir === 'LONG'
? 'text-signal-long'
: dir === 'SHORT'
? 'text-signal-short'
: 'text-text-muted'
}`}
>
{count}
</p>
<p className="text-[9px] text-text-muted font-medium">{dir}</p>
</div>
);
})}
</div>
</div>
</div>
</div>
);
}