92 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-08-22 16:51:23 +08:00
import useUserArtists, {
useMutationLikeAArtist,
} from '@/web/api/hooks/useUserArtists'
import contextMenus, { closeContextMenu } from '@/web/states/contextMenus'
import { AnimatePresence } from 'framer-motion'
import { useMemo, useState } from 'react'
import toast from 'react-hot-toast'
2022-10-28 20:29:04 +08:00
import { useTranslation } from 'react-i18next'
2022-08-22 16:51:23 +08:00
import { useCopyToClipboard } from 'react-use'
import { useSnapshot } from 'valtio'
import BasicContextMenu from './BasicContextMenu'
const ArtistContextMenu = () => {
2022-10-28 20:29:04 +08:00
const { t } = useTranslation()
2022-08-22 16:51:23 +08:00
const { cursorPosition, type, dataSourceID, target, options } =
useSnapshot(contextMenus)
const likeAArtist = useMutationLikeAArtist()
const [, copyToClipboard] = useCopyToClipboard()
const { data: likedArtists } = useUserArtists()
const followLabel = useMemo(() => {
return likedArtists?.data?.find(a => a.id === Number(dataSourceID))
2022-10-28 20:29:04 +08:00
? t`context-menu.unfollow`
: t`context-menu.follow`
}, [dataSourceID, likedArtists?.data, t])
2022-08-22 16:51:23 +08:00
return (
<AnimatePresence>
{cursorPosition && type === 'artist' && dataSourceID && target && (
<BasicContextMenu
target={target}
cursorPosition={cursorPosition}
onClose={closeContextMenu}
options={options}
items={[
{
type: 'item',
label: followLabel,
onClick: () => {
if (type !== 'artist' || !dataSourceID) {
return
}
likeAArtist.mutateAsync(Number(dataSourceID)).then(res => {
if (res?.code === 200) {
toast.success(
2022-10-28 20:29:04 +08:00
followLabel === t`context-menu.unfollow`
? t`context-menu.unfollowed`
: t`context-menu.followed`
2022-08-22 16:51:23 +08:00
)
}
})
},
},
{
type: 'divider',
},
{
type: 'submenu',
2022-10-28 20:29:04 +08:00
label: t`context-menu.share`,
2022-08-22 16:51:23 +08:00
items: [
{
type: 'item',
2022-10-28 20:29:04 +08:00
label: t`context-menu.copy-netease-link`,
2022-08-22 16:51:23 +08:00
onClick: () => {
copyToClipboard(
`https://music.163.com/#/artist?id=${dataSourceID}`
)
2022-10-28 20:29:04 +08:00
toast.success(t`toasts.copied`)
2022-08-22 16:51:23 +08:00
},
},
{
type: 'item',
label: 'Copy YPM Link',
onClick: () => {
copyToClipboard(
`${window.location.origin}/artist/${dataSourceID}`
)
2022-10-28 20:29:04 +08:00
toast.success(t`toasts.copied`)
2022-08-22 16:51:23 +08:00
},
},
],
},
]}
/>
)}
</AnimatePresence>
)
}
export default ArtistContextMenu