diff --git a/src/redux/source/sourcesSlice.ts b/src/redux/source/sourcesSlice.ts index 145e2e70..e607b5e6 100644 --- a/src/redux/source/sourcesSlice.ts +++ b/src/redux/source/sourcesSlice.ts @@ -5,6 +5,7 @@ import { Source } from '../../sources/types'; interface SourcesState { allSources: Source[]; + searchResults: Source[]; pinnedSourceIds: number[]; lastUsed: number | null; languageFilters: string[]; @@ -12,6 +13,7 @@ interface SourcesState { const initialState: SourcesState = { allSources: AllSources, + searchResults: [], pinnedSourceIds: [], lastUsed: null, languageFilters: ['English'], @@ -29,17 +31,13 @@ export const sourcesSlice = createSlice({ state.allSources = sources; }, searchSourcesAction: (state, action: PayloadAction) => { - state.allSources = AllSources.filter( - source => state.languageFilters.indexOf(source.lang) !== -1, - ) - .sort((a, b) => a.sourceName.localeCompare(b.sourceName)) - .filter( - (source: Source) => - source.sourceName - .toLowerCase() - .includes(action.payload.toLowerCase()) && - state.languageFilters.indexOf(source.lang) !== -1, - ); + if (state?.pinnedSourceIds === undefined) { + state.pinnedSourceIds = []; + } + + state.searchResults = state.allSources.filter((source: Source) => + source.sourceName.toLowerCase().includes(action.payload.toLowerCase()), + ); }, setLastUsedSource: (state, action: PayloadAction<{ sourceId: number }>) => { state.lastUsed = action.payload.sourceId; diff --git a/src/screens/browse/BrowseScreen.tsx b/src/screens/browse/BrowseScreen.tsx index 06ccf36f..14fb6a19 100644 --- a/src/screens/browse/BrowseScreen.tsx +++ b/src/screens/browse/BrowseScreen.tsx @@ -40,6 +40,7 @@ const BrowseScreen = () => { const { allSources, + searchResults, pinnedSourceIds = [], languageFilters = ['English'], lastUsed, @@ -93,6 +94,44 @@ const BrowseScreen = () => { [], ); + const sections = useMemo(() => { + const list = [ + { + header: getString('browseScreen.lastUsed'), + data: lastUsedSource, + }, + ]; + + if (pinnedSourceIds) { + list.push({ + header: getString('browseScreen.pinned'), + data: pinnedSources, + }); + } + + if (!onlyShowPinnedSources) { + if (searchText) { + list.push({ + header: getString('common.searchResults'), + data: searchResults, + }); + } else { + list.push({ + header: getString('browseScreen.all'), + data: allSources, + }); + } + } + + return list; + }, [ + lastUsedSource, + pinnedSourceIds, + onlyShowPinnedSources, + JSON.stringify(searchResults), + searchText, + ]); + return ( <> { ) : allSources.length === 0 ? null : ( <> diff --git a/strings/languages/en/strings.json b/strings/languages/en/strings.json index 9a2005f6..b9815707 100644 --- a/strings/languages/en/strings.json +++ b/strings/languages/en/strings.json @@ -13,7 +13,8 @@ "edit": "Edit", "name": "Name", "searchFor": "Search for", - "globally": "globally" + "globally": "globally", + "searchResults": "Search results" }, "library": "Library", "libraryScreen": { diff --git a/strings/types/index.ts b/strings/types/index.ts index 769e6ebd..23000d9f 100644 --- a/strings/types/index.ts +++ b/strings/types/index.ts @@ -17,6 +17,7 @@ export interface StringMap { 'common.name': 'string'; 'common.searchFor': 'string'; 'common.globally': 'string'; + 'common.searchResults': 'string'; 'library': 'string'; 'libraryScreen.searchbar': 'string'; 'libraryScreen.empty': 'string';