diff --git a/apps/web/components/album-view.tsx b/apps/web/components/album-view.tsx
index 4d55e83..d82bcda 100644
--- a/apps/web/components/album-view.tsx
+++ b/apps/web/components/album-view.tsx
@@ -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 (
-
{
- 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}
+
{
+ 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"
+ )}
>
-
-
-
-
-
-
-
-
{track.title}
- {track.artists ? (
-
- {track.artists.join(", ")}
-
- ) : null}
-
-
-
+ {!inRange && isActive ?
: null}
+ {!inRange && !isActive ?
: 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,
- }}
- />
+
+
+
+
{track.title}
+ {track.artists ? (
+
+ {track.artists.join(", ")}
+
+ ) : null}
+
+
+
+ {formatDuration(track.durationSeconds)}
+
+
+
+
-
+
)
}
diff --git a/apps/web/components/artists-view.tsx b/apps/web/components/artists-view.tsx
index 2322989..a1a0938 100644
--- a/apps/web/components/artists-view.tsx
+++ b/apps/web/components/artists-view.tsx
@@ -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 (
-
-
+
)
}
diff --git a/apps/web/components/media-context-menu.tsx b/apps/web/components/media-context-menu.tsx
index d216401..032dac0 100644
--- a/apps/web/components/media-context-menu.tsx
+++ b/apps/web/components/media-context-menu.tsx
@@ -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
+) {
+ return React.use(MenuKindContext) === "context" ? (
+
+ ) : (
+
+ )
+}
+
+export function MediaMenuSeparator() {
+ return React.use(MenuKindContext) === "context" ? (
+
+ ) : (
+
+ )
+}
+
function actionLabel(base: string, selectedCount: number) {
return selectedCount > 1 ? `${base} (${selectedCount})` : base
}
@@ -83,86 +112,86 @@ export function MediaContextMenuItems({
<>
{showShare ? (
<>
- void copyMediaShareLink(share!)}
>
Share
-
-
+
+
>
) : null}
- onAction("playlist")}
>
{actionLabel(playlistLabel, selectedCount)}
-
-
-
+
+ onAction("play-next")}
>
{actionLabel("Play Next", selectedCount)}
-
-
+ onAction("play-last")}
>
{actionLabel("Play Last", selectedCount)}
-
-
+ onAction("shuffle")}
>
{actionLabel("Shuffle", selectedCount)}
-
+
{stationArtistId ? (
- onAction("create-station")}
>
Create Station
-
+
) : null}
{artist || album ? (
<>
-
+
{artist ? (
-
+
Go to Artist
-
+
) : null}
{album ? (
-
+
Go to Album
-
+
) : null}
>
) : null}
{showFavorite ? (
<>
-
-
+ onAction("favorite")}
>
{actionLabel(favorited ? "Unfavorite" : "Favorite", selectedCount)}
-
+
>
) : 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 (
+ {
+ if (open) {
+ menu.onOpenMenu()
+ }
+ }}
+ >
+ {children}
+
+
+
+ {menu.children}
+
+
+
+ )
+}
+
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 (
{
diff --git a/apps/web/components/playlist-view.tsx b/apps/web/components/playlist-view.tsx
index 01b50d4..a5da623 100644
--- a/apps/web/components/playlist-view.tsx
+++ b/apps/web/components/playlist-view.tsx
@@ -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 ? (
+ <>
+
+ onRemove(song.id)}
+ >
+ {removeLabel}
+
+
+ >
+ ) : null,
+ }
+
return (
- 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"
- )}
- >
-
- {!inRange && isActive ?
: null}
- {!inRange && !isActive ?
: null}
+
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"
+ )}
>
-
onToggleFavorite(song.id)}
- />
-
-
-
- {song.title}
-
-
+ {!inRange && isActive ? : null}
+ {!inRange && !isActive ? : null}
+
- {formatDuration(song.durationSeconds)}
-
+
onToggleFavorite(song.id)}
+ />
- 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 ? (
- <>
-
- onRemove(song.id)}
- >
- {removeLabel}
-
-
- >
- ) : null}
-
+
+
+ {song.title}
+
+
+ {formatDuration(song.durationSeconds)}
+
+
+
+
-
+
)
})
diff --git a/apps/web/components/search-song-item.tsx b/apps/web/components/search-song-item.tsx
index 136d8ee..b4bfe05 100644
--- a/apps/web/components/search-song-item.tsx
+++ b/apps/web/components/search-song-item.tsx
@@ -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 (
-
-
-
-
-
-
-
-
{song.title}
-
+
+
- {}}
- 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"
- />
-
+
+
+
{song.title}
+
+
+
{song.artist.name}
+
+
+
+
+
+
)
}
diff --git a/apps/web/components/songs-view.tsx b/apps/web/components/songs-view.tsx
index b2bb036..0646849 100644
--- a/apps/web/components/songs-view.tsx
+++ b/apps/web/components/songs-view.tsx
@@ -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 (
- 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"
- >
-
-
- {!inRange ?
: null}
+
+ 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"
+ >
-
-
- {
- event.stopPropagation()
- onToggleFavorite(song.id)
- }}
- >
-
-
-
-
-
- {song.title}
- {song.explicit ? (
-
- E
-
- ) : null}
-
-
-
- event.stopPropagation()}
- >
- {song.artist.name}
-
-
-
- event.stopPropagation()}
- >
- {song.album.title}
-
-
-
-
+ {!inRange ?
: null}
+
- {formatDuration(song.durationSeconds)}
-
-
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,
- }}
- />
+
+
+ {
+ event.stopPropagation()
+ onToggleFavorite(song.id)
+ }}
+ >
+
+
+
+
+
+ {song.title}
+ {song.explicit ? (
+
+ E
+
+ ) : null}
+
+
+
+ event.stopPropagation()}
+ >
+ {song.artist.name}
+
+
+
+ event.stopPropagation()}
+ >
+ {song.album.title}
+
+
+
+
+ {formatDuration(song.durationSeconds)}
+
+
+
-
-
+
+
)
})
diff --git a/apps/web/hooks/use-cached-query.ts b/apps/web/hooks/use-cached-query.ts
index 87da8ff..98950ca 100644
--- a/apps/web/hooks/use-cached-query.ts
+++ b/apps/web/hooks/use-cached-query.ts
@@ -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()
const noopUnsubscribe = () => {}
@@ -65,6 +70,7 @@ export function useCachedQuery>(
/**
* `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: Query,
@@ -72,15 +78,28 @@ export function useCachedPaginatedQuery(
options: { initialNumItems: number }
) {
const scope = useCacheScope()
- const result = usePaginatedQuery(query, args, options)
const key =
args === "skip"
? null
: `${queryCacheKey(scope, query, args as Record)}: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[]>(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])