feat: updates

This commit is contained in:
qier222 2023-01-28 11:54:57 +08:00
parent 7ce516877e
commit ccebe0a67a
No known key found for this signature in database
74 changed files with 56065 additions and 2810 deletions

View file

@ -1,12 +1,8 @@
import { fetchAlbum } from '@/web/api/album'
import reactQueryClient from '@/web/utils/reactQueryClient'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import {
FetchAlbumParams,
AlbumApiNames,
FetchAlbumResponse,
} from '@/shared/api/Album'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { FetchAlbumParams, AlbumApiNames, FetchAlbumResponse } from '@/shared/api/Album'
import { useQuery } from '@tanstack/react-query'
const fetch = async (params: FetchAlbumParams) => {
@ -17,11 +13,9 @@ const fetch = async (params: FetchAlbumParams) => {
return album
}
const fetchFromCache = async (
params: FetchAlbumParams
): Promise<FetchAlbumResponse | undefined> =>
const fetchFromCache = async (params: FetchAlbumParams): Promise<FetchAlbumResponse | undefined> =>
window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
api: APIs.Album,
api: CacheAPIs.Album,
query: params,
})
@ -48,22 +42,14 @@ export default function useAlbum(params: FetchAlbumParams) {
}
export function fetchAlbumWithReactQuery(params: FetchAlbumParams) {
return reactQueryClient.fetchQuery(
[AlbumApiNames.FetchAlbum, params],
() => fetch(params),
{
staleTime: Infinity,
}
)
return reactQueryClient.fetchQuery([AlbumApiNames.FetchAlbum, params], () => fetch(params), {
staleTime: Infinity,
})
}
export async function prefetchAlbum(params: FetchAlbumParams) {
if (await fetchFromCache(params)) return
await reactQueryClient.prefetchQuery(
[AlbumApiNames.FetchAlbum, params],
() => fetch(params),
{
staleTime: Infinity,
}
)
await reactQueryClient.prefetchQuery([AlbumApiNames.FetchAlbum, params], () => fetch(params), {
staleTime: Infinity,
})
}

View file

@ -1,11 +1,7 @@
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 { CacheAPIs } from '@/shared/CacheAPIs'
import { FetchArtistParams, ArtistApiNames, FetchArtistResponse } from '@/shared/api/Artist'
import { useQuery } from '@tanstack/react-query'
import reactQueryClient from '@/web/utils/reactQueryClient'
@ -13,7 +9,7 @@ const fetchFromCache = async (
params: FetchArtistParams
): Promise<FetchArtistResponse | undefined> =>
window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
api: APIs.Artist,
api: CacheAPIs.Artist,
query: params,
})

View file

