Skip to content

Commit f9e732b

Browse files
committed
create new hook to get current path including the dynamic generated ones using a slug
1 parent ca9dcec commit f9e732b

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

src/components/Banners.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { FC } from 'react';
2-
import { useRouter } from 'next/router';
32
import { Box, Link } from '@chakra-ui/react';
43

4+
import { useCurrentPath } from '../hooks/useCurrentPath';
55
import { Banner } from './UI';
66

77
import { ACADEMIC_GRANTS_URL } from '../constants';
88

99
export const Banners: FC = () => {
10-
const router = useRouter();
10+
const path = useCurrentPath();
1111

12-
if (!router.pathname.includes(ACADEMIC_GRANTS_URL)) {
12+
if (!path.includes(ACADEMIC_GRANTS_URL)) {
1313
return (
1414
<Banner>
1515
<Box fontSize='paragraph' textAlign='center'>

src/components/layout/Layout.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Box, Container, ContainerProps } from '@chakra-ui/react';
22
import { FC, ReactNode } from 'react';
3-
import { useRouter } from 'next/router';
43

54
import { Footer, FooterBackgroundImage, HomepageHero } from '../UI';
5+
import { useCurrentPath } from '../../hooks/useCurrentPath';
66
import { Forms } from '../forms';
77

88
import {
@@ -40,10 +40,10 @@ import {
4040
} from '../../constants';
4141

4242
export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
43-
const router = useRouter();
43+
const path = useCurrentPath();
4444

4545
const renderContent = (): ReactNode => {
46-
if (router.pathname === HOME_URL) {
46+
if (path === HOME_URL) {
4747
return (
4848
<>
4949
<HomepageHero />
@@ -53,7 +53,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
5353
);
5454
}
5555

56-
if (router.pathname.startsWith(APPLICANTS_URL)) {
56+
if (path.startsWith(APPLICANTS_URL)) {
5757
return (
5858
<Box mt={-6}>
5959
<main>
@@ -63,7 +63,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
6363
);
6464
}
6565

66-
if (router.pathname.startsWith(ABOUT_URL)) {
66+
if (path.startsWith(ABOUT_URL)) {
6767
return (
6868
<Box mt={-6}>
6969
<main>
@@ -73,7 +73,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
7373
);
7474
}
7575

76-
if (router.pathname === DEVCON_GRANTS_URL) {
76+
if (path === DEVCON_GRANTS_URL) {
7777
return (
7878
<Box mt={{ md: -10, lg: 0 }}>
7979
<main>
@@ -83,7 +83,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
8383
);
8484
}
8585

86-
if (router.pathname === ACADEMIC_GRANTS_2022_URL) {
86+
if (path === ACADEMIC_GRANTS_2022_URL) {
8787
return (
8888
<Box mt={{ md: -10, lg: 0 }}>
8989
<main>
@@ -93,7 +93,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
9393
);
9494
}
9595

96-
if (router.pathname === ACADEMIC_GRANTS_2023_URL) {
96+
if (path === ACADEMIC_GRANTS_2023_URL) {
9797
return (
9898
<Box mt={{ md: -10, lg: 0 }}>
9999
<main>
@@ -103,7 +103,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
103103
);
104104
}
105105

106-
if (router.pathname === ACADEMIC_GRANTS_URL) {
106+
if (path === ACADEMIC_GRANTS_URL) {
107107
return (
108108
<Box mt={{ md: -10, lg: 0 }}>
109109
<main>
@@ -113,7 +113,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
113113
);
114114
}
115115

116-
if (router.pathname === MERGE_DATA_CHALLENGE_URL) {
116+
if (path === MERGE_DATA_CHALLENGE_URL) {
117117
return (
118118
<Box mt={{ md: -10, lg: 0 }}>
119119
<main>
@@ -124,7 +124,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
124124
}
125125

126126
// TODO: refactor these if conditions ????
127-
if (router.pathname === SEMAPHORE_GRANT_URL) {
127+
if (path === SEMAPHORE_GRANT_URL) {
128128
return (
129129
<Box mt={{ md: -10, lg: 0 }}>
130130
<main>
@@ -134,7 +134,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
134134
);
135135
}
136136

137-
if (router.pathname === LAYER_2_GRANTS_URL) {
137+
if (path === LAYER_2_GRANTS_URL) {
138138
return (
139139
<Box mt={{ md: -10, lg: 0 }}>
140140
<main>
@@ -144,7 +144,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
144144
);
145145
}
146146

147-
if (router.pathname === ACCOUNT_ABSTRACTION_GRANTS_URL) {
147+
if (path === ACCOUNT_ABSTRACTION_GRANTS_URL) {
148148
return (
149149
<Box mt={{ md: -10, lg: 0 }}>
150150
<main>
@@ -154,7 +154,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
154154
);
155155
}
156156

157-
if (router.pathname === RUN_A_NODE_GRANTS_URL) {
157+
if (path === RUN_A_NODE_GRANTS_URL) {
158158
return (
159159
<Box mt={{ md: -10, lg: 0 }}>
160160
<main>
@@ -164,7 +164,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
164164
);
165165
}
166166

167-
if (router.pathname === DATA_COLLECTION_ROUND_URL) {
167+
if (path === DATA_COLLECTION_ROUND_URL) {
168168
return (
169169
<Box mt={{ md: -10, lg: 0 }}>
170170
<main>
@@ -174,7 +174,7 @@ export const Layout: FC<ContainerProps> = ({ children, ...props }) => {
174174
);
175175
}
176176

177-
if (GRANTS_URLS.includes(router.pathname)) {
177+
if (GRANTS_URLS.includes(path)) {
178178
return (
179179
<Box mt={-6}>
180180
<main>

src/hooks/useCurrentPath.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useRouter } from 'next/router';
2+
3+
// Gets the current path from the slug param (if this is a dynamic route) or the
4+
// router pathname (if this is a static route)
5+
export const useCurrentPath = () => {
6+
const { query, pathname } = useRouter();
7+
8+
return query.slug ? query.slug.toString() : pathname;
9+
};

src/pages/_app.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { useEffect } from 'react';
22
import { ChakraProvider } from '@chakra-ui/react';
33
import type { AppProps } from 'next/app';
44
import Head from 'next/head';
5-
import { useRouter } from 'next/router';
65
import { init, push } from '@socialgouv/matomo-next';
76

87
import { Layout } from '../components/layout';
98
import { Banners } from '../components';
9+
import { useCurrentPath } from '../hooks/useCurrentPath';
1010

1111
import { getBg, getBgGradient, getLayoutHeight } from '../utils';
1212

@@ -24,7 +24,7 @@ import favicon16 from '../../public/images/favicon-16x16.png';
2424
import favicon32 from '../../public/images/favicon-32x32.png';
2525

2626
function MyApp({ Component, pageProps }: AppProps) {
27-
const router = useRouter();
27+
const path = useCurrentPath();
2828

2929
useEffect(() => {
3030
init({
@@ -47,9 +47,9 @@ function MyApp({ Component, pageProps }: AppProps) {
4747

4848
<Layout
4949
position='relative'
50-
bg={getBg(router.pathname)}
51-
bgGradient={getBgGradient(router.pathname)}
52-
h={{ base: '600px', lg: getLayoutHeight(router.pathname) }}
50+
bg={getBg(path)}
51+
bgGradient={getBgGradient(path)}
52+
h={{ base: '600px', lg: getLayoutHeight(path) }}
5353
>
5454
<Component {...pageProps} />
5555
</Layout>

0 commit comments

Comments
 (0)