From dce5030ac4a3601f181ea12070532cbdecd915e9 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Mon, 25 Oct 2021 15:09:07 -0700 Subject: [PATCH] Remember active tab for favorites page --- src/__tests__/App.test.tsx | 10 +++++++- src/components/starred/StarredView.tsx | 33 ++++++++++++++------------ src/redux/favoriteSlice.ts | 26 ++++++++++++++++++++ src/redux/store.ts | 2 ++ 4 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 src/redux/favoriteSlice.ts diff --git a/src/__tests__/App.test.tsx b/src/__tests__/App.test.tsx index b074ef1..6149f50 100644 --- a/src/__tests__/App.test.tsx +++ b/src/__tests__/App.test.tsx @@ -9,8 +9,9 @@ import { Player } from '../redux/playerSlice'; import { General } from '../redux/miscSlice'; import { Playlist } from '../redux/playlistSlice'; import { ConfigPage } from '../redux/configSlice'; -import App from '../App'; import { FolderSelection } from '../redux/folderSlice'; +import { FavoritePage } from '../redux/favoriteSlice'; +import App from '../App'; const middlewares: Middleware, any, Dispatch>[] | undefined = []; const mockStore = configureMockStore(middlewares); @@ -109,6 +110,12 @@ const configState: ConfigPage = { }, }; +const favoriteState: FavoritePage = { + active: { + tab: 'tracks', + }, +}; + const mockInitialState = { player: playerState, playQueue: playQueueState, @@ -116,6 +123,7 @@ const mockInitialState = { playlist: playlistState, folder: folderState, config: configState, + favorite: favoriteState, }; describe('App', () => { diff --git a/src/components/starred/StarredView.tsx b/src/components/starred/StarredView.tsx index 7669e9e..803094c 100644 --- a/src/components/starred/StarredView.tsx +++ b/src/components/starred/StarredView.tsx @@ -20,16 +20,15 @@ import ListViewType from '../viewtypes/ListViewType'; import GridViewType from '../viewtypes/GridViewType'; import { setStatus } from '../../redux/playerSlice'; import { StyledNavItem } from '../shared/styled'; -import useRouterQuery from '../../hooks/useRouterQuery'; +import { setActive } from '../../redux/favoriteSlice'; const StarredView = () => { const history = useHistory(); const dispatch = useAppDispatch(); - const query = useRouterQuery(); const queryClient = useQueryClient(); const multiSelect = useAppSelector((state) => state.multiSelect); const folder = useAppSelector((state) => state.folder); - const [page, setPage] = useState(query.get('page') || 'tracks'); + const favorite = useAppSelector((state) => state.favorite); const [viewType, setViewType] = useState(settings.getSync('albumViewType') || 'list'); const [musicFolder, setMusicFolder] = useState(undefined); @@ -49,10 +48,14 @@ const StarredView = () => { const [searchQuery, setSearchQuery] = useState(''); const filteredData = useSearchQuery( searchQuery, - page === 'tracks' ? data?.song : page === 'albums' ? data?.album : data?.artist, - page === 'tracks' + favorite.active.tab === 'tracks' + ? data?.song + : favorite.active.tab === 'albums' + ? data?.album + : data?.artist, + favorite.active.tab === 'tracks' ? ['title', 'artist', 'album', 'name', 'genre'] - : page === 'albums' + : favorite.active.tab === 'albums' ? ['name', 'artist', 'genre', 'year'] : ['name'] ); @@ -66,14 +69,14 @@ const StarredView = () => { if (e.ctrlKey) { dispatch(toggleSelected(rowData)); } else if (e.shiftKey) { - if (page === 'tracks') { + if (favorite.active.tab === 'tracks') { dispatch(setRangeSelected(rowData)); if (searchQuery !== '') { dispatch(toggleRangeSelected(filteredData)); } else { dispatch(toggleRangeSelected(data.song)); } - } else if (page === 'albums') { + } else if (favorite.active.tab === 'albums') { dispatch(setRangeSelected(rowData)); dispatch(toggleRangeSelected(searchQuery !== '' ? filteredData : data?.album)); } @@ -87,7 +90,7 @@ const StarredView = () => { timeout = null; dispatch(clearSelected()); - if (page === 'tracks') { + if (favorite.active.tab === 'tracks') { dispatch( setPlayQueueByRowClick({ entries: data.song, @@ -98,7 +101,7 @@ const StarredView = () => { ); dispatch(setStatus('PLAYING')); dispatch(fixPlayer2Index()); - } else if (page === 'albums') { + } else if (favorite.active.tab === 'albums') { history.push(`/library/album/${e.id}`); } else { history.push(`/library/artist/${e.id}`); @@ -138,7 +141,7 @@ const StarredView = () => { setPage(e)}> +