feat: sharing context menus, sharing songs
This commit is contained in:
13 files changed
+294
-124
No files matched your search
@@ -7,20 +7,33 @@ import { Metadata } from "next"
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ id: string }>
|
||||
searchParams: Promise<{ s?: string }>
|
||||
}
|
||||
|
||||
export async function generateMetadata({
|
||||
params,
|
||||
searchParams,
|
||||
}: PageProps): Promise<Metadata> {
|
||||
const { id } = await params
|
||||
const { s: songId } = await searchParams
|
||||
const album = await fetchQuery(api.library.browseAlbumDetail, {
|
||||
id: id as Id<"albums">,
|
||||
})
|
||||
|
||||
const sharedTrack =
|
||||
album && songId
|
||||
? album.tracks.find((track) => track.id === songId)
|
||||
: undefined
|
||||
const pageTitle = sharedTrack
|
||||
? `${sharedTrack.title} - ${album!.title}`
|
||||
: album
|
||||
? album.title
|
||||
: null
|
||||
|
||||
return {
|
||||
title: `${album ? `${album.title} - oTunes` : "oTunes"}`,
|
||||
openGraph: {
|
||||
title: `${album ? `${album.title} - oTunes` : "oTunes"}`,
|
||||
title: `${pageTitle ? `${pageTitle} - oTunes` : "oTunes"}`,
|
||||
description: `${album ? album.artist.name : null}`,
|
||||
images: album
|
||||
? { url: `/api/album/${album.id}/cover`, width: 512, height: 512 }
|
||||
@@ -29,8 +42,9 @@ export async function generateMetadata({
|
||||
}
|
||||
}
|
||||
|
||||
export default async function Page({ params }: PageProps) {
|
||||
export default async function Page({ params, searchParams }: PageProps) {
|
||||
const { id } = await params
|
||||
const { s: highlightedSongId } = await searchParams
|
||||
const album = await fetchQuery(api.library.browseAlbumDetail, {
|
||||
id: id as Id<"albums">,
|
||||
})
|
||||
@@ -53,5 +67,5 @@ export default async function Page({ params }: PageProps) {
|
||||
)
|
||||
}
|
||||
|
||||
return <AlbumView album={album} />
|
||||
return <AlbumView album={album} highlightedSongId={highlightedSongId} />
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { LibraryLoading } from "@/components/library-loading"
|
||||
import { PlaylistView } from "@/components/playlist-view"
|
||||
import { api } from "@/convex/_generated/api"
|
||||
import { convexAuthNextjsToken } from "@convex-dev/auth/nextjs/server"
|
||||
import { fetchQuery } from "convex/nextjs"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import { Metadata } from "next"
|
||||
@@ -9,13 +10,20 @@ interface PageProps {
|
||||
params: Promise<{ id: string }>
|
||||
}
|
||||
|
||||
async function fetchPlaylistDetail(id: Id<"playlists">) {
|
||||
const token = await convexAuthNextjsToken()
|
||||
return fetchQuery(
|
||||
api.playlists.browsePlaylistDetail,
|
||||
{ id },
|
||||
{ token }
|
||||
)
|
||||
}
|
||||
|
||||
export async function generateMetadata({
|
||||
params,
|
||||
}: PageProps): Promise<Metadata> {
|
||||
const { id } = await params
|
||||
const playlist = await fetchQuery(api.playlists.browsePlaylistDetail, {
|
||||
id: id as Id<"playlists">,
|
||||
})
|
||||
const playlist = await fetchPlaylistDetail(id as Id<"playlists">)
|
||||
|
||||
return {
|
||||
title: `${playlist ? `${playlist.title} - oTunes` : "oTunes"}`,
|
||||
@@ -31,9 +39,7 @@ export async function generateMetadata({
|
||||
|
||||
export default async function Page({ params }: PageProps) {
|
||||
const { id } = await params
|
||||
const playlist = await fetchQuery(api.playlists.browsePlaylistDetail, {
|
||||
id: id as Id<"playlists">,
|
||||
})
|
||||
const playlist = await fetchPlaylistDetail(id as Id<"playlists">)
|
||||
|
||||
if (playlist === undefined) {
|
||||
return (
|
||||
|
||||
@@ -149,6 +149,7 @@ export function AlbumCard({ album }: { album: BrowseAlbum }) {
|
||||
onAction={(action) => void handleCardAction(action)}
|
||||
favorited={isAlbumFavorite(album.id)}
|
||||
navigation={{ artist: { id: album.artist.id } }}
|
||||
share={{ type: "album", albumId: album.id }}
|
||||
/>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
|
||||
+47
-33
@@ -12,7 +12,7 @@ import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
|
||||
import { FavoriteMediaButton } from "@/components/favorite-media-button"
|
||||
import { useFavorites } from "@/components/favorites-provider"
|
||||
import { usePlayback } from "@/components/playback-provider"
|
||||
import { toast } from "sonner"
|
||||
import { copyAlbumLink } from "@/lib/media-links"
|
||||
|
||||
import {
|
||||
MediaContextMenu,
|
||||
@@ -46,15 +46,6 @@ function formatDuration(seconds: number): string {
|
||||
return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`
|
||||
}
|
||||
|
||||
function albumUrl(albumId: string) {
|
||||
return `${window.location.origin}/album/${albumId}`
|
||||
}
|
||||
|
||||
async function copyAlbumLink(albumId: string) {
|
||||
await navigator.clipboard.writeText(albumUrl(albumId))
|
||||
toast.success("Link Copied")
|
||||
}
|
||||
|
||||
function formatMetadata(genre: string | null, year: number | null): string | null {
|
||||
const parts: string[] = []
|
||||
if (genre) {
|
||||
@@ -120,10 +111,12 @@ function TrackRow({
|
||||
onMediaAction,
|
||||
isNowPlaying,
|
||||
onPlay,
|
||||
albumId,
|
||||
}: {
|
||||
track: BrowseAlbumTrack
|
||||
sectionIndex: number
|
||||
artist: BrowseAlbumDetail["artist"]
|
||||
albumId: string
|
||||
isActive: boolean
|
||||
isSelected: boolean
|
||||
selectionPosition: SelectionRangePosition
|
||||
@@ -241,6 +234,12 @@ function TrackRow({
|
||||
favorited={isFavorite}
|
||||
navigation={{
|
||||
artist: { id: artist.id },
|
||||
album: { id: albumId },
|
||||
}}
|
||||
share={{
|
||||
type: "song",
|
||||
albumId,
|
||||
songId: track.id,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
@@ -248,7 +247,13 @@ function TrackRow({
|
||||
)
|
||||
}
|
||||
|
||||
export function AlbumView({ album }: { album: BrowseAlbumDetail }) {
|
||||
export function AlbumView({
|
||||
album,
|
||||
highlightedSongId,
|
||||
}: {
|
||||
album: BrowseAlbumDetail
|
||||
highlightedSongId?: string
|
||||
}) {
|
||||
const { openAddToPlaylist } = useAddToPlaylist()
|
||||
const {
|
||||
isSongFavorite,
|
||||
@@ -266,6 +271,14 @@ export function AlbumView({ album }: { album: BrowseAlbumDetail }) {
|
||||
)
|
||||
const metadata = formatMetadata(album.genre, album.year)
|
||||
const [activeTrackId, setActiveTrackId] = React.useState("")
|
||||
const validHighlightedSongId = React.useMemo(() => {
|
||||
if (!highlightedSongId) {
|
||||
return undefined
|
||||
}
|
||||
return album.tracks.some((track) => track.id === highlightedSongId)
|
||||
? highlightedSongId
|
||||
: undefined
|
||||
}, [album.tracks, highlightedSongId])
|
||||
|
||||
const {
|
||||
selectedIds,
|
||||
@@ -275,7 +288,7 @@ export function AlbumView({ album }: { album: BrowseAlbumDetail }) {
|
||||
handlePointerDown,
|
||||
prepareContextMenu,
|
||||
clearSelection,
|
||||
} = useTrackSelection(album.tracks)
|
||||
} = useTrackSelection(album.tracks, validHighlightedSongId)
|
||||
|
||||
const selectionPositions = React.useMemo(
|
||||
() => getSelectionRangePositions(album.tracks.map((track) => track.id), selectedIds),
|
||||
@@ -348,28 +361,8 @@ export function AlbumView({ album }: { album: BrowseAlbumDetail }) {
|
||||
<div className="flex h-svh flex-col">
|
||||
<ScrollArea className="min-h-0 flex-1">
|
||||
<div className="pb-28">
|
||||
<div className="flex items-center justify-end gap-1 px-6 pt-4">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
aria-label="Share album"
|
||||
onClick={() => copyAlbumLink(album.id)}
|
||||
>
|
||||
<Share2 />
|
||||
</Button>
|
||||
<MediaContextMenu
|
||||
label="More album options"
|
||||
selectedCount={album.tracks.length}
|
||||
onOpenMenu={() => {}}
|
||||
onAction={handleAlbumMediaAction}
|
||||
favorited={isAlbumFavorite(album.id)}
|
||||
trackRowTrigger={false}
|
||||
triggerClassName="size-8"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="mx-auto w-full min-w-0 max-w-9/10">
|
||||
<div className="flex flex-col gap-8 pt-2 pb-8 sm:flex-row sm:items-end">
|
||||
<div className="flex flex-col gap-8 pt-6 pb-8 sm:flex-row sm:items-end">
|
||||
<div className="relative mx-auto aspect-square w-full max-w-60 shrink-0 overflow-hidden rounded-lg ring-1 ring-foreground/10 sm:mx-0">
|
||||
<Image
|
||||
src={albumCoverUrl(album.id)}
|
||||
@@ -426,6 +419,26 @@ export function AlbumView({ album }: { album: BrowseAlbumDetail }) {
|
||||
favoritedLabel={`Remove ${album.title} from favorites`}
|
||||
unfavoritedLabel={`Favorite ${album.title}`}
|
||||
/>
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="icon"
|
||||
className="rounded-full"
|
||||
aria-label="Share album"
|
||||
onClick={() => copyAlbumLink(album.id)}
|
||||
>
|
||||
<Share2 />
|
||||
</Button>
|
||||
<MediaContextMenu
|
||||
label="More album options"
|
||||
selectedCount={album.tracks.length}
|
||||
onOpenMenu={() => {}}
|
||||
onAction={handleAlbumMediaAction}
|
||||
favorited={isAlbumFavorite(album.id)}
|
||||
trackRowTrigger={false}
|
||||
triggerVariant="secondary"
|
||||
triggerClassName="rounded-full"
|
||||
share={{ type: "album", albumId: album.id }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -455,6 +468,7 @@ export function AlbumView({ album }: { album: BrowseAlbumDetail }) {
|
||||
track={track}
|
||||
sectionIndex={sectionTrackIndex}
|
||||
artist={album.artist}
|
||||
albumId={album.id}
|
||||
isActive={track.id === activeTrackId}
|
||||
isSelected={isSelected(track.id)}
|
||||
selectionPosition={selectionPositions[listIndex]}
|
||||
|
||||
@@ -126,6 +126,7 @@ export function ArtistView({
|
||||
triggerVariant="secondary"
|
||||
triggerClassName="size-11 rounded-full"
|
||||
triggerIconClassName="size-4"
|
||||
share={{ type: "artist", artistId: artist.id }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -81,6 +81,7 @@ function ArtistListItem({
|
||||
onOpenMenu={() => {}}
|
||||
onAction={onMediaAction}
|
||||
favorited={isFavorite}
|
||||
share={{ type: "artist", artistId: artist.id }}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
@@ -225,6 +226,7 @@ export function ArtistsView({
|
||||
trackRowTrigger={false}
|
||||
triggerVariant="secondary"
|
||||
triggerClassName="rounded-full"
|
||||
share={{ type: "artist", artistId: selectedArtist.id }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
ListPlus,
|
||||
ListStart,
|
||||
MoreHorizontal,
|
||||
Share2,
|
||||
Shuffle,
|
||||
Star,
|
||||
User,
|
||||
@@ -21,6 +22,10 @@ import {
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
import {
|
||||
copyMediaShareLink,
|
||||
type MediaShareContext,
|
||||
} from "@/lib/media-links"
|
||||
import { cn } from "@/lib/utils"
|
||||
|
||||
export type MediaContextAction =
|
||||
@@ -35,6 +40,21 @@ export type MediaContextNavigation = {
|
||||
album?: { id: string }
|
||||
}
|
||||
|
||||
export type { MediaShareContext } from "@/lib/media-links"
|
||||
|
||||
function shouldShowShare(
|
||||
share: MediaShareContext | undefined,
|
||||
selectedCount: number
|
||||
) {
|
||||
if (!share) {
|
||||
return false
|
||||
}
|
||||
if (share.type === "song") {
|
||||
return selectedCount === 1
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function actionLabel(base: string, selectedCount: number) {
|
||||
return selectedCount > 1 ? `${base} (${selectedCount})` : base
|
||||
}
|
||||
@@ -46,6 +66,7 @@ export function MediaContextMenuItems({
|
||||
showFavorite = true,
|
||||
favorited = false,
|
||||
navigation,
|
||||
share,
|
||||
}: {
|
||||
selectedCount: number
|
||||
onAction: (action: MediaContextAction) => void
|
||||
@@ -53,11 +74,25 @@ export function MediaContextMenuItems({
|
||||
showFavorite?: boolean
|
||||
favorited?: boolean
|
||||
navigation?: MediaContextNavigation
|
||||
share?: MediaShareContext
|
||||
}) {
|
||||
const { artist, album } = navigation ?? {}
|
||||
const showShare = shouldShowShare(share, selectedCount)
|
||||
|
||||
return (
|
||||
<>
|
||||
{showShare ? (
|
||||
<>
|
||||
<DropdownMenuItem
|
||||
className="justify-between"
|
||||
onClick={() => void copyMediaShareLink(share!)}
|
||||
>
|
||||
Share
|
||||
<Share2 />
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuSeparator />
|
||||
</>
|
||||
) : null}
|
||||
<DropdownMenuItem
|
||||
className="justify-between"
|
||||
onClick={() => onAction("playlist")}
|
||||
@@ -159,6 +194,7 @@ export function MediaContextMenu({
|
||||
showFavorite = true,
|
||||
favorited = false,
|
||||
navigation,
|
||||
share,
|
||||
inSelectionRange = false,
|
||||
trackRowTrigger = true,
|
||||
triggerClassName,
|
||||
@@ -175,6 +211,7 @@ export function MediaContextMenu({
|
||||
showFavorite?: boolean
|
||||
favorited?: boolean
|
||||
navigation?: MediaContextNavigation
|
||||
share?: MediaShareContext
|
||||
inSelectionRange?: boolean
|
||||
/** When false, the trigger uses default button/dropdown styling (page headers). */
|
||||
trackRowTrigger?: boolean
|
||||
@@ -215,6 +252,7 @@ export function MediaContextMenu({
|
||||
showFavorite={showFavorite}
|
||||
favorited={favorited}
|
||||
navigation={navigation}
|
||||
share={share}
|
||||
/>
|
||||
{children}
|
||||
</DropdownMenuContent>
|
||||
|
||||
@@ -94,6 +94,7 @@ export function PlaylistCard({ playlist }: { playlist: PlaylistSummary }) {
|
||||
selectedCount={1}
|
||||
showFavorite={false}
|
||||
onAction={(action) => void handleCardAction(action)}
|
||||
share={{ type: "playlist", playlistId: playlist.id }}
|
||||
/>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
|
||||
@@ -3,15 +3,8 @@
|
||||
import * as React from "react"
|
||||
import Image from "next/image"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useMutation } from "convex/react"
|
||||
import {
|
||||
MoreHorizontal,
|
||||
Play,
|
||||
Share2,
|
||||
Shuffle,
|
||||
Star,
|
||||
Trash2,
|
||||
} from "lucide-react"
|
||||
import { useMutation, useQuery } from "convex/react"
|
||||
import { Play, Share2, Shuffle, Star, Trash2 } from "lucide-react"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
|
||||
@@ -21,16 +14,12 @@ import { MediaCollectionFooter } from "@/components/media-collection-footer"
|
||||
import { TrackNumberPlayButton } from "@/components/track-number-play-button"
|
||||
import {
|
||||
MediaContextMenu,
|
||||
MediaContextMenuItems,
|
||||
type MediaContextAction,
|
||||
} from "@/components/media-context-menu"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
import { ScrollArea } from "@/components/ui/scroll-area"
|
||||
import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
|
||||
@@ -39,6 +28,7 @@ import { api } from "@/convex/_generated/api"
|
||||
import type { Id } from "@/convex/_generated/dataModel"
|
||||
import type { BrowsePlaylistDetail, BrowseSong } from "@/convex/library"
|
||||
import { albumCoverUrl, playlistCoverUrl } from "@/lib/cover-urls"
|
||||
import { copyPlaylistLink } from "@/lib/media-links"
|
||||
import { sumDurationSeconds } from "@/lib/media-summary"
|
||||
import { cn } from "@/lib/utils"
|
||||
import {
|
||||
@@ -61,18 +51,6 @@ function formatDuration(seconds: number): string {
|
||||
return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`
|
||||
}
|
||||
|
||||
function playlistUrl(playlist: BrowsePlaylistDetail) {
|
||||
if (playlist.isFavorites) {
|
||||
return `${window.location.origin}/playlist/favorites`
|
||||
}
|
||||
return `${window.location.origin}/playlist/${playlist.id}`
|
||||
}
|
||||
|
||||
async function copyPlaylistLink(playlist: BrowsePlaylistDetail) {
|
||||
await navigator.clipboard.writeText(playlistUrl(playlist))
|
||||
toast.success("Link Copied")
|
||||
}
|
||||
|
||||
function PlaylistSongCover({
|
||||
song,
|
||||
inRange,
|
||||
@@ -259,6 +237,11 @@ function PlaylistSongRow({
|
||||
artist: { id: song.artist.id },
|
||||
album: { id: song.album.id },
|
||||
}}
|
||||
share={{
|
||||
type: "song",
|
||||
albumId: song.album.id,
|
||||
songId: song.id,
|
||||
}}
|
||||
>
|
||||
{canRemove ? (
|
||||
<>
|
||||
@@ -282,6 +265,11 @@ function PlaylistSongRow({
|
||||
export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
const router = useRouter()
|
||||
const isFavorites = playlist.isFavorites === true
|
||||
const currentUser = useQuery(api.auth.currentUser)
|
||||
const isOwner =
|
||||
currentUser === undefined
|
||||
? playlist.isOwner
|
||||
: currentUser?._id === playlist.owner.id
|
||||
const { openAddToPlaylist } = useAddToPlaylist()
|
||||
const {
|
||||
isSongFavorite,
|
||||
@@ -427,7 +415,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
}
|
||||
|
||||
function handleDrop(targetIndex: number) {
|
||||
if (isFavorites || !playlist.isOwner || dragIndex === null || dragIndex === targetIndex) {
|
||||
if (isFavorites || !isOwner || dragIndex === null || dragIndex === targetIndex) {
|
||||
setDragIndex(null)
|
||||
return
|
||||
}
|
||||
@@ -479,54 +467,8 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
<div className="flex h-svh flex-col">
|
||||
<ScrollArea className="min-h-0 flex-1">
|
||||
<div className="pb-28">
|
||||
<div className="flex items-center justify-end gap-1 px-6 pt-4">
|
||||
{!isFavorites ? (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
aria-label="Share playlist"
|
||||
onClick={() => copyPlaylistLink(playlist)}
|
||||
>
|
||||
<Share2 />
|
||||
</Button>
|
||||
) : null}
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="size-8"
|
||||
aria-label="More playlist options"
|
||||
>
|
||||
<MoreHorizontal />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end" className="w-48">
|
||||
<MediaContextMenuItems
|
||||
selectedCount={songs.length}
|
||||
onAction={handlePlaylistMediaAction}
|
||||
playlistLabel={ADD_TO_ANOTHER_PLAYLIST}
|
||||
showFavorite={!isFavorites}
|
||||
/>
|
||||
{playlist.isOwner && !isFavorites ? (
|
||||
<>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
variant="destructive"
|
||||
className="justify-between"
|
||||
onClick={handleDeletePlaylist}
|
||||
>
|
||||
Delete Playlist
|
||||
<Trash2 />
|
||||
</DropdownMenuItem>
|
||||
</>
|
||||
) : null}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
|
||||
<div className="mx-auto w-full min-w-0 max-w-9/10">
|
||||
<div className="flex flex-col gap-8 px-6 pt-2 pb-8 sm:flex-row sm:items-end">
|
||||
<div className="flex flex-col gap-8 px-6 pt-6 pb-8 sm:flex-row sm:items-end">
|
||||
<div className="relative mx-auto aspect-square w-full max-w-60 shrink-0 overflow-hidden rounded-lg ring-1 ring-foreground/10 sm:mx-0">
|
||||
{isFavorites ? (
|
||||
<div className="flex size-full items-center justify-center bg-muted">
|
||||
@@ -581,7 +523,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
<Play className="fill-current" />
|
||||
Play
|
||||
</Button>
|
||||
{!playlist.isOwner ? (
|
||||
{!isOwner ? (
|
||||
<Button
|
||||
variant={isSubscribed ? "secondary" : "default"}
|
||||
className="h-10 rounded-full px-6"
|
||||
@@ -590,6 +532,51 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
{isSubscribed ? "Subscribed" : "Subscribe"}
|
||||
</Button>
|
||||
) : null}
|
||||
{!isFavorites ? (
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="icon"
|
||||
className="rounded-full"
|
||||
aria-label="Share playlist"
|
||||
onClick={() =>
|
||||
void copyPlaylistLink(playlist.id!, {
|
||||
isFavorites,
|
||||
})
|
||||
}
|
||||
>
|
||||
<Share2 />
|
||||
</Button>
|
||||
) : null}
|
||||
<MediaContextMenu
|
||||
label="More playlist options"
|
||||
selectedCount={songs.length}
|
||||
onOpenMenu={() => {}}
|
||||
onAction={handlePlaylistMediaAction}
|
||||
playlistLabel={ADD_TO_ANOTHER_PLAYLIST}
|
||||
showFavorite={!isFavorites}
|
||||
trackRowTrigger={false}
|
||||
triggerVariant="secondary"
|
||||
triggerClassName="rounded-full"
|
||||
share={{
|
||||
type: "playlist",
|
||||
playlistId: playlist.id ?? "favorites",
|
||||
isFavorites,
|
||||
}}
|
||||
>
|
||||
{isOwner && !isFavorites ? (
|
||||
<>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
variant="destructive"
|
||||
className="justify-between"
|
||||
onClick={handleDeletePlaylist}
|
||||
>
|
||||
Delete Playlist
|
||||
<Trash2 />
|
||||
</DropdownMenuItem>
|
||||
</>
|
||||
) : null}
|
||||
</MediaContextMenu>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -600,8 +587,8 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
key={song.id}
|
||||
song={song}
|
||||
index={index}
|
||||
isOwner={playlist.isOwner && !isFavorites}
|
||||
canRemove={isFavorites || playlist.isOwner}
|
||||
isOwner={isOwner && !isFavorites}
|
||||
canRemove={isFavorites || isOwner}
|
||||
showFavoriteInMenu={!isFavorites}
|
||||
removeLabel={
|
||||
isFavorites ? "Unfavorite" : "Remove from Playlist"
|
||||
@@ -633,14 +620,14 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
onMediaAction={handleMediaAction}
|
||||
onRemove={() => handleRemoveSong(song.id)}
|
||||
onDragStart={(event) => {
|
||||
if (isFavorites || !playlist.isOwner) {
|
||||
if (isFavorites || !isOwner) {
|
||||
return
|
||||
}
|
||||
setDragIndex(index)
|
||||
event.dataTransfer.effectAllowed = "move"
|
||||
}}
|
||||
onDragOver={(event) => {
|
||||
if (isFavorites || !playlist.isOwner) {
|
||||
if (isFavorites || !isOwner) {
|
||||
return
|
||||
}
|
||||
event.preventDefault()
|
||||
|
||||
@@ -71,6 +71,11 @@ export function SearchSongItem({ song }: { song: BrowseSong }) {
|
||||
artist: { id: song.artist.id },
|
||||
album: { id: song.album.id },
|
||||
}}
|
||||
share={{
|
||||
type: "song",
|
||||
albumId: song.album.id,
|
||||
songId: song.id,
|
||||
}}
|
||||
triggerClassName="size-8 shrink-0 opacity-0 transition-opacity group-hover:opacity-100 data-[state=open]:opacity-100"
|
||||
triggerVariant="ghost"
|
||||
/>
|
||||
|
||||
@@ -249,6 +249,11 @@ function SongRow({
|
||||
artist: { id: song.artist.id },
|
||||
album: { id: song.album.id },
|
||||
}}
|
||||
share={{
|
||||
type: "song",
|
||||
albumId: song.album.id,
|
||||
songId: song.id,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,11 +4,24 @@ import * as React from "react"
|
||||
|
||||
type Selectable = { id: string }
|
||||
|
||||
export function useTrackSelection<T extends Selectable>(items: T[]) {
|
||||
const [selectedIds, setSelectedIds] = React.useState<Set<string>>(
|
||||
() => new Set()
|
||||
export function useTrackSelection<T extends Selectable>(
|
||||
items: T[],
|
||||
initialSelectedId?: string
|
||||
) {
|
||||
const initialIndex =
|
||||
initialSelectedId !== undefined
|
||||
? items.findIndex((item) => item.id === initialSelectedId)
|
||||
: -1
|
||||
|
||||
const [selectedIds, setSelectedIds] = React.useState<Set<string>>(() => {
|
||||
if (initialIndex >= 0) {
|
||||
return new Set([initialSelectedId!])
|
||||
}
|
||||
return new Set()
|
||||
})
|
||||
const anchorIndexRef = React.useRef<number | null>(
|
||||
initialIndex >= 0 ? initialIndex : null
|
||||
)
|
||||
const anchorIndexRef = React.useRef<number | null>(null)
|
||||
|
||||
const isSelected = React.useCallback(
|
||||
(id: string) => selectedIds.has(id),
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { toast } from "sonner"
|
||||
|
||||
export type MediaShareContext =
|
||||
| { type: "album"; albumId: string }
|
||||
| { type: "song"; albumId: string; songId: string }
|
||||
| { type: "playlist"; playlistId: string; isFavorites?: boolean }
|
||||
| { type: "artist"; artistId: string }
|
||||
|
||||
function mediaOrigin() {
|
||||
if (typeof window === "undefined") {
|
||||
return ""
|
||||
}
|
||||
return window.location.origin
|
||||
}
|
||||
|
||||
export function albumShareUrl(albumId: string) {
|
||||
return `${mediaOrigin()}/album/${albumId}`
|
||||
}
|
||||
|
||||
export function songShareUrl(albumId: string, songId: string) {
|
||||
return `${mediaOrigin()}/album/${albumId}?s=${songId}`
|
||||
}
|
||||
|
||||
export function playlistShareUrl(
|
||||
playlistId: string,
|
||||
options?: { isFavorites?: boolean }
|
||||
) {
|
||||
if (options?.isFavorites) {
|
||||
return `${mediaOrigin()}/playlist/favorites`
|
||||
}
|
||||
return `${mediaOrigin()}/playlist/${playlistId}`
|
||||
}
|
||||
|
||||
export function artistShareUrl(artistId: string) {
|
||||
return `${mediaOrigin()}/artist/${artistId}`
|
||||
}
|
||||
|
||||
export function mediaShareUrl(share: MediaShareContext) {
|
||||
switch (share.type) {
|
||||
case "album":
|
||||
return albumShareUrl(share.albumId)
|
||||
case "song":
|
||||
return songShareUrl(share.albumId, share.songId)
|
||||
case "playlist":
|
||||
return playlistShareUrl(share.playlistId, {
|
||||
isFavorites: share.isFavorites,
|
||||
})
|
||||
case "artist":
|
||||
return artistShareUrl(share.artistId)
|
||||
}
|
||||
}
|
||||
|
||||
export async function copyTextToClipboard(text: string) {
|
||||
await navigator.clipboard.writeText(text)
|
||||
toast.success("Link Copied")
|
||||
}
|
||||
|
||||
export async function copyMediaShareLink(share: MediaShareContext) {
|
||||
await copyTextToClipboard(mediaShareUrl(share))
|
||||
}
|
||||
|
||||
export async function copyAlbumLink(albumId: string) {
|
||||
await copyMediaShareLink({ type: "album", albumId })
|
||||
}
|
||||
|
||||
export async function copySongLink(albumId: string, songId: string) {
|
||||
await copyMediaShareLink({ type: "song", albumId, songId })
|
||||
}
|
||||
|
||||
export async function copyPlaylistLink(
|
||||
playlistId: string,
|
||||
options?: { isFavorites?: boolean }
|
||||
) {
|
||||
await copyMediaShareLink({
|
||||
type: "playlist",
|
||||
playlistId,
|
||||
isFavorites: options?.isFavorites,
|
||||
})
|
||||
}
|
||||
|
||||
export async function copyArtistLink(artistId: string) {
|
||||
await copyMediaShareLink({ type: "artist", artistId })
|
||||
}
|
||||
Reference in new issue
Block a user