|
| 1 | +import { useMemo, useState } from "react"; |
| 2 | +import { useGetStockHistory } from "@/api/stock"; |
| 3 | +import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 4 | +import CandlestickChart from "@/components/valuecell/charts/candlestick-chart"; |
| 5 | +import { TimeUtils } from "@/lib/time"; |
| 6 | +import { cn } from "@/lib/utils"; |
| 7 | +import type { StockInterval } from "@/types/stock"; |
| 8 | + |
| 9 | +interface StockHistoryChartProps { |
| 10 | + ticker: string; |
| 11 | + className?: string; |
| 12 | +} |
| 13 | + |
| 14 | +const INTERVALS: { label: string; value: StockInterval }[] = [ |
| 15 | + { label: "1H", value: "1h" }, |
| 16 | + { label: "1D", value: "1d" }, |
| 17 | + { label: "1W", value: "1w" }, |
| 18 | +]; |
| 19 | + |
| 20 | +export const StockHistoryChart = ({ |
| 21 | + ticker, |
| 22 | + className, |
| 23 | +}: StockHistoryChartProps) => { |
| 24 | + const [interval, setInterval] = useState<StockInterval>("1d"); |
| 25 | + |
| 26 | + // Calculate date range based on interval |
| 27 | + const { startDate, endDate } = useMemo(() => { |
| 28 | + const now = TimeUtils.now(); |
| 29 | + |
| 30 | + let start = now; |
| 31 | + switch (interval) { |
| 32 | + case "1h": |
| 33 | + start = now.subtract(6, "month"); |
| 34 | + break; |
| 35 | + case "1d": |
| 36 | + start = now.subtract(3, "year"); |
| 37 | + break; |
| 38 | + case "1w": |
| 39 | + start = now.subtract(10, "year"); |
| 40 | + break; |
| 41 | + default: |
| 42 | + start = now.subtract(3, "year"); |
| 43 | + } |
| 44 | + |
| 45 | + return { |
| 46 | + startDate: start.format("YYYY-MM-DD"), |
| 47 | + endDate: now.format("YYYY-MM-DD"), |
| 48 | + }; |
| 49 | + }, [interval]); |
| 50 | + |
| 51 | + const { data: historyData, isLoading } = useGetStockHistory({ |
| 52 | + ticker, |
| 53 | + interval, |
| 54 | + start_date: startDate, |
| 55 | + end_date: endDate, |
| 56 | + }); |
| 57 | + |
| 58 | + // Transform data to CandlestickChart format |
| 59 | + const chartData = useMemo(() => { |
| 60 | + if (!historyData) return []; |
| 61 | + return historyData.map((item) => ({ |
| 62 | + time: item.time, |
| 63 | + open: item.open, |
| 64 | + high: item.high, |
| 65 | + low: item.low, |
| 66 | + close: item.close, |
| 67 | + volume: item.volume, |
| 68 | + })); |
| 69 | + }, [historyData]); |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className={cn("flex flex-col gap-4", className)}> |
| 73 | + <Tabs |
| 74 | + value={interval} |
| 75 | + onValueChange={(value) => setInterval(value as StockInterval)} |
| 76 | + > |
| 77 | + <TabsList> |
| 78 | + {INTERVALS.map((item) => ( |
| 79 | + <TabsTrigger key={item.value} value={item.value}> |
| 80 | + {item.label} |
| 81 | + </TabsTrigger> |
| 82 | + ))} |
| 83 | + </TabsList> |
| 84 | + </Tabs> |
| 85 | + <CandlestickChart |
| 86 | + data={chartData} |
| 87 | + height={500} |
| 88 | + loading={isLoading} |
| 89 | + showVolume |
| 90 | + /> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +}; |
0 commit comments