@ -1,6 +1,6 @@
import { fetchArtistAlbums } from '@/web/api/artist'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { FetchArtistAlbumsParams, ArtistApiNames } from '@/shared/api/Artist'
import { useQuery } from '@tanstack/react-query'
import reactQueryClient from '@/web/utils/reactQueryClient'
@ -13,7 +13,7 @@ export default function useArtistAlbums(params: FetchArtistAlbumsParams) {
// fetch from cache as placeholder
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
api: APIs.ArtistAlbum,
api: CacheAPIs.ArtistAlbum,
query: {
id: params.id,
},

View file

@ -1,11 +1,7 @@
import { fetchArtistMV } from '@/web/api/artist'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import {
FetchArtistMVParams,
ArtistApiNames,
FetchArtistMVResponse,
} from '@/shared/api/Artist'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { FetchArtistMVParams, ArtistApiNames, FetchArtistMVResponse } from '@/shared/api/Artist'
import { useQuery } from '@tanstack/react-query'
export default function useArtistMV(params: FetchArtistMVParams) {

View file

@ -1,6 +1,6 @@
import { fetchArtist } from '@/web/api/artist'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { ArtistApiNames } from '@/shared/api/Artist'
import { useQuery } from '@tanstack/react-query'
import reactQueryClient from '@/web/utils/reactQueryClient'
@ -11,21 +11,15 @@ export default function useArtists(ids: number[]) {
() =>
Promise.all(
ids.map(async id => {
const queryData = reactQueryClient.getQueryData([
ArtistApiNames.FetchArtist,
{ id },
])
const queryData = reactQueryClient.getQueryData([ArtistApiNames.FetchArtist, { id }])
if (queryData) return queryData
const cache = await window.ipcRenderer?.invoke(
IpcChannels.GetApiCache,
{
api: APIs.Artist,
query: {
id,
},
}
)
const cache = await window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
api: CacheAPIs.Artist,
query: {
id,
},
})
if (cache) return cache
return fetchArtist({ id })

View file

@ -1,7 +1,7 @@
import { fetchLyric } from '@/web/api/track'
import reactQueryClient from '@/web/utils/reactQueryClient'
import { FetchLyricParams, TrackApiNames } from '@/shared/api/Track'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { IpcChannels } from '@/shared/IpcChannels'
import { useQuery } from '@tanstack/react-query'
@ -12,7 +12,7 @@ export default function useLyric(params: FetchLyricParams) {
async () => {
// fetch from cache as initial data
const cache = window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
api: APIs.Lyric,
api: CacheAPIs.Lyric,
query: {
id: params.id,
},

View file

@ -1,12 +1,6 @@
import { fetchMV, fetchMVUrl } from '@/web/api/mv'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import {
MVApiNames,
FetchMVParams,
FetchMVResponse,
FetchMVUrlParams,
} from '@/shared/api/MV'
import { MVApiNames, FetchMVParams, FetchMVResponse, FetchMVUrlParams } from '@/shared/api/MV'
import { useQuery } from '@tanstack/react-query'
export default function useMV(params: FetchMVParams) {

View file

@ -1,7 +1,7 @@
import { fetchPlaylist } from '@/web/api/playlist'
import reactQueryClient from '@/web/utils/reactQueryClient'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import {
FetchPlaylistParams,
PlaylistApiNames,
@ -17,7 +17,7 @@ export const fetchFromCache = async (
params: FetchPlaylistParams
): Promise<FetchPlaylistResponse | undefined> =>
window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
api: APIs.Playlist,
api: CacheAPIs.Playlist,
query: params,
})

View file

@ -1,6 +1,6 @@
import { fetchSimilarArtists } from '@/web/api/artist'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { FetchSimilarArtistsParams, ArtistApiNames } from '@/shared/api/Artist'
import { useQuery } from '@tanstack/react-query'
import reactQueryClient from '@/web/utils/reactQueryClient'
@ -12,7 +12,7 @@ export default function useSimilarArtists(params: FetchSimilarArtistsParams) {
() => {
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
api: APIs.SimilarArtist,
api: CacheAPIs.SimilarArtist,
query: {
id: params.id,
},

View file

@ -8,7 +8,7 @@ import {
FetchTracksResponse,
TrackApiNames,
} from '@/shared/api/Track'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { useQuery } from '@tanstack/react-query'
export default function useTracks(params: FetchTracksParams) {
@ -17,7 +17,7 @@ export default function useTracks(params: FetchTracksParams) {
async () => {
// fetch from cache as initial data
const cache = await window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
api: APIs.Track,
api: CacheAPIs.Track,
query: {
ids: params.ids.join(','),
},
@ -40,7 +40,7 @@ export function fetchTracksWithReactQuery(params: FetchTracksParams) {
[TrackApiNames.FetchTracks, params],
async () => {
const cache = await window.ipcRenderer?.invoke(IpcChannels.GetApiCache, {
api: APIs.Track,
api: CacheAPIs.Track,
query: {
ids: params.ids.join(','),
},

View file

@ -1,6 +1,6 @@
import { fetchUserAccount } from '@/web/api/user'
import { UserApiNames, FetchUserAccountResponse } from '@/shared/api/User'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { IpcChannels } from '@/shared/IpcChannels'
import { useMutation, useQuery } from '@tanstack/react-query'
import { logout } from '../auth'
@ -16,7 +16,7 @@ export default function useUser() {
if (!existsQueryData) {
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
api: APIs.UserAccount,
api: CacheAPIs.UserAccount,
})
.then(cache => {
if (cache) reactQueryClient.setQueryData(key, cache)

View file

@ -2,12 +2,8 @@ import { likeAAlbum } from '@/web/api/album'
import { useMutation, useQuery } from '@tanstack/react-query'
import useUser from './useUser'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import {
FetchUserAlbumsParams,
UserApiNames,
FetchUserAlbumsResponse,
} from '@/shared/api/User'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { FetchUserAlbumsParams, UserApiNames, FetchUserAlbumsResponse } from '@/shared/api/User'
import { fetchUserAlbums } from '../user'
import toast from 'react-hot-toast'
import reactQueryClient from '@/web/utils/reactQueryClient'
@ -25,7 +21,7 @@ export default function useUserAlbums(params: FetchUserAlbumsParams = {}) {
if (!existsQueryData) {
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
api: APIs.UserAlbums,
api: CacheAPIs.UserAlbums,
query: params,
})
.then(cache => {
@ -72,9 +68,7 @@ export const useMutationLikeAAlbum = () => {
}
// Snapshot the previous value
const previousData = reactQueryClient.getQueryData(
key
) as FetchUserAlbumsResponse
const previousData = reactQueryClient.getQueryData(key) as FetchUserAlbumsResponse
const isLiked = !!previousData?.data.find(a => a.id === albumID)
const newAlbums = cloneDeep(previousData!)
@ -88,21 +82,17 @@ export const useMutationLikeAAlbum = () => {
console.log({ albumID })
const albumFromCache: FetchAlbumResponse | undefined =
reactQueryClient.getQueryData([
AlbumApiNames.FetchAlbum,
{ id: albumID },
])
const albumFromCache: FetchAlbumResponse | undefined = reactQueryClient.getQueryData([
AlbumApiNames.FetchAlbum,
{ id: albumID },
])
console.log({ albumFromCache })
// 从api获取专辑
const album: FetchAlbumResponse | undefined = albumFromCache
? albumFromCache
: await reactQueryClient.fetchQuery([
AlbumApiNames.FetchAlbum,
{ id: albumID },
])
: await reactQueryClient.fetchQuery([AlbumApiNames.FetchAlbum, { id: albumID }])
if (!album?.album) {
toast.error('Failed to like album: unable to fetch album info')

View file

@ -1,6 +1,6 @@
import { fetchUserArtists } from '@/web/api/user'
import { UserApiNames, FetchUserArtistsResponse } from '@/shared/api/User'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { IpcChannels } from '@/shared/IpcChannels'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
@ -18,7 +18,7 @@ export default function useUserArtists() {
if (!existsQueryData) {
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
api: APIs.UserArtists,
api: CacheAPIs.UserArtists,
})
.then(cache => {
if (cache) reactQueryClient.setQueryData(key, cache)
@ -59,32 +59,24 @@ export const useMutationLikeAArtist = () => {
}
// Snapshot the previous value
const previousData = reactQueryClient.getQueryData(
key
) as FetchUserArtistsResponse
const previousData = reactQueryClient.getQueryData(key) as FetchUserArtistsResponse
const isLiked = !!previousData?.data.find(a => a.id === artistID)
const newLikedArtists = cloneDeep(previousData!)
if (isLiked) {
newLikedArtists.data = previousData.data.filter(
a => a.id !== artistID
)
newLikedArtists.data = previousData.data.filter(a => a.id !== artistID)
} else {
// 从react-query缓存获取歌手信息
const artistFromCache: FetchArtistResponse | undefined =
reactQueryClient.getQueryData([
ArtistApiNames.FetchArtist,
{ id: artistID },
])
const artistFromCache: FetchArtistResponse | undefined = reactQueryClient.getQueryData([
ArtistApiNames.FetchArtist,
{ id: artistID },
])
// 从api获取歌手信息
const artist: FetchArtistResponse | undefined = artistFromCache
? artistFromCache
: await reactQueryClient.fetchQuery([
ArtistApiNames.FetchArtist,
{ id: artistID },
])
: await reactQueryClient.fetchQuery([ArtistApiNames.FetchArtist, { id: artistID }])
if (!artist?.artist) {
toast.error('Failed to like artist: unable to fetch artist info')

View file

@ -2,12 +2,9 @@ import { likeATrack } from '@/web/api/track'
import useUser from './useUser'
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { fetchUserLikedTracksIDs } from '../user'
import {
FetchUserLikedTracksIDsResponse,
UserApiNames,
} from '@/shared/api/User'
import { FetchUserLikedTracksIDsResponse, UserApiNames } from '@/shared/api/User'
import { useQuery } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import reactQueryClient from '@/web/utils/reactQueryClient'
@ -24,7 +21,7 @@ export default function useUserLikedTracksIDs() {
if (!existsQueryData) {
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
api: APIs.Likelist,
api: CacheAPIs.Likelist,
query: {
uid,
},

View file

@ -1,6 +1,6 @@
import { fetchListenedRecords } from '@/web/api/user'
import { UserApiNames, FetchListenedRecordsResponse } from '@/shared/api/User'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { IpcChannels } from '@/shared/IpcChannels'
import { useQuery } from '@tanstack/react-query'
import useUser from './useUser'
@ -18,7 +18,7 @@ export default function useUserListenedRecords(params: { type: 'week' | 'all' })
if (!existsQueryData) {
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
api: APIs.ListenedRecords,
api: CacheAPIs.ListenedRecords,
})
.then(cache => {
if (cache) reactQueryClient.setQueryData(key, cache)

View file

@ -2,7 +2,7 @@ import { likeAPlaylist } from '@/web/api/playlist'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import useUser from './useUser'
import { IpcChannels } from '@/shared/IpcChannels'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { fetchUserPlaylists } from '@/web/api/user'
import { FetchUserPlaylistsResponse, UserApiNames } from '@/shared/api/User'
import toast from 'react-hot-toast'
@ -33,7 +33,7 @@ export default function useUserPlaylists() {
if (!existsQueryData) {
window.ipcRenderer
?.invoke(IpcChannels.GetApiCache, {
api: APIs.UserPlaylist,
api: CacheAPIs.UserPlaylist,
query: {
uid: params.uid,
},
@ -46,11 +46,7 @@ export default function useUserPlaylists() {
return fetchUserPlaylists(params)
},
{
enabled: !!(
!!params.uid &&
params.uid !== 0 &&
params.offset !== undefined
),
enabled: !!(!!params.uid && params.uid !== 0 && params.offset !== undefined),
refetchOnWindowFocus: true,
}
)
@ -69,10 +65,7 @@ export const useMutationLikeAPlaylist = () => {
}
const response = await likeAPlaylist({
id: playlistID,
t:
userPlaylists.playlist.findIndex(p => p.id === playlistID) > -1
? 2
: 1,
t: userPlaylists.playlist.findIndex(p => p.id === playlistID) > -1 ? 2 : 1,
})
if (response.code !== 200) throw new Error((response as any).msg)
return response
@ -90,9 +83,7 @@ export const useMutationLikeAPlaylist = () => {
}
// Snapshot the previous value
const previousData = reactQueryClient.getQueryData(
key
) as FetchUserPlaylistsResponse
const previousData = reactQueryClient.getQueryData(key) as FetchUserPlaylistsResponse
const isLiked = !!previousData?.playlist.find(p => p.id === playlistID)
const newPlaylists = cloneDeep(previousData!)
@ -100,17 +91,12 @@ export const useMutationLikeAPlaylist = () => {
console.log({ isLiked })
if (isLiked) {
newPlaylists.playlist = previousData.playlist.filter(
p => p.id !== playlistID
)
newPlaylists.playlist = previousData.playlist.filter(p => p.id !== playlistID)
} else {
// 从react-query缓存获取歌单信息
const playlistFromCache: FetchPlaylistResponse | undefined =
reactQueryClient.getQueryData([
PlaylistApiNames.FetchPlaylist,
{ id: playlistID },
])
reactQueryClient.getQueryData([PlaylistApiNames.FetchPlaylist, { id: playlistID }])
// 从api获取歌单信息
const playlist: FetchPlaylistResponse | undefined = playlistFromCache
@ -121,9 +107,7 @@ export const useMutationLikeAPlaylist = () => {
])
if (!playlist?.playlist) {
toast.error(
'Failed to like playlist: unable to fetch playlist info'
)
toast.error('Failed to like playlist: unable to fetch playlist info')
throw new Error('unable to fetch playlist info')
}
newPlaylists.playlist.splice(1, 0, playlist.playlist)

View file

@ -3,7 +3,7 @@ import {
FetchListenedRecordsResponse,
FetchUserVideosResponse,
} from '@/shared/api/User'
import { APIs } from '@/shared/CacheAPIs'
import { CacheAPIs } from '@/shared/CacheAPIs'
import { IpcChannels } from '@/shared/IpcChannels'
import { useMutation, useQuery } from '@tanstack/react-query'
import useUser from './useUser'
@ -25,7 +25,7 @@ export default function useUserVideos() {
// if (!existsQueryData) {
// window.ipcRenderer
// ?.invoke(IpcChannels.GetApiCache, {
// api: APIs.Likelist,
// api: CacheAPIs.Likelist,
// query: {
// uid,
// },