- Virtualize mobile lists and defer queue loading - Split playback state from actions to reduce rerenders - Skip invalid or unnecessary Convex queries Reviewed-on: #18
253 lines
8.2 KiB
TypeScript
253 lines
8.2 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import Image from "next/image"
|
|
import Link from "next/link"
|
|
import { useRouter } from "next/navigation"
|
|
import { ChevronRightIcon, Play, Shuffle } from "lucide-react"
|
|
import { useConvex } from "convex/react"
|
|
|
|
import { AlbumCard } from "@/components/album-card"
|
|
import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
|
|
import { FavoriteMediaButton } from "@/components/favorite-media-button"
|
|
import { useFavorites } from "@/components/favorites-provider"
|
|
import {
|
|
MediaContextMenu,
|
|
type MediaContextAction,
|
|
} from "@/components/media-context-menu"
|
|
import { usePlayback } from "@/components/playback-provider"
|
|
import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
|
|
import { Button } from "@/components/ui/button"
|
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import { cn } from "@/lib/utils"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import type { BrowseAlbum, BrowseArtist } from "@otunes/convex/types"
|
|
import { artistCoverUrl } from "@/lib/cover-urls"
|
|
|
|
function ArtistAvatar({ artist }: { artist: BrowseArtist }) {
|
|
return (
|
|
<div className="relative size-8 shrink-0 overflow-hidden rounded-full ring-1 ring-foreground/10">
|
|
<Image
|
|
src={artistCoverUrl(artist.id)}
|
|
alt={artist.name}
|
|
fill
|
|
sizes="32px"
|
|
className="object-cover"
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function ArtistListItem({
|
|
artist,
|
|
albumCount,
|
|
isSelected,
|
|
isFavorite,
|
|
onSelect,
|
|
onMediaAction,
|
|
}: {
|
|
artist: BrowseArtist
|
|
albumCount: number
|
|
isSelected: boolean
|
|
isFavorite: boolean
|
|
onSelect: () => void
|
|
onMediaAction: (action: MediaContextAction) => void
|
|
}) {
|
|
return (
|
|
<div
|
|
className={cn(
|
|
"group flex w-full min-w-0 items-center gap-1 overflow-hidden pr-1",
|
|
isSelected ? "bg-primary text-primary-foreground" : "hover:bg-muted/50"
|
|
)}
|
|
>
|
|
<button
|
|
type="button"
|
|
onClick={onSelect}
|
|
className={cn(
|
|
"flex min-w-0 flex-1 items-center gap-3 overflow-hidden px-4 py-2.5 text-left text-sm transition-colors",
|
|
isSelected ? "text-primary-foreground" : ""
|
|
)}
|
|
>
|
|
<ArtistAvatar artist={artist} />
|
|
<span className="min-w-0 flex-1 truncate font-medium">
|
|
{artist.name}
|
|
</span>
|
|
</button>
|
|
<MediaContextMenu
|
|
label={`More options for ${artist.name}`}
|
|
selectedCount={albumCount}
|
|
inSelectionRange={isSelected}
|
|
onOpenMenu={() => {}}
|
|
onAction={onMediaAction}
|
|
favorited={isFavorite}
|
|
share={{ type: "artist", artistId: artist.id }}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export function ArtistsView({
|
|
artists,
|
|
albums,
|
|
artistId,
|
|
}: {
|
|
artists: BrowseArtist[]
|
|
albums: BrowseAlbum[]
|
|
artistId?: string
|
|
}) {
|
|
const router = useRouter()
|
|
const sortedArtists = React.useMemo(
|
|
() => [...artists].sort((a, b) => a.name.localeCompare(b.name)),
|
|
[artists]
|
|
)
|
|
|
|
const selectedId = React.useMemo(() => {
|
|
if (artistId && sortedArtists.some((artist) => artist.id === artistId)) {
|
|
return artistId
|
|
}
|
|
return sortedArtists[0]?.id ?? ""
|
|
}, [artistId, sortedArtists])
|
|
const convex = useConvex()
|
|
const { openAddArtistToPlaylist } = useAddToPlaylist()
|
|
const { isArtistFavorite, toggleArtistFavorite } = useFavorites()
|
|
const { playArtist, playArtistShuffled } = usePlayback()
|
|
const { runQueueAction } = useQueueMediaActions()
|
|
|
|
const selectedArtist = sortedArtists.find(
|
|
(artist) => artist.id === selectedId
|
|
)
|
|
const artistAlbums = albums.filter((album) => album.artist.id === selectedId)
|
|
|
|
const albumCountByArtistId = React.useMemo(() => {
|
|
const counts = new Map<string, number>()
|
|
for (const album of albums) {
|
|
counts.set(album.artist.id, (counts.get(album.artist.id) ?? 0) + 1)
|
|
}
|
|
return counts
|
|
}, [albums])
|
|
|
|
async function handleMediaAction(
|
|
artistId: string,
|
|
action: MediaContextAction
|
|
) {
|
|
switch (action) {
|
|
case "favorite":
|
|
void toggleArtistFavorite(artistId as Id<"artists">)
|
|
break
|
|
case "playlist":
|
|
void openAddArtistToPlaylist(artistId as Id<"artists">)
|
|
break
|
|
case "play-next":
|
|
case "play-last":
|
|
case "shuffle": {
|
|
const songIds = await convex.query(
|
|
api.playlists.getOrderedArtistSongIds,
|
|
{ artistId: artistId as Id<"artists"> }
|
|
)
|
|
void runQueueAction(action, songIds)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex min-h-0 flex-1">
|
|
<ScrollArea className="w-72 shrink-0 border-r">
|
|
<div className="w-full pb-28">
|
|
{sortedArtists.map((artist) => (
|
|
<ArtistListItem
|
|
key={artist.id}
|
|
artist={artist}
|
|
albumCount={albumCountByArtistId.get(artist.id) ?? 0}
|
|
isSelected={artist.id === selectedId}
|
|
isFavorite={isArtistFavorite(artist.id)}
|
|
onSelect={() => router.push(`/library/artists/${artist.id}`)}
|
|
onMediaAction={(action) =>
|
|
void handleMediaAction(artist.id, action)
|
|
}
|
|
/>
|
|
))}
|
|
</div>
|
|
</ScrollArea>
|
|
|
|
<ScrollArea className="min-w-0 flex-1">
|
|
{selectedArtist ? (
|
|
<div className="pb-28">
|
|
<div className="flex min-w-0 items-center justify-between gap-4 border-b px-6 py-4">
|
|
<Link
|
|
href={`/artist/${selectedArtist.id}`}
|
|
className="group inline-flex min-w-0 flex-1 items-center gap-1 text-xl font-bold tracking-tight hover:underline"
|
|
>
|
|
<span className="min-w-0 truncate">{selectedArtist.name}</span>
|
|
<ChevronRightIcon className="size-5 shrink-0 text-muted-foreground transition-transform group-hover:translate-x-0.5" />
|
|
</Link>
|
|
|
|
<div className="flex shrink-0 items-center gap-2">
|
|
<Button
|
|
size="icon"
|
|
className="rounded-full"
|
|
aria-label="Play"
|
|
onClick={() =>
|
|
void playArtist(selectedArtist.id as Id<"artists">)
|
|
}
|
|
>
|
|
<Play className="fill-current" />
|
|
</Button>
|
|
<Button
|
|
variant="secondary"
|
|
size="icon"
|
|
className="rounded-full"
|
|
aria-label="Shuffle"
|
|
onClick={() =>
|
|
void playArtistShuffled(selectedArtist.id as Id<"artists">)
|
|
}
|
|
>
|
|
<Shuffle />
|
|
</Button>
|
|
<FavoriteMediaButton
|
|
isFavorite={isArtistFavorite(selectedArtist.id)}
|
|
onToggle={() =>
|
|
void toggleArtistFavorite(
|
|
selectedArtist.id as Id<"artists">
|
|
)
|
|
}
|
|
favoritedLabel={`Remove ${selectedArtist.name} from favorites`}
|
|
unfavoritedLabel={`Favorite ${selectedArtist.name}`}
|
|
/>
|
|
<MediaContextMenu
|
|
label={`More options for ${selectedArtist.name}`}
|
|
selectedCount={artistAlbums.length}
|
|
onOpenMenu={() => {}}
|
|
onAction={(action) =>
|
|
void handleMediaAction(selectedArtist.id, action)
|
|
}
|
|
favorited={isArtistFavorite(selectedArtist.id)}
|
|
trackRowTrigger={false}
|
|
triggerVariant="secondary"
|
|
triggerClassName="rounded-full"
|
|
share={{ type: "artist", artistId: selectedArtist.id }}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="p-6">
|
|
{artistAlbums.length > 0 ? (
|
|
<div className="grid grid-cols-2 gap-x-6 gap-y-8 sm:grid-cols-3 lg:grid-cols-4">
|
|
{artistAlbums.map((album) => (
|
|
<AlbumCard key={album.id} album={album} />
|
|
))}
|
|
</div>
|
|
) : (
|
|
<p className="text-sm text-muted-foreground">
|
|
No albums for this artist.
|
|
</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
) : null}
|
|
</ScrollArea>
|
|
</div>
|
|
)
|
|
}
|