Skip to content

Commit 0a89d22

Browse files
authored
Save tabs number in Url to persist tab when go to other paths (#78)
* added an optional url parameter to the library path to get to a particular tab * Added Error checking when taking the parameter from the Url * Now Tabs persist with library options * FIxed a bug which made the backbutton appear in the Library route when tabs number was presents in the url
1 parent 0de0663 commit 0a89d22

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/App.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default function App() {
158158
<Route path="/manga/:id">
159159
<Manga />
160160
</Route>
161-
<Route path="/library">
161+
<Route path="/library/:tabParamNumber?">
162162
<Library />
163163
</Route>
164164
<Route path="/updates">

src/components/navbar/DefaultNavBar.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default function DefaultNavBar() {
105105
<Toolbar>
106106
{
107107
!navbarItems.some(({ path }) => path === history.location.pathname)
108+
&& !history.location.pathname.startsWith('/library')
108109
&& (
109110
<IconButton
110111
edge="start"

src/screens/Library.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import TabPanel from 'components/util/TabPanel';
1616
import LibraryOptions from 'components/library/LibraryOptions';
1717
import LibraryMangaGrid from 'components/library/LibraryMangaGrid';
1818
import LibrarySearch from 'components/library/LibrarySearch';
19+
import { useHistory, useParams } from 'react-router-dom';
1920

2021
interface IMangaCategory {
2122
category: ICategory
@@ -34,14 +35,20 @@ export default function Library() {
3435
);
3536
}, []);
3637

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

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

4447
const handleTabChange = (newTab: number) => {
48+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
49+
history.location.search === ''
50+
? history.replace(`/library/${newTab}`)
51+
: history.replace(`/library/${newTab}/${history.location.search}`);
4552
setTabNum(newTab);
4653
};
4754

@@ -54,10 +61,17 @@ export default function Library() {
5461
mangas: [] as IManga[],
5562
isFetched: false,
5663
}));
57-
5864
setTabs(categoryTabs);
5965
if (categoryTabs.length > 0) {
60-
setTabNum(categoryTabs[0].category.order);
66+
setTabNum(() => {
67+
if (tabParamNumber !== undefined
68+
&& !Number.isNaN(tabParamNumber)
69+
&& !!categories.find((cat) => cat.order === Number(tabParamNumber))) {
70+
return Number(tabParamNumber);
71+
}
72+
history.replace('/library');
73+
return categoryTabs[0].category.order;
74+
});
6175
}
6276
});
6377
}, []);

0 commit comments

Comments
 (0)