From 7b37a43a0d46fcfc1192c17e8e6e62e4b64113a9 Mon Sep 17 00:00:00 2001 From: MaryWylde Date: Tue, 23 Dec 2025 20:36:45 +0400 Subject: [PATCH] chore: fix state of active path when page is still loading --- src/components/ToolHeader/ToolHeader.tsx | 53 ++++++++++++++++-------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/components/ToolHeader/ToolHeader.tsx b/src/components/ToolHeader/ToolHeader.tsx index 60f0214..aad070e 100644 --- a/src/components/ToolHeader/ToolHeader.tsx +++ b/src/components/ToolHeader/ToolHeader.tsx @@ -4,6 +4,7 @@ import React, { useContext, useEffect, useMemo, + useRef, useState, } from 'react'; import { useRouter } from 'next/router'; @@ -61,6 +62,18 @@ type TToolHeader = { blockLanguageSwitcher?: boolean; hidden?: boolean; }; +const normalizePath = (p: string) => p.replace(/\/+$/, '') || '/'; + +const getActiveFromPath = (pathname: string) => { + const path = normalizePath(pathname); + + if (path.includes('/uxcore')) return 'uxcore'; + if (path.includes('/uxcg')) return 'uxcg'; + if (path.includes('/uxcp')) return 'uxcp'; + if (path.includes('/uxcat') || path.includes('/user')) return 'uxcat'; + + return null; +}; const ToolHeader: FC = ({ homepageLinkTarget = '_self', @@ -98,7 +111,9 @@ const ToolHeader: FC = ({ const [token, setToken] = useState(null); const [usernameIsTakenError, setUsernameIsTakenError] = useState(''); const [changedTitle, setChangedTitle] = useState(false); - const [activePage, setActivePage] = useState(router.asPath); + const [activePage, setActivePage] = useState(() => + getActiveFromPath(router.asPath), + ); const { ourProjects, @@ -116,6 +131,7 @@ const ToolHeader: FC = ({ const currentUsername = !!accountData && accountData.username; const currentEmail = accountData && accountData.email; const publicEmail = accountData && accountData.publicEmail; + const isRoutingRef = useRef(false); const linkedIn = userInfo?.user?.linkedin ? userInfo?.user?.linkedin @@ -141,35 +157,36 @@ const ToolHeader: FC = ({ }; const title = changedTitle ? userInfo?.title : userInfo?.user?.title; - const normalizePath = (p: string) => p.replace(/\/+$/, '') || '/'; - - const getActiveFromPath = (pathname: string) => { - const path = normalizePath(pathname); - - if (path.includes('/uxcore')) return 'uxcore'; - if (path.includes('/uxcg')) return 'uxcg'; - if (path.includes('/uxcp')) return 'uxcp'; - if (path.includes('/uxcat') || path.includes('/user')) return 'uxcat'; - - return null; - }; useEffect(() => { if (!router.isReady) return; - const sync = (url: string) => { + const onStart = () => { + isRoutingRef.current = true; + }; + + const onDone = (url: string) => { + isRoutingRef.current = false; + const next = getActiveFromPath(url); if (next) setActivePage(next); }; - sync(router.asPath); + router.events.on('routeChangeStart', onStart); + router.events.on('routeChangeComplete', onDone); + router.events.on('routeChangeError', () => { + isRoutingRef.current = false; + }); - router.events.on('routeChangeComplete', sync); + const initial = getActiveFromPath(router.asPath); + if (initial) setActivePage(initial); return () => { - router.events.off('routeChangeComplete', sync); + router.events.off('routeChangeStart', onStart); + router.events.off('routeChangeComplete', onDone); + router.events.off('routeChangeError', onDone as any); }; - }, [router.isReady, router]); + }, [router.isReady, router.events, router.asPath]); const openPodcastHandler = useCallback(() => { setOpenPodcast(prev => !prev);