Add genre list query

This commit is contained in:
jeffvli 2022-12-22 01:56:59 -08:00
parent b4301486f3
commit 223cf469f4
5 changed files with 69 additions and 4 deletions

View File

@ -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,
};

View File

@ -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,
};

View File

@ -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<Genre[]> | null | undefined;

View File

@ -0,0 +1 @@
export * from './queries/genre-list-query';

View File

@ -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,
});
};