Skip to content

Commit 916b57c

Browse files
authored
Merge pull request #24 from keepsimpleio/chore/header-active-page
chore: fix state of active path when page is still loading
2 parents 13d5ed1 + 7b37a43 commit 916b57c

1 file changed

Lines changed: 35 additions & 18 deletions

File tree

src/components/ToolHeader/ToolHeader.tsx

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, {
44
useContext,
55
useEffect,
66
useMemo,
7+
useRef,
78
useState,
89
} from 'react';
910
import { useRouter } from 'next/router';
@@ -61,6 +62,18 @@ type TToolHeader = {
6162
blockLanguageSwitcher?: boolean;
6263
hidden?: boolean;
6364
};
65+
const normalizePath = (p: string) => p.replace(/\/+$/, '') || '/';
66+
67+
const getActiveFromPath = (pathname: string) => {
68+
const path = normalizePath(pathname);
69+
70+
if (path.includes('/uxcore')) return 'uxcore';
71+
if (path.includes('/uxcg')) return 'uxcg';
72+
if (path.includes('/uxcp')) return 'uxcp';
73+
if (path.includes('/uxcat') || path.includes('/user')) return 'uxcat';
74+
75+
return null;
76+
};
6477

6578
const ToolHeader: FC<TToolHeader> = ({
6679
homepageLinkTarget = '_self',
@@ -98,7 +111,9 @@ const ToolHeader: FC<TToolHeader> = ({
98111
const [token, setToken] = useState<string | null>(null);
99112
const [usernameIsTakenError, setUsernameIsTakenError] = useState('');
100113
const [changedTitle, setChangedTitle] = useState(false);
101-
const [activePage, setActivePage] = useState<string>(router.asPath);
114+
const [activePage, setActivePage] = useState(() =>
115+
getActiveFromPath(router.asPath),
116+
);
102117

103118
const {
104119
ourProjects,
@@ -116,6 +131,7 @@ const ToolHeader: FC<TToolHeader> = ({
116131
const currentUsername = !!accountData && accountData.username;
117132
const currentEmail = accountData && accountData.email;
118133
const publicEmail = accountData && accountData.publicEmail;
134+
const isRoutingRef = useRef(false);
119135

120136
const linkedIn = userInfo?.user?.linkedin
121137
? userInfo?.user?.linkedin
@@ -141,35 +157,36 @@ const ToolHeader: FC<TToolHeader> = ({
141157
};
142158

143159
const title = changedTitle ? userInfo?.title : userInfo?.user?.title;
144-
const normalizePath = (p: string) => p.replace(/\/+$/, '') || '/';
145-
146-
const getActiveFromPath = (pathname: string) => {
147-
const path = normalizePath(pathname);
148-
149-
if (path.includes('/uxcore')) return 'uxcore';
150-
if (path.includes('/uxcg')) return 'uxcg';
151-
if (path.includes('/uxcp')) return 'uxcp';
152-
if (path.includes('/uxcat') || path.includes('/user')) return 'uxcat';
153-
154-
return null;
155-
};
156160

157161
useEffect(() => {
158162
if (!router.isReady) return;
159163

160-
const sync = (url: string) => {
164+
const onStart = () => {
165+
isRoutingRef.current = true;
166+
};
167+
168+
const onDone = (url: string) => {
169+
isRoutingRef.current = false;
170+
161171
const next = getActiveFromPath(url);
162172
if (next) setActivePage(next);
163173
};
164174

165-
sync(router.asPath);
175+
router.events.on('routeChangeStart', onStart);
176+
router.events.on('routeChangeComplete', onDone);
177+
router.events.on('routeChangeError', () => {
178+
isRoutingRef.current = false;
179+
});
166180

167-
router.events.on('routeChangeComplete', sync);
181+
const initial = getActiveFromPath(router.asPath);
182+
if (initial) setActivePage(initial);
168183

169184
return () => {
170-
router.events.off('routeChangeComplete', sync);
185+
router.events.off('routeChangeStart', onStart);
186+
router.events.off('routeChangeComplete', onDone);
187+
router.events.off('routeChangeError', onDone as any);
171188
};
172-
}, [router.isReady, router]);
189+
}, [router.isReady, router.events, router.asPath]);
173190

174191
const openPodcastHandler = useCallback(() => {
175192
setOpenPodcast(prev => !prev);

0 commit comments

Comments
 (0)