diff --git a/apps/web/components/album-view.tsx b/apps/web/components/album-view.tsx index ec3ce45..5fc401c 100644 --- a/apps/web/components/album-view.tsx +++ b/apps/web/components/album-view.tsx @@ -4,13 +4,11 @@ import { PageHeader } from "@/components/library-skeletons" import * as React from "react" import Image from "next/image" import Link from "next/link" -import { Play, Share2, Shuffle, Star } from "lucide-react" +import { Play, Share2, Shuffle } from "lucide-react" import { AudioFormatBadge } from "@/components/audio-format-badge" import { MediaCollectionFooter } from "@/components/media-collection-footer" -import { TrackNumberPlayButton } from "@/components/track-number-play-button" -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" @@ -20,37 +18,31 @@ import { MediaContextMenu, type MediaContextAction, } from "@/components/media-context-menu" +import { TrackRow } from "@/components/track-row" import { Button } from "@/components/ui/button" import { ScrollArea } from "@/components/ui/scroll-area" import { useCachedQuery } from "@/hooks/use-cached-query" import { api } from "@otunes/convex/_generated/api" -import { useQueueMediaActions } from "@/hooks/use-queue-media-actions" +import { + useMediaActions, + useTrackListMediaAction, +} from "@/hooks/use-media-actions" import { useTrackSelection } from "@/hooks/use-track-selection" import { cn } from "@/lib/utils" import type { Id } from "@otunes/convex/_generated/dataModel" -import type { BrowseAlbumDetail, BrowseAlbumTrack } from "@otunes/convex/types" +import type { + BrowseAlbumDetail, + BrowseAlbumTrack, + BrowseSong, +} from "@otunes/convex/types" import { getSelectionRangePositions, - isInSelectionRange, - ALBUM_TRACK_ROW, - ALBUM_TRACK_TITLE, - SelectionRangeHighlight, TRACK_LIST_CONTAINER, - TrackRowActiveHighlight, - TrackRowHoverHighlight, - TRACK_ROW_PADDING, - type SelectionRangePosition, } from "@/lib/selection-range-styles" import { albumCoverUrl } from "@/lib/cover-urls" import { sumDurationSeconds } from "@/lib/media-summary" import { albumDetailSongs, rememberSongs } from "@/lib/song-cache" -function formatDuration(seconds: number): string { - const minutes = Math.floor(seconds / 60) - const remainingSeconds = seconds % 60 - return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}` -} - function formatMetadata(genre: string | null, year: number | null): string | null { const parts: string[] = [] if (genre) { @@ -100,159 +92,6 @@ function groupAlbumTracksByDisk(tracks: BrowseAlbumTrack[]): AlbumTrackSection[] return sections } -function TrackRow({ - track, - sectionIndex, - artist, - isActive, - isSelected, - selectionPosition, - isFavorite, - selectedCount, - onSelect, - onPointerDown, - onToggleFavorite, - onPrepareContextMenu, - onMediaAction, - isNowPlaying, - onPlay, - albumId, -}: { - track: BrowseAlbumTrack - sectionIndex: number - artist: BrowseAlbumDetail["artist"] - albumId: string - isActive: boolean - isSelected: boolean - selectionPosition: SelectionRangePosition - isFavorite: boolean - selectedCount: number - onSelect: (event: React.MouseEvent) => void - onPointerDown: (event: React.MouseEvent) => void - onToggleFavorite: () => void - onPrepareContextMenu: () => void - onMediaAction: (action: MediaContextAction, trackId: string) => void - isNowPlaying: boolean - onPlay: () => void -}) { - const displayTrack = - track.track > 0 ? track.track : sectionIndex + 1 - const inRange = isInSelectionRange(selectionPosition) - - return ( -
{ - event.preventDefault() - onPlay() - }} - onKeyDown={(event) => { - if (event.key === "Enter" || event.key === " ") { - event.preventDefault() - onSelect(event as unknown as React.MouseEvent) - } - }} - className={cn( - "group relative w-full cursor-pointer select-none py-2.5 text-left text-sm transition-colors", - inRange && "text-primary-foreground" - )} - > - - {!inRange && isActive ? : null} - {!inRange && !isActive ? : null} -
-
- -
- - - -
-
{track.title}
- {track.artists ? ( -

- {track.artists.join(", ")} -

- ) : null} -
- - - {formatDuration(track.durationSeconds)} - - - onMediaAction(action, track.id)} - favorited={isFavorite} - navigation={{ - artist: { id: artist.id }, - album: { id: albumId }, - }} - share={{ - type: "song", - albumId, - songId: track.id, - }} - /> -
-
- ) -} - export function AlbumView({ album, highlightedSongId, @@ -260,17 +99,15 @@ export function AlbumView({ album: BrowseAlbumDetail highlightedSongId?: string }) { - const { openAddToPlaylist } = useAddToPlaylist() const { isSongFavorite, isAlbumFavorite, toggleSongFavorite, toggleAlbumFavorite, - favoriteSongs, } = useFavorites() const { currentSong, playFromSong, playAlbum, playAlbumShuffled } = usePlayback() - const { runQueueAction, createStation } = useQueueMediaActions() + const { runSongsAction } = useMediaActions() const albumSongIds = React.useMemo( () => album.tracks.map((track) => track.id as Id<"songs">), [album.tracks] @@ -308,7 +145,6 @@ export function AlbumView({ const { selectedIds, selectedCount, - isSelected, handleSelect, handlePointerDown, prepareContextMenu, @@ -336,55 +172,55 @@ export function AlbumView({ return indexById }, [album.tracks]) - function handleMediaAction( - action: MediaContextAction, - fallbackTrackId?: string - ) { - const targetIds = - selectedIds.size > 0 - ? Array.from(selectedIds) - : fallbackTrackId - ? [fallbackTrackId] - : activeTrackId - ? [activeTrackId] - : [] - switch (action) { - case "create-station": - void createStation(album.artist.id) - break - case "favorite": - void favoriteSongs(targetIds as Id<"songs">[]) - break - case "playlist": - openAddToPlaylist(targetIds as Id<"songs">[]) - break - case "play-next": - case "play-last": - case "shuffle": - void runQueueAction(action, targetIds as Id<"songs">[]) - break - } - } + const trackSongs = React.useMemo( + () => + new Map( + album.tracks.map((track) => [ + track.id, + { + id: track.id, + title: track.title, + durationSeconds: track.durationSeconds, + artist: album.artist, + album: { id: album.id, title: album.title, cover: album.cover }, + }, + ]) + ), + [album.tracks, album.artist, album.id, album.title, album.cover] + ) + + const handleMediaAction = useTrackListMediaAction({ + selectedIds, + artistId: album.artist.id, + }) + + const handleRowSelect = React.useCallback( + (index: number, event: React.MouseEvent) => { + const trackId = album.tracks[index].id + handleSelect(trackId, index, event) + setActiveTrackId(trackId) + }, + [album.tracks, handleSelect] + ) + + const handleRowPlay = React.useCallback( + (index: number) => { + void playFromSong(album.tracks[index].id, albumSongIds, `album:${album.id}`) + }, + [album.tracks, album.id, albumSongIds, playFromSong] + ) + + const handleToggleFavorite = React.useCallback( + (songId: string) => void toggleSongFavorite(songId as Id<"songs">), + [toggleSongFavorite] + ) function handleAlbumMediaAction(action: MediaContextAction) { - const targetIds = album.tracks.map((track) => track.id as Id<"songs">) - - switch (action) { - case "create-station": - void createStation(album.artist.id) - break - case "favorite": - void toggleAlbumFavorite(album.id as Id<"albums">) - break - case "playlist": - openAddToPlaylist(targetIds) - break - case "play-next": - case "play-last": - case "shuffle": - void runQueueAction(action, targetIds) - break + if (action === "favorite") { + void toggleAlbumFavorite(album.id as Id<"albums">) + return } + runSongsAction(action, albumSongIds, album.artist.id) } return ( @@ -518,39 +354,25 @@ export function AlbumView({ return ( 0 ? track.track : sectionTrackIndex + 1 + } + subtitle={track.artists?.join(", ")} isActive={track.id === activeTrackId} - isSelected={isSelected(track.id)} selectionPosition={selectionPositions[listIndex]} isFavorite={isSongFavorite(track.id)} - selectedCount={ - isSelected(track.id) && selectedCount > 0 - ? selectedCount - : 1 - } - onPointerDown={handlePointerDown} - onSelect={(event) => { - handleSelect(track.id, listIndex, event) - setActiveTrackId(track.id) - }} - onToggleFavorite={() => - void toggleSongFavorite(track.id as Id<"songs">) - } - onPrepareContextMenu={() => - prepareContextMenu(track.id) - } - onMediaAction={handleMediaAction} + selectedCount={selectedCount} + selectionStationArtistId={album.artist.id} isNowPlaying={currentSong?.id === track.id} - onPlay={() => - void playFromSong( - track.id as Id<"songs">, - albumSongIds, - `album:${album.id}` - ) - } + onPointerDown={handlePointerDown} + onSelect={handleRowSelect} + onPlay={handleRowPlay} + onToggleFavorite={handleToggleFavorite} + onPrepareContextMenu={prepareContextMenu} + onMediaAction={handleMediaAction} /> ) })} diff --git a/apps/web/components/artist-view.tsx b/apps/web/components/artist-view.tsx index 4b2fbf9..5bab21e 100644 --- a/apps/web/components/artist-view.tsx +++ b/apps/web/components/artist-view.tsx @@ -4,27 +4,19 @@ import { PageHeader } from "@/components/library-skeletons" import * as React from "react" import Image from "next/image" import { Play, Shuffle } from "lucide-react" -import { useConvex } from "convex/react" -import { useCacheScope } from "@/components/persistent-cache-provider" -import { useAddToPlaylist } from "@/components/add-to-playlist-provider" import { useFavorites } from "@/components/favorites-provider" import { usePlayback } from "@/components/playback-provider" -import { useQueueMediaActions } from "@/hooks/use-queue-media-actions" +import { useMediaActions } from "@/hooks/use-media-actions" import { AlbumCard } from "@/components/album-card" import { FavoriteMediaButton } from "@/components/favorite-media-button" -import { - MediaContextMenu, - type MediaContextAction, -} from "@/components/media-context-menu" +import { MediaContextMenu } from "@/components/media-context-menu" import { Button } from "@/components/ui/button" import { ScrollArea } from "@/components/ui/scroll-area" -import { api } from "@otunes/convex/_generated/api" import type { Id } from "@otunes/convex/_generated/dataModel" import type { BrowseAlbum, BrowseArtist } from "@otunes/convex/types" import { albumGridClassName } from "@/lib/album-grid-classes" import { artistCoverUrl } from "@/lib/cover-urls" -import { fetchCachedQuery } from "@otunes/convex/client/cached-query" export function ArtistView({ artist, @@ -34,34 +26,8 @@ export function ArtistView({ albums: BrowseAlbum[] }) { const { isArtistFavorite, toggleArtistFavorite } = useFavorites() - const { openAddArtistToPlaylist } = useAddToPlaylist() const { playArtist, playArtistShuffled } = usePlayback() - const { runQueueAction } = useQueueMediaActions() - const convex = useConvex() - const cacheScope = useCacheScope() - - async function handleMediaAction(action: MediaContextAction) { - switch (action) { - case "favorite": - void toggleArtistFavorite(artist.id as Id<"artists">) - break - case "playlist": - void openAddArtistToPlaylist(artist.id as Id<"artists">) - break - case "play-next": - case "play-last": - case "shuffle": { - const songIds = await fetchCachedQuery( - convex, - cacheScope, - api.playlists.getOrderedArtistSongIds, - { artistId: artist.id as Id<"artists"> } - ) - void runQueueAction(action, songIds) - break - } - } - } + const { runArtistAction } = useMediaActions() return (
@@ -118,7 +84,7 @@ export function ArtistView({ label={`More options for ${artist.name}`} selectedCount={albums.length} onOpenMenu={() => {}} - onAction={handleMediaAction} + onAction={(action) => void runArtistAction(action, artist.id)} favorited={isArtistFavorite(artist.id)} trackRowTrigger={false} triggerVariant="secondary" diff --git a/apps/web/components/artists-view.tsx b/apps/web/components/artists-view.tsx index 554e35d..48b3d8a 100644 --- a/apps/web/components/artists-view.tsx +++ b/apps/web/components/artists-view.tsx @@ -4,11 +4,8 @@ import * as React from "react" import Image from "next/image" import Link from "next/link" import { ChevronRightIcon, Play, Shuffle } from "lucide-react" -import { useConvex } from "convex/react" -import { useCacheScope } from "@/components/persistent-cache-provider" 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 { @@ -16,15 +13,13 @@ import { type MediaContextAction, } from "@/components/media-context-menu" import { usePlayback } from "@/components/playback-provider" -import { useQueueMediaActions } from "@/hooks/use-queue-media-actions" +import { useMediaActions } from "@/hooks/use-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" -import { fetchCachedQuery } from "@otunes/convex/client/cached-query" function ArtistAvatar({ artist }: { artist: BrowseArtist }) { return ( @@ -110,12 +105,9 @@ export function ArtistsView({ } return sortedArtists[0]?.id ?? "" }, [artistId, sortedArtists]) - const convex = useConvex() - const cacheScope = useCacheScope() - const { openAddArtistToPlaylist } = useAddToPlaylist() const { isArtistFavorite, toggleArtistFavorite } = useFavorites() const { playArtist, playArtistShuffled } = usePlayback() - const { runQueueAction } = useQueueMediaActions() + const { runArtistAction } = useMediaActions() const selectedArtist = sortedArtists.find( (artist) => artist.id === selectedId @@ -130,32 +122,6 @@ export function ArtistsView({ 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 fetchCachedQuery( - convex, - cacheScope, - api.playlists.getOrderedArtistSongIds, - { artistId: artistId as Id<"artists"> } - ) - void runQueueAction(action, songIds) - break - } - } - } - return (
@@ -169,7 +135,7 @@ export function ArtistsView({ isFavorite={isArtistFavorite(artist.id)} onSelect={() => onSelectArtist(artist.id)} onMediaAction={(action) => - void handleMediaAction(artist.id, action) + void runArtistAction(action, artist.id) } /> ))} @@ -225,7 +191,7 @@ export function ArtistsView({ selectedCount={artistAlbums.length} onOpenMenu={() => {}} onAction={(action) => - void handleMediaAction(selectedArtist.id, action) + void runArtistAction(action, selectedArtist.id) } favorited={isArtistFavorite(selectedArtist.id)} trackRowTrigger={false} diff --git a/apps/web/components/playlist-view.tsx b/apps/web/components/playlist-view.tsx index 57ceaa8..6dd6eaa 100644 --- a/apps/web/components/playlist-view.tsx +++ b/apps/web/components/playlist-view.tsx @@ -9,15 +9,14 @@ import { useMutation } from "convex/react" import { Play, SearchIcon, Share2, Shuffle, Star, Trash2 } from "lucide-react" import { toast } from "sonner" -import { useAddToPlaylist } from "@/components/add-to-playlist-provider" import { useFavorites } from "@/components/favorites-provider" import { usePlayback } from "@/components/playback-provider" import { MediaCollectionFooter } from "@/components/media-collection-footer" -import { TrackNumberPlayButton } from "@/components/track-number-play-button" import { MediaContextMenu, type MediaContextAction, } from "@/components/media-context-menu" +import { TrackRow } from "@/components/track-row" import { Button } from "@/components/ui/button" import { DropdownMenuItem, @@ -25,248 +24,22 @@ import { } from "@/components/ui/dropdown-menu" import { ScrollArea } from "@/components/ui/scroll-area" import { useCachedQuery } from "@/hooks/use-cached-query" -import { useQueueMediaActions } from "@/hooks/use-queue-media-actions" +import { + useMediaActions, + useTrackListMediaAction, +} from "@/hooks/use-media-actions" import { useTrackSelection } from "@/hooks/use-track-selection" import { api } from "@otunes/convex/_generated/api" import type { Id } from "@otunes/convex/_generated/dataModel" import type { BrowsePlaylistDetail, BrowseSong } from "@otunes/convex/types" -import { albumCoverUrl, playlistCoverUrl } from "@/lib/cover-urls" +import { playlistCoverUrl } from "@/lib/cover-urls" import { copyPlaylistLink } from "@/lib/media-links" import { sumDurationSeconds } from "@/lib/media-summary" import { rememberSongs } from "@/lib/song-cache" -import { cn } from "@/lib/utils" -import { - getSelectionRangePositions, - isInSelectionRange, - ALBUM_TRACK_ROW, - ALBUM_TRACK_TITLE, - SelectionRangeHighlight, - TrackRowActiveHighlight, - TrackRowHoverHighlight, - TRACK_ROW_PADDING, - type SelectionRangePosition, -} from "@/lib/selection-range-styles" +import { getSelectionRangePositions } from "@/lib/selection-range-styles" const ADD_TO_ANOTHER_PLAYLIST = "Add to Another Playlist" -function formatDuration(seconds: number): string { - const minutes = Math.floor(seconds / 60) - const remainingSeconds = seconds % 60 - return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}` -} - -function PlaylistSongCover({ - song, - inRange, - isFavorite, - onToggleFavorite, -}: { - song: BrowseSong - inRange: boolean - isFavorite: boolean - onToggleFavorite: () => void -}) { - return ( -
- {song.album.title} -
- -
-
- ) -} - -function PlaylistSongRow({ - song, - index, - isOwner, - canRemove, - isActive, - selectionPosition, - isFavorite, - selectedCount, - stationArtist, - isDragging, - onSelect, - onPointerDown, - onToggleFavorite, - onPrepareContextMenu, - onMediaAction, - onRemove, - onDragStart, - onDragOver, - onDrop, - showFavoriteInMenu = true, - removeLabel = "Remove from Playlist", - isNowPlaying, - onPlay, -}: { - song: BrowseSong - index: number - isOwner: boolean - canRemove: boolean - isActive: boolean - selectionPosition: SelectionRangePosition - isFavorite: boolean - selectedCount: number - stationArtist?: string - isDragging: boolean - onSelect: (event: React.MouseEvent) => void - onPointerDown: (event: React.MouseEvent) => void - onToggleFavorite: () => void - onPrepareContextMenu: () => void - onMediaAction: (action: MediaContextAction, songId: string) => void - onRemove: () => void - onDragStart: (event: React.DragEvent) => void - onDragOver: (event: React.DragEvent) => void - onDrop: (event: React.DragEvent) => void - showFavoriteInMenu?: boolean - removeLabel?: string - isNowPlaying: boolean - onPlay: () => void -}) { - const displayTrack = index + 1 - const inRange = isInSelectionRange(selectionPosition) - - return ( -
{ - event.preventDefault() - onPlay() - }} - onKeyDown={(event) => { - if (event.key === "Enter" || event.key === " ") { - event.preventDefault() - onSelect(event as unknown as React.MouseEvent) - } - }} - className={cn( - "group relative w-full cursor-pointer select-none py-2.5 text-left text-sm transition-colors", - inRange && "text-primary-foreground", - isDragging && "opacity-50" - )} - > - - {!inRange && isActive ? : null} - {!inRange && !isActive ? : null} -
- - - - - {song.title} - - - {formatDuration(song.durationSeconds)} - - - onMediaAction(action, song.id)} - playlistLabel={ADD_TO_ANOTHER_PLAYLIST} - showFavorite={showFavoriteInMenu} - favorited={isFavorite} - navigation={{ - artist: { id: song.artist.id }, - album: { id: song.album.id }, - }} - share={{ - type: "song", - albumId: song.album.id, - songId: song.id, - }} - > - {canRemove ? ( - <> - - - {removeLabel} - - - - ) : null} - -
-
- ) -} - export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { const router = useRouter() const isFavorites = playlist.isFavorites === true @@ -275,13 +48,8 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { currentUser === undefined ? playlist.isOwner : currentUser?._id === playlist.owner.id - const { openAddToPlaylist } = useAddToPlaylist() - const { - isSongFavorite, - toggleSongFavorite, - toggleSongsFavorite, - favoriteSongs: addFavoriteSongs, - } = useFavorites() + const { isSongFavorite, toggleSongFavorite, toggleSongsFavorite } = + useFavorites() const { currentSong, playInOrder, @@ -290,7 +58,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { playPlaylistShuffled, playFromSong, } = usePlayback() - const { runQueueAction, createStation } = useQueueMediaActions() + const { runSongsAction } = useMediaActions() const removeSongs = useMutation(api.playlists.removeSongsFromPlaylist) const reorderSongs = useMutation(api.playlists.reorderPlaylistSongs) const deletePlaylist = useMutation(api.playlists.deletePlaylist) @@ -322,6 +90,10 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { () => songs.map((song) => song.id as Id<"songs">), [songs] ) + const playlistPositionById = React.useMemo( + () => new Map(songs.map((song, index) => [song.id, index + 1])), + [songs] + ) const [activeTrackId, setActiveTrackId] = React.useState("") const [dragIndex, setDragIndex] = React.useState(null) const [isSubscribed, setIsSubscribed] = React.useState(playlist.isSubscribed) @@ -336,7 +108,6 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { const { selectedIds, selectedCount, - isSelected, handleSelect, handlePointerDown, prepareContextMenu, @@ -374,57 +145,37 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { } } - function resolveTargetIds(fallbackSongId?: string) { - return selectedIds.size > 0 - ? Array.from(selectedIds) - : fallbackSongId - ? [fallbackSongId] - : activeTrackId - ? [activeTrackId] - : [] - } - - function handleMediaAction( - action: MediaContextAction, - fallbackSongId?: string - ) { - const targetIds = resolveTargetIds(fallbackSongId) - - switch (action) { - case "create-station": { - const artistId = stationArtistId(songs, new Set(targetIds)) - if (artistId) void createStation(artistId) - break - } - case "favorite": - void addFavoriteSongs(targetIds as Id<"songs">[]) - break - case "playlist": - openAddToPlaylist(targetIds as Id<"songs">[]) - break - case "play-next": - case "play-last": - case "shuffle": - void runQueueAction(action, targetIds as Id<"songs">[]) - break - } - } + const handleMediaAction = useTrackListMediaAction({ selectedIds, songs }) function handlePlaylistMediaAction(action: MediaContextAction) { - const targetIds = songs.map((song) => song.id as Id<"songs">) + runSongsAction(action, playlistSongIds) + } - switch (action) { - case "favorite": - void addFavoriteSongs(targetIds) - break - case "playlist": - openAddToPlaylist(targetIds) - break - case "play-next": - case "play-last": - case "shuffle": - void runQueueAction(action, targetIds) - break + const canReorder = isOwner && !isFavorites && !normalizedQuery + + function handleRowSelect(index: number, event: React.MouseEvent) { + const songId = filteredSongs[index].id + handleSelect(songId, index, event) + setActiveTrackId(songId) + } + + function handleRowPlay(index: number) { + void playFromSong( + filteredSongs[index].id, + playlistSongIds, + `playlist:${playlist.id}` + ) + } + + function handleToggleFavorite(songId: string) { + const wasFavorite = isSongFavorite(songId) + void toggleSongFavorite(songId as Id<"songs">) + if (isFavorites && wasFavorite) { + setHiddenFavoriteIds((current) => { + const next = new Set(current) + next.add(songId) + return next + }) } } @@ -479,13 +230,7 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { } function handleDrop(targetIndex: number) { - if ( - normalizedQuery || - isFavorites || - !isOwner || - dragIndex === null || - dragIndex === targetIndex - ) { + if (!canReorder || dragIndex === null || dragIndex === targetIndex) { setDragIndex(null) return } @@ -677,74 +422,37 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
{filteredSongs.length > 0 ? ( filteredSongs.map((song, index) => ( - 0 - ? selectedCount - : 1 - } - stationArtist={ - isSelected(song.id) - ? selectedStationArtistId - : song.artist.id - } - isDragging={dragIndex === index} onPointerDown={handlePointerDown} - onSelect={(event) => { - handleSelect(song.id, index, event) - setActiveTrackId(song.id) - }} - onToggleFavorite={() => { - const wasFavorite = isSongFavorite(song.id) - void toggleSongFavorite(song.id as Id<"songs">) - if (isFavorites && wasFavorite) { - setHiddenFavoriteIds((current) => { - const next = new Set(current) - next.add(song.id) - return next - }) - } - }} - onPrepareContextMenu={() => prepareContextMenu(song.id)} + onSelect={handleRowSelect} + onPlay={handleRowPlay} + onToggleFavorite={handleToggleFavorite} + onPrepareContextMenu={prepareContextMenu} onMediaAction={handleMediaAction} - onRemove={() => handleRemoveSong(song.id)} - onDragStart={(event) => { - if (normalizedQuery || isFavorites || !isOwner) { - return - } - setDragIndex(index) - event.dataTransfer.effectAllowed = "move" - }} - onDragOver={(event) => { - if (normalizedQuery || isFavorites || !isOwner) { - return - } - event.preventDefault() - }} - onDrop={(event) => { - event.preventDefault() - handleDrop(index) - }} - isNowPlaying={currentSong?.id === song.id} - onPlay={() => - void playFromSong( - song.id as Id<"songs">, - playlistSongIds, - `playlist:${playlist.id}` - ) - } + onRemove={handleRemoveSong} + onDragStart={canReorder ? setDragIndex : undefined} + onDrop={canReorder ? handleDrop : undefined} /> )) ) : ( diff --git a/apps/web/components/queue-panel.tsx b/apps/web/components/queue-panel.tsx index 10544ac..6005a27 100644 --- a/apps/web/components/queue-panel.tsx +++ b/apps/web/components/queue-panel.tsx @@ -1,11 +1,10 @@ "use client" import * as React from "react" -import Image from "next/image" -import { Music2 } from "lucide-react" import { PlaybackSongMeta } from "@/components/playback-song-meta" import { usePlayback } from "@/components/playback-provider" +import { TrackCover, TrackRow } from "@/components/track-row" import { Button } from "@/components/ui/button" import { ScrollArea } from "@/components/ui/scroll-area" import { Skeleton } from "@/components/ui/skeleton" @@ -13,19 +12,11 @@ import { Spinner } from "@/components/ui/spinner" import { useTrackSelection } from "@/hooks/use-track-selection" import type { Id } from "@otunes/convex/_generated/dataModel" import type { BrowseSong, HistorySong, QueueSong } from "@otunes/convex/types" -import { albumCoverUrl } from "@/lib/cover-urls" import { getContiguousSelectedIndices, moveQueueBlock, } from "@/lib/move-queue-block" -import { - getSelectionRangePositions, - isInSelectionRange, - QueueSelectionRangeHighlight, - QueueTrackRowActiveHighlight, - QueueTrackRowHoverHighlight, - type SelectionRangePosition, -} from "@/lib/selection-range-styles" +import { getSelectionRangePositions } from "@/lib/selection-range-styles" import { useSongs } from "@/lib/song-cache" import { cn } from "@/lib/utils" @@ -61,48 +52,16 @@ function toSelectableQueue( }) } -function QueueSongCover({ - albumId, - title, - inRange, -}: { - albumId: string - title: string - inRange: boolean -}) { - return ( -
- {title} -
- ) -} - function HistorySongRow({ song }: { song: HistorySong }) { return (
  • - {song.album.id ? ( - - ) : ( -
    - -
    - )} + void - onPointerDown: (event: React.MouseEvent) => void - onSkipTo: () => void - onDragStart: (event: React.DragEvent) => void - onDragOver: (event: React.DragEvent) => void - onDrop: (event: React.DragEvent) => void - } ->(function QueueSongRow( - { - song, - isNowPlaying, - selectionPosition, - isDragging, - onSelect, - onPointerDown, - onSkipTo, - onDragStart, - onDragOver, - onDrop, - }, - ref -) { - const inRange = isInSelectionRange(selectionPosition) - - return ( -
  • { - event.preventDefault() - onSkipTo() - }} - onKeyDown={(event) => { - if (event.key === "Enter" || event.key === " ") { - event.preventDefault() - onSelect(event as unknown as React.MouseEvent) - } - }} - className={cn( - "group relative w-full cursor-pointer select-none py-2 text-left text-sm transition-colors", - inRange && "text-primary-foreground", - isDragging && "opacity-50" - )} - > - - {!inRange && isNowPlaying ? : null} - {!inRange && !isNowPlaying ? : null} -
    - {song.album.id ? ( - - ) : ( -
    - -
    - )} - - - - - {formatDuration(song.durationSeconds)} - -
    -
  • - ) -}) - function QueueSongRowSkeleton() { return (
  • @@ -243,7 +102,7 @@ export function QueuePanel() { skipToQueueIndex, } = usePlayback() const loadMoreRef = React.useRef(null) - const nowPlayingRef = React.useRef(null) + const nowPlayingRef = React.useRef(null) const [visibleCount, setVisibleCount] = React.useState( QUEUE_INITIAL_VISIBLE_COUNT ) @@ -311,12 +170,25 @@ export function QueuePanel() { return () => observer.disconnect() }, [hasMore]) - const { - selectedIds, - handleSelect, - handlePointerDown, - } = useTrackSelection( - selectableQueue.map((item) => ({ id: item.selectionId })) + const selectionItems = React.useMemo( + () => selectableQueue.map((item) => ({ id: item.selectionId })), + [selectableQueue] + ) + const { selectedIds, handleSelect, handlePointerDown } = + useTrackSelection(selectionItems) + + const handleRowSelect = React.useCallback( + (index: number, event: React.MouseEvent) => { + handleSelect(selectableQueue[index].selectionId, index, event) + }, + [selectableQueue, handleSelect] + ) + + const handleRowPlay = React.useCallback( + (index: number) => { + void skipToQueueIndex(selectableQueue[index].queueIndex) + }, + [selectableQueue, skipToQueueIndex] ) const selectionPositions = React.useMemo( @@ -403,27 +275,21 @@ export function QueuePanel() {
      {selectableQueue.map((item, index) => item.song ? ( - { - handleSelect(item.selectionId, index, event) - }} - onSkipTo={() => void skipToQueueIndex(item.queueIndex)} - onDragStart={(event) => { - setDragIndex(index) - event.dataTransfer.effectAllowed = "move" - }} - onDragOver={(event) => event.preventDefault()} - onDrop={(event) => { - event.preventDefault() - handleDrop(index) - }} + onSelect={handleRowSelect} + onPlay={handleRowPlay} + onDragStart={setDragIndex} + onDrop={handleDrop} /> ) : ( diff --git a/apps/web/components/songs-view.tsx b/apps/web/components/songs-view.tsx index 066fb5b..2bdf0ae 100644 --- a/apps/web/components/songs-view.tsx +++ b/apps/web/components/songs-view.tsx @@ -2,18 +2,19 @@ import { MobileSidebarTrigger } from "@/components/mobile-sidebar-trigger" import { stationArtistId } from "@/lib/station-artist" -import { useEffect, useMemo, useState, useRef, MouseEvent } from "react" -import Image from "next/image" -import Link from "next/link" -import { ArrowUpDown, Play, Star } from "lucide-react" +import { + useCallback, + useEffect, + useMemo, + useState, + useRef, + type MouseEvent, +} from "react" +import { ArrowUpDown } from "lucide-react" -import { useAddToPlaylist } from "@/components/add-to-playlist-provider" import { useFavorites } from "@/components/favorites-provider" import { usePlayback } from "@/components/playback-provider" -import { - MediaContextMenu, - type MediaContextAction, -} from "@/components/media-context-menu" +import { LibraryTrackListHeader, TrackRow } from "@/components/track-row" import { Button } from "@/components/ui/button" import { DropdownMenu, @@ -25,32 +26,17 @@ import { DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { useCachedPaginatedQuery } from "@/hooks/use-cached-query" -import { useQueueMediaActions } from "@/hooks/use-queue-media-actions" +import { useTrackListMediaAction } from "@/hooks/use-media-actions" import { useTrackSelection } from "@/hooks/use-track-selection" import { SongListSkeleton } from "@/components/library-skeletons" import { ScrollArea } from "@/components/ui/scroll-area" import { Spinner } from "@/components/ui/spinner" import { api } from "@otunes/convex/_generated/api" -import { - Table, - TableBody, - TableCell, - TableHead, - TableHeader, - TableRow, -} from "@/components/ui/table" import { cn } from "@/lib/utils" import type { Id } from "@otunes/convex/_generated/dataModel" -import type { BrowseSong } from "@otunes/convex/types" -import { albumCoverUrl } from "@/lib/cover-urls" import { getSelectionRangePositions, - isInSelectionRange, - ALBUM_TRACK_TITLE, - SongsSelectionRangeHighlight, - SongsTrackRowHoverHighlight, TRACK_ROW_PADDING, - type SelectionRangePosition, } from "@/lib/selection-range-styles" type SortKey = "title" | "artist" | "album" @@ -58,219 +44,9 @@ type SortDirection = "asc" | "desc" const SONGS_PAGE_SIZE = 50 -const songRowLayout = - "flex w-full items-center gap-x-4" -const nameColumnLayout = - "grid grid-cols-[2rem_2.5rem_minmax(0,10rem)] items-center gap-x-2" -const songColumnClass = "w-36 shrink-0 truncate" - -function formatDuration(seconds: number): string { - const minutes = Math.floor(seconds / 60) - const remainingSeconds = seconds % 60 - return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}` -} - -function SongCover({ - song, - inRange, - onPlay, -}: { - song: BrowseSong - inRange: boolean - onPlay: () => void -}) { - return ( -
      - {song.album.title} -
      - -
      -
      - ) -} - -function SongRow({ - song, - isSelected, - selectionPosition, - isFavorite, - selectedCount, - stationArtist, - onSelect, - onPointerDown, - onToggleFavorite, - onPrepareContextMenu, - onMediaAction, - onPlay, -}: { - song: BrowseSong - isSelected: boolean - selectionPosition: SelectionRangePosition - isFavorite: boolean - selectedCount: number - stationArtist?: string - onSelect: (event: MouseEvent) => void - onPointerDown: (event: MouseEvent) => void - onToggleFavorite: () => void - onPrepareContextMenu: () => void - onMediaAction: (action: MediaContextAction, songId: string) => void - onPlay: () => void -}) { - const inRange = isInSelectionRange(selectionPosition) - const linkClass = inRange - ? "text-primary-foreground hover:text-primary-foreground/80 hover:underline" - : "text-muted-foreground hover:text-foreground hover:underline" - - return ( - { - event.preventDefault() - onPlay() - }} - className="group cursor-pointer select-none border-none hover:bg-transparent" - > - -
      - - {!inRange ? : null} -
      -
      -
      - -
      - - - {song.title} - {song.explicit ? ( - - E - - ) : null} - -
      -
      - event.stopPropagation()} - > - {song.artist.name} - -
      -
      - event.stopPropagation()} - > - {song.album.title} - -
      -
      - - {formatDuration(song.durationSeconds)} - - onMediaAction(action, song.id)} - favorited={isFavorite} - navigation={{ - artist: { id: song.artist.id }, - album: { id: song.album.id }, - }} - share={{ - type: "song", - albumId: song.album.id, - songId: song.id, - }} - /> -
      -
      - - - ) -} - export function SongsView() { - const { openAddToPlaylist } = useAddToPlaylist() - const { isSongFavorite, toggleSongFavorite, favoriteSongs } = useFavorites() + const { isSongFavorite, toggleSongFavorite } = useFavorites() const { playFromLibrarySong } = usePlayback() - const { runQueueAction, createStation } = useQueueMediaActions() const [sortKey, setSortKey] = useState("title") const [sortDirection, setSortDirection] = useState("asc") @@ -311,7 +87,6 @@ export function SongsView() { const { selectedIds, selectedCount, - isSelected, handleSelect, handlePointerDown, prepareContextMenu, @@ -332,32 +107,26 @@ export function SongsView() { [songs, selectedIds] ) - function handleMediaAction(action: MediaContextAction, fallbackSongId?: string) { - const targetIds = - selectedIds.size > 0 - ? Array.from(selectedIds) - : fallbackSongId - ? [fallbackSongId] - : [] - switch (action) { - case "create-station": { - const artistId = stationArtistId(songs, new Set(targetIds)) - if (artistId) void createStation(artistId) - break - } - case "favorite": - void favoriteSongs(targetIds as Id<"songs">[]) - break - case "playlist": - openAddToPlaylist(targetIds as Id<"songs">[]) - break - case "play-next": - case "play-last": - case "shuffle": - void runQueueAction(action, targetIds as Id<"songs">[]) - break - } - } + const handleMediaAction = useTrackListMediaAction({ selectedIds, songs }) + + const handleRowSelect = useCallback( + (index: number, event: MouseEvent) => { + handleSelect(songs[index].id, index, event) + }, + [songs, handleSelect] + ) + + const handleRowPlay = useCallback( + (index: number) => { + void playFromLibrarySong(songs[index].id, { sortKey, sortDirection }) + }, + [songs, sortKey, sortDirection, playFromLibrarySong] + ) + + const handleToggleFavorite = useCallback( + (songId: string) => void toggleSongFavorite(songId as Id<"songs">), + [toggleSongFavorite] + ) if (status === "LoadingFirstPage" && !isCached) { return ( @@ -437,68 +206,25 @@ export function SongsView() { } }} > - - - - -
      -
      - - - Name -
      - - Artist - - - Album - -
      - - Time - - -
      - - - - - {songs.map((song, index) => ( - 0 - ? selectedCount - : 1 - } - onPointerDown={handlePointerDown} - onSelect={(event) => { - handleSelect(song.id, index, event) - }} - onToggleFavorite={() => - void toggleSongFavorite(song.id as Id<"songs">) - } - onPrepareContextMenu={() => prepareContextMenu(song.id)} - onMediaAction={handleMediaAction} - onPlay={() => - void playFromLibrarySong(song.id as Id<"songs">, { - sortKey, - sortDirection, - }) - } - /> - ))} - -
      + + {songs.map((song, index) => ( + + ))}
      + onSelect: (index: number, event: React.MouseEvent) => void + onPointerDown: (event: React.MouseEvent) => void + onPlay: (index: number) => void + onToggleFavorite?: (songId: string) => void + onPrepareContextMenu?: (songId: string) => void + /** Renders the "more options" menu when provided. */ + onMediaAction?: (action: MediaContextAction, songId: string) => void + onRemove?: (songId: string) => void + /** Makes the row draggable when provided. */ + onDragStart?: (index: number) => void + onDrop?: (index: number) => void +} + +const LIBRARY_ROW_LAYOUT = "flex w-full items-center gap-x-4" +const LIBRARY_NAME_COLUMNS = + "grid grid-cols-[2rem_2.5rem_minmax(0,10rem)] items-center gap-x-2" +const LIBRARY_COLUMN = "w-36 shrink-0 truncate" + +function formatDuration(seconds: number): string { + const minutes = Math.floor(seconds / 60) + const remainingSeconds = seconds % 60 + return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}` +} + +export function TrackCover({ + albumId, + alt, + inRange, + square = false, + children, +}: { + albumId: string | undefined + alt: string + inRange: boolean + square?: boolean + /** Overlay shown above the cover image. */ + children?: React.ReactNode +}) { + const ringClass = inRange ? "ring-white/25" : "ring-foreground/10" + + if (!albumId) { + return ( +
      + +
      + ) + } + + return ( +
      + {alt} + {children} +
      + ) +} + +function FavoriteStarButton({ + title, + isFavorite, + inRange, + className, + onToggle, +}: { + title: string + isFavorite: boolean + inRange: boolean + className?: string + onToggle: () => void +}) { + return ( + + ) +} + +function ExplicitBadge({ inRange }: { inRange: boolean }) { + return ( + + E + + ) +} + +function TrackTitle({ + song, + subtitle, + inRange, +}: { + song: BrowseSong + subtitle?: string + inRange: boolean +}) { + return ( +
      +
      + {song.title} + {song.explicit ? : null} +
      + {subtitle ? ( +

      + {subtitle} +

      + ) : null} +
      + ) +} + +export function LibraryTrackListHeader() { + const labelClass = "text-xs text-muted-foreground" + return ( +
      +
      + + + Name +
      + Artist + Album +
      + Time + +
      + ) +} + +function TrackRowComponent({ + variant, + song, + index, + trackNumber, + subtitle, + selectionPosition, + isActive = false, + isNowPlaying = false, + isFavorite = false, + isDragging = false, + selectedCount = 0, + selectionStationArtistId, + playlistLabel, + showFavoriteInMenu = true, + removeLabel, + ref, + onSelect, + onPointerDown, + onPlay, + onToggleFavorite, + onPrepareContextMenu, + onMediaAction, + onRemove, + onDragStart, + onDrop, +}: TrackRowProps) { + const inRange = isInSelectionRange(selectionPosition) + const flush = variant === "library" || variant === "queue" + const play = () => onPlay(index) + const toggleFavorite = () => onToggleFavorite?.(song.id) + + const favoriteStar = ( +
      + +
      + ) + + const trackNumberButton = ( + + ) + + const duration = ( + + {formatDuration(song.durationSeconds)} + + ) + + const menu = onMediaAction ? ( + 0 ? selectedCount : 1} + inSelectionRange={inRange} + onOpenMenu={() => onPrepareContextMenu?.(song.id)} + onAction={(action) => onMediaAction(action, song.id)} + playlistLabel={playlistLabel} + showFavorite={showFavoriteInMenu} + favorited={isFavorite} + navigation={{ + artist: { id: song.artist.id }, + album: { id: song.album.id }, + }} + share={{ type: "song", albumId: song.album.id, songId: song.id }} + > + {removeLabel && onRemove ? ( + <> + + onRemove(song.id)} + > + {removeLabel} + + + + ) : null} + + ) : null + + let content: React.ReactNode + switch (variant) { + case "library": { + const linkClass = inRange + ? "text-primary-foreground hover:text-primary-foreground/80 hover:underline" + : "text-muted-foreground hover:text-foreground hover:underline" + content = ( +
      +
      + {favoriteStar} + +
      + +
      +
      + +
      +
      + event.stopPropagation()} + > + {song.artist.name} + +
      +
      + event.stopPropagation()} + > + {song.album.title} + +
      +
      + {duration} + {menu} +
      + ) + break + } + case "album": + content = ( +
      + {favoriteStar} + {trackNumberButton} + + {duration} + {menu} +
      + ) + break + case "playlist": + content = ( +
      + +
      + +
      +
      + {trackNumberButton} + + {duration} + {menu} +
      + ) + break + case "queue": + content = ( +
      + + + {duration} +
      + ) + break + } + + const shellProps = { + role: "button", + tabIndex: 0, + draggable: onDragStart ? true : undefined, + onDragStart: onDragStart + ? (event: React.DragEvent) => { + event.dataTransfer.effectAllowed = "move" + onDragStart(index) + } + : undefined, + onDragOver: onDragStart + ? (event: React.DragEvent) => event.preventDefault() + : undefined, + onDrop: onDrop + ? (event: React.DragEvent) => { + event.preventDefault() + onDrop(index) + } + : undefined, + onMouseDown: onPointerDown, + onClick: (event: React.MouseEvent) => onSelect(index, event), + onDoubleClick: (event: React.MouseEvent) => { + event.preventDefault() + play() + }, + onKeyDown: (event: React.KeyboardEvent) => { + if (event.key === "Enter" || event.key === " ") { + event.preventDefault() + onSelect(index, event as unknown as React.MouseEvent) + } + }, + className: cn( + "group relative w-full cursor-pointer select-none text-left text-sm transition-colors", + flush ? "py-2" : "py-2.5", + inRange && "text-primary-foreground", + isDragging && "opacity-50" + ), + } + + const children = ( + <> + + {!inRange && isActive ? : null} + {!inRange && !isActive ? : null} + {content} + + ) + + return variant === "queue" ? ( +
    • } {...shellProps}> + {children} +
    • + ) : ( +
      } {...shellProps}> + {children} +
      + ) +} + +/** + * Pass stable callbacks: they receive the row's `index` or song id, so one + * handler can serve every row. + */ +export const TrackRow = React.memo(TrackRowComponent) diff --git a/apps/web/hooks/use-media-actions.ts b/apps/web/hooks/use-media-actions.ts new file mode 100644 index 0000000..d665acc --- /dev/null +++ b/apps/web/hooks/use-media-actions.ts @@ -0,0 +1,104 @@ +"use client" + +import * as React from "react" +import { useConvex } from "convex/react" + +import { useAddToPlaylist } from "@/components/add-to-playlist-provider" +import { useFavorites } from "@/components/favorites-provider" +import type { MediaContextAction } from "@/components/media-context-menu" +import { useCacheScope } from "@/components/persistent-cache-provider" +import { useQueueMediaActions } from "@/hooks/use-queue-media-actions" +import { stationArtistId } from "@/lib/station-artist" +import { api } from "@otunes/convex/_generated/api" +import type { Id } from "@otunes/convex/_generated/dataModel" +import { fetchCachedQuery } from "@otunes/convex/client/cached-query" + +export function useMediaActions() { + const { openAddToPlaylist, openAddArtistToPlaylist } = useAddToPlaylist() + const { favoriteSongs, toggleArtistFavorite } = useFavorites() + const { runQueueAction, createStation } = useQueueMediaActions() + const convex = useConvex() + const cacheScope = useCacheScope() + + const runSongsAction = React.useCallback( + ( + action: MediaContextAction, + songIds: Id<"songs">[], + stationArtist?: string + ) => { + switch (action) { + case "create-station": + if (stationArtist) void createStation(stationArtist) + break + case "favorite": + void favoriteSongs(songIds) + break + case "playlist": + openAddToPlaylist(songIds) + break + case "play-next": + case "play-last": + case "shuffle": + void runQueueAction(action, songIds) + break + } + }, + [createStation, favoriteSongs, openAddToPlaylist, runQueueAction] + ) + + const runArtistAction = React.useCallback( + async (action: MediaContextAction, artistId: string) => { + 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 fetchCachedQuery( + convex, + cacheScope, + api.playlists.getOrderedArtistSongIds, + { artistId: artistId as Id<"artists"> } + ) + void runQueueAction(action, songIds) + break + } + } + }, + [convex, cacheScope, toggleArtistFavorite, openAddArtistToPlaylist, runQueueAction] + ) + + return { runSongsAction, runArtistAction } +} + +/** + * Handles a track row menu action, targeting the current selection or else the + * row's song. The station artist is `artistId` when given, otherwise the artist + * every targeted song in `songs` shares. + */ +export function useTrackListMediaAction({ + selectedIds, + songs, + artistId, +}: { + selectedIds: ReadonlySet + songs?: { id: string; artist: { id: string } }[] + artistId?: string +}) { + const { runSongsAction } = useMediaActions() + + return React.useCallback( + (action: MediaContextAction, songId: string) => { + const targetIds = selectedIds.size > 0 ? Array.from(selectedIds) : [songId] + const stationArtist = + artistId ?? + (songs ? stationArtistId(songs, new Set(targetIds)) : undefined) + runSongsAction(action, targetIds as Id<"songs">[], stationArtist) + }, + [selectedIds, songs, artistId, runSongsAction] + ) +} diff --git a/apps/web/lib/selection-range-styles.tsx b/apps/web/lib/selection-range-styles.tsx index d871df0..0c575fa 100644 --- a/apps/web/lib/selection-range-styles.tsx +++ b/apps/web/lib/selection-range-styles.tsx @@ -63,10 +63,19 @@ export function selectionHighlightRadiusClass( export const TRACK_ROW_HIGHLIGHT_INSET = "absolute inset-y-0 left-2 right-2" +/** Full-width, square-corner highlight used by the library and queue lists. */ +export const FLUSH_ROW_HIGHLIGHT_INSET = "absolute inset-0" + +/** + * Track row highlights are inset rounded pills by default; `flush` makes them + * full-width with square corners. + */ export function SelectionRangeHighlight({ position, + flush = false, }: { position: SelectionRangePosition + flush?: boolean }) { if (position === "none") { return null @@ -77,34 +86,34 @@ export function SelectionRangeHighlight({ aria-hidden className={cn( "pointer-events-none bg-primary", - TRACK_ROW_HIGHLIGHT_INSET, - selectionHighlightRadiusClass(position) + flush + ? FLUSH_ROW_HIGHLIGHT_INSET + : [TRACK_ROW_HIGHLIGHT_INSET, selectionHighlightRadiusClass(position)] )} /> ) } -/** Inset rounded hover background matching the selection pill shape. */ -export function TrackRowHoverHighlight() { +export function TrackRowHoverHighlight({ flush = false }: { flush?: boolean }) { return (
      ) } -/** Inset rounded background for the active (now playing) row. */ -export function TrackRowActiveHighlight() { +/** Background for the active (focused or now playing) row. */ +export function TrackRowActiveHighlight({ flush = false }: { flush?: boolean }) { return (
      ) @@ -116,42 +125,6 @@ export const TRACK_ROW_PADDING = "pl-8 pr-10" /** Centers the track list in the main content area. */ export const TRACK_LIST_CONTAINER = "mx-auto w-full max-w-9/10" -/** Full-width highlight for library song rows (no horizontal inset). */ -export const SONGS_ROW_HIGHLIGHT_INSET = "absolute inset-0" - -export function SongsSelectionRangeHighlight({ - position, -}: { - position: SelectionRangePosition -}) { - if (position === "none") { - return null - } - - return ( -
      - ) -} - -/** Full-width hover background for library song rows (not rounded). */ -export function SongsTrackRowHoverHighlight() { - return ( -
      - ) -} - /** Album track row: star, track #, title (flex), duration, menu. */ export const ALBUM_TRACK_ROW = "flex w-full min-w-0 items-center gap-x-3" @@ -160,50 +133,3 @@ export const ALBUM_TRACK_TITLE = "min-w-0 flex-1 truncate font-normal" export function isInSelectionRange(position: SelectionRangePosition) { return position !== "none" } - -/** Full-width, square-corner highlights for the queue sidebar. */ -export const QUEUE_ROW_HIGHLIGHT_INSET = "absolute inset-0" - -export function QueueSelectionRangeHighlight({ - position, -}: { - position: SelectionRangePosition -}) { - if (position === "none") { - return null - } - - return ( -
      - ) -} - -export function QueueTrackRowHoverHighlight() { - return ( -
      - ) -} - -export function QueueTrackRowActiveHighlight() { - return ( -
      - ) -}