import { multiMatchSearch, search } from '@/web/api/search'
import player from '@/web/states/player'
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 '@tanstack/react-query'
import { useNavigate, useParams } from 'react-router-dom'
import Image from '@/web/components/Image'
import { cx } from '@emotion/css'
const Artists = ({ artists }: { artists: Artist[] }) => {
const navigate = useNavigate()
return (
<>
{artists.map(artist => (
navigate(`/artist/${artist.id}`)}
key={artist.id}
className='flex items-center py-2.5'
>
))}
>
)
}
const Albums = ({ albums }: { albums: Album[] }) => {
const navigate = useNavigate()
return (
<>
{albums.map(album => (
navigate(`/album/${album.id}`)}
key={album.id}
className='flex items-center py-2.5 text-neutral-200'
>
{album.name}
专辑 · {album?.artist.name} · {dayjs(album.publishTime).year()}
))}
>
)
}
const Track = ({
track,
isPlaying,
onPlay,
}: {
track?: Track
isPlaying?: boolean
onPlay: (id: number) => void
}) => {
return (
{
if (e.detail === 2 && track?.id) onPlay(track.id)
}}
>
{/* Cover */}
{/* Track info */}
{track?.name}
{track?.ar.map(a => a.name).join(', ')}
)
}
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 && (
{/* mx-2.5 mb-6 text-12 font-medium uppercase dark:text-neutral-300 lg:mx-0 lg:text-14
lg:font-bold */}
最佳匹配
{bestMatch.map(match => (
navigateBestMatch(match)}
key={`${match.id}${match.picUrl}`}
className='btn-hover-animation flex items-center py-3 after:rounded-xl after:bg-gray-100 dark:after:bg-white/10'
>
{match.name}
{(match as Artist).occupation === '歌手'
? '艺人'
: `专辑 · ${(match as Album).artist.name} · ${dayjs(
match.publishTime
).year()}`}
))}
)}
{/* Search result */}
{searchResult?.result?.artist?.artists && (
)}
{searchResult?.result?.album?.albums && (
)}
{searchResult?.result?.song?.songs && (
歌曲
{searchResult.result.song.songs.map(track => (
))}
)}
)
}
export default Search