2025-03-03 18:09:49 -08:00
|
|
|
import "./index.css";
|
2025-03-05 18:02:46 -08:00
|
|
|
import { useState, useMemo } from "react";
|
|
|
|
|
import {
|
|
|
|
|
ChartConfig,
|
|
|
|
|
ChartContainer,
|
|
|
|
|
ChartTooltip,
|
|
|
|
|
ChartTooltipContent,
|
|
|
|
|
} from "@/components/ui/chart";
|
|
|
|
|
import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts";
|
|
|
|
|
import { Price } from "../../../types";
|
|
|
|
|
import { format } from "date-fns";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
const chartConfig = {
|
|
|
|
|
price: {
|
|
|
|
|
label: "Price",
|
|
|
|
|
color: "hsl(var(--chart-1))",
|
|
|
|
|
},
|
|
|
|
|
} satisfies ChartConfig;
|
|
|
|
|
|
|
|
|
|
type DisplayRange = "1d" | "5d" | "1m";
|
|
|
|
|
|
|
|
|
|
function DisplayRangeSelector({
|
|
|
|
|
displayRange,
|
|
|
|
|
setDisplayRange,
|
|
|
|
|
}: {
|
|
|
|
|
displayRange: DisplayRange;
|
|
|
|
|
setDisplayRange: (range: DisplayRange) => void;
|
|
|
|
|
}) {
|
|
|
|
|
const sharedClass =
|
|
|
|
|
" bg-transparent text-gray-500 hover:bg-gray-50 transition-colors ease-in-out duration-200 p-2 cursor-pointer";
|
|
|
|
|
const selectedClass = `text-black bg-gray-100 hover:bg-gray-50`;
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-row items-center justify-start gap-2">
|
|
|
|
|
<Button
|
|
|
|
|
className={cn(sharedClass, displayRange === "1d" && selectedClass)}
|
|
|
|
|
variant={displayRange === "1d" ? "default" : "ghost"}
|
|
|
|
|
onClick={() => setDisplayRange("1d")}
|
|
|
|
|
>
|
|
|
|
|
1D
|
|
|
|
|
</Button>
|
|
|
|
|
<p className="text-gray-300">|</p>
|
|
|
|
|
<Button
|
|
|
|
|
className={cn(sharedClass, displayRange === "5d" && selectedClass)}
|
|
|
|
|
variant={displayRange === "5d" ? "default" : "ghost"}
|
|
|
|
|
onClick={() => setDisplayRange("5d")}
|
|
|
|
|
>
|
|
|
|
|
5D
|
|
|
|
|
</Button>
|
|
|
|
|
<p className="text-gray-300">|</p>
|
|
|
|
|
<Button
|
|
|
|
|
className={cn(sharedClass, displayRange === "1m" && selectedClass)}
|
|
|
|
|
variant={displayRange === "1m" ? "default" : "ghost"}
|
|
|
|
|
onClick={() => setDisplayRange("1m")}
|
|
|
|
|
>
|
|
|
|
|
1M
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-02-18 19:35:46 +01:00
|
|
|
|
2025-03-06 11:10:15 -08:00
|
|
|
function getPropsForDisplayRange(
|
|
|
|
|
displayRange: DisplayRange,
|
|
|
|
|
oneDayPrices: Price[],
|
|
|
|
|
thirtyDayPrices: Price[],
|
|
|
|
|
) {
|
2025-03-05 18:02:46 -08:00
|
|
|
const now = new Date();
|
2025-03-06 11:10:15 -08:00
|
|
|
const fiveDays = 5 * 24 * 60 * 60 * 1000; // 5 days in milliseconds
|
2025-03-05 18:02:46 -08:00
|
|
|
|
|
|
|
|
switch (displayRange) {
|
|
|
|
|
case "1d":
|
2025-03-06 11:10:15 -08:00
|
|
|
return oneDayPrices;
|
2025-03-05 18:02:46 -08:00
|
|
|
case "5d":
|
2025-03-06 11:10:15 -08:00
|
|
|
return thirtyDayPrices.filter(
|
|
|
|
|
(p) => new Date(p.time).getTime() >= now.getTime() - fiveDays,
|
2025-03-05 18:02:46 -08:00
|
|
|
);
|
|
|
|
|
case "1m":
|
2025-03-06 11:10:15 -08:00
|
|
|
return thirtyDayPrices;
|
|
|
|
|
default:
|
|
|
|
|
return [];
|
2025-03-05 18:02:46 -08:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-27 14:08:24 -08:00
|
|
|
export default function StockPrice(props: {
|
2025-03-05 18:02:46 -08:00
|
|
|
ticker: string;
|
|
|
|
|
oneDayPrices: Price[];
|
|
|
|
|
thirtyDayPrices: Price[];
|
2025-02-27 14:08:24 -08:00
|
|
|
}) {
|
2025-03-05 18:02:46 -08:00
|
|
|
const { ticker } = props;
|
2025-03-06 11:10:15 -08:00
|
|
|
const { oneDayPrices, thirtyDayPrices } = props;
|
2025-03-05 18:02:46 -08:00
|
|
|
const [displayRange, setDisplayRange] = useState<DisplayRange>("1d");
|
2025-03-05 16:35:39 +01:00
|
|
|
|
2025-03-05 18:02:46 -08:00
|
|
|
const {
|
|
|
|
|
currentPrice,
|
|
|
|
|
openPrice,
|
|
|
|
|
dollarChange,
|
|
|
|
|
percentChange,
|
|
|
|
|
highPrice,
|
|
|
|
|
lowPrice,
|
|
|
|
|
chartData,
|
|
|
|
|
change,
|
|
|
|
|
} = useMemo(() => {
|
2025-03-06 11:10:15 -08:00
|
|
|
const prices = getPropsForDisplayRange(
|
|
|
|
|
displayRange,
|
|
|
|
|
oneDayPrices,
|
|
|
|
|
thirtyDayPrices,
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-05 18:02:46 -08:00
|
|
|
const firstPrice = prices[0];
|
|
|
|
|
const lastPrice = prices[prices.length - 1];
|
|
|
|
|
|
|
|
|
|
const currentPrice = lastPrice?.close;
|
|
|
|
|
const openPrice = firstPrice?.open;
|
|
|
|
|
const dollarChange = currentPrice - openPrice;
|
|
|
|
|
const percentChange = ((currentPrice - openPrice) / openPrice) * 100;
|
|
|
|
|
|
|
|
|
|
const highPrice = prices.reduce(
|
|
|
|
|
(acc, p) => Math.max(acc, p.high),
|
|
|
|
|
-Infinity,
|
|
|
|
|
);
|
|
|
|
|
const lowPrice = prices.reduce((acc, p) => Math.min(acc, p.low), Infinity);
|
|
|
|
|
|
|
|
|
|
const chartData = prices.map((p) => ({
|
|
|
|
|
time: p.time,
|
|
|
|
|
price: p.close,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const change: "up" | "down" = dollarChange > 0 ? "up" : "down";
|
2025-03-03 17:53:00 -08:00
|
|
|
return {
|
2025-03-05 18:02:46 -08:00
|
|
|
currentPrice,
|
|
|
|
|
openPrice,
|
|
|
|
|
dollarChange,
|
|
|
|
|
percentChange,
|
|
|
|
|
highPrice,
|
|
|
|
|
lowPrice,
|
|
|
|
|
chartData,
|
|
|
|
|
change,
|
2025-03-03 17:53:00 -08:00
|
|
|
};
|
2025-03-06 11:10:15 -08:00
|
|
|
}, [oneDayPrices, thirtyDayPrices, displayRange]);
|
2025-03-03 17:53:00 -08:00
|
|
|
|
|
|
|
|
return (
|
2025-03-05 18:02:46 -08:00
|
|
|
<div className="w-full max-w-4xl rounded-xl shadow-md overflow-hidden border border-gray-200 flex flex-col gap-4 p-3">
|
|
|
|
|
<div className="flex items-center justify-start gap-4 mb-2 text-lg font-medium text-gray-700">
|
|
|
|
|
<p>{ticker}</p>
|
|
|
|
|
<p>${currentPrice}</p>
|
2025-03-03 17:53:00 -08:00
|
|
|
</div>
|
2025-03-05 18:02:46 -08:00
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
|
<p className={change === "up" ? "text-green-500" : "text-red-500"}>
|
|
|
|
|
${dollarChange.toFixed(2)} (${percentChange.toFixed(2)}%)
|
|
|
|
|
</p>
|
2025-03-03 17:53:00 -08:00
|
|
|
</div>
|
2025-03-05 18:02:46 -08:00
|
|
|
<div className="grid grid-cols-2 gap-4 text-sm">
|
|
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
|
<p>Open</p>
|
|
|
|
|
<p>High</p>
|
|
|
|
|
<p>Low</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
|
<p>${openPrice}</p>
|
|
|
|
|
<p>${highPrice}</p>
|
|
|
|
|
<p>${lowPrice}</p>
|
|
|
|
|
</div>
|
2025-03-03 17:53:00 -08:00
|
|
|
</div>
|
2025-03-05 18:02:46 -08:00
|
|
|
<DisplayRangeSelector
|
|
|
|
|
displayRange={displayRange}
|
|
|
|
|
setDisplayRange={setDisplayRange}
|
|
|
|
|
/>
|
|
|
|
|
<ChartContainer config={chartConfig}>
|
|
|
|
|
<LineChart
|
|
|
|
|
accessibilityLayer
|
|
|
|
|
data={chartData}
|
|
|
|
|
margin={{
|
|
|
|
|
left: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<CartesianGrid vertical={false} />
|
|
|
|
|
<XAxis
|
|
|
|
|
dataKey="time"
|
|
|
|
|
tickLine={false}
|
|
|
|
|
axisLine={false}
|
|
|
|
|
tickMargin={8}
|
|
|
|
|
tickFormatter={(value) => format(value, "h:mm a")}
|
|
|
|
|
/>
|
|
|
|
|
<YAxis
|
|
|
|
|
domain={[lowPrice - 2, highPrice + 2]}
|
|
|
|
|
tickLine={false}
|
|
|
|
|
axisLine={false}
|
|
|
|
|
tickMargin={8}
|
|
|
|
|
tickFormatter={(value) => `${value.toFixed(2)}`}
|
|
|
|
|
/>
|
|
|
|
|
<ChartTooltip
|
|
|
|
|
cursor={false}
|
|
|
|
|
content={
|
|
|
|
|
<ChartTooltipContent
|
|
|
|
|
hideLabel={false}
|
|
|
|
|
labelFormatter={(value) => format(value, "h:mm a")}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<Line dataKey="price" type="natural" strokeWidth={2} dot={false} />
|
|
|
|
|
</LineChart>
|
|
|
|
|
</ChartContainer>
|
2025-02-18 19:35:46 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|