refactor: move history tab behind backend apis
This commit is contained in:
parent
b433686776
commit
541c617717
@ -1,13 +1,12 @@
|
||||
import { useEffect, useState, useMemo } from 'react';
|
||||
import { supabase } from '../lib/supabaseClient';
|
||||
import { useAuth } from '../components/AuthContext';
|
||||
import { tableNameTransactions, tableNameOrders } from '../lib/const';
|
||||
import { aggregateHistoryLedger, buildHistoryLedger } from '../lib/tradeHistoryLedger';
|
||||
import {
|
||||
type LifecycleOrderRow
|
||||
} from '../lib/orderLifecycleLedger';
|
||||
import { useCanonicalLifecycle } from '../hooks/useCanonicalLifecycle';
|
||||
import { fetchTradeProfiles } from '../lib/profileApi';
|
||||
import { fetchTradeHistory } from '../lib/tradeHistoryApi';
|
||||
import { fetchPositionsBootstrap } from '../lib/positionsApi';
|
||||
|
||||
|
||||
interface TradeRecord {
|
||||
@ -321,89 +320,20 @@ export const HistoryTab = ({ botState }: HistoryTabProps) => {
|
||||
const fetchData = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const histColumnsV2 = 'id,timestamp,symbol,side,entry_price,exit_price,size,pnl,pnl_percent,reason,profile_id,stop_loss,take_profit,rules_metadata,created_at,trade_id,source';
|
||||
const histColumnsLegacy = 'id,timestamp,symbol,side,entry_price,exit_price,size,pnl,pnl_percent,reason,profile_id,stop_loss,take_profit,rules_metadata,created_at';
|
||||
const scope = profile?.role === 'admin' ? 'all' : 'user';
|
||||
const [historyRows, positionsBootstrap] = await Promise.all([
|
||||
fetchTradeHistory({ scope, limit: 5000 }),
|
||||
fetchPositionsBootstrap({ scope, limit: 5000 }),
|
||||
]);
|
||||
|
||||
let historyQuery = supabase
|
||||
.from(tableNameTransactions)
|
||||
.select(histColumnsV2)
|
||||
.order('created_at', { ascending: false });
|
||||
const profData = (positionsBootstrap.profiles || []).map((item: any) => ({
|
||||
id: String(item.id),
|
||||
name: String(item.name || item.id || 'Unnamed Profile'),
|
||||
allocated_capital: Number(item.allocated_capital || 0)
|
||||
}));
|
||||
|
||||
if (profile?.role !== 'admin') {
|
||||
historyQuery = historyQuery.eq('user_id', user.id);
|
||||
}
|
||||
|
||||
const { data: histDataV2, error: histErrorV2 } = await historyQuery;
|
||||
let historyData = histDataV2 as TradeRecord[] | null;
|
||||
|
||||
if (histErrorV2) {
|
||||
console.warn('[HistoryTab] V2 history query failed, falling back to legacy columns:', histErrorV2.message);
|
||||
let legacyHistoryQuery = supabase
|
||||
.from(tableNameTransactions)
|
||||
.select(histColumnsLegacy)
|
||||
.order('created_at', { ascending: false });
|
||||
|
||||
if (profile?.role !== 'admin') {
|
||||
legacyHistoryQuery = legacyHistoryQuery.eq('user_id', user.id);
|
||||
}
|
||||
|
||||
const { data: histDataLegacy, error: histErrorLegacy } = await legacyHistoryQuery;
|
||||
if (histErrorLegacy) {
|
||||
console.error('[HistoryTab] Legacy history query failed:', histErrorLegacy.message);
|
||||
}
|
||||
historyData = histDataLegacy as TradeRecord[] | null;
|
||||
}
|
||||
|
||||
let profData: Array<{ id: string; name: string; allocated_capital?: number }> = [];
|
||||
try {
|
||||
const profiles = await fetchTradeProfiles({ scope: profile?.role === 'admin' ? 'all' : 'user' });
|
||||
profData = profiles.map((item: any) => ({
|
||||
id: String(item.id),
|
||||
name: String(item.name || item.id || 'Unnamed Profile'),
|
||||
allocated_capital: Number(item.allocated_capital || 0)
|
||||
}));
|
||||
} catch (error: any) {
|
||||
console.error('[HistoryTab] Failed loading profiles:', error.message || error);
|
||||
}
|
||||
|
||||
const orderTagColumnsV2 = 'order_id,trade_id,profile_id,sub_tag,timestamp,filled_at,created_at,symbol,side,action,qty,quantity,price,status,source';
|
||||
const orderTagColumnsLegacy = 'order_id,trade_id,profile_id,timestamp,created_at,symbol,side,action,qty,price,status,source';
|
||||
let orderTagsQuery = supabase
|
||||
.from(tableNameOrders)
|
||||
.select(orderTagColumnsV2)
|
||||
.not('trade_id', 'is', null)
|
||||
.order('created_at', { ascending: false })
|
||||
.limit(5000);
|
||||
|
||||
if (profile?.role !== 'admin') {
|
||||
orderTagsQuery = orderTagsQuery.eq('user_id', user.id);
|
||||
}
|
||||
|
||||
const { data: orderTagsV2, error: orderTagsErrorV2 } = await orderTagsQuery;
|
||||
let orderTagRows = orderTagsV2 as OrderLifecycleRecord[] | null;
|
||||
|
||||
if (orderTagsErrorV2) {
|
||||
console.warn('[HistoryTab] V2 order-tag query failed, falling back to legacy columns:', orderTagsErrorV2.message);
|
||||
let legacyOrderTagsQuery = supabase
|
||||
.from(tableNameOrders)
|
||||
.select(orderTagColumnsLegacy)
|
||||
.not('trade_id', 'is', null)
|
||||
.order('created_at', { ascending: false })
|
||||
.limit(5000);
|
||||
|
||||
if (profile?.role !== 'admin') {
|
||||
legacyOrderTagsQuery = legacyOrderTagsQuery.eq('user_id', user.id);
|
||||
}
|
||||
|
||||
const { data: orderTagsLegacy, error: orderTagsErrorLegacy } = await legacyOrderTagsQuery;
|
||||
if (orderTagsErrorLegacy) {
|
||||
console.error('[HistoryTab] Legacy order-tag query failed:', orderTagsErrorLegacy.message);
|
||||
}
|
||||
orderTagRows = orderTagsLegacy as OrderLifecycleRecord[] | null;
|
||||
}
|
||||
|
||||
setDbHistory(historyData || []);
|
||||
setDbLifecycleOrders(orderTagRows || []);
|
||||
setDbHistory((historyRows as TradeRecord[]) || []);
|
||||
setDbLifecycleOrders((positionsBootstrap.orders as OrderLifecycleRecord[]) || []);
|
||||
setProfiles((profData as Profile[]) || []);
|
||||
} catch (err) {
|
||||
console.error("Unexpected error:", err);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user