feat(frontend): 完成 Zustand 状态管理迁移
- 将 App.jsx 中的 useState 迁移到 5 个 Zustand stores - useRuntimeStore: 连接状态、运行时配置 - useMarketStore: 市场数据、股票价格 - usePortfolioStore: 组合、持仓、交易 - useAgentStore: Agent 技能,工作区 - useUIStore: UI 状态、视图切换 - 保留 tickers useState(需与 INITIAL_TICKERS 同步) - 恢复 newsApi.js 和 tradingApi.js Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
55
frontend/src/services/tradingApi.js
Normal file
55
frontend/src/services/tradingApi.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const trimTrailingSlash = (value) => value.replace(/\/+$/, '');
|
||||
const isLocalDevHost = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
return false;
|
||||
}
|
||||
const host = String(window.location.hostname || '').trim().toLowerCase();
|
||||
return host === 'localhost' || host === '127.0.0.1';
|
||||
};
|
||||
|
||||
const TRADING_SERVICE_BASE = trimTrailingSlash(import.meta.env.VITE_TRADING_SERVICE_URL || '') || (
|
||||
isLocalDevHost() ? 'http://localhost:8001' : ''
|
||||
);
|
||||
|
||||
export function hasDirectTradingService() {
|
||||
return Boolean(TRADING_SERVICE_BASE);
|
||||
}
|
||||
|
||||
export async function fetchInsiderTradesDirect(ticker, startDate = null, endDate = null, limit = 50) {
|
||||
if (!TRADING_SERVICE_BASE) {
|
||||
throw new Error('Direct trading service is not configured');
|
||||
}
|
||||
|
||||
const params = new URLSearchParams();
|
||||
params.set('ticker', ticker);
|
||||
params.set('limit', String(limit));
|
||||
if (startDate) {
|
||||
params.set('start_date', startDate);
|
||||
}
|
||||
if (endDate) {
|
||||
params.set('end_date', endDate);
|
||||
}
|
||||
|
||||
const response = await fetch(`${TRADING_SERVICE_BASE}/api/insider-trades?${params.toString()}`);
|
||||
if (!response.ok) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function fetchStockHistoryDirect(ticker, startDate, endDate) {
|
||||
if (!TRADING_SERVICE_BASE) {
|
||||
throw new Error('Direct trading service is not configured');
|
||||
}
|
||||
|
||||
const params = new URLSearchParams();
|
||||
params.set('ticker', ticker);
|
||||
params.set('start_date', startDate);
|
||||
params.set('end_date', endDate);
|
||||
|
||||
const response = await fetch(`${TRADING_SERVICE_BASE}/api/prices?${params.toString()}`);
|
||||
if (!response.ok) {
|
||||
throw new Error(await response.text());
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
Reference in New Issue
Block a user