Skip to content

Commit 3ce173a

Browse files
committed
pagename optimized with Regular Expressions
1 parent 381c309 commit 3ce173a

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

client/modules/IDE/components/Header/MobileNav.jsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { setLanguage } from '../../actions/preferences';
3535
import Overlay from '../../../App/components/Overlay';
3636
import ProjectName from './ProjectName';
3737
import CollectionCreate from '../../../User/components/CollectionCreate';
38+
import useWhatPage from '../../hooks/useWhatPage';
3839

3940
const Nav = styled(NavBar)`
4041
background: ${prop('MobilePanel.default.background')};
@@ -203,33 +204,28 @@ const MobileNav = () => {
203204

204205
const { t } = useTranslation();
205206

206-
const { pathname } = useLocation();
207207
const editorLink = useSelector(selectSketchPath);
208+
const pageName = useWhatPage();
208209

209210
// TODO: remove the switch and use a props like mobileTitle <Nav layout=“dashboard” mobileTitle={t(‘Login’)} />
210211
function resolveTitle() {
211-
switch (pathname) {
212-
case '/':
213-
return project.name;
214-
case '/login':
212+
switch (pageName) {
213+
case 'login':
215214
return t('LoginView.Login');
216-
case '/signup':
215+
case 'signup':
217216
return t('LoginView.SignUp');
218-
case '/account':
217+
case 'account':
219218
return t('AccountView.Settings');
220-
case '/p5/sketches':
221-
case '/p5/collections':
219+
case 'examples':
222220
return t('Nav.File.Examples');
223-
case `/${user.username}/assets`:
224-
case `/${user.username}/collections`:
225-
case `/${user.username}/sketches`:
221+
case 'myStuff':
226222
return 'My Stuff';
227223
default:
228224
return project.name;
229225
}
230226
}
231227

232-
const title = useMemo(resolveTitle, [project, pathname]);
228+
const title = useMemo(resolveTitle, [pageName]);
233229

234230
const Logo = AsteriskIcon;
235231
return (
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-disable no-useless-escape */
2+
import { useEffect, useState } from 'react';
3+
import { useSelector } from 'react-redux';
4+
import { useLocation } from 'react-router-dom';
5+
6+
/**
7+
*
8+
* @returns {"home" | "myStuff" | "login" | "signup" | "account" | "examples"}
9+
*/
10+
const useWhatPage = () => {
11+
const projectName = useSelector((state) => state.project.name);
12+
const username = useSelector((state) => state.user.username);
13+
const { pathname } = useLocation();
14+
15+
const [pageName, setPageName] = useState(projectName);
16+
17+
const myStuffPattern = new RegExp(
18+
`(\/${username}\/(sketches\/?$|collections|assets)\/?)`
19+
);
20+
21+
useEffect(() => {
22+
if (myStuffPattern.test(pathname)) setPageName('myStuff');
23+
else if (pathname === '/login') setPageName('login');
24+
else if (pathname === '/signup') setPageName('signup');
25+
else if (pathname === '/account') setPageName('account');
26+
else if (pathname === '/p5/collections' || pathname === '/p5/sketches')
27+
setPageName('examples');
28+
console.log(pageName, pathname);
29+
}, [pathname]);
30+
31+
return pageName;
32+
};
33+
34+
export default useWhatPage;

0 commit comments

Comments
 (0)