Big refactoring
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { fetchData, type FetchDataResult } from '../api/ingestion';
|
||||
import { useToast } from '../components/ui/Toast';
|
||||
import { summarizeIngestionResult } from '../lib/ingestionStatus';
|
||||
|
||||
interface UseFetchSymbolDataOptions {
|
||||
includeSymbolPrefix?: boolean;
|
||||
invalidatePipelineReadiness?: boolean;
|
||||
}
|
||||
|
||||
export function useFetchSymbolData(options: UseFetchSymbolDataOptions = {}) {
|
||||
const { includeSymbolPrefix = false, invalidatePipelineReadiness = false } = options;
|
||||
const queryClient = useQueryClient();
|
||||
const { addToast } = useToast();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (symbol: string) => fetchData(symbol),
|
||||
onSuccess: (result: FetchDataResult, symbol: string) => {
|
||||
const normalized = symbol.toUpperCase();
|
||||
const summary = summarizeIngestionResult(result, normalized);
|
||||
const toastMessage = includeSymbolPrefix
|
||||
? `${normalized}: ${summary.message}`
|
||||
: summary.message;
|
||||
addToast(summary.toastType, toastMessage);
|
||||
|
||||
queryClient.invalidateQueries({ queryKey: ['ohlcv', symbol] });
|
||||
queryClient.invalidateQueries({ queryKey: ['sentiment', symbol] });
|
||||
queryClient.invalidateQueries({ queryKey: ['fundamentals', symbol] });
|
||||
queryClient.invalidateQueries({ queryKey: ['sr-levels', symbol] });
|
||||
queryClient.invalidateQueries({ queryKey: ['scores', symbol] });
|
||||
|
||||
if (invalidatePipelineReadiness) {
|
||||
queryClient.invalidateQueries({ queryKey: ['admin', 'pipeline-readiness'] });
|
||||
}
|
||||
},
|
||||
onError: (err: Error, symbol: string) => {
|
||||
const normalized = symbol.toUpperCase();
|
||||
const prefix = includeSymbolPrefix ? `${normalized}: ` : '';
|
||||
addToast('error', `${prefix}${err.message || 'Failed to fetch data'}`);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user