Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save tabs number in Url to persist tab when go to other paths #78

Merged
merged 4 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default function App() {
<Route path="/manga/:id">
<Manga />
</Route>
<Route path="/library">
<Route path="/library/:tabParamNumber?">
<Library />
</Route>
<Route path="/updates">
Expand Down
1 change: 1 addition & 0 deletions src/components/navbar/DefaultNavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export default function DefaultNavBar() {
<Toolbar>
{
!navbarItems.some(({ path }) => path === history.location.pathname)
&& !history.location.pathname.startsWith('/library')
&& (
<IconButton
edge="start"
Expand Down
18 changes: 16 additions & 2 deletions src/screens/Library.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import TabPanel from 'components/util/TabPanel';
import LibraryOptions from 'components/library/LibraryOptions';
import LibraryMangaGrid from 'components/library/LibraryMangaGrid';
import LibrarySearch from 'components/library/LibrarySearch';
import { useHistory, useParams } from 'react-router-dom';

interface IMangaCategory {
category: ICategory
Expand All @@ -34,14 +35,20 @@ export default function Library() {
);
}, []);

const { tabParamNumber } = useParams<{ tabParamNumber: string }>();
const [tabs, setTabs] = useState<IMangaCategory[]>();
const [tabNum, setTabNum] = useState<number>(0);
const history = useHistory();

// a hack so MangaGrid doesn't stop working. I won't change it in case
// if I do manga pagination for library..
const [lastPageNum, setLastPageNum] = useState<number>(1);

const handleTabChange = (newTab: number) => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
history.location.search === ''
? history.replace(`/library/${newTab}`)
: history.replace(`/library/${newTab}/${history.location.search}`);
setTabNum(newTab);
};

Expand All @@ -54,10 +61,17 @@ export default function Library() {
mangas: [] as IManga[],
isFetched: false,
}));

setTabs(categoryTabs);
if (categoryTabs.length > 0) {
setTabNum(categoryTabs[0].category.order);
setTabNum(() => {
if (tabParamNumber !== undefined
&& !Number.isNaN(tabParamNumber)
&& !!categories.find((cat) => cat.order === Number(tabParamNumber))) {
return Number(tabParamNumber);
}
history.replace('/library');
return categoryTabs[0].category.order;
});
}
});
}, []);
Expand Down