feat: updates

This commit is contained in:
qier222 2022-06-06 01:00:25 +08:00
parent cf7a4528dd
commit 0e58bb6e80
No known key found for this signature in database
GPG key ID: 9C85007ED905F14D
44 changed files with 1027 additions and 496 deletions

View file

@ -17,6 +17,12 @@ const fetch = async (params: FetchAlbumParams, noCache?: boolean) => {
return album
}
const fetchFromCache = (id: number): FetchAlbumResponse =>
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
api: APIs.Album,
query: { id },
})
export default function useAlbum(params: FetchAlbumParams, noCache?: boolean) {
return useQuery(
[AlbumApiNames.FetchAlbum, params.id],
@ -24,13 +30,7 @@ export default function useAlbum(params: FetchAlbumParams, noCache?: boolean) {
{
enabled: !!params.id,
staleTime: 24 * 60 * 60 * 1000, // 24 hours
placeholderData: (): FetchAlbumResponse =>
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
api: APIs.Album,
query: {
id: params.id,
},
}),
placeholderData: () => fetchFromCache(params.id),
}
)
}
@ -46,6 +46,7 @@ export function fetchAlbumWithReactQuery(params: FetchAlbumParams) {
}
export async function prefetchAlbum(params: FetchAlbumParams) {
if (fetchFromCache(params.id)) return
await reactQueryClient.prefetchQuery(
[AlbumApiNames.FetchAlbum, params.id],
() => fetch(params),

View file

@ -0,0 +1,27 @@
import { fetchArtist } from '@/web/api/artist'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import {
FetchArtistParams,
ArtistApiNames,
FetchArtistResponse,
} from '@/shared/api/Artist'
import { useQuery } from 'react-query'
export default function useArtists(ids: number[]) {
return useQuery(
['fetchArtists', ids],
() => Promise.all(ids.map(id => fetchArtist({ id }, false))),
{
enabled: !!ids && ids.length > 0,
staleTime: 5 * 60 * 1000, // 5 mins
// placeholderData: (): FetchArtistResponse[] =>
// window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
// api: APIs.Artist,
// query: {
// ids,
// },
// }),
}
)
}

View file

@ -13,6 +13,12 @@ const fetch = (params: FetchPlaylistParams, noCache?: boolean) => {
return fetchPlaylist(params, !!noCache)
}
export const fetchFromCache = (id: number): FetchPlaylistResponse | undefined =>
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
api: APIs.Playlist,
query: { id },
})
export default function usePlaylist(
params: FetchPlaylistParams,
noCache?: boolean
@ -23,13 +29,7 @@ export default function usePlaylist(
{
enabled: !!(params.id && params.id > 0 && !isNaN(Number(params.id))),
refetchOnWindowFocus: true,
placeholderData: (): FetchPlaylistResponse | undefined =>
window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
api: APIs.Playlist,
query: {
id: params.id,
},
}),
placeholderData: () => fetchFromCache(params.id),
}
)
}
@ -45,6 +45,7 @@ export function fetchPlaylistWithReactQuery(params: FetchPlaylistParams) {
}
export async function prefetchPlaylist(params: FetchPlaylistParams) {
if (fetchFromCache(params.id)) return
await reactQueryClient.prefetchQuery(
[PlaylistApiNames.FetchPlaylist, params],
() => fetch(params),

View file

@ -0,0 +1,33 @@
import {
fetchListenedRecords,
FetchListenedRecordsParams,
} from '@/web/api/user'
import { UserApiNames } from '@/shared/api/User'
import { APIs } from '@/shared/CacheAPIs'
import { IpcChannels } from '@/shared/IpcChannels'
import { useQuery } from 'react-query'
import useUser from './useUser'
export default function useUserListenedRecords(params: {
type: 'week' | 'all'
}) {
const { data: user } = useUser()
const uid = user?.account?.id || 0
return useQuery(
[UserApiNames.FetchListenedRecords],
() =>
fetchListenedRecords({
uid,
type: params.type === 'week' ? 1 : 0,
}),
{
refetchOnWindowFocus: false,
enabled: !!uid,
// placeholderData: (): FetchUserArtistsResponse =>
// window.ipcRenderer?.sendSync(IpcChannels.GetApiCacheSync, {
// api: APIs.UserArtists,
// }),
}
)
}