From 28bb925b585d11107a9fba109c4b6945345e9eda Mon Sep 17 00:00:00 2001 From: Smilinko Date: Sun, 10 Dec 2023 00:06:52 +0100 Subject: [PATCH] Fixes always active menu route --- src/components/PageLayout/MenuMain/MenuMain.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/components/PageLayout/MenuMain/MenuMain.tsx b/src/components/PageLayout/MenuMain/MenuMain.tsx index adb599a3..fac0abe9 100644 --- a/src/components/PageLayout/MenuMain/MenuMain.tsx +++ b/src/components/PageLayout/MenuMain/MenuMain.tsx @@ -92,9 +92,16 @@ const MenuMainItem: FC<{caption: string; url: string}> = ({caption, url}) => { // potrebne koncove lomitko pre porovnanie s URLkami z BE const pathWithSlash = `${router.asPath}/` - // ak sme na `/matik/vysledky/44/leto/2`, orezme to na dlzku `url`, v zavere porovnajme - // (teda v podstate zistime, ci `pathWithSlash` zacina znakmi `url`) - const active = pathWithSlash.slice(0, url.length) === url + let active + + const urlArray = url.split('/') + if (urlArray.length === 3 && urlArray[0] === '' && urlArray[2] === '') { + // riesi case ked url je napr. /strom/ a teda nestaci porovnanie so .startsWith + // urlArray je tak v tvare ['', 'strom', ''] co overuje dlzka 3 a zaciatok/koniec ako '' + active = pathWithSlash === url + } else { + active = pathWithSlash.startsWith(url) + } return (