Files
otunes/apps/mobile/app/(app)/(tabs)/home/browse/artists.tsx
T

127 lines
3.9 KiB
TypeScript

import { Ionicons } from "@expo/vector-icons"
import { usePaginatedQuery, useQuery } from "convex/react"
import { Stack } from "expo-router"
import { useCallback } from "react"
import { FlatList, Pressable, StyleSheet, Text } from "react-native"
import { MediaContextMenu } from "@/components/media-context-menu"
import { CoverImage } from "@/components/cover-image"
import { LoadingView } from "@/components/ui/primitives"
import { contentBottomInset } from "@/components/player"
import { useServerConfig } from "@/contexts/server-config"
import { useDebouncedSearch } from "@/hooks/use-debounced-search"
import { useAppNavigation } from "@/lib/app-navigation"
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
import { api } from "@otunes/convex/_generated/api"
import type { BrowseArtist } from "@otunes/convex/types"
import { artistCoverUrl } from "@/lib/urls"
import { spacing, useThemeColors } from "@/lib/theme"
const PAGE_SIZE = 50
export default function ArtistsScreen() {
const { push } = useAppNavigation()
const colors = useThemeColors()
const { config } = useServerConfig()
const { searchQuery, onQueryChange, onSearchSubmit } = useDebouncedSearch()
const { results, status, loadMore } = usePaginatedQuery(
api.library.browseArtistsPaginated,
{},
{ initialNumItems: PAGE_SIZE }
)
const searchResults = useQuery(
api.library.searchLibrary,
searchQuery ? { query: searchQuery } : "skip"
)
const { handleArtistAction, isArtistFavorite } = useMediaMenuActions()
const artists = searchQuery ? searchResults?.artists : results
const handleChangeText = useCallback(
(event: { nativeEvent: { text: string } }) => {
onQueryChange(event.nativeEvent.text)
},
[onQueryChange]
)
const searchBar = (
<Stack.Screen
options={{
headerSearchBarOptions: {
placeholder: "Search",
onChangeText: handleChangeText,
onSearchButtonPress: onSearchSubmit,
},
}}
/>
)
if (artists === undefined || !config) {
return (
<>
{searchBar}
<LoadingView />
</>
)
}
return (
<>
{searchBar}
<FlatList
data={artists}
keyExtractor={(artist) => artist.id}
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
onEndReached={() => {
if (!searchQuery && status === "CanLoadMore") loadMore(PAGE_SIZE)
}}
onEndReachedThreshold={0.5}
renderItem={({ item: artist }: { item: BrowseArtist }) => (
<MediaContextMenu
favorited={isArtistFavorite(artist.id)}
share={{ type: "artist", artistId: artist.id }}
onAction={(action) => void handleArtistAction(artist, action)}
>
<Pressable
style={[styles.row, { borderBottomColor: colors.border }]}
onPress={() => push(`/artist/${artist.id}`)}
>
<CoverImage
uri={artistCoverUrl(config.serverUrl, artist.id)}
hasCover={artist.photo != null}
style={styles.avatar}
/>
<Text
style={[styles.name, { color: colors.foreground }]}
numberOfLines={1}
>
{artist.name}
</Text>
<Ionicons name="chevron-forward" size={16} color={colors.mutedForeground} />
</Pressable>
</MediaContextMenu>
)}
/>
</>
)
}
const styles = StyleSheet.create({
row: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
paddingHorizontal: spacing.lg,
paddingVertical: spacing.md,
borderBottomWidth: StyleSheet.hairlineWidth,
},
avatar: {
width: 44,
height: 44,
borderRadius: 22,
},
name: { flex: 1, fontSize: 16, fontWeight: "600" },
})