Skip to content

Commit 20dc80e

Browse files
fix(web): remove circular dependency causing trading symbols input bug (#632) (#671)
**Problem:** Unable to type comma-separated trading symbols in the input field. When typing "BTCUSDT," → comma immediately disappears → cannot add more symbols. **Root Cause:** Circular state dependency between `useEffect` and `handleInputChange`: ```typescript // ❌ Lines 146-149: useEffect syncs selectedCoins → formData useEffect(() => { const symbolsString = selectedCoins.join(',') setFormData(prev => ({ ...prev, trading_symbols: symbolsString })) }, [selectedCoins]) // Lines 150-153: handleInputChange syncs formData → selectedCoins if (field === 'trading_symbols') { const coins = value.split(',').map(...).filter(...) setSelectedCoins(coins) } ``` **Execution Flow:** 1. User types: `"BTCUSDT,"` 2. `handleInputChange` fires → splits by comma → filters empty → `selectedCoins = ["BTCUSDT"]` 3. `useEffect` fires → joins → overwrites input to `"BTCUSDT"` ❌ **Trailing comma removed!** 4. User cannot continue typing **Solution:** Remove the redundant `useEffect` (lines 146-149) and update `handleCoinToggle` to directly sync both states: ```typescript // ✅ handleCoinToggle now updates both states const handleCoinToggle = (coin: string) => { setSelectedCoins(prev => { const newCoins = prev.includes(coin) ? ... : ... // Directly update formData.trading_symbols const symbolsString = newCoins.join(',') setFormData(current => ({ ...current, trading_symbols: symbolsString })) return newCoins }) } ``` **Why This Works:** - **Quick selector buttons** (`handleCoinToggle`): Now updates both states ✅ - **Manual input** (`handleInputChange`): Already updates both states ✅ - **No useEffect interference**: User can type freely ✅ **Impact:** - ✅ Manual typing of comma-separated symbols now works - ✅ Quick selector buttons still work correctly - ✅ No circular dependency - ✅ Cleaner unidirectional data flow Fixes #632 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: tinkle-community <[email protected]>
1 parent ffa8985 commit 20dc80e

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

web/src/components/TraderConfigModal.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ export function TraderConfigModal({
153153
fetchPromptTemplates()
154154
}, [])
155155

156-
// 当选择的币种改变时,更新输入框
157-
useEffect(() => {
158-
const symbolsString = selectedCoins.join(',')
159-
setFormData((prev) => ({ ...prev, trading_symbols: symbolsString }))
160-
}, [selectedCoins])
161-
162156
if (!isOpen) return null
163157

164158
const handleInputChange = (field: keyof TraderConfigData, value: any) => {
@@ -176,11 +170,15 @@ export function TraderConfigModal({
176170

177171
const handleCoinToggle = (coin: string) => {
178172
setSelectedCoins((prev) => {
179-
if (prev.includes(coin)) {
180-
return prev.filter((c) => c !== coin)
181-
} else {
182-
return [...prev, coin]
183-
}
173+
const newCoins = prev.includes(coin)
174+
? prev.filter((c) => c !== coin)
175+
: [...prev, coin]
176+
177+
// 同时更新 formData.trading_symbols
178+
const symbolsString = newCoins.join(',')
179+
setFormData((current) => ({ ...current, trading_symbols: symbolsString }))
180+
181+
return newCoins
184182
})
185183
}
186184

0 commit comments

Comments
 (0)