Files
jellyfin-audio-player/src/screens/Music/stacks/Search.tsx

123 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-07-06 16:40:30 +02:00
import React, { useState, useEffect, useRef, useCallback } from 'react';
2020-07-05 23:15:30 +02:00
import Input from 'components/Input';
2020-07-06 16:40:30 +02:00
import { Text, View } from 'react-native';
2020-07-05 23:15:30 +02:00
import styled from 'styled-components/native';
import { useTypedSelector } from 'store';
import Fuse from 'fuse.js';
2020-07-06 16:40:30 +02:00
import { Album } from 'store/music/types';
import { FlatList } from 'react-native-gesture-handler';
import TouchableHandler from 'components/TouchableHandler';
import { useNavigation } from '@react-navigation/native';
import { useGetImage } from 'utility/JellyfinApi';
import { NavigationProp } from '../types';
import FastImage from 'react-native-fast-image';
2020-07-26 14:45:32 +02:00
import { colors } from 'components/Colors';
2020-07-05 23:15:30 +02:00
const Container = styled.View`
padding: 0 20px;
`;
2020-07-06 16:40:30 +02:00
const AlbumImage = styled(FastImage)`
border-radius: 4px;
width: 25px;
height: 25px;
margin-right: 10px;
`;
const HalfOpacity = styled.Text`
opacity: 0.5;
margin-top: 2px;
font-size: 12px;
`;
const SearchResult = styled.View`
flex-direction: row;
align-items: center;
border-bottom-width: 1px;
margin-left: 15px;
padding-right: 15px;
height: 50px;
`;
const fuseOptions = {
keys: ['Name', 'AlbumArtist', 'AlbumArtists', 'Artists'],
threshold: 0.1,
includeScore: true,
};
2020-07-05 23:15:30 +02:00
export default function Search() {
2020-07-06 16:40:30 +02:00
// Prepare state
2020-07-05 23:15:30 +02:00
const [searchTerm, setSearchTerm] = useState('');
const albums = useTypedSelector(state => state.music.albums.entities);
2020-07-06 16:40:30 +02:00
const [results, setResults] = useState<Fuse.FuseResult<Album>[]>([]);
2020-07-26 14:45:32 +02:00
const fuse = useRef<Fuse<Album, typeof fuseOptions>>();
2020-07-06 16:40:30 +02:00
// Prepare helpers
const navigation = useNavigation<NavigationProp>();
const getImage = useGetImage();
2020-07-05 23:15:30 +02:00
2020-07-06 16:40:30 +02:00
/**
* Since it is impractical to have a global fuse variable, we need to
* instantiate it for thsi function. With this effect, we generate a new
* Fuse instance every time the albums change. This can of course be done
* more intelligently by removing and adding the changed albums, but this is
* an open todo.
*/
2020-07-05 23:15:30 +02:00
useEffect(() => {
2020-07-06 16:40:30 +02:00
fuse.current = new Fuse(Object.values(albums) as Album[], fuseOptions);
2020-07-05 23:15:30 +02:00
}, [albums]);
2020-07-06 16:40:30 +02:00
/**
* Whenever the search term changes, we gather results from Fuse and assign
* them to state
*/
2020-07-05 23:15:30 +02:00
useEffect(() => {
2020-07-06 16:40:30 +02:00
// GUARD: In some extraordinary cases, Fuse might not be presented since
// it is assigned via refs. In this case, we can't handle any searching.
if (!fuse.current) {
return;
}
setResults(fuse.current.search(searchTerm));
2020-07-05 23:15:30 +02:00
}, [searchTerm, setResults, fuse]);
2020-07-06 16:40:30 +02:00
// Handlers
const selectAlbum = useCallback((id: string) =>
navigation.navigate('Album', { id, album: albums[id] as Album }), [navigation, albums]
);
const HeaderComponent = React.useMemo(() => (
2020-07-05 23:15:30 +02:00
<Container>
2020-07-26 14:45:32 +02:00
<Input value={searchTerm} onChangeText={setSearchTerm} style={colors.input} placeholder="Search..." />
2020-07-06 16:40:30 +02:00
{(searchTerm.length && !results.length) ? <Text>No results...</Text> : null}
2020-07-05 23:15:30 +02:00
</Container>
2020-07-06 16:40:30 +02:00
), [searchTerm, results, setSearchTerm]);
// GUARD: We cannot search for stuff unless Fuse is loaded with results.
// Therefore we delay rendering to when we are certain it's there.
if (!fuse.current) {
return null;
}
return (
<FlatList
data={results}
renderItem={({ item: { item: album } }) =>(
<TouchableHandler id={album.Id} onPress={selectAlbum}>
2020-07-26 14:45:32 +02:00
<SearchResult style={colors.border}>
2020-07-06 16:40:30 +02:00
<AlbumImage source={{ uri: getImage(album.Id) }} />
<View>
2020-07-26 14:45:32 +02:00
<Text numberOfLines={1} ellipsizeMode="tail" style={colors.text}>
2020-07-06 16:40:30 +02:00
{album.Name} - {album.AlbumArtist}
</Text>
2020-07-26 14:45:32 +02:00
<HalfOpacity style={colors.text}>Album</HalfOpacity>
2020-07-06 16:40:30 +02:00
</View>
</SearchResult>
</TouchableHandler>
)}
keyExtractor={(item) => item.refIndex.toString()}
ListHeaderComponent={HeaderComponent}
extraData={searchTerm}
/>
2020-07-05 23:15:30 +02:00
);
}