2022-05-12 02:45:43 +08:00
|
|
|
import { multiMatchSearch, search } from '@/web/api/search'
|
|
|
|
import Cover from '@/web/components/Cover'
|
|
|
|
import TrackGrid from '@/web/components/TracksGrid'
|
2022-07-11 11:06:41 +08:00
|
|
|
import player from '@/web/states/player'
|
2022-05-12 02:45:43 +08:00
|
|
|
import { resizeImage } from '@/web/utils/common'
|
2022-04-16 21:14:03 +08:00
|
|
|
import { SearchTypes, SearchApiNames } from '@/shared/api/Search'
|
2022-04-10 23:37:36 +08:00
|
|
|
import dayjs from 'dayjs'
|
2022-05-12 02:45:43 +08:00
|
|
|
import { useMemo, useCallback } from 'react'
|
|
|
|
import toast from 'react-hot-toast'
|
2022-08-03 23:48:39 +08:00
|
|
|
import { useQuery } from '@tanstack/react-query'
|
2022-05-12 02:45:43 +08:00
|
|
|
import { useNavigate, useParams } from 'react-router-dom'
|
2022-03-29 00:11:05 +08:00
|
|
|
|
|
|
|
const Artists = ({ artists }: { artists: Artist[] }) => {
|
|
|
|
const navigate = useNavigate()
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{artists.map(artist => (
|
|
|
|
<div
|
|
|
|
onClick={() => navigate(`/artist/${artist.id}`)}
|
|
|
|
key={artist.id}
|
|
|
|
className='btn-hover-animation flex items-center p-2.5 after:rounded-xl after:bg-gray-100 dark:after:bg-white/10'
|
|
|
|
>
|
|
|
|
<div className='mr-4 h-14 w-14'>
|
|
|
|
<Cover
|
|
|
|
imageUrl={resizeImage(artist.img1v1Url, 'xs')}
|
|
|
|
roundedClass='rounded-full'
|
|
|
|
showHover={false}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div className='text-lg font-semibold dark:text-white'>
|
|
|
|
{artist.name}
|
|
|
|
</div>
|
|
|
|
<div className='mt-0.5 text-sm font-medium text-gray-500 dark:text-gray-400'>
|
|
|
|
艺人
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const Albums = ({ albums }: { albums: Album[] }) => {
|
|
|
|
const navigate = useNavigate()
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{albums.map(album => (
|
|
|
|
<div
|
|
|
|
onClick={() => navigate(`/album/${album.id}`)}
|
|
|
|
key={album.id}
|
|
|
|
className='btn-hover-animation flex items-center p-2.5 after:rounded-xl after:bg-gray-100 dark:after:bg-white/10'
|
|
|
|
>
|
|
|
|
<div className='mr-4 h-14 w-14'>
|
|
|
|
<Cover
|
|
|
|
imageUrl={resizeImage(album.picUrl, 'xs')}
|
|
|
|
roundedClass='rounded-lg'
|
|
|
|
showHover={false}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div className='text-lg font-semibold dark:text-white'>
|
|
|
|
{album.name}
|
|
|
|
</div>
|
|
|
|
<div className='mt-0.5 text-sm font-medium text-gray-500 dark:text-gray-400'>
|
2022-04-10 23:37:36 +08:00
|
|
|
专辑 · {album?.artist.name} · {dayjs(album.publishTime).year()}
|
2022-03-29 00:11:05 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-27 15:21:48 +08:00
|
|
|
const Search = () => {
|
2022-04-16 22:39:51 +08:00
|
|
|
const { keywords = '', type = 'all' } = useParams()
|
2022-03-29 00:11:05 +08:00
|
|
|
|
|
|
|
const searchType: keyof typeof SearchTypes =
|
|
|
|
type.toUpperCase() in SearchTypes
|
|
|
|
? (type.toUpperCase() as keyof typeof SearchTypes)
|
2022-04-16 22:39:51 +08:00
|
|
|
: 'All'
|
2022-03-29 00:11:05 +08:00
|
|
|
|
|
|
|
const { data: bestMatchRaw, isLoading: isLoadingBestMatch } = useQuery(
|
2022-04-16 22:39:51 +08:00
|
|
|
[SearchApiNames.MultiMatchSearch, keywords],
|
2022-03-29 00:11:05 +08:00
|
|
|
() => multiMatchSearch({ keywords })
|
|
|
|
)
|
|
|
|
|
|
|
|
const bestMatch = useMemo(() => {
|
|
|
|
if (!bestMatchRaw?.result) return []
|
|
|
|
return bestMatchRaw.result.orders
|
|
|
|
.filter(order => ['album', 'artist'].includes(order)) // 暂时只支持专辑和艺人
|
|
|
|
.map(order => {
|
|
|
|
return bestMatchRaw.result[order][0]
|
|
|
|
})
|
|
|
|
.slice(0, 2)
|
|
|
|
}, [bestMatchRaw?.result])
|
|
|
|
|
|
|
|
const { data: searchResult, isLoading: isLoadingSearchResult } = useQuery(
|
2022-04-16 22:39:51 +08:00
|
|
|
[SearchApiNames.Search, keywords, searchType],
|
2022-03-29 00:11:05 +08:00
|
|
|
() => search({ keywords, type: searchType })
|
|
|
|
)
|
|
|
|
|
2022-04-02 18:46:08 +08:00
|
|
|
const handlePlayTracks = useCallback(
|
|
|
|
(trackID: number | null = null) => {
|
2022-04-30 02:08:25 +08:00
|
|
|
const tracks = searchResult?.result?.song?.songs
|
2022-04-02 18:46:08 +08:00
|
|
|
if (!tracks?.length) {
|
|
|
|
toast('无法播放歌单')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
player.playAList(
|
|
|
|
tracks.map(t => t.id),
|
|
|
|
trackID
|
|
|
|
)
|
|
|
|
},
|
2022-04-30 02:08:25 +08:00
|
|
|
[searchResult?.result?.song?.songs]
|
2022-04-02 18:46:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const navigate = useNavigate()
|
|
|
|
const navigateBestMatch = (match: Artist | Album) => {
|
|
|
|
if ((match as Artist).albumSize !== undefined) {
|
|
|
|
navigate(`/artist/${match.id}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ((match as Album).artist !== undefined) {
|
|
|
|
navigate(`/album/${match.id}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:11:05 +08:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className='mt-6 mb-8 text-4xl font-semibold dark:text-white'>
|
|
|
|
<span className='text-gray-500'>搜索</span> "{keywords}"
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{/* Best match */}
|
|
|
|
{bestMatch.length !== 0 && (
|
|
|
|
<div className='mb-6'>
|
|
|
|
<div className='mb-2 text-sm font-medium text-gray-400'>最佳匹配</div>
|
|
|
|
<div className='grid grid-cols-2'>
|
|
|
|
{bestMatch.map(match => (
|
|
|
|
<div
|
2022-04-02 18:46:08 +08:00
|
|
|
onClick={() => navigateBestMatch(match)}
|
2022-03-29 00:11:05 +08:00
|
|
|
key={`${match.id}${match.picUrl}`}
|
|
|
|
className='btn-hover-animation flex items-center p-3 after:rounded-xl after:bg-gray-100 dark:after:bg-white/10'
|
|
|
|
>
|
|
|
|
<div className='mr-6 h-24 w-24'>
|
|
|
|
<Cover
|
|
|
|
imageUrl={resizeImage(match.picUrl, 'xs')}
|
|
|
|
showHover={false}
|
|
|
|
roundedClass='rounded-full'
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div className='text-xl font-semibold dark:text-white'>
|
|
|
|
{match.name}
|
|
|
|
</div>
|
|
|
|
<div className='mt-0.5 font-medium text-gray-500 dark:text-gray-400'>
|
|
|
|
{(match as Artist).occupation === '歌手' ? '艺人' : '专辑'}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{/* Search result */}
|
|
|
|
<div className='grid grid-cols-2 gap-6'>
|
2022-04-30 02:08:25 +08:00
|
|
|
{searchResult?.result?.artist?.artists && (
|
2022-03-29 00:11:05 +08:00
|
|
|
<div>
|
|
|
|
<div className='mb-2 text-sm font-medium text-gray-400'>艺人</div>
|
|
|
|
<Artists artists={searchResult.result.artist.artists} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2022-04-30 02:08:25 +08:00
|
|
|
{searchResult?.result?.album?.albums && (
|
2022-03-29 00:11:05 +08:00
|
|
|
<div>
|
|
|
|
<div className='mb-2 text-sm font-medium text-gray-400'>专辑</div>
|
|
|
|
<Albums albums={searchResult.result.album.albums} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2022-04-30 02:08:25 +08:00
|
|
|
{searchResult?.result?.song?.songs && (
|
2022-03-29 00:11:05 +08:00
|
|
|
<div className='col-span-2'>
|
|
|
|
<div className='mb-2 text-sm font-medium text-gray-400'>歌曲</div>
|
2022-04-02 18:46:08 +08:00
|
|
|
<TrackGrid
|
|
|
|
tracks={searchResult.result.song.songs}
|
|
|
|
cols={3}
|
|
|
|
onTrackDoubleClick={handlePlayTracks}
|
|
|
|
/>
|
2022-03-29 00:11:05 +08:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
2022-03-27 15:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Search
|