Files
otunes/apps/mobile/components/mini-player.tsx
T
joshuafhiggins 9da758b196 perf: optimize app rendering and data loading (#18)
- Virtualize mobile lists and defer queue loading
- Split playback state from actions to reduce rerenders
- Skip invalid or unnecessary Convex queries

Reviewed-on: #18
2026-09-05 16:35:37 -04:00

343 lines
8.0 KiB
TypeScript

import { Ionicons } from "@expo/vector-icons"
import { NativeTabs } from "expo-router/unstable-native-tabs"
import { useRouter } from "expo-router"
import { Platform, Pressable, StyleSheet, Text, View } from "react-native"
import { CoverImage } from "@/components/cover-image"
import { usePlaybackActions, usePlaybackQueue } from "@/contexts/playback"
import { useServerConfig } from "@/contexts/server-config"
import { albumCoverUrl } from "@/lib/urls"
import {
miniPlayerHeight,
tabBarHeight,
useBlurTint,
useMiniPlayerBackground,
useThemeColors,
} from "@/lib/theme"
function MiniPlayerContent({
placement,
variant = "bar",
onOpenPlayer,
}: {
placement: "regular" | "inline"
variant: "bar" | "accessory"
onOpenPlayer: () => void
}) {
const colors = useThemeColors()
const { config } = useServerConfig()
const { currentSong, isPlaying } = usePlaybackQueue()
const { setIsPlaying, skipNext } = usePlaybackActions()
if (!currentSong || !config) {
return null
}
const compact = placement === "inline"
const isAccessory = variant === "accessory"
const artworkStyle = isAccessory ? styles.accessoryArtwork : styles.artwork
const rowStyle = compact
? styles.inlineContent
: isAccessory
? styles.accessoryContent
: styles.content
if (compact) {
return (
<Pressable style={rowStyle} onPress={onOpenPlayer}>
<CoverImage
uri={albumCoverUrl(config.serverUrl, currentSong.album.id)}
hasCover={currentSong.album.cover != null}
style={
isAccessory ? styles.accessoryInlineArtwork : styles.inlineArtwork
}
/>
<View
style={isAccessory ? styles.accessoryInlineMeta : styles.inlineMeta}
>
<Text
style={[
isAccessory ? styles.accessoryTitle : styles.title,
{ color: colors.foreground },
]}
numberOfLines={1}
>
{currentSong.title}
</Text>
</View>
<Pressable
style={styles.controlButton}
onPress={(event) => {
event.stopPropagation()
setIsPlaying(!isPlaying)
}}
hitSlop={6}
>
<Ionicons
name={isPlaying ? "pause" : "play"}
size={20}
color={colors.foreground}
/>
</Pressable>
</Pressable>
)
}
return (
<Pressable style={rowStyle} onPress={onOpenPlayer}>
<CoverImage
uri={albumCoverUrl(config.serverUrl, currentSong.album.id)}
hasCover={currentSong.album.cover != null}
style={artworkStyle}
/>
<View style={isAccessory ? styles.accessoryMeta : styles.meta}>
<Text
style={[
isAccessory ? styles.accessoryTitle : styles.title,
{ color: colors.foreground },
]}
numberOfLines={1}
>
{currentSong.title}
</Text>
<Text
style={[
isAccessory ? styles.accessoryArtist : styles.artist,
{ color: colors.mutedForeground },
]}
numberOfLines={1}
>
{currentSong.artist.name}
</Text>
</View>
<View style={styles.controls}>
<Pressable
style={styles.controlButton}
onPress={(event) => {
event.stopPropagation()
setIsPlaying(!isPlaying)
}}
hitSlop={6}
>
<Ionicons
name={isPlaying ? "pause" : "play"}
size={24}
color={colors.foreground}
/>
</Pressable>
<Pressable
style={styles.controlButton}
onPress={(event) => {
event.stopPropagation()
void skipNext()
}}
hitSlop={6}
>
<Ionicons name="play-forward" size={24} color={colors.foreground} />
</Pressable>
</View>
</Pressable>
)
}
function MiniPlayerChrome({ children }: { children: React.ReactNode }) {
const colors = useThemeColors()
const backgroundColor = useMiniPlayerBackground()
return (
<View style={[styles.androidBar, { backgroundColor }]}>
<View style={[styles.topBorder, { backgroundColor: colors.border }]} />
{children}
</View>
)
}
export function TabBottomAccessory() {
const router = useRouter()
const { currentSong, queueSongIds } = usePlaybackQueue()
const placement = NativeTabs.BottomAccessory.usePlacement()
if (!currentSong || queueSongIds.length === 0) {
return null
}
const content = (
<View style={styles.accessoryContainer}>
<MiniPlayerContent
placement={placement}
variant="accessory"
onOpenPlayer={() => router.push("/player")}
/>
</View>
)
if (Platform.OS === "android") {
return <MiniPlayerChrome>{content}</MiniPlayerChrome>
}
return content
}
//
// function useFloatingMiniPlayerLayout() {
// const segments = useSegments() as string[]
// const insets = useSafeAreaInsets()
//
// const onPlayerModal = segments.includes("player")
// const onAuthFlow =
// segments.includes("signin") ||
// segments.includes("signup") ||
// segments.includes("server")
// const onTabs = segments.includes("(tabs)")
//
// const bottom = insets.bottom + (onTabs ? 0 : 0)
//
// return {
// bottom,
// hidden: onPlayerModal || onAuthFlow || onTabs,
// }
// }
//
// export function FloatingMiniPlayer() {
// const router = useRouter()
// const { bottom, hidden } = useFloatingMiniPlayerLayout()
// const { currentSong, queueSongIds } = usePlayback()
//
// if (hidden || !currentSong || queueSongIds.length === 0) {
// return null
// }
//
// return (
// <View pointerEvents="box-none" style={[styles.floatingContainer, { bottom }]}>
// <MiniPlayerChrome>
// <MiniPlayerContent onOpenPlayer={() => router.push("/player")} />
// </MiniPlayerChrome>
// </View>
// )
// }
const styles = StyleSheet.create({
floatingContainer: {
position: "absolute",
left: 0,
right: 0,
zIndex: 100,
},
accessoryContainer: {
flex: 1,
justifyContent: "center",
alignSelf: "stretch",
},
blur: {
overflow: "hidden",
},
androidBar: {
overflow: "hidden",
},
topBorder: {
height: StyleSheet.hairlineWidth,
},
content: {
flexDirection: "row",
alignItems: "center",
paddingHorizontal: 16,
paddingVertical: 10,
minHeight: miniPlayerHeight,
gap: 12,
},
accessoryContent: {
flex: 1,
flexDirection: "row",
alignItems: "center",
alignSelf: "stretch",
paddingHorizontal: 12,
paddingVertical: 8,
gap: 12,
},
inlineContent: {
flex: 1,
flexDirection: "row",
alignItems: "center",
alignSelf: "stretch",
paddingHorizontal: 12,
paddingVertical: 8,
gap: 10,
},
artwork: {
width: 36,
height: 36,
borderRadius: 5,
},
accessoryArtwork: {
width: 32,
height: 32,
borderRadius: 4,
},
inlineArtwork: {
width: 28,
height: 28,
borderRadius: 4,
},
accessoryInlineArtwork: {
width: 24,
height: 24,
borderRadius: 4,
},
meta: {
flex: 1,
minWidth: 0,
justifyContent: "center",
},
accessoryMeta: {
flex: 1,
minWidth: 0,
justifyContent: "center",
paddingTop: 3,
},
inlineMeta: {
flex: 1,
minWidth: 0,
justifyContent: "center",
},
accessoryInlineMeta: {
flex: 1,
minWidth: 0,
justifyContent: "center",
paddingTop: 2,
},
title: {
fontSize: 15,
fontWeight: "500",
lineHeight: 18,
},
accessoryTitle: {
fontSize: 14,
fontWeight: "500",
lineHeight: 17,
},
artist: {
fontSize: 13,
lineHeight: 16,
marginTop: 2,
},
accessoryArtist: {
fontSize: 12,
lineHeight: 15,
marginTop: 1,
},
controls: {
flexDirection: "row",
alignItems: "center",
gap: 4,
marginLeft: 4,
},
controlButton: {
width: 36,
height: 36,
alignItems: "center",
justifyContent: "center",
},
})
export { miniPlayerHeight, tabBarHeight }