From c92b7bf1cced78e570dfed9c83e9ab46c4cfe429 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Thu, 22 Oct 2026 18:41:30 -0400 Subject: [PATCH] feat(mobile): add create station context menu action - Add artist station playback to mobile and web media menus - Restrict multi-song station creation to selections by one artist --- .../app/(app)/(tabs)/home/album/[id].tsx | 5 +++- apps/mobile/app/(app)/player.tsx | 2 ++ apps/mobile/components/media-cards.tsx | 1 + apps/mobile/components/media-context-menu.tsx | 11 +++++++ apps/mobile/components/player.tsx | 1 + apps/mobile/hooks/use-media-menu-actions.ts | 28 ++++++++++++++--- apps/web/components/album-card.tsx | 6 +++- apps/web/components/album-view.tsx | 10 ++++++- apps/web/components/media-context-menu.tsx | 15 ++++++++++ apps/web/components/playlist-view.tsx | 21 ++++++++++++- apps/web/components/search-song-item.tsx | 6 +++- apps/web/components/songs-view.tsx | 21 ++++++++++++- apps/web/hooks/use-queue-media-actions.ts | 22 ++++++++++---- apps/web/lib/station-artist.test.mjs | 30 +++++++++++++++++++ apps/web/lib/station-artist.ts | 14 +++++++++ 15 files changed, 178 insertions(+), 15 deletions(-) create mode 100644 apps/web/lib/station-artist.test.mjs create mode 100644 apps/web/lib/station-artist.ts diff --git a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx index df05deb..8c7c878 100644 --- a/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/album/[id].tsx @@ -149,6 +149,7 @@ export default function AlbumScreen() { headerTintColor: palette.foreground, headerRight: () => ( , songIds, - action + action, + album.artist.id as Id<"artists"> ) } > @@ -321,6 +323,7 @@ const AlbumTrackRow = memo(function AlbumTrackRow({ }) { return ( void handleAlbumAction(album, action)} diff --git a/apps/mobile/components/media-context-menu.tsx b/apps/mobile/components/media-context-menu.tsx index 7b800de..eb19a47 100644 --- a/apps/mobile/components/media-context-menu.tsx +++ b/apps/mobile/components/media-context-menu.tsx @@ -20,6 +20,7 @@ export type MediaAction = | "play-next" | "play-last" | "shuffle" + | "create-station" | "favorite" | "go-artist" | "go-album" @@ -35,6 +36,7 @@ export type MediaMenuConfig = { playlistLabel?: string favorited?: boolean showFavorite?: boolean + stationArtistId?: string navigation?: MediaNavigation share?: MediaShareContext includeActions?: MediaAction[] @@ -78,6 +80,11 @@ const MEDIA_ACTION_ITEMS: { label: (countSuffix) => `Shuffle${countSuffix}`, image: "shuffle", }, + { + action: "create-station", + label: () => "Create Station", + image: "shuffle", + }, { action: "go-artist", label: () => "Go to Artist", @@ -105,6 +112,10 @@ function isActionIncluded( action: MediaAction, config: Omit ): boolean { + if (action === "create-station" && !config.stationArtistId) { + return false + } + if (config.includeActions) { return config.includeActions.includes(action) } diff --git a/apps/mobile/components/player.tsx b/apps/mobile/components/player.tsx index 86c4941..ced7f43 100644 --- a/apps/mobile/components/player.tsx +++ b/apps/mobile/components/player.tsx @@ -136,6 +136,7 @@ export const SongListRow = memo(function SongListRow({ inset={inset} onPress={handlePress} contextMenu={{ + stationArtistId: song.artist.id, favorited, playlistLabel, navigation: { diff --git a/apps/mobile/hooks/use-media-menu-actions.ts b/apps/mobile/hooks/use-media-menu-actions.ts index 21fac0f..b367040 100644 --- a/apps/mobile/hooks/use-media-menu-actions.ts +++ b/apps/mobile/hooks/use-media-menu-actions.ts @@ -46,7 +46,8 @@ export function useMediaMenuActions() { toggleSongFavorite, favoriteSongs, } = useFavorites() - const { addToQueueNext, addToQueueLast, playShuffled } = usePlaybackActions() + const { addToQueueNext, addToQueueLast, playShuffled, playArtistShuffled } = + usePlaybackActions() const runQueueAction = useCallback( async (action: MediaAction, songIds: Id<"songs">[]) => { @@ -74,6 +75,11 @@ export function useMediaMenuActions() { async (song: BrowseSong, action: MediaAction) => { const songIds = [song.id as Id<"songs">] switch (action) { + case "create-station": + await runMediaAction(() => + playArtistShuffled(song.artist.id as Id<"artists">) + ) + break case "playlist": openAddToPlaylist(songIds) break @@ -95,13 +101,22 @@ export function useMediaMenuActions() { break } }, - [openAddToPlaylist, push, runQueueAction, toggleSongFavorite] + [ + openAddToPlaylist, + push, + runQueueAction, + toggleSongFavorite, + playArtistShuffled, + ] ) const handleAlbumAction = useCallback( async (album: BrowseAlbum, action: MediaAction) => { await runMediaAction(async () => { switch (action) { + case "create-station": + await playArtistShuffled(album.artist.id as Id<"artists">) + break case "playlist": await openAddAlbumToPlaylist(album.id as Id<"albums">) break @@ -132,6 +147,7 @@ export function useMediaMenuActions() { convex, cacheScope, openAddAlbumToPlaylist, + playArtistShuffled, push, runQueueAction, toggleAlbumFavorite, @@ -207,9 +223,13 @@ export function useMediaMenuActions() { async ( albumId: Id<"albums">, songIds: Id<"songs">[], - action: MediaAction + action: MediaAction, + artistId: Id<"artists"> ) => { switch (action) { + case "create-station": + await runMediaAction(() => playArtistShuffled(artistId)) + break case "favorite": await toggleAlbumFavorite(albumId) break @@ -225,7 +245,7 @@ export function useMediaMenuActions() { break } }, - [openAddToPlaylist, runQueueAction, toggleAlbumFavorite] + [openAddToPlaylist, runQueueAction, toggleAlbumFavorite, playArtistShuffled] ) const handleAlbumTrackAction = useCallback( diff --git a/apps/web/components/album-card.tsx b/apps/web/components/album-card.tsx index d4c287a..b17a7a6 100644 --- a/apps/web/components/album-card.tsx +++ b/apps/web/components/album-card.tsx @@ -49,7 +49,7 @@ export function AlbumCard({ album }: { album: BrowseAlbum }) { const { openAddAlbumToPlaylist } = useAddToPlaylist() const { isAlbumFavorite, toggleAlbumFavorite } = useFavorites() const { playAlbum } = usePlayback() - const { runQueueAction } = useQueueMediaActions() + const { runQueueAction, createStation } = useQueueMediaActions() const [menuOpen, setMenuOpen] = React.useState(false) const [hovered, setHovered] = React.useState(false) const showActions = hovered || menuOpen @@ -71,6 +71,9 @@ export function AlbumCard({ album }: { album: BrowseAlbum }) { async function handleCardAction(action: MediaContextAction) { switch (action) { + case "create-station": + void createStation(album.artist.id) + break case "playlist": void openAddAlbumToPlaylist(album.id as Id<"albums">) break @@ -153,6 +156,7 @@ export function AlbumCard({ album }: { album: BrowseAlbum }) { void handleCardAction(action)} favorited={isAlbumFavorite(album.id)} diff --git a/apps/web/components/album-view.tsx b/apps/web/components/album-view.tsx index 2ac3df5..9a8cb2c 100644 --- a/apps/web/components/album-view.tsx +++ b/apps/web/components/album-view.tsx @@ -228,6 +228,7 @@ function TrackRow({ album.tracks.map((track) => track.id as Id<"songs">), [album.tracks] @@ -330,6 +331,9 @@ export function AlbumView({ ? [activeTrackId] : [] switch (action) { + case "create-station": + void createStation(album.artist.id) + break case "favorite": void favoriteSongs(targetIds as Id<"songs">[]) break @@ -348,6 +352,9 @@ export function AlbumView({ 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 @@ -442,6 +449,7 @@ export function AlbumView({ {}} onAction={handleAlbumMediaAction} diff --git a/apps/web/components/media-context-menu.tsx b/apps/web/components/media-context-menu.tsx index 3c5b729..9621781 100644 --- a/apps/web/components/media-context-menu.tsx +++ b/apps/web/components/media-context-menu.tsx @@ -33,6 +33,7 @@ export type MediaContextAction = | "play-next" | "play-last" | "shuffle" + | "create-station" | "favorite" export type MediaContextNavigation = { @@ -65,6 +66,7 @@ export function MediaContextMenuItems({ playlistLabel = "Add to Playlist", showFavorite = true, favorited = false, + stationArtistId, navigation, share, }: { @@ -73,6 +75,7 @@ export function MediaContextMenuItems({ playlistLabel?: string showFavorite?: boolean favorited?: boolean + stationArtistId?: string navigation?: MediaContextNavigation share?: MediaShareContext }) { @@ -122,6 +125,15 @@ export function MediaContextMenuItems({ {actionLabel("Shuffle", selectedCount)} + {stationArtistId ? ( + onAction("create-station")} + > + Create Station + + + ) : null} {artist || album ? ( <> @@ -193,6 +205,7 @@ export function MediaContextMenu({ playlistLabel, showFavorite = true, favorited = false, + stationArtistId, navigation, share, inSelectionRange = false, @@ -210,6 +223,7 @@ export function MediaContextMenu({ playlistLabel?: string showFavorite?: boolean favorited?: boolean + stationArtistId?: string navigation?: MediaContextNavigation share?: MediaShareContext inSelectionRange?: boolean @@ -251,6 +265,7 @@ export function MediaContextMenu({ playlistLabel={playlistLabel} showFavorite={showFavorite} favorited={favorited} + stationArtistId={stationArtistId} navigation={navigation} share={share} /> diff --git a/apps/web/components/playlist-view.tsx b/apps/web/components/playlist-view.tsx index 76a9e55..b33533c 100644 --- a/apps/web/components/playlist-view.tsx +++ b/apps/web/components/playlist-view.tsx @@ -1,5 +1,6 @@ "use client" +import { stationArtistId } from "@/lib/station-artist" import * as React from "react" import Image from "next/image" import { useRouter } from "next/navigation" @@ -124,6 +125,7 @@ function PlaylistSongRow({ selectionPosition, isFavorite, selectedCount, + stationArtist, isDragging, onSelect, onPointerDown, @@ -147,6 +149,7 @@ function PlaylistSongRow({ selectionPosition: SelectionRangePosition isFavorite: boolean selectedCount: number + stationArtist?: string isDragging: boolean onSelect: (event: React.MouseEvent) => void onPointerDown: (event: React.MouseEvent) => void @@ -226,6 +229,7 @@ function PlaylistSongRow({ stationArtistId(songs, selectedIds), + [songs, selectedIds] + ) + const selectionPositions = React.useMemo( () => getSelectionRangePositions(songs.map((song) => song.id), selectedIds), [songs, selectedIds] @@ -363,6 +372,11 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { 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 @@ -637,6 +651,11 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { ? selectedCount : 1 } + stationArtist={ + isSelected(song.id) + ? selectedStationArtistId + : song.artist.id + } isDragging={dragIndex === index} onPointerDown={handlePointerDown} onSelect={(event) => { diff --git a/apps/web/components/search-song-item.tsx b/apps/web/components/search-song-item.tsx index 3023ab2..136d8ee 100644 --- a/apps/web/components/search-song-item.tsx +++ b/apps/web/components/search-song-item.tsx @@ -16,11 +16,14 @@ import { albumCoverUrl } from "@/lib/cover-urls" export function SearchSongItem({ song }: { song: BrowseSong }) { const { openAddToPlaylist } = useAddToPlaylist() const { isSongFavorite, toggleSongFavorite } = useFavorites() - const { runQueueAction } = useQueueMediaActions() + const { runQueueAction, createStation } = useQueueMediaActions() const songIds = [song.id as Id<"songs">] function handleAction(action: MediaContextAction) { switch (action) { + case "create-station": + void createStation(song.artist.id) + break case "playlist": openAddToPlaylist(songIds) break @@ -64,6 +67,7 @@ export function SearchSongItem({ song }: { song: BrowseSong }) { {}} onAction={handleAction} diff --git a/apps/web/components/songs-view.tsx b/apps/web/components/songs-view.tsx index 63bb749..3271feb 100644 --- a/apps/web/components/songs-view.tsx +++ b/apps/web/components/songs-view.tsx @@ -1,5 +1,6 @@ "use client" +import { stationArtistId } from "@/lib/station-artist" import { useEffect, useMemo, useState, useRef, MouseEvent } from "react" import Image from "next/image" import Link from "next/link" @@ -115,6 +116,7 @@ function SongRow({ selectionPosition, isFavorite, selectedCount, + stationArtist, onSelect, onPointerDown, onToggleFavorite, @@ -127,6 +129,7 @@ function SongRow({ selectionPosition: SelectionRangePosition isFavorite: boolean selectedCount: number + stationArtist?: string onSelect: (event: MouseEvent) => void onPointerDown: (event: MouseEvent) => void onToggleFavorite: () => void @@ -238,6 +241,7 @@ function SongRow({ ("title") const [sortDirection, setSortDirection] = useState("asc") @@ -313,6 +317,11 @@ export function SongsView() { clearSelection, } = useTrackSelection(songs) + const selectedStationArtistId = useMemo( + () => stationArtistId(songs, selectedIds), + [songs, selectedIds] + ) + const selectionPositions = useMemo( () => getSelectionRangePositions( @@ -330,6 +339,11 @@ export function SongsView() { ? [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 @@ -449,6 +463,11 @@ export function SongsView() { isSelected={isSelected(song.id)} selectionPosition={selectionPositions[index]} isFavorite={isSongFavorite(song.id)} + stationArtist={ + isSelected(song.id) + ? selectedStationArtistId + : song.artist.id + } selectedCount={ isSelected(song.id) && selectedCount > 0 ? selectedCount diff --git a/apps/web/hooks/use-queue-media-actions.ts b/apps/web/hooks/use-queue-media-actions.ts index c676020..2837a81 100644 --- a/apps/web/hooks/use-queue-media-actions.ts +++ b/apps/web/hooks/use-queue-media-actions.ts @@ -23,14 +23,13 @@ function queueActionMessage(action: QueueMediaAction, count: number) { ? `Queued ${count} songs to play last` : "Queued to play last" case "shuffle": - return count > 1 - ? `Playing ${count} songs shuffled` - : "Playing shuffled" + return count > 1 ? `Playing ${count} songs shuffled` : "Playing shuffled" } } export function useQueueMediaActions() { - const { addToQueueNext, addToQueueLast, playShuffled } = usePlayback() + const { addToQueueNext, addToQueueLast, playShuffled, playArtistShuffled } = + usePlayback() const runQueueAction = React.useCallback( async (action: QueueMediaAction, songIds: Id<"songs">[]) => { @@ -60,5 +59,18 @@ export function useQueueMediaActions() { [addToQueueNext, addToQueueLast, playShuffled] ) - return { runQueueAction } + const createStation = React.useCallback( + async (artistId: string) => { + try { + await playArtistShuffled(artistId as Id<"artists">) + } catch (error) { + toast.error( + error instanceof Error ? error.message : "Failed to create station" + ) + } + }, + [playArtistShuffled] + ) + + return { runQueueAction, createStation } } diff --git a/apps/web/lib/station-artist.test.mjs b/apps/web/lib/station-artist.test.mjs new file mode 100644 index 0000000..da75532 --- /dev/null +++ b/apps/web/lib/station-artist.test.mjs @@ -0,0 +1,30 @@ +import assert from "node:assert/strict" +import test from "node:test" + +import { stationArtistId } from "./station-artist.ts" + +const songs = [ + { id: "song-a", artist: { id: "artist-a" } }, + { id: "song-b", artist: { id: "artist-a" } }, + { id: "song-c", artist: { id: "artist-b" } }, +] + +test("a single song or a selection sharing one artist can create a station", () => { + assert.equal(stationArtistId(songs, new Set(["song-c"])), "artist-b") + assert.equal( + stationArtistId(songs, new Set(["song-a", "song-b"])), + "artist-a" + ) +}) + +test("mixed artists cannot create a station", () => { + assert.equal(stationArtistId(songs, new Set(["song-a", "song-c"])), undefined) +}) + +test("empty or partially missing selections cannot create a station", () => { + assert.equal(stationArtistId(songs, new Set()), undefined) + assert.equal( + stationArtistId(songs, new Set(["song-a", "missing"])), + undefined + ) +}) diff --git a/apps/web/lib/station-artist.ts b/apps/web/lib/station-artist.ts new file mode 100644 index 0000000..f31373d --- /dev/null +++ b/apps/web/lib/station-artist.ts @@ -0,0 +1,14 @@ +type ArtistItem = { id: string; artist: { id: string } } + +/** Returns an artist only when every target item is present and shares it. */ +export function stationArtistId( + items: ArtistItem[], + targetIds: ReadonlySet +) { + const targets = items.filter((item) => targetIds.has(item.id)) + const artistId = targets[0]?.artist.id + return targets.length === targetIds.size && + targets.every((item) => item.artist.id === artistId) + ? artistId + : undefined +} -- 2.54.1