import React, { useMemo, useState } from "react"; import { Artist } from "../api/entities/artist"; import { Genre } from "../api/entities/genre"; import { TimeOptions } from "../api/entities/time-options"; import { TimePreset } from "../api/entities/time-preset.enum"; import { TopArtistsItem } from "../api/entities/top-artists-item"; import { useTopGenres } from "../hooks/use-api"; import { useAuthProtection } from "../hooks/use-auth-protection"; import { capitalizeString } from "../util/capitalizeString"; import { getMaxCount } from "../util/getMaxCount"; import { ReportTimeOptions } from "./ReportTimeOptions"; import { Spinner } from "./Spinner"; import { TopListItem } from "./TopListItem"; export const ReportTopGenres: React.FC = () => { const { requireUser } = useAuthProtection(); const [timeOptions, setTimeOptions] = useState({ timePreset: TimePreset.LAST_90_DAYS, customTimeStart: new Date("2020"), customTimeEnd: new Date(), }); const options = useMemo( () => ({ time: timeOptions, }), [timeOptions], ); const { topGenres, isLoading } = useTopGenres(options); const reportHasItems = topGenres.length !== 0; requireUser(); const maxCount = getMaxCount(topGenres); return (

Top Genres

{isLoading && } {!reportHasItems && !isLoading && (

Report is emtpy! :(

)} {reportHasItems && topGenres.map(({ genre, artists, count }) => ( ))}
); }; const ReportItem: React.FC<{ genre: Genre; artists: TopArtistsItem[]; count: number; maxCount: number; }> = ({ genre, artists, count, maxCount }) => { const artistList = artists .map(({ artist, count: artistCount }) => ( )) // @ts-expect-error .reduce((acc, curr) => (acc === null ? [curr] : [acc, ", ", curr]), null); return ( ); }; const ArtistItem: React.FC<{ artist: Artist; count: number; }> = ({ artist, count }) => ( {artist.name} );