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:
jeffvli
2026-01-17 19:57:12 -08:00
parent 27f82aef94
commit 4155cd0963
23 changed files with 190 additions and 49 deletions
@@ -1,7 +1,9 @@
import { queryOptions } from '@tanstack/react-query';
import { api } from '/@/renderer/api';
import { controller } from '/@/renderer/api/controller';
import { queryKeys } from '/@/renderer/api/query-keys';
import { getOptimizedListCount } from '/@/renderer/api/utils-list-count';
import { QueryHookArgs } from '/@/renderer/lib/react-query';
import {
AlbumArtistDetailQuery,
@@ -38,8 +40,25 @@ export const artistsQueries = {
},
albumArtistListCount: (args: QueryHookArgs<ListCountQuery<AlbumArtistListQuery>>) => {
return queryOptions({
gcTime: 1000 * 60 * 60 * 12,
queryFn: ({ signal }) => {
gcTime: 1000 * 60 * 60,
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({
apiClientProps: { serverId: args.serverId, signal },
query: args.query,
@@ -49,7 +68,7 @@ export const artistsQueries = {
args.serverId,
Object.keys(args.query).length === 0 ? undefined : args.query,
),
staleTime: 1000 * 60 * 60 * 12,
staleTime: 1000 * 60 * 60,
...args.options,
});
},
@@ -67,8 +86,25 @@ export const artistsQueries = {
},
artistListCount: (args: QueryHookArgs<ListCountQuery<ArtistListQuery>>) => {
return queryOptions({
gcTime: 1000 * 60 * 60 * 12,
queryFn: ({ signal }) => {
gcTime: 1000 * 60 * 60,
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
.getArtistList({
apiClientProps: { serverId: args.serverId, signal },
@@ -80,7 +116,7 @@ export const artistsQueries = {
args.serverId,
Object.keys(args.query).length === 0 ? undefined : args.query,
),
staleTime: 1000 * 60 * 60 * 12,
staleTime: 1000 * 60 * 60,
...args.options,
});
},
@@ -31,7 +31,7 @@ export const AlbumArtistListInfiniteGrid = ({
size,
}: AlbumArtistListInfiniteGridProps) => {
const listCountQuery = artistsQueries.albumArtistListCount({
query: { ...query },
query: { ...query, limit: itemsPerPage },
serverId: serverId,
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
@@ -38,7 +38,7 @@ export const AlbumArtistListInfiniteTable = ({
size = 'default',
}: AlbumArtistListInfiniteTableProps) => {
const listCountQuery = artistsQueries.albumArtistListCount({
query: { ...query },
query: { ...query, limit: itemsPerPage },
serverId: serverId,
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
@@ -32,15 +32,15 @@ export const AlbumArtistListPaginatedGrid = ({
serverId,
size,
}: AlbumArtistListPaginatedGridProps) => {
const { currentPage, onChange } = useItemListPagination();
const listCountQuery = artistsQueries.albumArtistListCount({
query: { ...query },
query: { ...query, limit: itemsPerPage },
serverId: serverId,
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
const listQueryFn = api.controller.getAlbumArtistList;
const { currentPage, onChange } = useItemListPagination();
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
currentPage,
eventKey: ItemListKey.ALBUM_ARTIST,
@@ -39,15 +39,15 @@ export const AlbumArtistListPaginatedTable = ({
serverId,
size = 'default',
}: AlbumArtistListPaginatedTableProps) => {
const { currentPage, onChange } = useItemListPagination();
const listCountQuery = artistsQueries.albumArtistListCount({
query: { ...query },
query: { ...query, limit: itemsPerPage },
serverId: serverId,
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
const listQueryFn = api.controller.getAlbumArtistList;
const { currentPage, onChange } = useItemListPagination();
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
currentPage,
eventKey: ItemListKey.ALBUM_ARTIST,
@@ -30,7 +30,7 @@ export const ArtistListInfiniteGrid = ({
size,
}: ArtistListInfiniteGridProps) => {
const listCountQuery = artistsQueries.artistListCount({
query: { ...query },
query: { ...query, limit: itemsPerPage },
serverId: serverId,
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
@@ -37,7 +37,7 @@ export const ArtistListInfiniteTable = ({
size = 'default',
}: ArtistListInfiniteTableProps) => {
const listCountQuery = artistsQueries.artistListCount({
query: { ...query },
query: { ...query, limit: itemsPerPage },
serverId: serverId,
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
@@ -31,15 +31,15 @@ export const ArtistListPaginatedGrid = ({
serverId,
size,
}: ArtistListPaginatedGridProps) => {
const { currentPage, onChange } = useItemListPagination();
const listCountQuery = artistsQueries.artistListCount({
query: { ...query },
query: { ...query, limit: itemsPerPage },
serverId: serverId,
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
const listQueryFn = api.controller.getArtistList;
const { currentPage, onChange } = useItemListPagination();
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
currentPage,
eventKey: ItemListKey.ARTIST,
@@ -38,15 +38,15 @@ export const ArtistListPaginatedTable = ({
serverId,
size = 'default',
}: ArtistListPaginatedTableProps) => {
const { currentPage, onChange } = useItemListPagination();
const listCountQuery = artistsQueries.artistListCount({
query: { ...query },
query: { ...query, limit: itemsPerPage },
serverId: serverId,
}) as UseSuspenseQueryOptions<number, Error, number, readonly unknown[]>;
const listQueryFn = api.controller.getArtistList;
const { currentPage, onChange } = useItemListPagination();
const { data, pageCount, totalItemCount } = useItemListPaginatedLoader({
currentPage,
eventKey: ItemListKey.ARTIST,