cleaned up web-app

This commit is contained in:
Boki 2025-07-04 22:25:23 -04:00
parent 805ce0ebf1
commit 3843dc95a3
21 changed files with 105 additions and 2420 deletions

View file

@ -1 +0,0 @@
export { useBacktest } from './useBacktest';

View file

@ -1,231 +0,0 @@
import { useCallback, useEffect, useRef, useState } from 'react';
import type { BacktestJob, BacktestRequest, BacktestResult } from '../services/backtestApi';
import { backtestApi, } from '../services/backtestApi';
interface UseBacktestReturn {
// State
backtest: BacktestJob | null;
results: BacktestResult | null;
isLoading: boolean;
isPolling: boolean;
error: string | null;
// Actions
loadBacktest: (id: string) => Promise<void>;
createBacktest: (request: BacktestRequest) => Promise<void>;
updateBacktest: (id: string, request: BacktestRequest) => Promise<void>;
cancelBacktest: () => Promise<void>;
reset: () => void;
}
export function useBacktest(): UseBacktestReturn {
const [backtest, setBacktest] = useState<BacktestJob | null>(null);
const [results, setResults] = useState<BacktestResult | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [isPolling, setIsPolling] = useState(false);
const [error, setError] = useState<string | null>(null);
const pollingIntervalRef = useRef<NodeJS.Timeout | null>(null);
// Poll for status updates
const pollStatus = useCallback(async (backtestId: string) => {
try {
const updatedBacktest = await backtestApi.getBacktest(backtestId);
setBacktest(updatedBacktest);
if (updatedBacktest.status === 'completed') {
// Fetch results
const backtestResults = await backtestApi.getBacktestResults(backtestId);
setResults(backtestResults);
setIsPolling(false);
// Clear polling interval
if (pollingIntervalRef.current) {
clearInterval(pollingIntervalRef.current);
pollingIntervalRef.current = null;
}
} else if (updatedBacktest.status === 'failed' || updatedBacktest.status === 'cancelled') {
setIsPolling(false);
// Clear polling interval
if (pollingIntervalRef.current) {
clearInterval(pollingIntervalRef.current);
pollingIntervalRef.current = null;
}
if (updatedBacktest.status === 'failed' && updatedBacktest.error) {
setError(updatedBacktest.error);
}
}
} catch (err) {
console.error('Error polling backtest status:', err);
// Don't stop polling on transient errors
}
}, []);
// Load a specific backtest by ID
const loadBacktest = useCallback(async (id: string) => {
setIsLoading(true);
setError(null);
try {
const loadedBacktest = await backtestApi.getBacktest(id);
setBacktest(loadedBacktest);
// If completed, also load results
if (loadedBacktest.status === 'completed') {
const backtestResults = await backtestApi.getBacktestResults(id);
setResults(backtestResults);
}
// If running, start polling
if (loadedBacktest.status === 'running') {
setIsPolling(true);
pollingIntervalRef.current = setInterval(() => {
pollStatus(id);
}, 2000);
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load backtest');
} finally {
setIsLoading(false);
}
}, [pollStatus]);
// Create a new backtest
const createBacktest = useCallback(async (request: BacktestRequest) => {
setIsLoading(true);
setError(null);
setResults(null);
try {
const newBacktest = await backtestApi.createBacktest(request);
setBacktest(newBacktest);
// Start polling for updates
setIsPolling(true);
pollingIntervalRef.current = setInterval(() => {
pollStatus(newBacktest.id);
}, 2000); // Poll every 2 seconds
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create backtest');
} finally {
setIsLoading(false);
}
}, [pollStatus]);
// Update an existing backtest
const updateBacktest = useCallback(async (id: string, request: BacktestRequest) => {
setIsLoading(true);
setError(null);
try {
// For now, we'll delete and recreate since update isn't implemented in the API
await backtestApi.deleteBacktest(id);
const newBacktest = await backtestApi.createBacktest(request);
setBacktest(newBacktest);
// Start polling for updates
setIsPolling(true);
pollingIntervalRef.current = setInterval(() => {
pollStatus(newBacktest.id);
}, 2000);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to update backtest');
} finally {
setIsLoading(false);
}
}, [pollStatus]);
// Cancel running backtest
const cancelBacktest = useCallback(async () => {
if (!backtest || backtest.status !== 'running') return;
try {
await backtestApi.cancelBacktest(backtest.id);
setBacktest({ ...backtest, status: 'cancelled' });
setIsPolling(false);
// Clear polling interval
if (pollingIntervalRef.current) {
clearInterval(pollingIntervalRef.current);
pollingIntervalRef.current = null;
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to cancel backtest');
}
}, [backtest]);
// Reset state
const reset = useCallback(() => {
setBacktest(null);
setResults(null);
setError(null);
setIsLoading(false);
setIsPolling(false);
// Clear polling interval
if (pollingIntervalRef.current) {
clearInterval(pollingIntervalRef.current);
pollingIntervalRef.current = null;
}
}, []);
// Cleanup on unmount
useEffect(() => {
return () => {
if (pollingIntervalRef.current) {
clearInterval(pollingIntervalRef.current);
}
};
}, []);
return {
backtest,
results,
isLoading,
isPolling,
error,
loadBacktest,
createBacktest,
updateBacktest,
cancelBacktest,
reset,
};
}
// Separate hook for listing backtests
interface UseBacktestListReturn {
backtests: BacktestJob[];
isLoading: boolean;
error: string | null;
loadBacktests: (limit?: number, offset?: number) => Promise<void>;
}
export function useBacktestList(): UseBacktestListReturn {
const [backtests, setBacktests] = useState<BacktestJob[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const loadBacktests = useCallback(async (limit = 50, offset = 0) => {
setIsLoading(true);
setError(null);
try {
const list = await backtestApi.listBacktests(limit, offset);
setBacktests(list);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load backtests');
} finally {
setIsLoading(false);
}
}, []);
return {
backtests,
isLoading,
error,
loadBacktests,
};
}