+ 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"
+
+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 (
+
+
+ {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 = (
+
+ )
+ 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 5f534a4..0c575fa 100644
--- a/apps/web/lib/selection-range-styles.tsx
+++ b/apps/web/lib/selection-range-styles.tsx
@@ -46,7 +46,7 @@ export function getSelectionRangePositions(
return positions
}
-function selectionHighlightRadiusClass(
+export function selectionHighlightRadiusClass(
position: SelectionRangePosition
) {
switch (position) {
@@ -61,12 +61,21 @@ function selectionHighlightRadiusClass(
}
}
-const TRACK_ROW_HIGHLIGHT_INSET = "absolute inset-y-0 left-2 right-2"
+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). */
-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. */
-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 (
-
- )
-}