feat: top genres report

This commit is contained in:
Julian Tölle 2021-10-26 20:09:52 +02:00
parent 62119d44b0
commit a0c28e2324
21 changed files with 317 additions and 104 deletions

View file

@ -8,6 +8,7 @@ import { RecentListens } from "./components/RecentListens";
import { ReportListens } from "./components/ReportListens";
import { ReportTopAlbums } from "./components/ReportTopAlbums";
import { ReportTopArtists } from "./components/ReportTopArtists";
import { ReportTopGenres } from "./components/ReportTopGenres";
import { ReportTopTracks } from "./components/ReportTopTracks";
import { useAuth } from "./hooks/use-auth";
import "./tailwind/generated.css";
@ -38,6 +39,7 @@ export function App() {
/>
<Route path="/reports/top-albums" exact component={ReportTopAlbums} />
<Route path="/reports/top-tracks" exact component={ReportTopTracks} />
<Route path="/reports/top-genres" exact component={ReportTopGenres} />
</Switch>
</main>
<footer>

View file

@ -9,6 +9,8 @@ import { TopAlbumsItem } from "./entities/top-albums-item";
import { TopAlbumsOptions } from "./entities/top-albums-options";
import { TopArtistsItem } from "./entities/top-artists-item";
import { TopArtistsOptions } from "./entities/top-artists-options";
import { TopGenresItem } from "./entities/top-genres-item";
import { TopGenresOptions } from "./entities/top-genres-options";
import { TopTracksItem } from "./entities/top-tracks-item";
import { TopTracksOptions } from "./entities/top-tracks-options";
@ -188,3 +190,40 @@ export const getTopTracks = async (
} = res;
return items;
};
export const getTopGenres = async (
options: TopGenresOptions,
client: AxiosInstance
): Promise<TopGenresItem[]> => {
const {
time: { timePreset, customTimeStart, customTimeEnd },
} = options;
const res = await client.get<{ items: TopGenresItem[] }>(
`/api/v1/reports/top-Genres`,
{
params: {
timePreset,
customTimeStart: formatISO(customTimeStart),
customTimeEnd: formatISO(customTimeEnd),
},
}
);
switch (res.status) {
case 200: {
break;
}
case 401: {
throw new UnauthenticatedError(`No token or token expired`);
}
default: {
throw new Error(`Unable to getTopGenres: ${res.status}`);
}
}
const {
data: { items },
} = res;
return items;
};

View file

@ -0,0 +1,4 @@
export interface Genre {
id: string;
name: string;
}

View file

@ -0,0 +1,8 @@
import { Genre } from "./genre";
import { TopArtistsItem } from "./top-artists-item";
export interface TopGenresItem {
genre: Genre;
artists: TopArtistsItem[];
count: number;
}

View file

@ -0,0 +1,5 @@
import { TimeOptions } from "./time-options";
export interface TopGenresOptions {
time: TimeOptions;
}

View file

@ -34,6 +34,9 @@ export const NavBar: React.FC = () => {
<Link to="/reports/top-tracks">
<NavItem>Top Tracks</NavItem>
</Link>
<Link to="/reports/top-genres">
<NavItem>Top Genres</NavItem>
</Link>
</>
)}
</div>

View file

@ -0,0 +1,106 @@
import React, { useMemo, useState } from "react";
import { Redirect } from "react-router-dom";
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 { useAuth } from "../hooks/use-auth";
import { capitalizeString } from "../util/capitalizeString";
import { getMaxCount } from "../util/getMaxCount";
import { ReportTimeOptions } from "./ReportTimeOptions";
import { TopListItem } from "./TopListItem";
export const ReportTopGenres: React.FC = () => {
const { user } = useAuth();
const [timeOptions, setTimeOptions] = useState<TimeOptions>({
timePreset: TimePreset.LAST_90_DAYS,
customTimeStart: new Date(0),
customTimeEnd: new Date(),
});
const options = useMemo(
() => ({
time: timeOptions,
}),
[timeOptions]
);
const { topGenres, isLoading } = useTopGenres(options);
const reportHasItems = !isLoading && topGenres.length !== 0;
if (!user) {
return <Redirect to="/" />;
}
const maxCount = getMaxCount(topGenres);
return (
<div className="md:flex md:justify-center p-4">
<div className="md:flex-shrink-0 min-w-full xl:min-w-0 xl:w-2/3 max-w-screen-lg">
<div className="flex justify-between">
<p className="text-2xl font-normal text-gray-700">Top Genres</p>
</div>
<div className="shadow-xl bg-gray-100 rounded p-5 m-2">
<ReportTimeOptions
timeOptions={timeOptions}
setTimeOptions={setTimeOptions}
/>
{isLoading && (
<div>
<div className="loader rounded-full border-8 h-64 w-64"></div>
</div>
)}
{!reportHasItems && (
<div>
<p>Report is emtpy! :(</p>
</div>
)}
{reportHasItems &&
topGenres.map(({ genre, artists, count }) => (
<ReportItem
genre={genre}
count={count}
artists={artists}
maxCount={maxCount}
/>
))}
</div>
</div>
</div>
);
};
const ReportItem: React.FC<{
genre: Genre;
artists: TopArtistsItem[];
count: number;
maxCount: number;
}> = ({ genre, artists, count, maxCount }) => {
const artistList = artists
.map(({ artist, count: artistCount }) => (
<ArtistItem artist={artist} count={artistCount} />
))
// @ts-expect-error
.reduce((acc, curr) => (acc === null ? [curr] : [acc, ", ", curr]), null);
return (
<TopListItem
key={genre.id}
title={capitalizeString(genre.name)}
subTitle={artistList}
count={count}
maxCount={maxCount}
/>
);
};
const ArtistItem: React.FC<{
artist: Artist;
count: number;
}> = ({ artist, count }) => (
<span title={`Listens: ${count}`}>{artist.name}</span>
);

View file

@ -3,7 +3,7 @@ import React from "react";
export interface TopListItemProps {
key: string;
title: string;
subTitle?: string;
subTitle?: string | React.ReactNode;
count: number;
/**

View file

@ -4,12 +4,14 @@ import {
getRecentListens,
getTopAlbums,
getTopArtists,
getTopGenres,
getTopTracks,
} from "../api/api";
import { ListenReportOptions } from "../api/entities/listen-report-options";
import { PaginationOptions } from "../api/entities/pagination-options";
import { TopAlbumsOptions } from "../api/entities/top-albums-options";
import { TopArtistsOptions } from "../api/entities/top-artists-options";
import { TopGenresOptions } from "../api/entities/top-genres-options";
import { TopTracksOptions } from "../api/entities/top-tracks-options";
import { useApiClient } from "./use-api-client";
import { useAsync } from "./use-async";
@ -105,3 +107,20 @@ export const useTopTracks = (options: TopTracksOptions) => {
return { topTracks, isLoading, error };
};
export const useTopGenres = (options: TopGenresOptions) => {
const { client } = useApiClient();
const fetchData = useMemo(
() => () => getTopGenres(options, client),
[options, client]
);
const {
value: topGenres,
pending: isLoading,
error,
} = useAsync(fetchData, INITIAL_EMPTY_ARRAY);
return { topGenres, isLoading, error };
};

View file

@ -0,0 +1,8 @@
export const capitalizeString = (str: string): string => {
const arr = str.split(" ");
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
}
return arr.join(" ");
};