Files
KisStock/pages/AutoTrading.tsx
2026-02-01 20:24:04 +09:00

289 lines
17 KiB
TypeScript

import React, { useState } from 'react';
import { Cpu, Plus, Calendar, Zap, Trash2, Activity, Clock, LayoutGrid, Layers, X } from 'lucide-react';
import { StockItem, AutoTradeConfig, MarketType, WatchlistGroup } from '../types';
interface AutoTradingProps {
marketMode: MarketType;
stocks: StockItem[];
configs: AutoTradeConfig[];
groups: WatchlistGroup[];
onAddConfig: (config: Omit<AutoTradeConfig, 'id' | 'active'>) => void;
onToggleConfig: (id: string) => void;
onDeleteConfig: (id: string) => void;
}
const AutoTrading: React.FC<AutoTradingProps> = ({ marketMode, stocks, configs, groups, onAddConfig, onToggleConfig, onDeleteConfig }) => {
const [showAddModal, setShowAddModal] = useState(false);
const [targetType, setTargetType] = useState<'SINGLE' | 'GROUP'>('SINGLE');
const [newConfig, setNewConfig] = useState<Partial<AutoTradeConfig>>({
type: 'ACCUMULATION',
frequency: 'DAILY',
quantity: 1,
executionTime: '09:00',
specificDay: 1
});
const handleAdd = () => {
if (newConfig.type) {
if (targetType === 'SINGLE' && !newConfig.stockCode) return;
if (targetType === 'GROUP' && !newConfig.groupId) return;
const stockName = targetType === 'SINGLE'
? stocks.find(s => s.code === newConfig.stockCode)?.name || '알 수 없음'
: groups.find(g => g.id === newConfig.groupId)?.name || '알 수 없는 그룹';
onAddConfig({
stockCode: targetType === 'SINGLE' ? newConfig.stockCode : undefined,
groupId: targetType === 'GROUP' ? newConfig.groupId : undefined,
stockName: stockName,
type: newConfig.type as 'ACCUMULATION' | 'TRAILING_STOP',
quantity: newConfig.quantity || 1,
frequency: newConfig.frequency as 'DAILY' | 'WEEKLY' | 'MONTHLY',
specificDay: newConfig.specificDay,
executionTime: newConfig.executionTime || '09:00',
trailingPercent: newConfig.trailingPercent,
market: marketMode
});
setShowAddModal(false);
}
};
const getDayLabel = (config: AutoTradeConfig) => {
if (config.frequency === 'DAILY') return '매일';
if (config.frequency === 'WEEKLY') {
const days = ['일', '월', '화', '수', '목', '금', '토'];
return `매주 ${days[config.specificDay || 0]}요일`;
}
if (config.frequency === 'MONTHLY') return `매월 ${config.specificDay}`;
return '';
};
const filteredStocks = stocks.filter(s => s.market === marketMode);
const filteredGroups = groups.filter(g => g.codes.some(code => stocks.find(s => s.code === code)?.market === marketMode));
return (
<div className="space-y-6 animate-in slide-in-from-bottom-6 duration-500 pb-20 pr-4">
<div className="flex justify-between items-center bg-white p-6 rounded-2xl shadow-sm border border-slate-100">
<div>
<h3 className="text-xl font-black text-slate-800 uppercase tracking-tighter flex items-center gap-3">
<Cpu className="text-emerald-500" size={24} /> {marketMode === MarketType.DOMESTIC ? '국내' : '해외'}
</h3>
<p className="text-[12px] font-black text-slate-400 uppercase tracking-widest mt-1 flex items-center gap-2">
<span className="relative flex h-2 w-2">
<span className={`animate-ping absolute inline-flex h-full w-full rounded-full opacity-75 ${configs.filter(c => c.active && c.market === marketMode).length > 0 ? 'bg-emerald-400' : 'bg-slate-300'}`}></span>
<span className={`relative inline-flex rounded-full h-2 w-2 ${configs.filter(c => c.active && c.market === marketMode).length > 0 ? 'bg-emerald-500' : 'bg-slate-400'}`}></span>
</span>
{configs.filter(c => c.active && c.market === marketMode).length}
</p>
</div>
<button
onClick={() => { setShowAddModal(true); setTargetType('SINGLE'); }}
className="bg-slate-900 text-white px-6 py-2.5 rounded-xl font-black text-[12px] uppercase tracking-widest flex items-center gap-2 hover:bg-slate-800 transition-all shadow-md active:scale-95"
>
<Plus size={18} />
</button>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{configs.filter(c => c.market === marketMode).map(config => (
<div key={config.id} className={`bg-white p-5 rounded-2xl shadow-sm border transition-all relative overflow-hidden group hover:shadow-md ${config.active ? 'border-emerald-200' : 'border-slate-100 grayscale-[0.5]'}`}>
<div className={`absolute top-0 left-0 w-full h-1.5 transition-colors ${config.active ? (config.groupId ? 'bg-indigo-500' : (config.type === 'ACCUMULATION' ? 'bg-blue-500' : 'bg-orange-500')) : 'bg-slate-300'}`}></div>
<div className="flex justify-between items-start mb-6">
<div className="flex items-center gap-4">
<div className={`p-3 rounded-xl shadow-sm transition-colors ${config.active ? (config.groupId ? 'bg-indigo-50 text-indigo-600' : (config.type === 'ACCUMULATION' ? 'bg-blue-50 text-blue-600' : 'bg-orange-50 text-orange-600')) : 'bg-slate-100 text-slate-400'}`}>
{config.groupId ? <Layers size={20} /> : <Activity size={20} />}
</div>
<div>
<h4 className={`font-black text-[16px] leading-tight mb-0.5 transition-colors ${config.active ? 'text-slate-900' : 'text-slate-400'}`}>{config.stockName}</h4>
<p className="text-[10px] text-slate-400 font-mono font-bold tracking-widest uppercase">{config.groupId ? 'GROUP AGENT' : config.stockCode}</p>
</div>
</div>
{/* 활성화 토글 스위치 */}
<button
onClick={() => onToggleConfig(config.id)}
className={`relative inline-flex h-5 w-9 items-center rounded-full transition-all focus:outline-none ${config.active ? 'bg-emerald-500' : 'bg-slate-200'}`}
>
<span className={`inline-block h-3.5 w-3.5 transform rounded-full bg-white shadow-sm transition-transform ${config.active ? 'translate-x-4.5' : 'translate-x-1'}`} />
</button>
</div>
<div className="space-y-4">
<div className={`p-4 rounded-xl space-y-3 border transition-colors ${config.active ? 'bg-slate-50/80 border-slate-100' : 'bg-slate-50/30 border-transparent'}`}>
<div className="flex justify-between items-center text-[11px] font-black uppercase tracking-widest">
<span className="text-slate-400"></span>
<span className={config.active ? (config.groupId ? 'text-indigo-600' : 'text-slate-600') : 'text-slate-300'}>{config.groupId ? '그룹 일괄' : '개별 자산'}</span>
</div>
<div className="flex justify-between items-center text-[11px] font-black uppercase tracking-widest">
<span className="text-slate-400"> </span>
<span className={`px-2 py-0.5 rounded-lg transition-colors ${config.active ? (config.type === 'ACCUMULATION' ? 'bg-blue-100 text-blue-600' : 'bg-orange-100 text-orange-600') : 'bg-slate-100 text-slate-300'}`}>
{config.type === 'ACCUMULATION' ? '적립식 매수' : 'TS 자동매매'}
</span>
</div>
<div className="flex justify-between items-center text-[12px] font-bold">
<span className="text-slate-400 uppercase tracking-widest text-[11px]"></span>
<span className={`flex items-center gap-2 transition-colors ${config.active ? 'text-slate-700' : 'text-slate-300'}`}><Calendar size={14} className="text-slate-400" /> {getDayLabel(config)}</span>
</div>
<div className="flex justify-between items-center text-[12px] font-bold">
<span className="text-slate-400 uppercase tracking-widest text-[11px]"></span>
<span className={`flex items-center gap-2 transition-colors ${config.active ? 'text-slate-700' : 'text-slate-300'}`}><Clock size={14} className="text-slate-400" /> {config.executionTime} / {config.quantity}</span>
</div>
</div>
<div className="pt-4 border-t border-slate-100 flex justify-between items-center">
<div className="flex items-center gap-2">
<span className={`w-2 h-2 rounded-full transition-colors ${config.active ? 'bg-emerald-500' : 'bg-slate-300'}`}></span>
<span className="text-[11px] font-black text-slate-400 uppercase tracking-widest">{config.active ? '에이전트 운용 중' : '일시 중단됨'}</span>
</div>
<button
onClick={() => onDeleteConfig(config.id)}
className="p-2 text-slate-300 hover:text-rose-500 hover:bg-rose-50 rounded-lg transition-all"
>
<Trash2 size={18} />
</button>
</div>
</div>
</div>
))}
</div>
{showAddModal && (
<div className="fixed inset-0 z-[100] bg-slate-900/60 backdrop-blur-sm flex items-center justify-center p-6">
<div className="bg-white w-full max-w-xl rounded-2xl p-8 shadow-2xl animate-in zoom-in-95 duration-300 border border-slate-100 overflow-hidden">
<div className="flex justify-between items-center mb-8">
<h3 className="text-xl font-black text-slate-900 uppercase tracking-tighter flex items-center gap-3">
<Zap className="text-yellow-400 fill-yellow-400" size={24} />
</h3>
<button onClick={() => setShowAddModal(false)} className="p-2 hover:bg-slate-100 rounded-lg transition-colors"><X size={20} className="text-slate-400" /></button>
</div>
<div className="space-y-6">
<div className="space-y-3">
<label className="text-[11px] font-black text-slate-400 uppercase tracking-widest ml-1"> </label>
<div className="flex bg-slate-100 p-1.5 rounded-xl shadow-inner">
<button
onClick={() => setTargetType('SINGLE')}
className={`flex-1 py-2.5 rounded-lg text-[12px] font-black transition-all ${targetType === 'SINGLE' ? 'bg-white text-slate-900 shadow-sm' : 'text-slate-400 hover:text-slate-600'}`}
>
</button>
<button
onClick={() => setTargetType('GROUP')}
className={`flex-1 py-2.5 rounded-lg text-[12px] font-black transition-all ${targetType === 'GROUP' ? 'bg-white text-slate-900 shadow-sm' : 'text-slate-400 hover:text-slate-600'}`}
>
</button>
</div>
</div>
<div className="grid grid-cols-2 gap-6">
<div className="space-y-3">
<label className="text-[11px] font-black text-slate-400 uppercase tracking-widest ml-1">
{targetType === 'SINGLE' ? '자산 선택' : '그룹 선택'}
</label>
{targetType === 'SINGLE' ? (
<select
className="w-full p-3 bg-slate-50 rounded-xl border-2 border-transparent focus:border-blue-500 outline-none font-bold text-slate-800 text-[14px] shadow-sm"
onChange={(e) => setNewConfig({...newConfig, stockCode: e.target.value})}
value={newConfig.stockCode || ''}
>
<option value=""> </option>
{filteredStocks.map(s => <option key={s.code} value={s.code}>{s.name}</option>)}
</select>
) : (
<select
className="w-full p-3 bg-slate-50 rounded-xl border-2 border-transparent focus:border-blue-500 outline-none font-bold text-slate-800 text-[14px] shadow-sm"
onChange={(e) => setNewConfig({...newConfig, groupId: e.target.value})}
value={newConfig.groupId || ''}
>
<option value=""> </option>
{filteredGroups.map(g => <option key={g.id} value={g.id}>{g.name}</option>)}
</select>
)}
</div>
<div className="space-y-3">
<label className="text-[11px] font-black text-slate-400 uppercase tracking-widest ml-1"> </label>
<input
type="number"
className="w-full p-3 bg-slate-50 rounded-xl border-2 border-transparent focus:border-blue-500 outline-none font-black text-slate-800 text-center text-[18px] shadow-sm"
value={newConfig.quantity}
onChange={(e) => setNewConfig({...newConfig, quantity: parseInt(e.target.value)})}
/>
</div>
</div>
<div className="space-y-3">
<label className="text-[11px] font-black text-slate-400 uppercase tracking-widest ml-1"> </label>
<div className="grid grid-cols-3 gap-3">
{[
{ val: 'DAILY', label: '매일' },
{ val: 'WEEKLY', label: '매주' },
{ val: 'MONTHLY', label: '매월' }
].map(freq => (
<button
key={freq.val}
onClick={() => setNewConfig({...newConfig, frequency: freq.val as any, specificDay: freq.val === 'DAILY' ? undefined : 1})}
className={`py-3 rounded-xl text-[12px] font-black transition-all border-2 ${newConfig.frequency === freq.val ? 'bg-slate-900 text-white border-slate-900 shadow-md' : 'bg-white text-slate-400 border-slate-100 hover:border-slate-300'}`}
>
{freq.label}
</button>
))}
</div>
</div>
<div className="grid grid-cols-2 gap-6">
{newConfig.frequency !== 'DAILY' && (
<div className="space-y-3">
<label className="text-[11px] font-black text-slate-400 uppercase tracking-widest ml-1">
{newConfig.frequency === 'WEEKLY' ? '요일' : '날짜'}
</label>
<select
className="w-full p-3 bg-slate-50 rounded-xl border-2 border-transparent focus:border-blue-500 outline-none font-bold text-slate-800 text-[14px] shadow-sm"
value={newConfig.specificDay}
onChange={(e) => setNewConfig({...newConfig, specificDay: parseInt(e.target.value)})}
>
{newConfig.frequency === 'WEEKLY' ? (
['일', '월', '화', '수', '목', '금', '토'].map((d, i) => <option key={d} value={i}>{d}</option>)
) : (
Array.from({length: 31}, (_, i) => i + 1).map(d => <option key={d} value={d}>{d}</option>)
)}
</select>
</div>
)}
<div className="space-y-3">
<label className="text-[11px] font-black text-slate-400 uppercase tracking-widest ml-1">퀀 </label>
<input
type="time"
className="w-full p-3 bg-slate-50 rounded-xl border-2 border-transparent focus:border-blue-500 outline-none font-bold text-slate-800 text-[14px] shadow-sm"
value={newConfig.executionTime}
onChange={(e) => setNewConfig({...newConfig, executionTime: e.target.value})}
/>
</div>
</div>
<div className="flex gap-4 pt-6">
<button
onClick={() => setShowAddModal(false)}
className="flex-1 py-3 bg-slate-100 text-slate-400 rounded-xl font-black uppercase text-[12px] tracking-widest hover:bg-slate-200 transition-all"
>
</button>
<button
onClick={handleAdd}
className="flex-1 py-3 bg-blue-600 text-white rounded-xl font-black uppercase text-[12px] tracking-widest hover:bg-blue-700 transition-all shadow-md"
>
</button>
</div>
</div>
</div>
</div>
)}
</div>
);
};
export default AutoTrading;