-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuseUsdcGainChartData.ts
53 lines (51 loc) · 1.3 KB
/
useUsdcGainChartData.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { useQuery } from "@tanstack/react-query"
import {
getUsdcGainChartData,
UsdcGainChartData,
} from "data/actions/common/getUsdcGainChartData"
import { useEffect } from "react"
import { useMarketChart } from "./useMarketChart"
export const useUsdcGainChartData = ({
day,
interval,
firstDate,
enabled,
onSuccess,
}: {
day: number
interval?: "hourly" | "daily"
firstDate?: Date
enabled?: boolean
onSuccess?: (data: UsdcGainChartData) => void
}) => {
const usdcMarketChart = useMarketChart({
asset: "usd-coin",
day,
interval,
enabled,
})
const query = useQuery({
queryKey: ["USE_USDC_GAIN_CHART_DATA", usdcMarketChart.data, day, interval],
queryFn: async () => {
if (!day) throw new Error("day is undefined")
if (!usdcMarketChart.data) {
throw new Error("market chart data is undefined")
}
return await getUsdcGainChartData({
day,
interval: interval ?? "daily",
firstDate,
usdcData: usdcMarketChart.data,
})
},
enabled: Boolean(day) && !!usdcMarketChart.data && enabled,
}
)
useEffect(() => {
if (enabled && query.data) {
onSuccess && onSuccess(query.data)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [Boolean(query.isFetched), enabled])
return query
}