import { multiMatchSearch, search } from '@/web/api/search' import Cover from '@/web/components/Cover' import TrackGrid from '@/web/components/TracksGrid' import { player } from '@/web/store' import { resizeImage } from '@/web/utils/common' import { SearchTypes, SearchApiNames } from '@/shared/api/Search' import dayjs from 'dayjs' import { useMemo, useCallback } from 'react' import toast from 'react-hot-toast' import { useQuery } from 'react-query' import { useNavigate, useParams } from 'react-router-dom' const Artists = ({ artists }: { artists: Artist[] }) => { const navigate = useNavigate() return ( <> {artists.map(artist => (
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' >
{artist.name}
艺人
))} ) } const Albums = ({ albums }: { albums: Album[] }) => { const navigate = useNavigate() return ( <> {albums.map(album => (
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' >
{album.name}
专辑 · {album?.artist.name} · {dayjs(album.publishTime).year()}
))} ) } const Search = () => { const { keywords = '', type = 'all' } = useParams() const searchType: keyof typeof SearchTypes = type.toUpperCase() in SearchTypes ? (type.toUpperCase() as keyof typeof SearchTypes) : 'All' const { data: bestMatchRaw, isLoading: isLoadingBestMatch } = useQuery( [SearchApiNames.MultiMatchSearch, keywords], () => 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( [SearchApiNames.Search, keywords, searchType], () => search({ keywords, type: searchType }) ) const handlePlayTracks = useCallback( (trackID: number | null = null) => { const tracks = searchResult?.result?.song?.songs if (!tracks?.length) { toast('无法播放歌单') return } player.playAList( tracks.map(t => t.id), trackID ) }, [searchResult?.result?.song?.songs] ) 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 } } return (
搜索 "{keywords}"
{/* Best match */} {bestMatch.length !== 0 && (
最佳匹配
{bestMatch.map(match => (
navigateBestMatch(match)} 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' >
{match.name}
{(match as Artist).occupation === '歌手' ? '艺人' : '专辑'}
))}
)} {/* Search result */}
{searchResult?.result?.artist?.artists && (
艺人
)} {searchResult?.result?.album?.albums && (
专辑
)} {searchResult?.result?.song?.songs && (
歌曲
)}
) } export default Search