import { useParams, useRouter } from "@tanstack/react-router"; import { LabelMedium } from "baseui/typography"; import dayjs from "dayjs"; import numeral from "numeral"; import { useEffect, useState } from "react"; import { Area, AreaChart, Tooltip, TooltipProps, XAxis } from "recharts"; import useChart from "../../hooks/useChart"; const CustomTooltip = ({ active, payload, label, }: TooltipProps) => { if (active && payload && payload.length) { return (
{dayjs(label).format("dddd DD MMMM YYYY")}: {" "} {numeral(payload[0].value).format("0,0")}
); } return null; }; const formatXAxis = (tickItem: string) => dayjs(tickItem).format("MMM D"); function ScrobblesAreaChart() { const { getScrobblesChart, getAlbumChart, getArtistChart, getSongChart, getProfileChart, } = useChart(); const { state: { location: { pathname }, }, } = useRouter(); const { did, rkey } = useParams({ strict: false }); const [data, setData] = useState< { date: string; count: number; }[] >([]); useEffect(() => { const fetchScrobblesChart = async () => { if (pathname === "/") { return; } if (pathname.startsWith("/profile")) { const charts = await getProfileChart(did!); setData(charts); return; } if (pathname.includes("/artist/")) { const charts = await getArtistChart( `at://${did}/app.rocksky.artist/${rkey}`, ); setData(charts); return; } if (pathname.includes("/album/")) { const charts = await getAlbumChart( `at://${did}/app.rocksky.album/${rkey}`, ); setData(charts); return; } if (pathname.includes("/song/")) { const charts = await getSongChart( `at://${did}/app.rocksky.song/${rkey}`, ); setData(charts); return; } if (pathname.includes("/scrobble/")) { const charts = await getSongChart( `at://${did}/app.rocksky.scrobble/${rkey}`, ); setData(charts); return; } }; fetchScrobblesChart(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [pathname]); return ( <> {!pathname.includes("/playlist/") && ( <> Scrobble Stats } labelFormatter={(label) => dayjs(label).format("YYYY-MM-DD")} /> )} ); } export default ScrobblesAreaChart;