Playlists draw a 2x2 of their first albums automatically. Owners can pick from random grids, reshuffle, or upload an image on web and mobile. Covers are rendered once into Convex storage and redrawn only when the playlist's albums change. Web and iPad sidebars show playlist covers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
421 lines
11 KiB
TypeScript
421 lines
11 KiB
TypeScript
import type { MenuAction } from "@expo/ui/community/menu"
|
|
import { MenuView } from "@/components/media-menu"
|
|
import { requireNativeView } from "expo"
|
|
import type { ComponentType, ReactNode } from "react"
|
|
import {
|
|
Platform,
|
|
Text,
|
|
useWindowDimensions,
|
|
View,
|
|
type StyleProp,
|
|
type ViewProps,
|
|
type ViewStyle,
|
|
} from "react-native"
|
|
|
|
import { Ionicons } from "@expo/vector-icons"
|
|
import { CoverImage } from "@/components/cover-image"
|
|
import { useThemeColors } from "@/lib/theme"
|
|
import { useAppNavigation } from "@/lib/app-navigation"
|
|
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import type { MediaShareContext } from "@/lib/media-links"
|
|
import { shouldShowShare } from "@/lib/media-links"
|
|
import { shareMediaLink } from "@/lib/share-media-link"
|
|
|
|
export type MediaAction =
|
|
| "share"
|
|
| "playlist"
|
|
| "play-next"
|
|
| "play-last"
|
|
| "shuffle"
|
|
| "create-station"
|
|
| "favorite"
|
|
| "go-artist"
|
|
| "go-album"
|
|
| "remove-from-playlist"
|
|
| "change-cover"
|
|
| "delete-playlist"
|
|
|
|
export type MediaNavigation = {
|
|
artist?: { id: string }
|
|
album?: { id: string }
|
|
}
|
|
|
|
export type MediaMenuPreview = {
|
|
title: string
|
|
artist?: string
|
|
album?: string
|
|
artworkUrl: string
|
|
hasArtwork: boolean
|
|
kind: "album" | "artist" | "song"
|
|
}
|
|
|
|
export type MediaMenuConfig = {
|
|
preview?: MediaMenuPreview
|
|
selectedCount?: number
|
|
playlistLabel?: string
|
|
favorited?: boolean
|
|
showFavorite?: boolean
|
|
showRemoveFromPlaylist?: boolean
|
|
stationArtistId?: string
|
|
navigation?: MediaNavigation
|
|
share?: MediaShareContext
|
|
includeActions?: MediaAction[]
|
|
onAction: (action: MediaAction) => void
|
|
}
|
|
|
|
type MediaMenuWrapperProps = MediaMenuConfig & {
|
|
children: ReactNode
|
|
style?: StyleProp<ViewStyle>
|
|
}
|
|
|
|
const QUEUE_ACTIONS: MediaAction[] = ["play-next", "play-last", "shuffle"]
|
|
|
|
const MEDIA_ACTION_ITEMS: {
|
|
action: MediaAction
|
|
label: (
|
|
countSuffix: string,
|
|
favorited: boolean | undefined,
|
|
playlistLabel: string
|
|
) => string
|
|
image: NonNullable<MenuAction["image"]> & string
|
|
destructive?: boolean
|
|
}[] = [
|
|
{
|
|
action: "playlist",
|
|
label: (countSuffix, _favorited, playlistLabel) =>
|
|
`${playlistLabel}${countSuffix}`,
|
|
image: "text.badge.plus",
|
|
},
|
|
{
|
|
action: "play-next",
|
|
label: (countSuffix) => `Play Next${countSuffix}`,
|
|
image: "text.line.first.and.arrowtriangle.forward",
|
|
},
|
|
{
|
|
action: "play-last",
|
|
label: (countSuffix) => `Play Last${countSuffix}`,
|
|
image: "text.line.last.and.arrowtriangle.forward",
|
|
},
|
|
{
|
|
action: "shuffle",
|
|
label: (countSuffix) => `Shuffle${countSuffix}`,
|
|
image: "shuffle",
|
|
},
|
|
{
|
|
action: "create-station",
|
|
label: () => "Create Station",
|
|
image: "shuffle",
|
|
},
|
|
{
|
|
action: "go-artist",
|
|
label: () => "Go to Artist",
|
|
image: "music.mic",
|
|
},
|
|
{
|
|
action: "go-album",
|
|
label: () => "Go to Album",
|
|
image: "square.stack",
|
|
},
|
|
{
|
|
action: "favorite",
|
|
label: (countSuffix, favorited) =>
|
|
`${favorited ? "Unfavorite" : "Favorite"}${countSuffix}`,
|
|
image: "star",
|
|
},
|
|
{
|
|
action: "remove-from-playlist",
|
|
label: () => "Remove from Playlist",
|
|
image: "minus.circle",
|
|
destructive: true,
|
|
},
|
|
{
|
|
action: "change-cover",
|
|
label: () => "Change Cover",
|
|
image: "photo",
|
|
},
|
|
{
|
|
action: "delete-playlist",
|
|
label: () => "Delete Playlist",
|
|
image: "trash",
|
|
},
|
|
]
|
|
|
|
function isActionIncluded(
|
|
action: MediaAction,
|
|
config: Omit<MediaMenuConfig, "onAction">
|
|
): boolean {
|
|
if (action === "create-station" && !config.stationArtistId) {
|
|
return false
|
|
}
|
|
|
|
if (action === "remove-from-playlist") {
|
|
return config.showRemoveFromPlaylist === true
|
|
}
|
|
|
|
if (config.includeActions) {
|
|
return config.includeActions.includes(action)
|
|
}
|
|
|
|
if (action === "delete-playlist" || action === "change-cover") {
|
|
return false
|
|
}
|
|
|
|
if (action === "share") {
|
|
return shouldShowShare(config.share, config.selectedCount ?? 1)
|
|
}
|
|
|
|
if (action === "favorite" && config.showFavorite === false) {
|
|
return false
|
|
}
|
|
|
|
if (action === "go-artist" && !config.navigation?.artist) {
|
|
return false
|
|
}
|
|
|
|
if (action === "go-album" && !config.navigation?.album) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
export function buildMediaMenuActions(
|
|
config: Omit<MediaMenuConfig, "onAction">
|
|
): MenuAction[] {
|
|
const {
|
|
selectedCount = 1,
|
|
favorited,
|
|
playlistLabel = "Add to Playlist",
|
|
} = config
|
|
const countSuffix = selectedCount > 1 ? ` (${selectedCount})` : ""
|
|
|
|
const actions: MenuAction[] = []
|
|
|
|
if (isActionIncluded("share", config)) {
|
|
actions.push({
|
|
id: "share",
|
|
title: "Share",
|
|
image: "square.and.arrow.up",
|
|
})
|
|
}
|
|
|
|
for (const item of MEDIA_ACTION_ITEMS) {
|
|
if (!isActionIncluded(item.action, config)) {
|
|
continue
|
|
}
|
|
|
|
actions.push({
|
|
id: item.action,
|
|
title: item.label(countSuffix, favorited, playlistLabel),
|
|
image: item.action === "favorite" && favorited ? "star.fill" : item.image,
|
|
...(item.destructive ? { attributes: { destructive: true } } : {}),
|
|
})
|
|
}
|
|
|
|
return actions
|
|
}
|
|
|
|
function MediaMenuBase({
|
|
children,
|
|
style,
|
|
shouldOpenOnLongPress,
|
|
onAction,
|
|
share,
|
|
...actionConfig
|
|
}: MediaMenuWrapperProps & { shouldOpenOnLongPress?: boolean }) {
|
|
const { config } = useServerConfig()
|
|
|
|
return (
|
|
<MenuView
|
|
shouldOpenOnLongPress={shouldOpenOnLongPress}
|
|
actions={buildMediaMenuActions({ ...actionConfig, share })}
|
|
onPressAction={(event) => {
|
|
const action = event.nativeEvent.event as MediaAction
|
|
if (action === "share" && share && config) {
|
|
void shareMediaLink(config.serverUrl, share)
|
|
return
|
|
}
|
|
onAction(action)
|
|
}}
|
|
style={[{ alignSelf: "stretch" }, style]}
|
|
>
|
|
{children}
|
|
</MenuView>
|
|
)
|
|
}
|
|
|
|
// Long-press menus are mounted once per list row. `MenuView` hosts a SwiftUI
|
|
// view tree per row, which makes opening long lists stall the main thread, so
|
|
// on iOS rows use the UIKit context menu view that ships with expo-router.
|
|
const NativeContextMenuView =
|
|
Platform.OS === "ios"
|
|
? requireNativeView<
|
|
ViewProps & {
|
|
disableForceFlatten?: boolean
|
|
children: ReactNode
|
|
onPreviewTappedAnimationCompleted?: () => void
|
|
}
|
|
>("ExpoRouterNativeLinkPreview", "NativeLinkPreviewView")
|
|
: null
|
|
|
|
const NativeContextMenuPreview =
|
|
Platform.OS === "ios"
|
|
? requireNativeView<
|
|
ViewProps & {
|
|
preferredContentSize: { width: number; height: number }
|
|
children: ReactNode
|
|
}
|
|
>("ExpoRouterNativeLinkPreview", "NativeLinkPreviewContentView")
|
|
: null
|
|
|
|
function MediaPreview({ preview }: { preview: MediaMenuPreview }) {
|
|
const colors = useThemeColors()
|
|
const { width: windowWidth, fontScale } = useWindowDimensions()
|
|
if (!NativeContextMenuPreview) return null
|
|
|
|
const width = Math.min(360, windowWidth - 32)
|
|
const height = Math.max(124, 32 + 76 * fontScale)
|
|
const isArtist = preview.kind === "artist"
|
|
return (
|
|
// Match Expo Router's preview wrapper: UIKit mounts this separately, but
|
|
// Yoga must also keep it out of the trigger's normal layout.
|
|
<NativeContextMenuPreview
|
|
preferredContentSize={{ width, height }}
|
|
style={{ position: "absolute", top: 0, left: 0 }}
|
|
>
|
|
<View
|
|
style={{
|
|
width,
|
|
height,
|
|
padding: 16,
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: 16,
|
|
backgroundColor: colors.card,
|
|
}}
|
|
>
|
|
<CoverImage
|
|
uri={preview.artworkUrl}
|
|
hasCover={preview.hasArtwork}
|
|
style={{ width: 88, height: 88, borderRadius: isArtist ? 44 : 12 }}
|
|
/>
|
|
<View style={{ flex: 1, gap: 2 }}>
|
|
<Text
|
|
numberOfLines={2}
|
|
style={{
|
|
color: colors.foreground,
|
|
fontSize: 17,
|
|
fontWeight: "600",
|
|
}}
|
|
>
|
|
{preview.title}
|
|
</Text>
|
|
{preview.artist ? (
|
|
<Text
|
|
numberOfLines={1}
|
|
style={{ color: colors.mutedForeground, fontSize: 15 }}
|
|
>
|
|
{preview.artist}
|
|
</Text>
|
|
) : null}
|
|
{preview.album ? (
|
|
<Text
|
|
numberOfLines={1}
|
|
style={{ color: colors.mutedForeground, fontSize: 14 }}
|
|
>
|
|
{preview.album}
|
|
</Text>
|
|
) : null}
|
|
</View>
|
|
{preview.kind !== "song" ? (
|
|
<Ionicons
|
|
name="chevron-forward"
|
|
size={20}
|
|
color={colors.mutedForeground}
|
|
/>
|
|
) : null}
|
|
</View>
|
|
</NativeContextMenuPreview>
|
|
)
|
|
}
|
|
|
|
const NativeContextMenuAction: ComponentType<{
|
|
identifier: string
|
|
title: string
|
|
icon?: string
|
|
destructive?: boolean
|
|
onSelected: () => void
|
|
}> | null =
|
|
Platform.OS === "ios"
|
|
? requireNativeView(
|
|
"ExpoRouterNativeLinkPreview",
|
|
"LinkPreviewNativeActionView"
|
|
)
|
|
: null
|
|
|
|
function NativeMediaContextMenu({
|
|
children,
|
|
preview,
|
|
onAction,
|
|
share,
|
|
...actionConfig
|
|
}: MediaMenuWrapperProps) {
|
|
const { config } = useServerConfig()
|
|
const { push } = useAppNavigation()
|
|
|
|
if (!NativeContextMenuView || !NativeContextMenuAction) {
|
|
return children
|
|
}
|
|
|
|
const handleAction = (action: MediaAction) => {
|
|
if (action === "share" && share && config) {
|
|
void shareMediaLink(config.serverUrl, share)
|
|
return
|
|
}
|
|
onAction(action)
|
|
}
|
|
|
|
return (
|
|
<NativeContextMenuView
|
|
style={{ display: "contents" }}
|
|
disableForceFlatten
|
|
onPreviewTappedAnimationCompleted={() => {
|
|
if (share?.type === "artist") push(`/artist/${share.artistId}`)
|
|
else if (share?.type === "album" || share?.type === "song")
|
|
push(`/album/${share.albumId}`)
|
|
}}
|
|
>
|
|
{/* The native menu retains its first trigger. Keep that view mounted when
|
|
focus changes replace the Siri entity wrapper inside it. */}
|
|
<View collapsable={false}>{children}</View>
|
|
{preview && (actionConfig.selectedCount ?? 1) === 1 ? (
|
|
<MediaPreview preview={preview} />
|
|
) : null}
|
|
{buildMediaMenuActions({ ...actionConfig, share }).map((action) => (
|
|
<NativeContextMenuAction
|
|
key={action.id}
|
|
identifier={action.id ?? action.title}
|
|
title={action.title}
|
|
icon={typeof action.image === "string" ? action.image : undefined}
|
|
destructive={action.attributes?.destructive}
|
|
onSelected={() => handleAction(action.id as MediaAction)}
|
|
/>
|
|
))}
|
|
</NativeContextMenuView>
|
|
)
|
|
}
|
|
|
|
export function MediaContextMenu(props: MediaMenuWrapperProps) {
|
|
if (Platform.OS === "ios") {
|
|
return <NativeMediaContextMenu {...props} />
|
|
}
|
|
return <MediaMenuBase {...props} shouldOpenOnLongPress />
|
|
}
|
|
|
|
export function MediaActionMenu(props: MediaMenuWrapperProps) {
|
|
return <MediaMenuBase {...props} />
|
|
}
|
|
|
|
export const MEDIA_QUEUE_ACTIONS = QUEUE_ACTIONS
|