From 223cf469f4fa4c63addebf3f0f5c46b248174484 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Thu, 22 Dec 2022 01:56:59 -0800 Subject: [PATCH] Add genre list query --- src/renderer/api/controller.ts | 5 +++ src/renderer/api/normalize.ts | 39 +++++++++++++++++-- src/renderer/api/types.ts | 3 +- src/renderer/features/genres/index.ts | 1 + .../genres/queries/genre-list-query.ts | 25 ++++++++++++ 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/renderer/features/genres/index.ts create mode 100644 src/renderer/features/genres/queries/genre-list-query.ts diff --git a/src/renderer/api/controller.ts b/src/renderer/api/controller.ts index aa0a1b46..025454c0 100644 --- a/src/renderer/api/controller.ts +++ b/src/renderer/api/controller.ts @@ -187,9 +187,14 @@ const getMusicFolderList = async (args: MusicFolderListArgs) => { return (apiController('getMusicFolderList') as ControllerEndpoint['getMusicFolderList'])?.(args); }; +const getGenreList = async (args: GenreListArgs) => { + return (apiController('getGenreList') as ControllerEndpoint['getGenreList'])?.(args); +}; + export const controller = { getAlbumDetail, getAlbumList, + getGenreList, getMusicFolderList, getSongList, }; diff --git a/src/renderer/api/normalize.ts b/src/renderer/api/normalize.ts index 75cc0d8c..034c3fd2 100644 --- a/src/renderer/api/normalize.ts +++ b/src/renderer/api/normalize.ts @@ -1,10 +1,16 @@ import { jfNormalize } from '/@/renderer/api/jellyfin.api'; -import type { JFAlbum, JFMusicFolderList, JFSong } from '/@/renderer/api/jellyfin.types'; +import type { + JFAlbum, + JFGenreList, + JFMusicFolderList, + JFSong, +} from '/@/renderer/api/jellyfin.types'; import { ndNormalize } from '/@/renderer/api/navidrome.api'; -import type { NDAlbum, NDSong } from '/@/renderer/api/navidrome.types'; -import { SSMusicFolderList } from '/@/renderer/api/subsonic.types'; +import type { NDAlbum, NDGenreList, NDSong } from '/@/renderer/api/navidrome.types'; +import { SSGenreList, SSMusicFolderList } from '/@/renderer/api/subsonic.types'; import type { RawAlbumListResponse, + RawGenreListResponse, RawMusicFolderListResponse, RawSongListResponse, } from '/@/renderer/api/types'; @@ -79,8 +85,35 @@ const musicFolderList = ( return musicFolders; }; +const genreList = (data: RawGenreListResponse | undefined, server: ServerListItem | null) => { + let genres; + switch (server?.type) { + case 'jellyfin': + genres = (data as JFGenreList)?.Items.map((item) => ({ + id: String(item.Id), + name: item.Name, + })); + break; + case 'navidrome': + genres = (data as NDGenreList)?.map((item) => ({ + id: String(item.id), + name: item.name, + })); + break; + case 'subsonic': + genres = (data as SSGenreList)?.map((item) => ({ + id: item.value, + name: item.value, + })); + break; + } + + return genres; +}; + export const normalize = { albumList, + genreList, musicFolderList, songList, }; diff --git a/src/renderer/api/types.ts b/src/renderer/api/types.ts index fceb4c3b..b73f701f 100644 --- a/src/renderer/api/types.ts +++ b/src/renderer/api/types.ts @@ -39,6 +39,7 @@ import { SSAlbumArtistList, SSAlbumArtistDetail, SSMusicFolderList, + SSGenreList, } from '/@/renderer/api/subsonic.types'; export enum SortOrder { @@ -253,7 +254,7 @@ type BaseEndpointArgs = { }; // Genre List -export type RawGenreListResponse = NDGenreList | JFGenreList | undefined; +export type RawGenreListResponse = NDGenreList | JFGenreList | SSGenreList | undefined; export type GenreListResponse = BasePaginatedResponse | null | undefined; diff --git a/src/renderer/features/genres/index.ts b/src/renderer/features/genres/index.ts new file mode 100644 index 00000000..d27b2441 --- /dev/null +++ b/src/renderer/features/genres/index.ts @@ -0,0 +1 @@ +export * from './queries/genre-list-query'; diff --git a/src/renderer/features/genres/queries/genre-list-query.ts b/src/renderer/features/genres/queries/genre-list-query.ts new file mode 100644 index 00000000..41179b8a --- /dev/null +++ b/src/renderer/features/genres/queries/genre-list-query.ts @@ -0,0 +1,25 @@ +import { useCallback } from 'react'; +import { useQuery } from '@tanstack/react-query'; +import { controller } from '/@/renderer/api/controller'; +import { queryKeys } from '/@/renderer/api/query-keys'; +import type { GenreListQuery, RawGenreListResponse } from '/@/renderer/api/types'; +import type { QueryOptions } from '/@/renderer/lib/react-query'; +import { useCurrentServer } from '/@/renderer/store'; +import { api } from '/@/renderer/api'; + +export const useGenreList = (query: GenreListQuery, options?: QueryOptions) => { + const server = useCurrentServer(); + + return useQuery({ + cacheTime: 1000 * 60 * 60 * 2, + enabled: !!server?.id, + queryFn: ({ signal }) => controller.getGenreList({ query, server, signal }), + queryKey: queryKeys.genres.list(server?.id || ''), + select: useCallback( + (data: RawGenreListResponse | undefined) => api.normalize.genreList(data, server), + [server], + ), + staleTime: 1000 * 60 * 60, + ...options, + }); +};