Prevent my-traders API calls when user is not logged in

- Add authentication checks to SWR calls in App.tsx and AITradersPage.tsx
- Only call api.getTraders when user and token are available
- Modify loadConfigs to skip authenticated API calls when not logged in
- Load only public supported models/exchanges for unauthenticated users
- Update useEffect dependencies to include user and token

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
icy
2025-11-03 23:14:10 +08:00
parent 22f3d3fb64
commit e762939655
2 changed files with 27 additions and 6 deletions

View File

@@ -88,10 +88,14 @@ function App() {
// window.location.hash = page === 'competition' ? '' : 'trader';
// };
// 获取trader列表
const { data: traders } = useSWR<TraderInfo[]>('traders', api.getTraders, {
refreshInterval: 10000,
});
// 获取trader列表(仅在用户登录时)
const { data: traders } = useSWR<TraderInfo[]>(
user && token ? 'traders' : null,
api.getTraders,
{
refreshInterval: 10000,
}
);
// 当获取到traders后设置默认选中第一个
useEffect(() => {

View File

@@ -4,6 +4,7 @@ import { api } from '../lib/api';
import type { TraderInfo, CreateTraderRequest, AIModel, Exchange } from '../types';
import { useLanguage } from '../contexts/LanguageContext';
import { t, type Language } from '../i18n/translations';
import { useAuth } from '../contexts/AuthContext';
import { getExchangeIcon } from './ExchangeIcons';
import { getModelIcon } from './ModelIcons';
import { TraderConfigModal } from './TraderConfigModal';
@@ -35,6 +36,7 @@ interface AITradersPageProps {
export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
const { language } = useLanguage();
const { user, token } = useAuth();
const [showCreateModal, setShowCreateModal] = useState(false);
const [showEditModal, setShowEditModal] = useState(false);
const [showModelModal, setShowModelModal] = useState(false);
@@ -53,7 +55,7 @@ export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
});
const { data: traders, mutate: mutateTraders } = useSWR<TraderInfo[]>(
'traders',
user && token ? 'traders' : null,
api.getTraders,
{ refreshInterval: 5000 }
);
@@ -61,6 +63,21 @@ export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
// 加载AI模型和交易所配置
useEffect(() => {
const loadConfigs = async () => {
if (!user || !token) {
// 未登录时只加载公开的支持模型和交易所
try {
const [supportedModels, supportedExchanges] = await Promise.all([
api.getSupportedModels(),
api.getSupportedExchanges()
]);
setSupportedModels(supportedModels);
setSupportedExchanges(supportedExchanges);
} catch (err) {
console.error('Failed to load supported configs:', err);
}
return;
}
try {
const [modelConfigs, exchangeConfigs, supportedModels, supportedExchanges] = await Promise.all([
api.getModelConfigs(),
@@ -88,7 +105,7 @@ export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
}
};
loadConfigs();
}, []);
}, [user, token]);
// 显示所有用户的模型和交易所配置(用于调试)
const configuredModels = allModels || [];