2020-05-31 23:26:06 +02:00
|
|
|
import React, { useMemo, useState } from "react";
|
2020-11-07 19:14:41 +01:00
|
|
|
import { TimeOptions } from "../api/entities/time-options";
|
2020-05-31 23:26:06 +02:00
|
|
|
import { TimePreset } from "../api/entities/time-preset.enum";
|
2020-09-05 23:35:53 +02:00
|
|
|
import { useTopArtists } from "../hooks/use-api";
|
2022-06-25 17:17:06 +02:00
|
|
|
import { useAuthProtection } from "../hooks/use-auth-protection";
|
2021-05-22 17:32:20 +02:00
|
|
|
import { getMaxCount } from "../util/getMaxCount";
|
2020-07-12 17:16:33 +02:00
|
|
|
import { ReportTimeOptions } from "./ReportTimeOptions";
|
2022-06-25 18:04:25 +02:00
|
|
|
import { Spinner } from "./Spinner";
|
2020-11-15 02:33:39 +01:00
|
|
|
import { TopListItem } from "./TopListItem";
|
2020-11-07 19:14:41 +01:00
|
|
|
|
2020-05-31 23:26:06 +02:00
|
|
|
export const ReportTopArtists: React.FC = () => {
|
2022-06-25 17:17:06 +02:00
|
|
|
const { requireUser } = useAuthProtection();
|
2020-05-31 23:26:06 +02:00
|
|
|
|
2020-07-12 17:16:33 +02:00
|
|
|
const [timeOptions, setTimeOptions] = useState<TimeOptions>({
|
2020-05-31 23:26:06 +02:00
|
|
|
timePreset: TimePreset.LAST_90_DAYS,
|
2022-06-25 17:40:53 +02:00
|
|
|
customTimeStart: new Date("2020"),
|
2020-05-31 23:26:06 +02:00
|
|
|
customTimeEnd: new Date(),
|
|
|
|
|
});
|
|
|
|
|
|
2020-09-05 23:35:53 +02:00
|
|
|
const options = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
time: timeOptions,
|
|
|
|
|
}),
|
2023-09-16 13:02:19 +02:00
|
|
|
[timeOptions],
|
2020-11-07 19:14:41 +01:00
|
|
|
);
|
2020-05-31 23:26:06 +02:00
|
|
|
|
2020-09-05 23:35:53 +02:00
|
|
|
const { topArtists, isLoading } = useTopArtists(options);
|
|
|
|
|
|
2022-06-25 17:40:20 +02:00
|
|
|
const reportHasItems = topArtists.length !== 0;
|
2021-05-22 17:32:20 +02:00
|
|
|
const maxCount = getMaxCount(topArtists);
|
2020-05-31 23:26:06 +02:00
|
|
|
|
2022-06-25 17:17:06 +02:00
|
|
|
requireUser();
|
2020-05-31 23:26:06 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="md:flex md:justify-center p-4">
|
2022-06-25 14:23:39 +02:00
|
|
|
<div className="md:shrink-0 min-w-full xl:min-w-0 xl:w-2/3 max-w-screen-lg">
|
2020-05-31 23:26:06 +02:00
|
|
|
<div className="flex justify-between">
|
2022-07-24 17:45:29 +02:00
|
|
|
<p className="text-2xl font-normal text-gray-700 dark:text-gray-400">
|
|
|
|
|
Top Artists
|
|
|
|
|
</p>
|
2020-05-31 23:26:06 +02:00
|
|
|
</div>
|
2022-07-24 17:45:29 +02:00
|
|
|
<div className="shadow-xl bg-gray-100 dark:bg-gray-800 rounded p-5 m-2">
|
2020-07-12 17:16:33 +02:00
|
|
|
<ReportTimeOptions
|
|
|
|
|
timeOptions={timeOptions}
|
|
|
|
|
setTimeOptions={setTimeOptions}
|
2020-05-31 23:26:06 +02:00
|
|
|
/>
|
2022-06-25 18:04:25 +02:00
|
|
|
{isLoading && <Spinner className="m-8" />}
|
|
|
|
|
|
2022-06-25 17:40:20 +02:00
|
|
|
{!reportHasItems && !isLoading && (
|
2020-05-31 23:26:06 +02:00
|
|
|
<div>
|
|
|
|
|
<p>Report is emtpy! :(</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{reportHasItems &&
|
2020-09-05 23:35:53 +02:00
|
|
|
topArtists.map(({ artist, count }) => (
|
2021-05-22 17:32:20 +02:00
|
|
|
<TopListItem
|
|
|
|
|
key={artist.id}
|
|
|
|
|
title={artist.name}
|
|
|
|
|
count={count}
|
|
|
|
|
maxCount={maxCount}
|
|
|
|
|
/>
|
2020-05-31 23:26:06 +02:00
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|