Skip to content
New issue

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

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

Already on GitHub? # 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 2 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
15 changes: 13 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,17 @@ 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) => {
history.replace(`/library/${newTab}`);
setTabNum(newTab);
};

Expand All @@ -54,10 +58,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