Files
otunes/apps/mobile/components/media-context-menu.tsx
T

202 lines
4.6 KiB
TypeScript

import { MenuView, type MenuAction } from "@expo/ui/community/menu"
import type { ReactNode } from "react"
import type { StyleProp, ViewStyle } from "react-native"
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"
| "favorite"
| "go-artist"
| "go-album"
| "delete-playlist"
export type MediaNavigation = {
artist?: { id: string }
album?: { id: string }
}
export type MediaMenuConfig = {
selectedCount?: number
playlistLabel?: string
favorited?: boolean
showFavorite?: boolean
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
}[] = [
{
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: "go-artist",
label: () => "Go to Artist",
image: "person",
},
{
action: "go-album",
label: () => "Go to Album",
image: "opticaldisc",
},
{
action: "favorite",
label: (countSuffix, favorited) =>
`${favorited ? "Unfavorite" : "Favorite"}${countSuffix}`,
image: "star",
},
{
action: "delete-playlist",
label: () => "Delete Playlist",
image: "trash",
},
]
function isActionIncluded(
action: MediaAction,
config: Omit<MediaMenuConfig, "onAction">
): boolean {
if (config.includeActions) {
return config.includeActions.includes(action)
}
if (action === "delete-playlist") {
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,
})
}
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>
)
}
export function MediaContextMenu(props: MediaMenuWrapperProps) {
return <MediaMenuBase {...props} shouldOpenOnLongPress />
}
export function MediaActionMenu(props: MediaMenuWrapperProps) {
return <MediaMenuBase {...props} />
}
export const MEDIA_QUEUE_ACTIONS = QUEUE_ACTIONS