mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-09 20:29:36 +02:00
optimize ND/JF list fetch
- no longer requires 2 separate fetches for count and data - the list count includes the first page so we set the query data directly
This commit is contained in:
@@ -0,0 +1,68 @@
|
|||||||
|
import { QueryClient } from '@tanstack/react-query';
|
||||||
|
|
||||||
|
import { getServerById } from '/@/renderer/store';
|
||||||
|
import { ServerType } from '/@/shared/types/domain-types';
|
||||||
|
|
||||||
|
interface OptimizedListCountOptions<TQuery, TListQuery, TResponse> {
|
||||||
|
client: QueryClient;
|
||||||
|
listQueryFn: (args: {
|
||||||
|
apiClientProps: { serverId: string; signal?: AbortSignal };
|
||||||
|
query: TListQuery;
|
||||||
|
}) => Promise<TResponse>;
|
||||||
|
listQueryKeyFn: (serverId: string, query: TListQuery) => readonly unknown[];
|
||||||
|
query: TQuery;
|
||||||
|
serverId: string;
|
||||||
|
signal?: AbortSignal;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getOptimizedListCount = async <
|
||||||
|
TQuery,
|
||||||
|
TListQuery extends { limit?: number; startIndex?: number },
|
||||||
|
TResponse extends { totalRecordCount: null | number },
|
||||||
|
>({
|
||||||
|
client,
|
||||||
|
listQueryFn,
|
||||||
|
listQueryKeyFn,
|
||||||
|
query,
|
||||||
|
serverId,
|
||||||
|
signal,
|
||||||
|
}: OptimizedListCountOptions<TQuery, TListQuery, TResponse>): Promise<null | number> => {
|
||||||
|
const server = getServerById(serverId);
|
||||||
|
|
||||||
|
if (server?.type !== ServerType.NAVIDROME && server?.type !== ServerType.JELLYFIN) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const limit =
|
||||||
|
typeof query === 'object' &&
|
||||||
|
query !== null &&
|
||||||
|
'limit' in query &&
|
||||||
|
typeof (query as any).limit === 'number' &&
|
||||||
|
(query as any).limit > 0
|
||||||
|
? (query as any).limit
|
||||||
|
: 100;
|
||||||
|
|
||||||
|
// In most cases, the list count is called when entering the first page, so we fetch from the first page
|
||||||
|
// This optimization will only help in this case, otherwise we still need 2 requests to get both the count and the data
|
||||||
|
const pageQuery = {
|
||||||
|
...query,
|
||||||
|
limit,
|
||||||
|
startIndex: 0,
|
||||||
|
} as unknown as TListQuery;
|
||||||
|
|
||||||
|
const pageQueryKey = listQueryKeyFn(serverId, pageQuery);
|
||||||
|
const cachedPage = client.getQueryData(pageQueryKey);
|
||||||
|
|
||||||
|
if (cachedPage && typeof cachedPage === 'object' && 'totalRecordCount' in cachedPage) {
|
||||||
|
return (cachedPage as TResponse).totalRecordCount ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pageResult = await listQueryFn({
|
||||||
|
apiClientProps: { serverId, signal },
|
||||||
|
query: pageQuery,
|
||||||
|
});
|
||||||
|
|
||||||
|
client.setQueryData(pageQueryKey, pageResult);
|
||||||
|
|
||||||
|
return pageResult.totalRecordCount ?? 0;
|
||||||
|
};
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
import { queryOptions } from '@tanstack/react-query';
|
import { queryOptions } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { api } from '/@/renderer/api';
|
import { api } from '/@/renderer/api';
|
||||||
|
import { controller } from '/@/renderer/api/controller';
|
||||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||||
|
import { getOptimizedListCount } from '/@/renderer/api/utils-list-count';
|
||||||
import { QueryHookArgs } from '/@/renderer/lib/react-query';
|
import { QueryHookArgs } from '/@/renderer/lib/react-query';
|
||||||
import { AlbumDetailQuery, AlbumListQuery, ListCountQuery } from '/@/shared/types/domain-types';
|
import { AlbumDetailQuery, AlbumListQuery, ListCountQuery } from '/@/shared/types/domain-types';
|
||||||
|
|
||||||
@@ -36,8 +38,25 @@ export const albumQueries = {
|
|||||||
},
|
},
|
||||||
listCount: (args: QueryHookArgs<ListCountQuery<AlbumListQuery>>) => {
|
listCount: (args: QueryHookArgs<ListCountQuery<AlbumListQuery>>) => {
|
||||||
return queryOptions({
|
return queryOptions({
|
||||||
gcTime: 1000 * 60 * 60 * 12,
|
gcTime: 1000 * 60 * 60,
|
||||||
queryFn: ({ signal }) => {
|
queryFn: async ({ client, signal }) => {
|
||||||
|
const optimizedCount = await getOptimizedListCount<
|
||||||
|
ListCountQuery<AlbumListQuery>,
|
||||||
|
AlbumListQuery,
|
||||||
|
{ totalRecordCount: null | number }
|
||||||
|
>({
|
||||||
|
client,
|
||||||
|
listQueryFn: controller.getAlbumList,
|
||||||
|
listQueryKeyFn: (serverId, query) => queryKeys.albums.list(serverId, query),
|
||||||
|
query: args.query,
|
||||||
|
serverId: args.serverId,
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (optimizedCount !== null) {
|
||||||
|
return optimizedCount;
|
||||||
|
}
|
||||||
|
|
||||||
return api.controller.getAlbumListCount({
|
return api.controller.getAlbumListCount({
|
||||||
apiClientProps: { serverId: args.serverId, signal },
|
apiClientProps: { serverId: args.serverId, signal },
|
||||||
query: args.query,
|
query: args.query,
|
||||||
@@ -48,7 +67,7 @@ export const albumQueries = {
|
|||||||
args.query,
|
args.query,
|
||||||
args.query?.artistIds?.length === 1 ? args.query?.artistIds[0] : undefined,
|
args.query?.artistIds?.length === 1 ? args.query?.artistIds[0] : undefined,
|
||||||
),
|
),
|
||||||
staleTime: 1000 * 60 * 60 * 12,
|
staleTime: 1000 * 60 * 60,
|
||||||
...args.options,
|
...args.options,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export const AlbumListInfiniteGrid = ({
|
|||||||
size,
|
size,
|
||||||
}: AlbumListInfiniteGridProps) => {
|
}: AlbumListInfiniteGridProps) => {
|
||||||
const listCountQuery = albumQueries.listCount({
|
const listCountQuery = albumQueries.listCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export const AlbumListInfiniteTable = ({
|
|||||||
size = 'default',
|
size = 'default',
|
||||||
}: AlbumListInfiniteTableProps) => {
|
}: AlbumListInfiniteTableProps) => {
|
||||||
const listCountQuery = albumQueries.listCount({
|
const listCountQuery = albumQueries.listCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -31,15 +31,15 @@ export const AlbumListPaginatedGrid = ({
|
|||||||
serverId,
|
serverId,
|
||||||
size,
|
size,
|
||||||
}: AlbumListPaginatedGridProps) => {
|
}: AlbumListPaginatedGridProps) => {
|
||||||
|
const { currentPage, onChange } = useItemListPagination();
|
||||||
|
|
||||||
const listCountQuery = albumQueries.listCount({
|
const listCountQuery = albumQueries.listCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
const listQueryFn = api.controller.getAlbumList;
|
const listQueryFn = api.controller.getAlbumList;
|
||||||
|
|
||||||
const { currentPage, onChange } = useItemListPagination();
|
|
||||||
|
|
||||||
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
||||||
currentPage,
|
currentPage,
|
||||||
eventKey: ItemListKey.ALBUM,
|
eventKey: ItemListKey.ALBUM,
|
||||||
|
|||||||
@@ -38,15 +38,15 @@ export const AlbumListPaginatedTable = ({
|
|||||||
serverId,
|
serverId,
|
||||||
size = 'default',
|
size = 'default',
|
||||||
}: AlbumListPaginatedTableProps) => {
|
}: AlbumListPaginatedTableProps) => {
|
||||||
|
const { currentPage, onChange } = useItemListPagination();
|
||||||
|
|
||||||
const listCountQuery = albumQueries.listCount({
|
const listCountQuery = albumQueries.listCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
const listQueryFn = api.controller.getAlbumList;
|
const listQueryFn = api.controller.getAlbumList;
|
||||||
|
|
||||||
const { currentPage, onChange } = useItemListPagination();
|
|
||||||
|
|
||||||
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
||||||
currentPage,
|
currentPage,
|
||||||
eventKey: ItemListKey.ALBUM,
|
eventKey: ItemListKey.ALBUM,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { queryOptions } from '@tanstack/react-query';
|
import { queryOptions } from '@tanstack/react-query';
|
||||||
|
|
||||||
import { api } from '/@/renderer/api';
|
import { api } from '/@/renderer/api';
|
||||||
|
import { controller } from '/@/renderer/api/controller';
|
||||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||||
|
import { getOptimizedListCount } from '/@/renderer/api/utils-list-count';
|
||||||
import { QueryHookArgs } from '/@/renderer/lib/react-query';
|
import { QueryHookArgs } from '/@/renderer/lib/react-query';
|
||||||
import {
|
import {
|
||||||
AlbumArtistDetailQuery,
|
AlbumArtistDetailQuery,
|
||||||
@@ -38,8 +40,25 @@ export const artistsQueries = {
|
|||||||
},
|
},
|
||||||
albumArtistListCount: (args: QueryHookArgs<ListCountQuery<AlbumArtistListQuery>>) => {
|
albumArtistListCount: (args: QueryHookArgs<ListCountQuery<AlbumArtistListQuery>>) => {
|
||||||
return queryOptions({
|
return queryOptions({
|
||||||
gcTime: 1000 * 60 * 60 * 12,
|
gcTime: 1000 * 60 * 60,
|
||||||
queryFn: ({ signal }) => {
|
queryFn: async ({ client, signal }) => {
|
||||||
|
const optimizedCount = await getOptimizedListCount<
|
||||||
|
ListCountQuery<AlbumArtistListQuery>,
|
||||||
|
AlbumArtistListQuery,
|
||||||
|
{ totalRecordCount: null | number }
|
||||||
|
>({
|
||||||
|
client,
|
||||||
|
listQueryFn: controller.getAlbumArtistList,
|
||||||
|
listQueryKeyFn: queryKeys.albumArtists.list,
|
||||||
|
query: args.query,
|
||||||
|
serverId: args.serverId,
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (optimizedCount !== null) {
|
||||||
|
return optimizedCount;
|
||||||
|
}
|
||||||
|
|
||||||
return api.controller.getAlbumArtistListCount({
|
return api.controller.getAlbumArtistListCount({
|
||||||
apiClientProps: { serverId: args.serverId, signal },
|
apiClientProps: { serverId: args.serverId, signal },
|
||||||
query: args.query,
|
query: args.query,
|
||||||
@@ -49,7 +68,7 @@ export const artistsQueries = {
|
|||||||
args.serverId,
|
args.serverId,
|
||||||
Object.keys(args.query).length === 0 ? undefined : args.query,
|
Object.keys(args.query).length === 0 ? undefined : args.query,
|
||||||
),
|
),
|
||||||
staleTime: 1000 * 60 * 60 * 12,
|
staleTime: 1000 * 60 * 60,
|
||||||
...args.options,
|
...args.options,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -67,8 +86,25 @@ export const artistsQueries = {
|
|||||||
},
|
},
|
||||||
artistListCount: (args: QueryHookArgs<ListCountQuery<ArtistListQuery>>) => {
|
artistListCount: (args: QueryHookArgs<ListCountQuery<ArtistListQuery>>) => {
|
||||||
return queryOptions({
|
return queryOptions({
|
||||||
gcTime: 1000 * 60 * 60 * 12,
|
gcTime: 1000 * 60 * 60,
|
||||||
queryFn: ({ signal }) => {
|
queryFn: async ({ client, signal }) => {
|
||||||
|
const optimizedCount = await getOptimizedListCount<
|
||||||
|
ListCountQuery<ArtistListQuery>,
|
||||||
|
ArtistListQuery,
|
||||||
|
{ totalRecordCount: null | number }
|
||||||
|
>({
|
||||||
|
client,
|
||||||
|
listQueryFn: controller.getArtistList,
|
||||||
|
listQueryKeyFn: queryKeys.artists.list,
|
||||||
|
query: args.query,
|
||||||
|
serverId: args.serverId,
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (optimizedCount !== null) {
|
||||||
|
return optimizedCount;
|
||||||
|
}
|
||||||
|
|
||||||
return api.controller
|
return api.controller
|
||||||
.getArtistList({
|
.getArtistList({
|
||||||
apiClientProps: { serverId: args.serverId, signal },
|
apiClientProps: { serverId: args.serverId, signal },
|
||||||
@@ -80,7 +116,7 @@ export const artistsQueries = {
|
|||||||
args.serverId,
|
args.serverId,
|
||||||
Object.keys(args.query).length === 0 ? undefined : args.query,
|
Object.keys(args.query).length === 0 ? undefined : args.query,
|
||||||
),
|
),
|
||||||
staleTime: 1000 * 60 * 60 * 12,
|
staleTime: 1000 * 60 * 60,
|
||||||
...args.options,
|
...args.options,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export const AlbumArtistListInfiniteGrid = ({
|
|||||||
size,
|
size,
|
||||||
}: AlbumArtistListInfiniteGridProps) => {
|
}: AlbumArtistListInfiniteGridProps) => {
|
||||||
const listCountQuery = artistsQueries.albumArtistListCount({
|
const listCountQuery = artistsQueries.albumArtistListCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export const AlbumArtistListInfiniteTable = ({
|
|||||||
size = 'default',
|
size = 'default',
|
||||||
}: AlbumArtistListInfiniteTableProps) => {
|
}: AlbumArtistListInfiniteTableProps) => {
|
||||||
const listCountQuery = artistsQueries.albumArtistListCount({
|
const listCountQuery = artistsQueries.albumArtistListCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -32,15 +32,15 @@ export const AlbumArtistListPaginatedGrid = ({
|
|||||||
serverId,
|
serverId,
|
||||||
size,
|
size,
|
||||||
}: AlbumArtistListPaginatedGridProps) => {
|
}: AlbumArtistListPaginatedGridProps) => {
|
||||||
|
const { currentPage, onChange } = useItemListPagination();
|
||||||
|
|
||||||
const listCountQuery = artistsQueries.albumArtistListCount({
|
const listCountQuery = artistsQueries.albumArtistListCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
const listQueryFn = api.controller.getAlbumArtistList;
|
const listQueryFn = api.controller.getAlbumArtistList;
|
||||||
|
|
||||||
const { currentPage, onChange } = useItemListPagination();
|
|
||||||
|
|
||||||
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
||||||
currentPage,
|
currentPage,
|
||||||
eventKey: ItemListKey.ALBUM_ARTIST,
|
eventKey: ItemListKey.ALBUM_ARTIST,
|
||||||
|
|||||||
@@ -39,15 +39,15 @@ export const AlbumArtistListPaginatedTable = ({
|
|||||||
serverId,
|
serverId,
|
||||||
size = 'default',
|
size = 'default',
|
||||||
}: AlbumArtistListPaginatedTableProps) => {
|
}: AlbumArtistListPaginatedTableProps) => {
|
||||||
|
const { currentPage, onChange } = useItemListPagination();
|
||||||
|
|
||||||
const listCountQuery = artistsQueries.albumArtistListCount({
|
const listCountQuery = artistsQueries.albumArtistListCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
const listQueryFn = api.controller.getAlbumArtistList;
|
const listQueryFn = api.controller.getAlbumArtistList;
|
||||||
|
|
||||||
const { currentPage, onChange } = useItemListPagination();
|
|
||||||
|
|
||||||
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
||||||
currentPage,
|
currentPage,
|
||||||
eventKey: ItemListKey.ALBUM_ARTIST,
|
eventKey: ItemListKey.ALBUM_ARTIST,
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ export const ArtistListInfiniteGrid = ({
|
|||||||
size,
|
size,
|
||||||
}: ArtistListInfiniteGridProps) => {
|
}: ArtistListInfiniteGridProps) => {
|
||||||
const listCountQuery = artistsQueries.artistListCount({
|
const listCountQuery = artistsQueries.artistListCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export const ArtistListInfiniteTable = ({
|
|||||||
size = 'default',
|
size = 'default',
|
||||||
}: ArtistListInfiniteTableProps) => {
|
}: ArtistListInfiniteTableProps) => {
|
||||||
const listCountQuery = artistsQueries.artistListCount({
|
const listCountQuery = artistsQueries.artistListCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -31,15 +31,15 @@ export const ArtistListPaginatedGrid = ({
|
|||||||
serverId,
|
serverId,
|
||||||
size,
|
size,
|
||||||
}: ArtistListPaginatedGridProps) => {
|
}: ArtistListPaginatedGridProps) => {
|
||||||
|
const { currentPage, onChange } = useItemListPagination();
|
||||||
|
|
||||||
const listCountQuery = artistsQueries.artistListCount({
|
const listCountQuery = artistsQueries.artistListCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
const listQueryFn = api.controller.getArtistList;
|
const listQueryFn = api.controller.getArtistList;
|
||||||
|
|
||||||
const { currentPage, onChange } = useItemListPagination();
|
|
||||||
|
|
||||||
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
||||||
currentPage,
|
currentPage,
|
||||||
eventKey: ItemListKey.ARTIST,
|
eventKey: ItemListKey.ARTIST,
|
||||||
|
|||||||
@@ -38,15 +38,15 @@ export const ArtistListPaginatedTable = ({
|
|||||||
serverId,
|
serverId,
|
||||||
size = 'default',
|
size = 'default',
|
||||||
}: ArtistListPaginatedTableProps) => {
|
}: ArtistListPaginatedTableProps) => {
|
||||||
|
const { currentPage, onChange } = useItemListPagination();
|
||||||
|
|
||||||
const listCountQuery = artistsQueries.artistListCount({
|
const listCountQuery = artistsQueries.artistListCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
const listQueryFn = api.controller.getArtistList;
|
const listQueryFn = api.controller.getArtistList;
|
||||||
|
|
||||||
const { currentPage, onChange } = useItemListPagination();
|
|
||||||
|
|
||||||
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
||||||
currentPage,
|
currentPage,
|
||||||
eventKey: ItemListKey.ARTIST,
|
eventKey: ItemListKey.ARTIST,
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export const genresQueries = {
|
|||||||
},
|
},
|
||||||
listCount: (args: QueryHookArgs<ListCountQuery<GenreListQuery>>) => {
|
listCount: (args: QueryHookArgs<ListCountQuery<GenreListQuery>>) => {
|
||||||
return queryOptions({
|
return queryOptions({
|
||||||
gcTime: 1000 * 60 * 60 * 12,
|
gcTime: 1000 * 60 * 60,
|
||||||
queryFn: ({ signal }) => {
|
queryFn: ({ signal }) => {
|
||||||
return api.controller
|
return api.controller
|
||||||
.getGenreList({
|
.getGenreList({
|
||||||
@@ -41,7 +41,7 @@ export const genresQueries = {
|
|||||||
args.serverId,
|
args.serverId,
|
||||||
Object.keys(args.query).length === 0 ? undefined : args.query,
|
Object.keys(args.query).length === 0 ? undefined : args.query,
|
||||||
),
|
),
|
||||||
staleTime: 1000 * 60 * 60 * 12,
|
staleTime: 1000 * 60 * 60,
|
||||||
...args.options,
|
...args.options,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export const playlistsQueries = {
|
|||||||
},
|
},
|
||||||
listCount: (args: QueryHookArgs<ListCountQuery<PlaylistListQuery>>) => {
|
listCount: (args: QueryHookArgs<ListCountQuery<PlaylistListQuery>>) => {
|
||||||
return queryOptions({
|
return queryOptions({
|
||||||
gcTime: 1000 * 60 * 60 * 12,
|
gcTime: 1000 * 60 * 60,
|
||||||
queryFn: ({ signal }) => {
|
queryFn: ({ signal }) => {
|
||||||
return api.controller.getPlaylistListCount({
|
return api.controller.getPlaylistListCount({
|
||||||
apiClientProps: { serverId: args.serverId, signal },
|
apiClientProps: { serverId: args.serverId, signal },
|
||||||
@@ -49,7 +49,7 @@ export const playlistsQueries = {
|
|||||||
args.serverId || '',
|
args.serverId || '',
|
||||||
Object.keys(args.query).length === 0 ? undefined : args.query,
|
Object.keys(args.query).length === 0 ? undefined : args.query,
|
||||||
),
|
),
|
||||||
staleTime: 1000 * 60 * 60 * 12,
|
staleTime: 1000 * 60 * 60,
|
||||||
...args.options,
|
...args.options,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { queryOptions } from '@tanstack/react-query';
|
|||||||
import { api } from '/@/renderer/api';
|
import { api } from '/@/renderer/api';
|
||||||
import { controller } from '/@/renderer/api/controller';
|
import { controller } from '/@/renderer/api/controller';
|
||||||
import { queryKeys } from '/@/renderer/api/query-keys';
|
import { queryKeys } from '/@/renderer/api/query-keys';
|
||||||
|
import { getOptimizedListCount } from '/@/renderer/api/utils-list-count';
|
||||||
import { QueryHookArgs } from '/@/renderer/lib/react-query';
|
import { QueryHookArgs } from '/@/renderer/lib/react-query';
|
||||||
import {
|
import {
|
||||||
ArtistRadioQuery,
|
ArtistRadioQuery,
|
||||||
@@ -53,8 +54,25 @@ export const songsQueries = {
|
|||||||
},
|
},
|
||||||
listCount: (args: QueryHookArgs<ListCountQuery<SongListQuery>>) => {
|
listCount: (args: QueryHookArgs<ListCountQuery<SongListQuery>>) => {
|
||||||
return queryOptions({
|
return queryOptions({
|
||||||
gcTime: 1000 * 60 * 60 * 12,
|
gcTime: 1000 * 60 * 60,
|
||||||
queryFn: ({ signal }) => {
|
queryFn: async ({ client, signal }) => {
|
||||||
|
const optimizedCount = await getOptimizedListCount<
|
||||||
|
ListCountQuery<SongListQuery>,
|
||||||
|
SongListQuery,
|
||||||
|
{ totalRecordCount: null | number }
|
||||||
|
>({
|
||||||
|
client,
|
||||||
|
listQueryFn: controller.getSongList,
|
||||||
|
listQueryKeyFn: queryKeys.songs.list,
|
||||||
|
query: args.query,
|
||||||
|
serverId: args.serverId,
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (optimizedCount !== null) {
|
||||||
|
return optimizedCount;
|
||||||
|
}
|
||||||
|
|
||||||
return api.controller.getSongListCount({
|
return api.controller.getSongListCount({
|
||||||
apiClientProps: { serverId: args.serverId, signal },
|
apiClientProps: { serverId: args.serverId, signal },
|
||||||
query: args.query,
|
query: args.query,
|
||||||
@@ -64,7 +82,7 @@ export const songsQueries = {
|
|||||||
args.serverId,
|
args.serverId,
|
||||||
Object.keys(args.query).length === 0 ? undefined : args.query,
|
Object.keys(args.query).length === 0 ? undefined : args.query,
|
||||||
),
|
),
|
||||||
staleTime: 1000 * 60 * 60 * 12,
|
staleTime: 1000 * 60 * 60,
|
||||||
...args.options,
|
...args.options,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export const SongListInfiniteGrid = ({
|
|||||||
size,
|
size,
|
||||||
}: SongListInfiniteGridProps) => {
|
}: SongListInfiniteGridProps) => {
|
||||||
const listCountQuery = songsQueries.listCount({
|
const listCountQuery = songsQueries.listCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ export const SongListInfiniteTable = ({
|
|||||||
size = 'default',
|
size = 'default',
|
||||||
}: SongListInfiniteTableProps) => {
|
}: SongListInfiniteTableProps) => {
|
||||||
const listCountQuery = songsQueries.listCount({
|
const listCountQuery = songsQueries.listCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
|
|||||||
@@ -24,15 +24,15 @@ export const SongListPaginatedGrid = ({
|
|||||||
serverId,
|
serverId,
|
||||||
size,
|
size,
|
||||||
}: SongListPaginatedGridProps) => {
|
}: SongListPaginatedGridProps) => {
|
||||||
|
const { currentPage, onChange } = useItemListPagination();
|
||||||
|
|
||||||
const listCountQuery = songsQueries.listCount({
|
const listCountQuery = songsQueries.listCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
const listQueryFn = api.controller.getSongList;
|
const listQueryFn = api.controller.getSongList;
|
||||||
|
|
||||||
const { currentPage, onChange } = useItemListPagination();
|
|
||||||
|
|
||||||
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
||||||
currentPage,
|
currentPage,
|
||||||
eventKey: ItemListKey.SONG,
|
eventKey: ItemListKey.SONG,
|
||||||
|
|||||||
@@ -34,15 +34,15 @@ export const SongListPaginatedTable = ({
|
|||||||
serverId,
|
serverId,
|
||||||
size = 'default',
|
size = 'default',
|
||||||
}: SongListPaginatedTableProps) => {
|
}: SongListPaginatedTableProps) => {
|
||||||
|
const { currentPage, onChange } = useItemListPagination();
|
||||||
|
|
||||||
const listCountQuery = songsQueries.listCount({
|
const listCountQuery = songsQueries.listCount({
|
||||||
query: { ...query },
|
query: { ...query, limit: itemsPerPage },
|
||||||
serverId: serverId,
|
serverId: serverId,
|
||||||
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
|
||||||
|
|
||||||
const listQueryFn = api.controller.getSongList;
|
const listQueryFn = api.controller.getSongList;
|
||||||
|
|
||||||
const { currentPage, onChange } = useItemListPagination();
|
|
||||||
|
|
||||||
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
|
||||||
currentPage,
|
currentPage,
|
||||||
eventKey: ItemListKey.SONG,
|
eventKey: ItemListKey.SONG,
|
||||||
|
|||||||
@@ -487,7 +487,7 @@ export interface AlbumListQuery extends AlbumListNavidromeQuery, BaseQuery<Album
|
|||||||
// Album List
|
// Album List
|
||||||
export type AlbumListResponse = BasePaginatedResponse<Album[]>;
|
export type AlbumListResponse = BasePaginatedResponse<Album[]>;
|
||||||
|
|
||||||
export type ListCountQuery<TQuery> = Omit<TQuery, 'limit' | 'startIndex'>;
|
export type ListCountQuery<TQuery> = Omit<TQuery, 'startIndex'>;
|
||||||
|
|
||||||
interface AlbumListNavidromeQuery {
|
interface AlbumListNavidromeQuery {
|
||||||
hasRating?: boolean;
|
hasRating?: boolean;
|
||||||
|
|||||||
Reference in New Issue
Block a user