import React, { useMemo } from 'react';
import { FlatList, HStack, useBreakpointValue, useTheme, Text, Row } from 'native-base';
import { useNavigation } from '../../Navigation';
import { ArrowRotateLeft, Cup } from 'iconsax-react-native';
import { View, StyleSheet } from 'react-native';
import { useQuery } from '../../Queries';
import { translate } from '../../i18n/i18n';
import SearchBarComponent from '../../components/V2/SearchBar';
import SearchHistory from '../../components/V2/SearchHistory';
import MusicItem from '../../components/UI/MusicItem';
import API from '../../API';
import LoadingComponent from '../../components/Loading';
import ErrorView from '../ErrorView';
import { useLikeSongMutation } from '../../utils/likeSongMutation';
export type searchProps = {
artist: number | undefined;
genre: number | undefined;
query: string;
};
const MusicListNoOpti = ({ list }: { list: any[] }) => {
const { colors } = useTheme();
const screenSize = useBreakpointValue({ base: 'small', md: 'md', xl: 'xl' });
const isBigScreen = screenSize === 'xl';
const headerComponent = useMemo(
() => (
{translate('musicListTitleSong')}
{[
{ text: translate('musicListTitleLastScore'), icon: ArrowRotateLeft },
{ text: translate('musicListTitleBestScore'), icon: Cup },
].map((value) => (
{/* Conditional rendering based on screen size. */}
{isBigScreen && (
{value.text}
)}
{/* Icon with color based on the current color scheme. */}
))}
),
[colors.coolGray[500], isBigScreen]
);
return (
}
keyExtractor={(item) => item.artist + item.song}
/>
);
};
// eslint-disable-next-line @typescript-eslint/ban-types
const SearchView = () => {
const navigation = useNavigation();
const artistsQuery = useQuery(API.getAllArtists());
const [searchQuery, setSearchQuery] = React.useState({} as searchProps);
const rawResult = useQuery(API.searchSongs(searchQuery), {
enabled: !!searchQuery.query || !!searchQuery.artist || !!searchQuery.genre,
onSuccess() {
const artist =
artistsQuery?.data?.find(({ id }) => id == searchQuery.artist)?.name ??
'unknown artist';
searchQuery.query ? API.createSearchHistoryEntry(searchQuery.query, 'song') : null;
if (artist != 'unknown artist') API.createSearchHistoryEntry(artist, 'artist');
},
});
const userQuery = useQuery(API.getUserInfo());
const likedSongs = useQuery(API.getLikedSongs());
const { mutateAsync } = useLikeSongMutation();
let result: any[] = [];
if (userQuery.isLoading || likedSongs.isLoading || artistsQuery.isLoading) {
return ;
}
if (userQuery.isError) {
return ;
}
if (userQuery.isSuccess) {
result =
rawResult.data?.map((song) => {
const isLiked = likedSongs.data?.some((x) => x.songId == song.id) ?? false;
return {
artist:
artistsQuery?.data?.find(({ id }) => id == song?.artistId)?.name ??
'unknown artist',
song: song?.name,
image: song?.cover,
level: song?.difficulties.chordcomplexity,
lastScore: song?.lastScore,
bestScore: song?.bestScore,
liked: isLiked,
onLike: () => {
mutateAsync({ songId: song.id, like: !isLiked }).then(() =>
likedSongs.refetch()
);
},
onPlay: () => navigation.navigate('Play', { songId: song.id }),
};
}) ?? [];
}
return (
setSearchQuery(query)} />
{result.length != 0 ? : }
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
gap: 2,
borderRadius: 10,
overflow: 'hidden',
},
});
export default SearchView;