285 lines
7.2 KiB
TypeScript
285 lines
7.2 KiB
TypeScript
import { SongSwipeRow } from "@/components/song-swipe-row"
|
|
import type { SwipeRowGroup } from "@/components/ios-swipe-to-remove-row"
|
|
import { PaletteText } from "@/components/palette-text"
|
|
import { MusicEntityView } from "@/components/music-entity-view"
|
|
import { collectionInset } from "@/components/collection-list"
|
|
import { CoverImage } from "@/components/cover-image"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import type { BrowseSong } from "@otunes/convex/types"
|
|
import type { CoverPalette } from "@/lib/cover-palette"
|
|
import { formatDuration } from "@/lib/format"
|
|
import { albumCoverUrl } from "@/lib/urls"
|
|
import {
|
|
miniPlayerHeight,
|
|
tabletMiniPlayerHeight,
|
|
spacing,
|
|
useThemeColors,
|
|
} from "@/lib/theme"
|
|
import { memo, useCallback } from "react"
|
|
import { Platform, Pressable, StyleSheet, View } from "react-native"
|
|
|
|
import {
|
|
MediaContextMenu,
|
|
type MediaAction,
|
|
type MediaMenuConfig,
|
|
} from "@/components/media-context-menu"
|
|
|
|
function consumeLongPress() {}
|
|
|
|
export const SongRow = memo(function SongRow({
|
|
song,
|
|
onPress,
|
|
contextMenu,
|
|
trailing,
|
|
palette,
|
|
inset = false,
|
|
swipe,
|
|
}: {
|
|
song: BrowseSong
|
|
onPress?: () => void
|
|
contextMenu?: Omit<MediaMenuConfig, "onAction"> & {
|
|
onAction: MediaMenuConfig["onAction"]
|
|
}
|
|
trailing?: React.ReactNode
|
|
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
|
|
swipe?: {
|
|
group: SwipeRowGroup
|
|
onPlayNext: () => void
|
|
onAddToQueue: () => void
|
|
onRemove?: () => void
|
|
backgroundColor: string
|
|
}
|
|
/** Album/playlist layout: the divider starts at the title, not the artwork. */
|
|
inset?: boolean
|
|
}) {
|
|
const { config } = useServerConfig()
|
|
const colors = useThemeColors()
|
|
if (!config) return null
|
|
|
|
const foreground = palette?.foreground ?? colors.foreground
|
|
const secondaryForeground =
|
|
palette?.secondaryForeground ?? colors.mutedForeground
|
|
const divider = palette?.divider ?? colors.border
|
|
|
|
const content = (
|
|
<>
|
|
<View style={styles.rowMeta}>
|
|
<PaletteText
|
|
tone={palette ? "foreground" : undefined}
|
|
style={[styles.rowTitle, { color: foreground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{song.title}
|
|
</PaletteText>
|
|
<PaletteText
|
|
tone={palette ? "secondaryForeground" : undefined}
|
|
style={[styles.rowSubtitle, { color: secondaryForeground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{song.artist.name} · {song.album.title}
|
|
</PaletteText>
|
|
</View>
|
|
<PaletteText
|
|
tone={palette ? "secondaryForeground" : undefined}
|
|
style={[styles.duration, { color: secondaryForeground }]}
|
|
>
|
|
{formatDuration(song.durationSeconds)}
|
|
</PaletteText>
|
|
{trailing}
|
|
</>
|
|
)
|
|
|
|
const rowContent = (
|
|
<>
|
|
<CoverImage
|
|
uri={albumCoverUrl(config.serverUrl, song.album.id)}
|
|
hasCover={song.album.cover != null}
|
|
style={styles.rowArtwork}
|
|
/>
|
|
{inset ? (
|
|
<View style={[styles.insetContent, { borderBottomColor: divider }]}>
|
|
{content}
|
|
</View>
|
|
) : (
|
|
content
|
|
)}
|
|
</>
|
|
)
|
|
const rowStyle = inset
|
|
? styles.insetRow
|
|
: [styles.row, { borderBottomColor: divider }]
|
|
const row =
|
|
swipe && Platform.OS !== "web" ? (
|
|
<SongSwipeRow
|
|
group={swipe.group}
|
|
onPlayNext={swipe.onPlayNext}
|
|
onAddToQueue={swipe.onAddToQueue}
|
|
onRemove={swipe.onRemove}
|
|
backgroundColor={swipe.backgroundColor}
|
|
onPress={onPress}
|
|
style={rowStyle}
|
|
>
|
|
{rowContent}
|
|
</SongSwipeRow>
|
|
) : (
|
|
<Pressable
|
|
style={rowStyle}
|
|
onPress={onPress}
|
|
onLongPress={contextMenu ? consumeLongPress : undefined}
|
|
>
|
|
{rowContent}
|
|
</Pressable>
|
|
)
|
|
|
|
if (!contextMenu) {
|
|
return (
|
|
<MusicEntityView kind="song" id={song.id}>
|
|
{row}
|
|
</MusicEntityView>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<MediaContextMenu {...contextMenu}>
|
|
<MusicEntityView kind="song" id={song.id}>
|
|
{row}
|
|
</MusicEntityView>
|
|
</MediaContextMenu>
|
|
)
|
|
})
|
|
|
|
/**
|
|
* A `SongRow` with the standard song context menu. Pass stable `onPlay` and
|
|
* `onAction` callbacks so rows in long lists only re-render when their own
|
|
* song or favorite state changes.
|
|
*/
|
|
export const SongListRow = memo(function SongListRow({
|
|
song,
|
|
favorited,
|
|
playlistLabel,
|
|
palette,
|
|
inset,
|
|
swipe,
|
|
removable,
|
|
onPlay,
|
|
onAction,
|
|
}: {
|
|
song: BrowseSong
|
|
favorited: boolean
|
|
playlistLabel?: string
|
|
palette?: Pick<CoverPalette, "foreground" | "secondaryForeground" | "divider">
|
|
inset?: boolean
|
|
swipe?: {
|
|
group: SwipeRowGroup
|
|
onPlayNext: () => void
|
|
onAddToQueue: () => void
|
|
onRemove?: () => void
|
|
backgroundColor: string
|
|
}
|
|
/** Shows "Remove from Playlist" in the context menu. */
|
|
removable?: boolean
|
|
onPlay: (song: BrowseSong) => void
|
|
onAction: (song: BrowseSong, action: MediaAction) => void
|
|
}) {
|
|
const { config } = useServerConfig()
|
|
const handlePress = useCallback(() => onPlay(song), [onPlay, song])
|
|
|
|
return (
|
|
<SongRow
|
|
song={song}
|
|
palette={palette}
|
|
inset={inset}
|
|
swipe={swipe}
|
|
onPress={handlePress}
|
|
contextMenu={{
|
|
preview: config
|
|
? {
|
|
kind: "song",
|
|
title: song.title,
|
|
artist: song.artist.name,
|
|
album: song.album.title,
|
|
artworkUrl: albumCoverUrl(config.serverUrl, song.album.id),
|
|
hasArtwork: song.album.cover != null,
|
|
}
|
|
: undefined,
|
|
stationArtistId: song.artist.id,
|
|
favorited,
|
|
playlistLabel,
|
|
showRemoveFromPlaylist: removable,
|
|
navigation: {
|
|
artist: { id: song.artist.id },
|
|
album: { id: song.album.id },
|
|
},
|
|
share: {
|
|
type: "song",
|
|
albumId: song.album.id,
|
|
songId: song.id,
|
|
title: `${song.title} - ${song.album.title}`,
|
|
},
|
|
onAction: (action) => onAction(song, action),
|
|
}}
|
|
/>
|
|
)
|
|
})
|
|
|
|
const tabletPlayerInset = tabletMiniPlayerHeight + spacing.sm + spacing.lg
|
|
export const tabContentBottomInset =
|
|
Platform.OS === "ios" && Platform.isPad ? tabletPlayerInset : spacing.lg
|
|
export const contentBottomInset =
|
|
Platform.OS === "ios" && Platform.isPad
|
|
? tabletPlayerInset
|
|
: Platform.OS === "android"
|
|
? spacing.lg
|
|
: miniPlayerHeight + spacing.lg
|
|
|
|
const styles = StyleSheet.create({
|
|
row: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.md,
|
|
minHeight: 64,
|
|
paddingVertical: 10,
|
|
paddingHorizontal: spacing.lg,
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
},
|
|
insetRow: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.md,
|
|
paddingLeft: collectionInset,
|
|
},
|
|
insetContent: {
|
|
flex: 1,
|
|
minWidth: 0,
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
alignSelf: "stretch",
|
|
gap: spacing.md,
|
|
minHeight: 64,
|
|
paddingVertical: 10,
|
|
marginRight: collectionInset,
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
},
|
|
rowArtwork: {
|
|
width: 44,
|
|
height: 44,
|
|
borderRadius: 6,
|
|
},
|
|
rowMeta: {
|
|
flex: 1,
|
|
minWidth: 0,
|
|
},
|
|
rowTitle: {
|
|
fontSize: 16,
|
|
fontWeight: "500",
|
|
},
|
|
rowSubtitle: {
|
|
fontSize: 14,
|
|
marginTop: 3,
|
|
},
|
|
duration: {
|
|
fontSize: 12,
|
|
marginRight: spacing.xs,
|
|
},
|
|
})
|