feat(web): right-click menus on track rows and deep Songs scroll restore

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
joshuafhigginsandClaude Opus 5.5 committed 2026-11-16 14:37:38 -05:00 Verified
1 parent 7351299841
commit 9c1536578b
7 files changed
+543 -419

No files matched your search

+111 -105
View File
@@ -22,6 +22,8 @@ import { copyAlbumLink } from "@/lib/media-links"
import {
MediaContextMenu,
type MediaContextAction,
MediaContextMenuArea,
type MediaContextMenuProps,
} from "@/components/media-context-menu"
import { Button } from "@/components/ui/button"
import { ScrollArea } from "@/components/ui/scroll-area"
@@ -142,117 +144,121 @@ function TrackRow({
track.track > 0 ? track.track : sectionIndex + 1
const inRange = isInSelectionRange(selectionPosition)
const menu: MediaContextMenuProps = {
label: `More options for ${track.title}`,
stationArtistId: artist.id,
selectedCount,
inSelectionRange: inRange,
onOpenMenu: onPrepareContextMenu,
onAction: (action) => onMediaAction(action, track.id),
favorited: isFavorite,
navigation: {
artist: { id: artist.id },
album: { id: albumId },
},
share: {
type: "song",
albumId,
songId: track.id,
},
}
return (
<div
role="button"
tabIndex={0}
onMouseDown={onPointerDown}
onClick={onSelect}
onDoubleClick={(event) => {
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"
)}
>
<SelectionRangeHighlight position={selectionPosition} />
{!inRange && isActive ? <TrackRowActiveHighlight /> : null}
{!inRange && !isActive ? <TrackRowHoverHighlight /> : null}
<MediaContextMenuArea menu={menu}>
<div
className={cn("relative z-10", ALBUM_TRACK_ROW, TRACK_ROW_PADDING)}
role="button"
tabIndex={0}
onMouseDown={onPointerDown}
onClick={onSelect}
onDoubleClick={(event) => {
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"
)}
>
<div className="flex shrink-0 items-center justify-center">
<Button
variant="ghost"
size="icon-sm"
className={cn(
"size-8 opacity-0 transition-opacity group-hover:opacity-100",
inRange
? "text-primary-foreground hover:bg-white/15 hover:text-primary-foreground"
: "text-primary hover:text-primary",
(isFavorite || isActive || isSelected) && "opacity-100"
)}
aria-label={
isFavorite
? `Remove ${track.title} from favorites`
: `Favorite ${track.title}`
}
onClick={(event) => {
event.stopPropagation()
onToggleFavorite()
}}
>
<Star
className={cn(
"size-4",
isFavorite && (inRange ? "fill-current" : "fill-primary")
)}
/>
</Button>
</div>
<TrackNumberPlayButton
displayTrack={displayTrack}
title={track.title}
isActive={isActive}
isNowPlaying={isNowPlaying}
inRange={inRange}
onPlay={onPlay}
/>
<div className="min-w-0 flex-1">
<div className={ALBUM_TRACK_TITLE}>{track.title}</div>
{track.artists ? (
<p
className={cn(
"truncate text-xs",
inRange
? "text-primary-foreground/80"
: "text-muted-foreground"
)}
>
{track.artists.join(", ")}
</p>
) : null}
</div>
<span
className={cn(
"shrink-0 text-right tabular-nums",
inRange ? "text-primary-foreground" : "text-muted-foreground"
)}
<SelectionRangeHighlight position={selectionPosition} />
{!inRange && isActive ? <TrackRowActiveHighlight /> : null}
{!inRange && !isActive ? <TrackRowHoverHighlight /> : null}
<div
className={cn("relative z-10", ALBUM_TRACK_ROW, TRACK_ROW_PADDING)}
>
{formatDuration(track.durationSeconds)}
</span>
<div className="flex shrink-0 items-center justify-center">
<Button
variant="ghost"
size="icon-sm"
className={cn(
"size-8 opacity-0 transition-opacity group-hover:opacity-100",
inRange
? "text-primary-foreground hover:bg-white/15 hover:text-primary-foreground"
: "text-primary hover:text-primary",
(isFavorite || isActive || isSelected) && "opacity-100"
)}
aria-label={
isFavorite
? `Remove ${track.title} from favorites`
: `Favorite ${track.title}`
}
onClick={(event) => {
event.stopPropagation()
onToggleFavorite()
}}
>
<Star
className={cn(
"size-4",
isFavorite && (inRange ? "fill-current" : "fill-primary")
)}
/>
</Button>
</div>
<MediaContextMenu
label={`More options for ${track.title}`}
stationArtistId={artist.id}
selectedCount={selectedCount}
inSelectionRange={inRange}
onOpenMenu={onPrepareContextMenu}
onAction={(action) => onMediaAction(action, track.id)}
favorited={isFavorite}
navigation={{
artist: { id: artist.id },
album: { id: albumId },
}}
share={{
type: "song",
albumId,
songId: track.id,
}}
/>
<TrackNumberPlayButton
displayTrack={displayTrack}
title={track.title}
isActive={isActive}
isNowPlaying={isNowPlaying}
inRange={inRange}
onPlay={onPlay}
/>
<div className="min-w-0 flex-1">
<div className={ALBUM_TRACK_TITLE}>{track.title}</div>
{track.artists ? (
<p
className={cn(
"truncate text-xs",
inRange
? "text-primary-foreground/80"
: "text-muted-foreground"
)}
>
{track.artists.join(", ")}
</p>
) : null}
</div>
<span
className={cn(
"shrink-0 text-right tabular-nums",
inRange ? "text-primary-foreground" : "text-muted-foreground"
)}
>
{formatDuration(track.durationSeconds)}
</span>
<MediaContextMenu {...menu} />
</div>
</div>
</div>
</MediaContextMenuArea>
)
}
+34 -26
View File
@@ -14,6 +14,8 @@ import { useFavorites } from "@/components/favorites-provider"
import {
MediaContextMenu,
type MediaContextAction,
MediaContextMenuArea,
type MediaContextMenuProps,
} from "@/components/media-context-menu"
import { usePlaybackActions } from "@/components/playback-provider"
import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
@@ -55,36 +57,42 @@ function ArtistListItem({
onSelect: () => void
onMediaAction: (action: MediaContextAction) => void
}) {
const menu: MediaContextMenuProps = {
label: `More options for ${artist.name}`,
selectedCount: albumCount,
inSelectionRange: isSelected,
onOpenMenu: () => {},
onAction: onMediaAction,
favorited: isFavorite,
share: { type: "artist", artistId: artist.id },
}
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}
<MediaContextMenuArea menu={menu}>
<div
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" : ""
"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"
)}
>
<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>
<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 {...menu} />
</div>
</MediaContextMenuArea>
)
}
+113 -42
View File
@@ -15,6 +15,13 @@ import {
} from "lucide-react"
import { Button } from "@/components/ui/button"
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuTrigger,
} from "@/components/ui/context-menu"
import {
DropdownMenu,
DropdownMenuContent,
@@ -53,6 +60,28 @@ function shouldShowShare(
return true
}
// Lets the same items render inside a dropdown or a right-click menu.
const MenuKindContext = React.createContext<"dropdown" | "context">("dropdown")
/** Menu item that works in both `MediaContextMenu` and `MediaContextMenuArea`. */
export function MediaMenuItem(
props: React.ComponentProps<typeof DropdownMenuItem>
) {
return React.use(MenuKindContext) === "context" ? (
<ContextMenuItem {...props} />
) : (
<DropdownMenuItem {...props} />
)
}
export function MediaMenuSeparator() {
return React.use(MenuKindContext) === "context" ? (
<ContextMenuSeparator />
) : (
<DropdownMenuSeparator />
)
}
function actionLabel(base: string, selectedCount: number) {
return selectedCount > 1 ? `${base} (${selectedCount})` : base
}
@@ -83,86 +112,86 @@ export function MediaContextMenuItems({
<>
{showShare ? (
<>
<DropdownMenuItem
<MediaMenuItem
className="justify-between"
onClick={() => void copyMediaShareLink(share!)}
>
Share
<Share2 />
</DropdownMenuItem>
<DropdownMenuSeparator />
</MediaMenuItem>
<MediaMenuSeparator />
</>
) : null}
<DropdownMenuItem
<MediaMenuItem
className="justify-between"
onClick={() => onAction("playlist")}
>
{actionLabel(playlistLabel, selectedCount)}
<ListPlus />
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
</MediaMenuItem>
<MediaMenuSeparator />
<MediaMenuItem
className="justify-between"
onClick={() => onAction("play-next")}
>
{actionLabel("Play Next", selectedCount)}
<ListStart />
</DropdownMenuItem>
<DropdownMenuItem
</MediaMenuItem>
<MediaMenuItem
className="justify-between"
onClick={() => onAction("play-last")}
>
{actionLabel("Play Last", selectedCount)}
<ListEnd />
</DropdownMenuItem>
<DropdownMenuItem
</MediaMenuItem>
<MediaMenuItem
className="justify-between"
onClick={() => onAction("shuffle")}
>
{actionLabel("Shuffle", selectedCount)}
<Shuffle />
</DropdownMenuItem>
</MediaMenuItem>
{stationArtistId ? (
<DropdownMenuItem
<MediaMenuItem
className="justify-between"
onClick={() => onAction("create-station")}
>
Create Station
<Shuffle />
</DropdownMenuItem>
</MediaMenuItem>
) : null}
{artist || album ? (
<>
<DropdownMenuSeparator />
<MediaMenuSeparator />
{artist ? (
<DropdownMenuItem asChild className="justify-between">
<MediaMenuItem asChild className="justify-between">
<Link href={`/artist/${artist.id}`}>
Go to Artist
<MicVocal />
</Link>
</DropdownMenuItem>
</MediaMenuItem>
) : null}
{album ? (
<DropdownMenuItem asChild className="justify-between">
<MediaMenuItem asChild className="justify-between">
<Link href={`/album/${album.id}`}>
Go to Album
<GalleryVerticalEnd />
</Link>
</DropdownMenuItem>
</MediaMenuItem>
) : null}
</>
) : null}
{showFavorite ? (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
<MediaMenuSeparator />
<MediaMenuItem
variant={favorited ? "destructive" : "default"}
className="justify-between"
onClick={() => onAction("favorite")}
>
{actionLabel(favorited ? "Unfavorite" : "Favorite", selectedCount)}
<Star />
</DropdownMenuItem>
</MediaMenuItem>
</>
) : null}
</>
@@ -194,6 +223,67 @@ export function mediaContextMenuTriggerClassName({
)
}
export type MediaContextMenuProps = {
label: string
selectedCount: number
onOpenMenu: () => void
onAction: (action: MediaContextAction) => void
playlistLabel?: string
showFavorite?: boolean
favorited?: boolean
stationArtistId?: string
navigation?: MediaContextNavigation
share?: MediaShareContext
inSelectionRange?: boolean
/** When false, the trigger uses default button/dropdown styling (page headers). */
trackRowTrigger?: boolean
triggerClassName?: string
triggerVariant?: "ghost" | "secondary"
triggerIconClassName?: string
align?: "start" | "end"
/** Extra items; use `MediaMenuItem` so they also work in the right-click menu. */
children?: React.ReactNode
}
/**
* Opens `menu` (the props given to the row's `MediaContextMenu`) when
* `children`, a single element such as a track row, is right-clicked.
*/
export function MediaContextMenuArea({
menu,
children,
}: {
menu: MediaContextMenuProps
children: React.ReactElement
}) {
return (
<ContextMenu
onOpenChange={(open) => {
if (open) {
menu.onOpenMenu()
}
}}
>
<ContextMenuTrigger asChild>{children}</ContextMenuTrigger>
<ContextMenuContent className="w-48">
<MenuKindContext value="context">
<MediaContextMenuItems
selectedCount={menu.selectedCount}
onAction={menu.onAction}
playlistLabel={menu.playlistLabel}
showFavorite={menu.showFavorite}
favorited={menu.favorited}
stationArtistId={menu.stationArtistId}
navigation={menu.navigation}
share={menu.share}
/>
{menu.children}
</MenuKindContext>
</ContextMenuContent>
</ContextMenu>
)
}
export function MediaContextMenu({
label,
selectedCount,
@@ -212,26 +302,7 @@ export function MediaContextMenu({
triggerIconClassName = "size-4",
align = "end",
children,
}: {
label: string
selectedCount: number
onOpenMenu: () => void
onAction: (action: MediaContextAction) => void
playlistLabel?: string
showFavorite?: boolean
favorited?: boolean
stationArtistId?: string
navigation?: MediaContextNavigation
share?: MediaShareContext
inSelectionRange?: boolean
/** When false, the trigger uses default button/dropdown styling (page headers). */
trackRowTrigger?: boolean
triggerClassName?: string
triggerVariant?: "ghost" | "secondary"
triggerIconClassName?: string
align?: "start" | "end"
children?: React.ReactNode
}) {
}: MediaContextMenuProps) {
return (
<DropdownMenu
onOpenChange={(open) => {
+96 -89
View File
@@ -20,6 +20,10 @@ import { TrackNumberPlayButton } from "@/components/track-number-play-button"
import {
MediaContextMenu,
type MediaContextAction,
MediaContextMenuArea,
MediaMenuItem,
MediaMenuSeparator,
type MediaContextMenuProps,
} from "@/components/media-context-menu"
import { Button } from "@/components/ui/button"
import {
@@ -177,101 +181,104 @@ const PlaylistSongRow = React.memo(function PlaylistSongRow({
const select = (event: React.MouseEvent) => onSelect(song.id, index, event)
const inRange = isInSelectionRange(selectionPosition)
const menu: MediaContextMenuProps = {
label: `More options for ${song.title}`,
stationArtistId: stationArtist,
selectedCount,
inSelectionRange: inRange,
onOpenMenu: () => onPrepareContextMenu(song.id),
onAction: (action) => 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,
},
children: canRemove ? (
<>
<MediaMenuSeparator />
<MediaMenuItem
variant="destructive"
className="justify-between"
onClick={() => onRemove(song.id)}
>
{removeLabel}
<Trash2 />
</MediaMenuItem>
</>
) : null,
}
return (
<div
role="button"
tabIndex={0}
draggable={isOwner}
onDragStart={(event) => onDragStart(index, event)}
onDragOver={onDragOver}
onDrop={(event) => onDrop(index, event)}
onMouseDown={onPointerDown}
onClick={select}
onDoubleClick={(event) => {
event.preventDefault()
play()
}}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault()
select(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"
)}
>
<SelectionRangeHighlight position={selectionPosition} />
{!inRange && isActive ? <TrackRowActiveHighlight /> : null}
{!inRange && !isActive ? <TrackRowHoverHighlight /> : null}
<MediaContextMenuArea menu={menu}>
<div
className={cn("relative z-10", ALBUM_TRACK_ROW, TRACK_ROW_PADDING)}
role="button"
tabIndex={0}
draggable={isOwner}
onDragStart={(event) => onDragStart(index, event)}
onDragOver={onDragOver}
onDrop={(event) => onDrop(index, event)}
onMouseDown={onPointerDown}
onClick={select}
onDoubleClick={(event) => {
event.preventDefault()
play()
}}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault()
select(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"
)}
>
<PlaylistSongCover
song={song}
inRange={inRange}
isFavorite={isFavorite}
onToggleFavorite={() => onToggleFavorite(song.id)}
/>
<TrackNumberPlayButton
displayTrack={displayTrack}
title={song.title}
isActive={isActive}
isNowPlaying={isNowPlaying}
inRange={inRange}
onPlay={play}
/>
<span className={ALBUM_TRACK_TITLE}>{song.title}</span>
<span
className={cn(
"shrink-0 text-right tabular-nums",
inRange ? "text-primary-foreground" : "text-muted-foreground"
)}
<SelectionRangeHighlight position={selectionPosition} />
{!inRange && isActive ? <TrackRowActiveHighlight /> : null}
{!inRange && !isActive ? <TrackRowHoverHighlight /> : null}
<div
className={cn("relative z-10", ALBUM_TRACK_ROW, TRACK_ROW_PADDING)}
>
{formatDuration(song.durationSeconds)}
</span>
<PlaylistSongCover
song={song}
inRange={inRange}
isFavorite={isFavorite}
onToggleFavorite={() => onToggleFavorite(song.id)}
/>
<MediaContextMenu
label={`More options for ${song.title}`}
stationArtistId={stationArtist}
selectedCount={selectedCount}
inSelectionRange={inRange}
onOpenMenu={() => onPrepareContextMenu(song.id)}
onAction={(action) => 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 ? (
<>
<DropdownMenuSeparator />
<DropdownMenuItem
variant="destructive"
className="justify-between"
onClick={() => onRemove(song.id)}
>
{removeLabel}
<Trash2 />
</DropdownMenuItem>
</>
) : null}
</MediaContextMenu>
<TrackNumberPlayButton
displayTrack={displayTrack}
title={song.title}
isActive={isActive}
isNowPlaying={isNowPlaying}
inRange={inRange}
onPlay={play}
/>
<span className={ALBUM_TRACK_TITLE}>{song.title}</span>
<span
className={cn(
"shrink-0 text-right tabular-nums",
inRange ? "text-primary-foreground" : "text-muted-foreground"
)}
>
{formatDuration(song.durationSeconds)}
</span>
<MediaContextMenu {...menu} />
</div>
</div>
</div>
</MediaContextMenuArea>
)
})
+49 -42
View File
@@ -8,6 +8,8 @@ import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
import {
MediaContextMenu,
type MediaContextAction,
MediaContextMenuArea,
type MediaContextMenuProps,
} from "@/components/media-context-menu"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { BrowseSong } from "@otunes/convex/types"
@@ -38,52 +40,57 @@ export function SearchSongItem({ song }: { song: BrowseSong }) {
}
}
const menu: MediaContextMenuProps = {
label: `More options for ${song.title}`,
stationArtistId: song.artist.id,
selectedCount: 1,
onOpenMenu: () => {},
onAction: handleAction,
favorited: isSongFavorite(song.id),
navigation: {
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",
}
return (
<div className="group flex items-center gap-3 rounded-lg px-1 py-1.5 transition-colors hover:bg-muted/50">
<Link
href={`/album/${song.album.id}`}
className="relative size-11 shrink-0 overflow-hidden rounded ring-1 ring-foreground/10"
>
<Image
src={albumCoverUrl(song.album.id)}
alt={song.album.title}
fill
sizes="44px"
className="object-cover"
/>
</Link>
<div className="min-w-0 flex-1">
<Link href={`/album/${song.album.id}`} className="hover:underline">
<p className="truncate text-sm font-medium">{song.title}</p>
</Link>
<MediaContextMenuArea menu={menu}>
<div className="group flex items-center gap-3 rounded-lg px-1 py-1.5 transition-colors hover:bg-muted/50">
<Link
href={`/artist/${song.artist.id}`}
className="text-muted-foreground hover:text-foreground hover:underline"
href={`/album/${song.album.id}`}
className="relative size-11 shrink-0 overflow-hidden rounded ring-1 ring-foreground/10"
>
<p className="truncate text-sm">{song.artist.name}</p>
<Image
src={albumCoverUrl(song.album.id)}
alt={song.album.title}
fill
sizes="44px"
className="object-cover"
/>
</Link>
</div>
<MediaContextMenu
label={`More options for ${song.title}`}
stationArtistId={song.artist.id}
selectedCount={1}
onOpenMenu={() => {}}
onAction={handleAction}
favorited={isSongFavorite(song.id)}
navigation={{
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"
/>
</div>
<div className="min-w-0 flex-1">
<Link href={`/album/${song.album.id}`} className="hover:underline">
<p className="truncate text-sm font-medium">{song.title}</p>
</Link>
<Link
href={`/artist/${song.artist.id}`}
className="text-muted-foreground hover:text-foreground hover:underline"
>
<p className="truncate text-sm">{song.artist.name}</p>
</Link>
</div>
<MediaContextMenu {...menu} />
</div>
</MediaContextMenuArea>
)
}
+119 -113
View File
@@ -13,6 +13,8 @@ import { usePlaybackActions } from "@/components/playback-provider"
import {
MediaContextMenu,
type MediaContextAction,
MediaContextMenuArea,
type MediaContextMenuProps,
} from "@/components/media-context-menu"
import { Button } from "@/components/ui/button"
import {
@@ -141,124 +143,128 @@ const SongRow = memo(function SongRow({
? "text-primary-foreground hover:text-primary-foreground/80 hover:underline"
: "text-muted-foreground hover:text-foreground hover:underline"
const menu: MediaContextMenuProps = {
label: `More options for ${song.title}`,
stationArtistId: stationArtist,
selectedCount,
align: "end",
inSelectionRange: inRange,
onOpenMenu: () => onPrepareContextMenu(song.id),
onAction: (action) => 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,
},
}
return (
<div
onMouseDown={onPointerDown}
onClick={(event) => onSelect(song.id, index, event)}
onDoubleClick={(event) => {
event.preventDefault()
play()
}}
className="group cursor-pointer select-none whitespace-nowrap text-sm transition-colors has-aria-expanded:bg-muted/50"
>
<div
className={cn(
"relative py-2",
inRange && "text-primary-foreground"
)}
>
<SongsSelectionRangeHighlight position={selectionPosition} />
{!inRange ? <SongsTrackRowHoverHighlight /> : null}
<MediaContextMenuArea menu={menu}>
<div
onMouseDown={onPointerDown}
onClick={(event) => onSelect(song.id, index, event)}
onDoubleClick={(event) => {
event.preventDefault()
play()
}}
className="group cursor-pointer select-none whitespace-nowrap text-sm transition-colors has-aria-expanded:bg-muted/50"
>
<div
className={cn("relative z-10", songRowLayout, TRACK_ROW_PADDING)}
className={cn(
"relative py-2",
inRange && "text-primary-foreground"
)}
>
<div className={nameColumnLayout}>
<div className="flex items-center justify-center">
<Button
variant="ghost"
size="icon-sm"
className={cn(
"size-8 opacity-0 transition-opacity group-hover:opacity-100",
inRange
? "text-primary-foreground hover:bg-white/15 hover:text-primary-foreground"
: "text-primary hover:text-primary",
(isFavorite || isSelected) && "opacity-100"
)}
aria-label={
isFavorite
? `Remove ${song.title} from favorites`
: `Favorite ${song.title}`
}
onClick={(event) => {
event.stopPropagation()
onToggleFavorite(song.id)
}}
>
<Star
className={cn(
"size-4",
isFavorite && (inRange ? "fill-current" : "fill-primary")
)}
/>
</Button>
</div>
<SongCover song={song} inRange={inRange} onPlay={play} />
<span className={ALBUM_TRACK_TITLE}>
{song.title}
{song.explicit ? (
<span
className={cn(
"ml-1.5 inline-flex size-3.5 shrink-0 items-center justify-center rounded-sm text-[9px] font-semibold leading-none",
inRange
? "bg-white/20 text-primary-foreground"
: "bg-muted-foreground/20 text-muted-foreground"
)}
aria-label="Explicit"
>
E
</span>
) : null}
</span>
</div>
<div className={songColumnClass}>
<Link
href={"/artist/" + song.artist.id}
className={linkClass}
onClick={(event) => event.stopPropagation()}
>
{song.artist.name}
</Link>
</div>
<div className={songColumnClass}>
<Link
href={"/album/" + song.album.id}
className={linkClass}
onClick={(event) => event.stopPropagation()}
>
{song.album.title}
</Link>
</div>
<div className="min-w-4 flex-1" />
<span
className={cn(
"shrink-0 text-right tabular-nums",
inRange ? "text-primary-foreground" : "text-muted-foreground"
)}
<SongsSelectionRangeHighlight position={selectionPosition} />
{!inRange ? <SongsTrackRowHoverHighlight /> : null}
<div
className={cn("relative z-10", songRowLayout, TRACK_ROW_PADDING)}
>
{formatDuration(song.durationSeconds)}
</span>
<MediaContextMenu
label={`More options for ${song.title}`}
stationArtistId={stationArtist}
selectedCount={selectedCount}
align="end"
inSelectionRange={inRange}
onOpenMenu={() => onPrepareContextMenu(song.id)}
onAction={(action) => 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,
}}
/>
<div className={nameColumnLayout}>
<div className="flex items-center justify-center">
<Button
variant="ghost"
size="icon-sm"
className={cn(
"size-8 opacity-0 transition-opacity group-hover:opacity-100",
inRange
? "text-primary-foreground hover:bg-white/15 hover:text-primary-foreground"
: "text-primary hover:text-primary",
(isFavorite || isSelected) && "opacity-100"
)}
aria-label={
isFavorite
? `Remove ${song.title} from favorites`
: `Favorite ${song.title}`
}
onClick={(event) => {
event.stopPropagation()
onToggleFavorite(song.id)
}}
>
<Star
className={cn(
"size-4",
isFavorite && (inRange ? "fill-current" : "fill-primary")
)}
/>
</Button>
</div>
<SongCover song={song} inRange={inRange} onPlay={play} />
<span className={ALBUM_TRACK_TITLE}>
{song.title}
{song.explicit ? (
<span
className={cn(
"ml-1.5 inline-flex size-3.5 shrink-0 items-center justify-center rounded-sm text-[9px] font-semibold leading-none",
inRange
? "bg-white/20 text-primary-foreground"
: "bg-muted-foreground/20 text-muted-foreground"
)}
aria-label="Explicit"
>
E
</span>
) : null}
</span>
</div>
<div className={songColumnClass}>
<Link
href={"/artist/" + song.artist.id}
className={linkClass}
onClick={(event) => event.stopPropagation()}
>
{song.artist.name}
</Link>
</div>
<div className={songColumnClass}>
<Link
href={"/album/" + song.album.id}
className={linkClass}
onClick={(event) => event.stopPropagation()}
>
{song.album.title}
</Link>
</div>
<div className="min-w-4 flex-1" />
<span
className={cn(
"shrink-0 text-right tabular-nums",
inRange ? "text-primary-foreground" : "text-muted-foreground"
)}
>
{formatDuration(song.durationSeconds)}
</span>
<MediaContextMenu {...menu} />
</div>
</div>
</div>
</div>
</div>
</MediaContextMenuArea>
)
})
+21 -2
View File
@@ -9,13 +9,18 @@ import {
type PaginatedQueryReference,
} from "convex/react"
import type { FunctionReference } from "convex/server"
import { useCallback, useEffect, useSyncExternalStore } from "react"
import { useCallback, useEffect, useMemo, useSyncExternalStore } from "react"
import { useCacheScope } from "@/components/persistent-cache-provider"
import { queryCacheKey } from "@otunes/convex/client/cached-query"
import { readCache, subscribeCache, writeCache } from "@/lib/persistent-cache"
const MAX_CACHED_PAGINATED_ITEMS = 200
const MAX_RESTORED_PAGINATED_ITEMS = 1000
// Items loaded per paginated query this session, so a remount (e.g. back
// navigation) loads as much as before and scroll restoration can reach it.
const loadedItemCounts = new Map<string, number>()
const noopUnsubscribe = () => {}
@@ -65,6 +70,7 @@ export function useCachedQuery<Query extends FunctionReference<"query">>(
/**
* `usePaginatedQuery` that shows the first persisted pages while the first live
* page loads. `isCached` is true while those persisted results are shown.
* Remounts start with as many items as were last loaded this session.
*/
export function useCachedPaginatedQuery<Query extends PaginatedQueryReference>(
query: Query,
@@ -72,15 +78,28 @@ export function useCachedPaginatedQuery<Query extends PaginatedQueryReference>(
options: { initialNumItems: number }
) {
const scope = useCacheScope()
const result = usePaginatedQuery(query, args, options)
const key =
args === "skip"
? null
: `${queryCacheKey(scope, query, args as Record<string, unknown>)}:pages`
// Fixed per key: `usePaginatedQuery` restarts when this changes.
const initialNumItems = useMemo(
() =>
Math.max(
options.initialNumItems,
Math.min(
(key && loadedItemCounts.get(key)) || 0,
MAX_RESTORED_PAGINATED_ITEMS
)
),
[key, options.initialNumItems]
)
const result = usePaginatedQuery(query, args, { initialNumItems })
const cached = useCachedValue<PaginatedQueryItem<Query>[]>(key)
useEffect(() => {
if (key && result.status !== "LoadingFirstPage") {
loadedItemCounts.set(key, result.results.length)
writeCache(key, result.results.slice(0, MAX_CACHED_PAGINATED_ITEMS))
}
}, [key, result.status, result.results])