Files
otunes/apps/mobile/app/(app)/(tabs)/home/artist/[id].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

172 lines
5.3 KiB
TypeScript

import { useQuery } from "convex/react"
import { Stack, useLocalSearchParams } from "expo-router"
import { Pressable, StyleSheet, Text, View } from "react-native"
import { Ionicons } from "@expo/vector-icons"
import { CoverImage } from "@/components/cover-image"
import { MediaGridList } from "@/components/media-cards"
import { MediaActionMenu } from "@/components/media-actions-sheet"
import { Button, LoadingView } from "@/components/ui/primitives"
import { contentBottomInset } from "@/components/player"
import { useFavorites } from "@/contexts/favorites"
import { usePlaybackActions } from "@/contexts/playback"
import { useServerConfig } from "@/contexts/server-config"
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import { artistCoverUrl } from "@/lib/urls"
import { spacing, useThemeColors } from "@/lib/theme"
export default function ArtistScreen() {
const { id } = useLocalSearchParams<{ id: string }>()
const colors = useThemeColors()
const { config } = useServerConfig()
const data = useQuery(
api.library.browseArtistDetail,
typeof id === "string" ? { id: id as Id<"artists"> } : "skip"
)
const { playArtist, playArtistShuffled } = usePlaybackActions()
const { isArtistFavorite, toggleArtistFavorite } = useFavorites()
const { handleArtistAction, isArtistFavorite: isArtistFav } =
useMediaMenuActions()
if (data === undefined || !config) {
return (
<>
<Stack.Screen
options={{
title: "",
headerLargeTitle: false,
}}
/>
<LoadingView />
</>
)
}
if (data === null) {
return (
<>
<Stack.Screen
options={{
title: "",
headerLargeTitle: false,
}}
/>
<View style={[styles.empty, { backgroundColor: colors.background }]}>
<Text style={{ color: colors.mutedForeground }}>
Artist not found.
</Text>
</View>
</>
)
}
return (
<>
<Stack.Screen
options={{
title: "",
headerLargeTitle: false,
headerRight: () => (
<MediaActionMenu
selectedCount={data.albums.length}
favorited={isArtistFav(data.artist.id)}
share={{ type: "artist", artistId: data.artist.id }}
onAction={(action) =>
void handleArtistAction(data.artist, action)
}
>
<Pressable hitSlop={8}>
<Ionicons
name="ellipsis-horizontal"
size={22}
color={colors.foreground}
/>
</Pressable>
</MediaActionMenu>
),
}}
/>
<MediaGridList
content={data.albums}
paddingBottom={contentBottomInset}
contentInset={false}
ListHeaderComponent={
<View style={styles.hero}>
<CoverImage
uri={artistCoverUrl(config.serverUrl, data.artist.id)}
hasCover={data.artist.photo != null}
style={styles.avatar}
/>
<Text style={[styles.title, { color: colors.foreground }]}>
{data.artist.name}
</Text>
<View style={styles.actions}>
<Pressable
onPress={() => void toggleArtistFavorite(data.artist.id)}
hitSlop={8}
style={[styles.iconAction, { backgroundColor: colors.muted }]}
>
<Ionicons
name={
isArtistFavorite(data.artist.id) ? "star" : "star-outline"
}
size={24}
color={
isArtistFavorite(data.artist.id)
? colors.primary
: colors.foreground
}
/>
</Pressable>
<Button
title="Play"
icon="play"
onPress={() => void playArtist(data.artist.id)}
style={styles.playButton}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel="Shuffle"
hitSlop={8}
onPress={() => void playArtistShuffled(data.artist.id)}
style={[styles.iconAction, { backgroundColor: colors.muted }]}
>
<Ionicons name="shuffle" size={24} color={colors.foreground} />
</Pressable>
</View>
</View>
}
/>
</>
)
}
const styles = StyleSheet.create({
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
hero: { alignItems: "center", padding: spacing.lg, gap: spacing.sm },
avatar: {
width: 180,
height: 180,
borderRadius: 90,
},
title: { fontSize: 28, fontWeight: "800", textAlign: "center" },
actions: {
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
gap: spacing.sm,
marginTop: spacing.sm,
},
playButton: { borderRadius: 999, minWidth: 160 },
iconAction: {
width: 44,
height: 44,
borderRadius: 22,
alignItems: "center",
justifyContent: "center",
},
})