refactor(web): restructure AITradersPage into modular architecture (#1023)

* refactor(web): restructure AITradersPage into modular architecture
Refactored the massive 2652-line AITradersPage.tsx into a clean, modular architecture following React best practices.
**Changes:**
- Decomposed 2652-line component into 12 focused modules
- Introduced Zustand stores for config and modal state management
- Extracted all business logic into useTraderActions custom hook (633 lines)
- Created reusable section components (PageHeader, TradersGrid, etc.)
- Separated complex modal logic into dedicated components
- Added TraderConfig type, eliminated all any types
- Fixed critical bugs in configuredExchanges logic and getState() usage
**File Structure:**
- Main page reduced from 2652 → 234 lines (91% reduction)
- components/traders/: 7 UI components + 5 section components
- stores/: tradersConfigStore, tradersModalStore
- hooks/: useTraderActions (all business logic)
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
* chore: ignore PR_DESCRIPTION.md
* fix(web): restore trader dashboard navigation functionality
Fixed missing navigation logic in refactored AITradersPage. The "查看" (View) button now correctly navigates to the trader dashboard.
**Root Cause:**
During refactoring, the `useNavigate` hook and default navigation logic were inadvertently omitted from the main page component.
**Changes:**
- Added `useNavigate` import from react-router-dom
- Implemented `handleTraderSelect` function with fallback navigation
- Restored original behavior: use `onTraderSelect` prop if provided, otherwise navigate to `/dashboard?trader=${traderId}`
**Testing:**
-  Click "查看" button navigates to trader dashboard
-  Query parameter correctly passed to dashboard
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
* fix(web): correct type definitions for trader configuration
Fixed TypeScript build errors by using the correct `TraderConfigData` type instead of the incorrect `TraderConfig` type.
**Root Cause:**
During refactoring, a new `TraderConfig` type was incorrectly created that extended `CreateTraderRequest` (with fields like `name`, `ai_model_id`). However, the `TraderConfigModal` component and API responses actually use `TraderConfigData` (with fields like `trader_name`, `ai_model`).
**Changes:**
- Replaced all `TraderConfig` references with `TraderConfigData`:
  - stores/tradersModalStore.ts
  - hooks/useTraderActions.ts
  - lib/api.ts
- Removed incorrect `TraderConfig` type definition from types.ts
- Added null check for `editingTrader.trader_id` to satisfy TypeScript
**Build Status:**
-  TypeScript compilation: PASS
-  Vite production build: PASS
Co-Authored-By: tinkle-community <tinklefund@gmail.com>
---------
Co-authored-by: tinkle-community <tinklefund@gmail.com>
This commit is contained in:
Ember
2025-11-15 12:33:48 +08:00
committed by tangmengqiu
parent 7eab056394
commit c64d4ff549
19 changed files with 3050 additions and 21 deletions

View File

@@ -0,0 +1,97 @@
import { Brain } from 'lucide-react'
import { t, Language } from '../../../i18n/translations'
import { getModelIcon } from '../../ModelIcons'
import { getShortName } from '../utils'
import type { AIModel } from '../../../types'
interface AIModelsSectionProps {
language: Language
configuredModels: AIModel[]
isModelInUse: (modelId: string) => boolean
onModelClick: (modelId: string) => void
}
export function AIModelsSection({
language,
configuredModels,
isModelInUse,
onModelClick,
}: AIModelsSectionProps) {
return (
<div className="binance-card p-3 md:p-4">
<h3
className="text-base md:text-lg font-semibold mb-3 flex items-center gap-2"
style={{ color: '#EAECEF' }}
>
<Brain className="w-4 h-4 md:w-5 md:h-5" style={{ color: '#60a5fa' }} />
{t('aiModels', language)}
</h3>
<div className="space-y-2 md:space-y-3">
{configuredModels.map((model) => {
const inUse = isModelInUse(model.id)
return (
<div
key={model.id}
className={`flex items-center justify-between p-2 md:p-3 rounded transition-all ${
inUse
? 'cursor-not-allowed'
: 'cursor-pointer hover:bg-gray-700'
}`}
style={{ background: '#0B0E11', border: '1px solid #2B3139' }}
onClick={() => onModelClick(model.id)}
>
<div className="flex items-center gap-2 md:gap-3">
<div className="w-7 h-7 md:w-8 md:h-8 flex items-center justify-center flex-shrink-0">
{getModelIcon(model.provider || model.id, {
width: 28,
height: 28,
}) || (
<div
className="w-7 h-7 md:w-8 md:h-8 rounded-full flex items-center justify-center text-xs md:text-sm font-bold"
style={{
background:
model.id === 'deepseek' ? '#60a5fa' : '#c084fc',
color: '#fff',
}}
>
{getShortName(model.name)[0]}
</div>
)}
</div>
<div className="min-w-0">
<div
className="font-semibold text-sm md:text-base truncate"
style={{ color: '#EAECEF' }}
>
{getShortName(model.name)}
</div>
<div className="text-xs" style={{ color: '#848E9C' }}>
{inUse
? t('inUse', language)
: model.enabled
? t('enabled', language)
: t('configured', language)}
</div>
</div>
</div>
<div
className={`w-2.5 h-2.5 md:w-3 md:h-3 rounded-full flex-shrink-0 ${model.enabled ? 'bg-green-400' : 'bg-gray-500'}`}
/>
</div>
)
})}
{configuredModels.length === 0 && (
<div
className="text-center py-6 md:py-8"
style={{ color: '#848E9C' }}
>
<Brain className="w-10 h-10 md:w-12 md:h-12 mx-auto mb-2 opacity-50" />
<div className="text-xs md:text-sm">
{t('noModelsConfigured', language)}
</div>
</div>
)}
</div>
</div>
)
}

View File

@@ -0,0 +1,87 @@
import { Landmark } from 'lucide-react'
import { t, type Language } from '../../../i18n/translations'
import { getExchangeIcon } from '../../ExchangeIcons'
import { getShortName } from '../index'
import type { Exchange } from '../../../types'
interface ExchangesSectionProps {
language: Language
configuredExchanges: Exchange[]
isExchangeInUse: (exchangeId: string) => boolean
onExchangeClick: (exchangeId: string) => void
}
export function ExchangesSection({
language,
configuredExchanges,
isExchangeInUse,
onExchangeClick,
}: ExchangesSectionProps) {
return (
<div className="binance-card p-3 md:p-4">
<h3
className="text-base md:text-lg font-semibold mb-3 flex items-center gap-2"
style={{ color: '#EAECEF' }}
>
<Landmark
className="w-4 h-4 md:w-5 md:h-5"
style={{ color: '#F0B90B' }}
/>
{t('exchanges', language)}
</h3>
<div className="space-y-2 md:space-y-3">
{configuredExchanges.map((exchange) => {
const inUse = isExchangeInUse(exchange.id)
return (
<div
key={exchange.id}
className={`flex items-center justify-between p-2 md:p-3 rounded transition-all ${
inUse
? 'cursor-not-allowed'
: 'cursor-pointer hover:bg-gray-700'
}`}
style={{ background: '#0B0E11', border: '1px solid #2B3139' }}
onClick={() => onExchangeClick(exchange.id)}
>
<div className="flex items-center gap-2 md:gap-3">
<div className="w-7 h-7 md:w-8 md:h-8 flex items-center justify-center flex-shrink-0">
{getExchangeIcon(exchange.id, { width: 28, height: 28 })}
</div>
<div className="min-w-0">
<div
className="font-semibold text-sm md:text-base truncate"
style={{ color: '#EAECEF' }}
>
{getShortName(exchange.name)}
</div>
<div className="text-xs" style={{ color: '#848E9C' }}>
{exchange.type.toUpperCase()} {' '}
{inUse
? t('inUse', language)
: exchange.enabled
? t('enabled', language)
: t('configured', language)}
</div>
</div>
</div>
<div
className={`w-2.5 h-2.5 md:w-3 md:h-3 rounded-full flex-shrink-0 ${exchange.enabled ? 'bg-green-400' : 'bg-gray-500'}`}
/>
</div>
)
})}
{configuredExchanges.length === 0 && (
<div
className="text-center py-6 md:py-8"
style={{ color: '#848E9C' }}
>
<Landmark className="w-10 h-10 md:w-12 md:h-12 mx-auto mb-2 opacity-50" />
<div className="text-xs md:text-sm">
{t('noExchangesConfigured', language)}
</div>
</div>
)}
</div>
</div>
)
}

View File

@@ -0,0 +1,117 @@
import { Bot, Plus, Radio } from 'lucide-react'
import { t, type Language } from '../../../i18n/translations'
interface PageHeaderProps {
language: Language
tradersCount: number
configuredModelsCount: number
configuredExchangesCount: number
onAddModel: () => void
onAddExchange: () => void
onConfigureSignalSource: () => void
onCreateTrader: () => void
}
export function PageHeader({
language,
tradersCount,
configuredModelsCount,
configuredExchangesCount,
onAddModel,
onAddExchange,
onConfigureSignalSource,
onCreateTrader,
}: PageHeaderProps) {
const canCreateTrader =
configuredModelsCount > 0 && configuredExchangesCount > 0
return (
<div className="flex flex-col md:flex-row items-start md:items-center justify-between gap-3 md:gap-0">
<div className="flex items-center gap-3 md:gap-4">
<div
className="w-10 h-10 md:w-12 md:h-12 rounded-xl flex items-center justify-center"
style={{
background: 'linear-gradient(135deg, #F0B90B 0%, #FCD535 100%)',
boxShadow: '0 4px 14px rgba(240, 185, 11, 0.4)',
}}
>
<Bot className="w-5 h-5 md:w-6 md:h-6" style={{ color: '#000' }} />
</div>
<div>
<h1
className="text-xl md:text-2xl font-bold flex items-center gap-2"
style={{ color: '#EAECEF' }}
>
{t('aiTraders', language)}
<span
className="text-xs font-normal px-2 py-1 rounded"
style={{
background: 'rgba(240, 185, 11, 0.15)',
color: '#F0B90B',
}}
>
{tradersCount} {t('active', language)}
</span>
</h1>
<p className="text-xs" style={{ color: '#848E9C' }}>
{t('manageAITraders', language)}
</p>
</div>
</div>
<div className="flex gap-2 md:gap-3 w-full md:w-auto overflow-hidden flex-wrap md:flex-nowrap">
<button
onClick={onAddModel}
className="px-3 md:px-4 py-2 rounded text-xs md:text-sm font-semibold transition-all hover:scale-105 flex items-center gap-1 md:gap-2 whitespace-nowrap"
style={{
background: '#2B3139',
color: '#EAECEF',
border: '1px solid #474D57',
}}
>
<Plus className="w-3 h-3 md:w-4 md:h-4" />
{t('aiModels', language)}
</button>
<button
onClick={onAddExchange}
className="px-3 md:px-4 py-2 rounded text-xs md:text-sm font-semibold transition-all hover:scale-105 flex items-center gap-1 md:gap-2 whitespace-nowrap"
style={{
background: '#2B3139',
color: '#EAECEF',
border: '1px solid #474D57',
}}
>
<Plus className="w-3 h-3 md:w-4 md:h-4" />
{t('exchanges', language)}
</button>
<button
onClick={onConfigureSignalSource}
className="px-3 md:px-4 py-2 rounded text-xs md:text-sm font-semibold transition-all hover:scale-105 flex items-center gap-1 md:gap-2 whitespace-nowrap"
style={{
background: '#2B3139',
color: '#EAECEF',
border: '1px solid #474D57',
}}
>
<Radio className="w-3 h-3 md:w-4 md:h-4" />
{t('signalSource', language)}
</button>
<button
onClick={onCreateTrader}
disabled={!canCreateTrader}
className="px-3 md:px-4 py-2 rounded text-xs md:text-sm font-semibold transition-all hover:scale-105 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-1 md:gap-2 whitespace-nowrap"
style={{
background: canCreateTrader ? '#F0B90B' : '#2B3139',
color: canCreateTrader ? '#000' : '#848E9C',
}}
>
<Plus className="w-4 h-4" />
{t('createTrader', language)}
</button>
</div>
</div>
)
}

View File

@@ -0,0 +1,54 @@
import { AlertTriangle } from 'lucide-react'
import { t, type Language } from '../../../i18n/translations'
interface SignalSourceWarningProps {
language: Language
onConfigure: () => void
}
export function SignalSourceWarning({
language,
onConfigure,
}: SignalSourceWarningProps) {
return (
<div
className="rounded-lg px-4 py-3 flex items-start gap-3 animate-slide-in"
style={{
background: 'rgba(246, 70, 93, 0.1)',
border: '1px solid rgba(246, 70, 93, 0.3)',
}}
>
<AlertTriangle
size={20}
className="flex-shrink-0 mt-0.5"
style={{ color: '#F6465D' }}
/>
<div className="flex-1">
<div className="font-semibold mb-1" style={{ color: '#F6465D' }}>
{t('signalSourceNotConfigured', language)}
</div>
<div className="text-sm" style={{ color: '#848E9C' }}>
<p className="mb-2">{t('signalSourceWarningMessage', language)}</p>
<p>
<strong>{t('solutions', language)}</strong>
</p>
<ul className="list-disc list-inside space-y-1 ml-2 mt-1">
<li>"{t('signalSource', language)}"API地址</li>
<li>"使用币种池""使用OI Top"</li>
<li></li>
</ul>
</div>
<button
onClick={onConfigure}
className="mt-3 px-3 py-1.5 rounded text-sm font-semibold transition-all hover:scale-105"
style={{
background: '#F0B90B',
color: '#000',
}}
>
{t('configureSignalSourceNow', language)}
</button>
</div>
</div>
)
}

View File

@@ -0,0 +1,172 @@
import { Bot, BarChart3, Trash2, Pencil } from 'lucide-react'
import { t, type Language } from '../../../i18n/translations'
import { getModelDisplayName } from '../index'
import type { TraderInfo } from '../../../types'
interface TradersGridProps {
language: Language
traders: TraderInfo[] | undefined
onTraderSelect: (traderId: string) => void
onEditTrader: (traderId: string) => void
onDeleteTrader: (traderId: string) => void
onToggleTrader: (traderId: string, running: boolean) => void
}
export function TradersGrid({
language,
traders,
onTraderSelect,
onEditTrader,
onDeleteTrader,
onToggleTrader,
}: TradersGridProps) {
if (!traders || traders.length === 0) {
return (
<div className="text-center py-12 md:py-16" style={{ color: '#848E9C' }}>
<Bot className="w-16 h-16 md:w-24 md:h-24 mx-auto mb-3 md:mb-4 opacity-50" />
<div className="text-base md:text-lg font-semibold mb-2">
{t('noTraders', language)}
</div>
<div className="text-xs md:text-sm mb-3 md:mb-4">
{t('createFirstTrader', language)}
</div>
</div>
)
}
return (
<div className="space-y-3 md:space-y-4">
{traders.map((trader) => (
<div
key={trader.trader_id}
className="flex flex-col md:flex-row md:items-center justify-between p-3 md:p-4 rounded transition-all hover:translate-y-[-1px] gap-3 md:gap-4"
style={{ background: '#0B0E11', border: '1px solid #2B3139' }}
>
<div className="flex items-center gap-3 md:gap-4">
<div
className="w-10 h-10 md:w-12 md:h-12 rounded-full flex items-center justify-center flex-shrink-0"
style={{
background: trader.ai_model.includes('deepseek')
? '#60a5fa'
: '#c084fc',
color: '#fff',
}}
>
<Bot className="w-5 h-5 md:w-6 md:h-6" />
</div>
<div className="min-w-0">
<div
className="font-bold text-base md:text-lg truncate"
style={{ color: '#EAECEF' }}
>
{trader.trader_name}
</div>
<div
className="text-xs md:text-sm truncate"
style={{
color: trader.ai_model.includes('deepseek')
? '#60a5fa'
: '#c084fc',
}}
>
{getModelDisplayName(
trader.ai_model.split('_').pop() || trader.ai_model
)}{' '}
Model {trader.exchange_id?.toUpperCase()}
</div>
</div>
</div>
<div className="flex items-center gap-3 md:gap-4 flex-wrap md:flex-nowrap">
{/* Status */}
<div className="text-center">
<div
className={`px-2 md:px-3 py-1 rounded text-xs font-bold ${
trader.is_running
? 'bg-green-100 text-green-800'
: 'bg-red-100 text-red-800'
}`}
style={
trader.is_running
? {
background: 'rgba(14, 203, 129, 0.1)',
color: '#0ECB81',
}
: {
background: 'rgba(246, 70, 93, 0.1)',
color: '#F6465D',
}
}
>
{trader.is_running
? t('running', language)
: t('stopped', language)}
</div>
</div>
{/* Actions: 禁止换行,超出横向滚动 */}
<div className="flex gap-1.5 md:gap-2 flex-nowrap overflow-x-auto items-center">
<button
onClick={() => onTraderSelect(trader.trader_id)}
className="px-2 md:px-3 py-1.5 md:py-2 rounded text-xs md:text-sm font-semibold transition-all hover:scale-105 flex items-center gap-1 whitespace-nowrap"
style={{
background: 'rgba(99, 102, 241, 0.1)',
color: '#6366F1',
}}
>
<BarChart3 className="w-3 h-3 md:w-4 md:h-4" />
{t('view', language)}
</button>
<button
onClick={() => onEditTrader(trader.trader_id)}
disabled={trader.is_running}
className="px-2 md:px-3 py-1.5 md:py-2 rounded text-xs md:text-sm font-semibold transition-all hover:scale-105 disabled:opacity-50 disabled:cursor-not-allowed whitespace-nowrap flex items-center gap-1"
style={{
background: trader.is_running
? 'rgba(132, 142, 156, 0.1)'
: 'rgba(255, 193, 7, 0.1)',
color: trader.is_running ? '#848E9C' : '#FFC107',
}}
>
<Pencil className="w-3 h-3 md:w-4 md:h-4" />
{t('edit', language)}
</button>
<button
onClick={() =>
onToggleTrader(trader.trader_id, trader.is_running || false)
}
className="px-2 md:px-3 py-1.5 md:py-2 rounded text-xs md:text-sm font-semibold transition-all hover:scale-105 whitespace-nowrap"
style={
trader.is_running
? {
background: 'rgba(246, 70, 93, 0.1)',
color: '#F6465D',
}
: {
background: 'rgba(14, 203, 129, 0.1)',
color: '#0ECB81',
}
}
>
{trader.is_running ? t('stop', language) : t('start', language)}
</button>
<button
onClick={() => onDeleteTrader(trader.trader_id)}
className="px-2 md:px-3 py-1.5 md:py-2 rounded text-xs md:text-sm font-semibold transition-all hover:scale-105"
style={{
background: 'rgba(246, 70, 93, 0.1)',
color: '#F6465D',
}}
>
<Trash2 className="w-3 h-3 md:w-4 md:h-4" />
</button>
</div>
</div>
</div>
))}
</div>
)
}