186 lines
4.9 KiB
TypeScript
186 lines
4.9 KiB
TypeScript
import { Ionicons } from "@expo/vector-icons"
|
|
import { Stack } from "expo-router"
|
|
import { memo, useCallback } from "react"
|
|
import {
|
|
FlatList,
|
|
Pressable,
|
|
StyleSheet,
|
|
Text,
|
|
type ListRenderItemInfo,
|
|
} from "react-native"
|
|
|
|
import {
|
|
MediaContextMenu,
|
|
type MediaAction,
|
|
} from "@/components/media-context-menu"
|
|
import { CoverImage } from "@/components/cover-image"
|
|
import { ArtistListSkeleton } from "@/components/skeletons"
|
|
import { contentBottomInset } from "@/components/player"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import {
|
|
useCachedPaginatedQuery,
|
|
useCachedQuery,
|
|
} from "@/hooks/use-cached-query"
|
|
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 { longListProps } from "@/lib/list"
|
|
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, isCached } = useCachedPaginatedQuery(
|
|
api.library.browseArtistsPaginated,
|
|
{},
|
|
{ initialNumItems: PAGE_SIZE }
|
|
)
|
|
const searchResults = useCachedQuery(
|
|
api.library.searchLibrary,
|
|
searchQuery ? { query: searchQuery } : "skip"
|
|
)
|
|
const { handleArtistAction, isArtistFavorite } = useMediaMenuActions()
|
|
const artists = searchQuery
|
|
? searchResults?.artists
|
|
: status === "LoadingFirstPage" && !isCached
|
|
? undefined
|
|
: results
|
|
|
|
const serverUrl = config?.serverUrl ?? ""
|
|
const handleOpenArtist = useCallback(
|
|
(artistId: string) => push(`/artist/${artistId}`),
|
|
[push]
|
|
)
|
|
const renderItem = useCallback(
|
|
({ item: artist }: ListRenderItemInfo<BrowseArtist>) => (
|
|
<ArtistRow
|
|
artist={artist}
|
|
serverUrl={serverUrl}
|
|
favorited={isArtistFavorite(artist.id)}
|
|
onOpen={handleOpenArtist}
|
|
onAction={handleArtistAction}
|
|
/>
|
|
),
|
|
[handleArtistAction, handleOpenArtist, isArtistFavorite, serverUrl]
|
|
)
|
|
|
|
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}
|
|
<ArtistListSkeleton />
|
|
</>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{searchBar}
|
|
|
|
<FlatList
|
|
{...longListProps}
|
|
data={artists}
|
|
keyExtractor={artistKey}
|
|
renderItem={renderItem}
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
contentContainerStyle={styles.listContent}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
onEndReached={() => {
|
|
if (!searchQuery && status === "CanLoadMore") loadMore(PAGE_SIZE)
|
|
}}
|
|
onEndReachedThreshold={0.5}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
function artistKey(artist: BrowseArtist) {
|
|
return artist.id
|
|
}
|
|
|
|
const ArtistRow = memo(function ArtistRow({
|
|
artist,
|
|
serverUrl,
|
|
favorited,
|
|
onOpen,
|
|
onAction,
|
|
}: {
|
|
artist: BrowseArtist
|
|
serverUrl: string
|
|
favorited: boolean
|
|
onOpen: (artistId: string) => void
|
|
onAction: (artist: BrowseArtist, action: MediaAction) => Promise<void>
|
|
}) {
|
|
const colors = useThemeColors()
|
|
|
|
return (
|
|
<MediaContextMenu
|
|
favorited={favorited}
|
|
share={{ type: "artist", artistId: artist.id, title: artist.name }}
|
|
onAction={(action) => void onAction(artist, action)}
|
|
>
|
|
<Pressable
|
|
style={[styles.row, { borderBottomColor: colors.border }]}
|
|
onPress={() => onOpen(artist.id)}
|
|
>
|
|
<CoverImage
|
|
uri={artistCoverUrl(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({
|
|
listContent: { paddingBottom: contentBottomInset },
|
|
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" },
|
|
})
|