|
1 |
| -export const useDataAvailabilityStore = defineStore('dataAvailability', () => { |
2 |
| - const manifest = ref<DataAvailabilityManifestItem[]>([]); |
3 |
| - const status = ref<LoadingStatus>('idle'); |
4 |
| - const toast = useToast(); |
5 |
| - |
6 |
| - const isLoading = computed(() => status.value === 'loading'); |
7 |
| - |
8 |
| - async function fetchManifest() { |
9 |
| - if (manifest.value.length > 0) return; |
10 |
| - |
11 |
| - status.value = 'loading'; |
12 |
| - try { |
13 |
| - manifest.value = await $fetch<DataAvailabilityManifestItem[]>('/api/data-availability'); |
14 |
| - status.value = 'success'; |
15 |
| - } catch (e: any) { |
16 |
| - toast.add({ |
17 |
| - title: 'Error Loading Market Data', |
18 |
| - description: e.data?.error || 'Could not connect to the API to get available market data.', |
19 |
| - color: 'error', |
20 |
| - icon: 'i-heroicons-exclamation-triangle', |
21 |
| - }); |
22 |
| - console.error('Failed to fetch data availability manifest:', e); |
23 |
| - status.value = 'error'; |
24 |
| - } |
25 |
| - } |
26 |
| - |
27 |
| - const availableSymbols = computed(() => manifest.value.map(item => item.symbol).sort()); |
28 |
| - |
29 |
| - const getTimeframesForSymbol = (symbol: string | null): TimeframeData[] => { |
30 |
| - if (!symbol) return []; |
31 |
| - const symbolData = manifest.value.find(item => item.symbol === symbol); |
32 |
| - return symbolData?.timeframes ?? []; |
33 |
| - }; |
34 |
| - |
35 |
| - const getDateRangeFor = (symbol: string | null, timeframe: string | null): { min?: string, max?: string } => { |
36 |
| - if (!symbol || !timeframe) return {}; |
37 |
| - |
38 |
| - const symbolTimeframes = getTimeframesForSymbol(symbol); |
39 |
| - const timeframeData = symbolTimeframes.find(tf => tf.timeframe === timeframe); |
40 |
| - |
41 |
| - if (!timeframeData) return {}; |
42 |
| - |
43 |
| - return { |
44 |
| - min: timeframeData.startDate.split('T')[0], |
45 |
| - max: timeframeData.endDate.split('T')[0], |
46 |
| - }; |
47 |
| - }; |
48 |
| - |
49 |
| - |
50 |
| - return { |
51 |
| - manifest, |
52 |
| - status, |
53 |
| - isLoading, |
54 |
| - fetchManifest, |
55 |
| - availableSymbols, |
56 |
| - getTimeframesForSymbol, |
57 |
| - getDateRangeFor, |
58 |
| - }; |
59 |
| -}); |
| 1 | +export const useDataAvailabilityStore = defineStore('dataAvailability', () => { |
| 2 | + const manifest = ref<DataAvailabilityManifestItem[]>([]); |
| 3 | + const status = ref<LoadingStatus>('idle'); |
| 4 | + const toast = useToast(); |
| 5 | + |
| 6 | + const isLoading = computed(() => status.value === 'loading'); |
| 7 | + |
| 8 | + async function fetchManifest(force = false) { |
| 9 | + if (manifest.value.length > 0 && !force) return; |
| 10 | + |
| 11 | + status.value = 'loading'; |
| 12 | + try { |
| 13 | + manifest.value = await $fetch<DataAvailabilityManifestItem[]>('/api/data-availability'); |
| 14 | + status.value = 'success'; |
| 15 | + } catch (e: any) { |
| 16 | + toast.add({ |
| 17 | + title: 'Error Loading Market Data', |
| 18 | + description: e.data?.error || 'Could not connect to the API to get available market data.', |
| 19 | + color: 'error', |
| 20 | + icon: 'i-heroicons-exclamation-triangle', |
| 21 | + }); |
| 22 | + console.error('Failed to fetch data availability manifest:', e); |
| 23 | + status.value = 'error'; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + const availableSymbols = computed(() => manifest.value.map(item => item.symbol).sort()); |
| 28 | + |
| 29 | + const getTimeframesForSymbol = (symbol: string | null): TimeframeData[] => { |
| 30 | + if (!symbol) return []; |
| 31 | + const symbolData = manifest.value.find(item => item.symbol === symbol); |
| 32 | + return symbolData?.timeframes ?? []; |
| 33 | + }; |
| 34 | + |
| 35 | + const getDateRangeFor = (symbol: string | null, timeframe: string | null): { min?: string, max?: string } => { |
| 36 | + if (!symbol || !timeframe) return {}; |
| 37 | + |
| 38 | + const symbolTimeframes = getTimeframesForSymbol(symbol); |
| 39 | + const timeframeData = symbolTimeframes.find(tf => tf.timeframe === timeframe); |
| 40 | + |
| 41 | + if (!timeframeData) return {}; |
| 42 | + |
| 43 | + return { |
| 44 | + min: timeframeData.startDate.split('T')[0], |
| 45 | + max: timeframeData.endDate.split('T')[0], |
| 46 | + }; |
| 47 | + }; |
| 48 | + |
| 49 | + |
| 50 | + return { |
| 51 | + manifest, |
| 52 | + status, |
| 53 | + isLoading, |
| 54 | + fetchManifest, |
| 55 | + availableSymbols, |
| 56 | + getTimeframesForSymbol, |
| 57 | + getDateRangeFor, |
| 58 | + }; |
| 59 | +}); |
0 commit comments