|
| 1 | +import { useQuery } from "@tanstack/react-query"; |
| 2 | +import { reservoirsApi } from "@/services/apis/reservoirs.api"; |
| 3 | +import { ReservoirOperationRead, ReservoirRead } from "@/types/reservoirs"; |
| 4 | + |
| 5 | +export function useReservoirs() { |
| 6 | + return useQuery({ |
| 7 | + queryKey: ["reservoirs"], |
| 8 | + queryFn: async () => { |
| 9 | + const response = await reservoirsApi.reservoirs.list({ limit: 1000 }); |
| 10 | + return response.data; |
| 11 | + }, |
| 12 | + staleTime: 5 * 60 * 1000, // 5 minutes |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +export function useReservoirOperations( |
| 17 | + reservoirId: number | null, |
| 18 | + startDate?: Date | null, |
| 19 | + endDate?: Date | null, |
| 20 | +) { |
| 21 | + return useQuery({ |
| 22 | + queryKey: ["reservoir-operations", reservoirId, startDate, endDate], |
| 23 | + queryFn: async () => { |
| 24 | + if (!reservoirId || !startDate || !endDate) |
| 25 | + return []; |
| 26 | + |
| 27 | + const response = await reservoirsApi.operations.list({ |
| 28 | + reservoir_id: reservoirId, |
| 29 | + start_date: startDate.toISOString().split("T")[0], |
| 30 | + end_date: endDate.toISOString().split("T")[0], |
| 31 | + }); |
| 32 | + |
| 33 | + return response.data; |
| 34 | + }, |
| 35 | + enabled: !!reservoirId && !!startDate && !!endDate, |
| 36 | + staleTime: 2 * 60 * 1000, // 2 minutes |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +export function useAllReservoirOperations( |
| 41 | + selectedDate: Date | null, |
| 42 | + reservoirs: ReservoirRead[], |
| 43 | +) { |
| 44 | + return useQuery({ |
| 45 | + queryKey: ["reservoir-operations", "all", selectedDate], |
| 46 | + queryFn: async () => { |
| 47 | + if (!selectedDate || reservoirs.length === 0) { |
| 48 | + return { |
| 49 | + operationsMap: new Map<number, ReservoirOperationRead[]>(), |
| 50 | + sliderMarks: {}, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + const targetDate = new Date(selectedDate); |
| 55 | + const startTime = new Date(targetDate); |
| 56 | + const endTime = new Date(targetDate); |
| 57 | + endTime.setDate(endTime.getDate() + 1); |
| 58 | + |
| 59 | + const operationsMap = new Map<number, ReservoirOperationRead[]>(); |
| 60 | + const marks = {} as Record<number, string>; |
| 61 | + |
| 62 | + // Fetch operations data for all reservoirs |
| 63 | + const response = await reservoirsApi.operations.list({ |
| 64 | + start_date: startTime.toISOString().split("T")[0], |
| 65 | + end_date: endTime.toISOString().split("T")[0], |
| 66 | + limit: 10000, |
| 67 | + }); |
| 68 | + |
| 69 | + // Group operations by reservoir |
| 70 | + for (const operation of response.data) { |
| 71 | + if (!operationsMap.has(operation.reservoir_id)) { |
| 72 | + operationsMap.set(operation.reservoir_id, []); |
| 73 | + } |
| 74 | + operationsMap.get(operation.reservoir_id)!.push(operation); |
| 75 | + |
| 76 | + // Build slider marks from timestamps |
| 77 | + const hour = new Date(operation.timestamp).getHours(); |
| 78 | + marks[hour] = `${hour}:00`; |
| 79 | + } |
| 80 | + |
| 81 | + return { operationsMap, sliderMarks: marks }; |
| 82 | + }, |
| 83 | + enabled: !!selectedDate && reservoirs.length > 0, |
| 84 | + staleTime: 2 * 60 * 1000, // 2 minutes |
| 85 | + }); |
| 86 | +} |
0 commit comments