import { multiMatchSearch, search, SearchApiNames, SearchTypes, } from '@/renderer/api/search' import Cover from '@/renderer/components/Cover' import TrackGrid from '@/renderer/components/TracksGrid' import { player } from '@/renderer/store' import { resizeImage } from '@/renderer/utils/common' import dayjs from 'dayjs' 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.MULTI_MATCH_SEARCH, 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