From 70efcfeaa0603415dd992cb662d8efb960e6e49a Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 26 Sep 2023 15:54:28 +0200 Subject: refactor(routes): replace hardcoded routes with constants It makes it easier to change a route if needed and it avoid typo mistakes. I also refactored a bit the concerned files to be complient with the new ESlint config. However, I should rewrite the pages to reduce the number of statements. --- src/components/templates/layout/layout.tsx | 60 ++++++++++++++++++------------ 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'src/components/templates/layout/layout.tsx') diff --git a/src/components/templates/layout/layout.tsx b/src/components/templates/layout/layout.tsx index beb6562..7c97901 100644 --- a/src/components/templates/layout/layout.tsx +++ b/src/components/templates/layout/layout.tsx @@ -1,8 +1,16 @@ +/* eslint-disable max-statements */ import Script from 'next/script'; -import { FC, ReactElement, ReactNode, useRef, useState } from 'react'; +import { + type FC, + type ReactElement, + type ReactNode, + useRef, + useState, +} from 'react'; import { useIntl } from 'react-intl'; -import { Person, SearchAction, WebSite, WithContext } from 'schema-dts'; -import { type NextPageWithLayoutOptions } from '../../../types'; +import type { Person, SearchAction, WebSite, WithContext } from 'schema-dts'; +import type { NextPageWithLayoutOptions } from '../../../types'; +import { ROUTES } from '../../../utils/constants'; import { useRouteChange, useScrollPosition, @@ -25,7 +33,6 @@ import { Header, type HeaderProps, } from '../../organisms'; -import photo from '/public/armand-philippot.jpg'; import styles from './layout.module.scss'; export type QueryAction = SearchAction & { @@ -121,25 +128,25 @@ export const Layout: FC = ({ { id: 'blog', label: blogLabel, - href: '/blog', + href: ROUTES.BLOG, logo: , }, { id: 'projects', label: projectsLabel, - href: '/projets', + href: ROUTES.PROJECTS, logo: , }, { id: 'cv', label: cvLabel, - href: '/cv', + href: ROUTES.CV, logo: , }, { id: 'contact', label: contactLabel, - href: '/contact', + href: ROUTES.CONTACT, logo: , }, ]; @@ -151,14 +158,14 @@ export const Layout: FC = ({ }); const footerNav: FooterProps['navItems'] = [ - { id: 'legal-notice', label: legalNoticeLabel, href: '/mentions-legales' }, + { id: 'legal-notice', label: legalNoticeLabel, href: ROUTES.LEGAL_NOTICE }, ]; const searchActionSchema: QueryAction = { '@type': 'SearchAction', target: { '@type': 'EntryPoint', - urlTemplate: `${url}/recherche?s={search_term_string}`, + urlTemplate: `${url}${ROUTES.SEARCH}?s={search_term_string}`, }, query: 'required', 'query-input': 'required name=search_term_string', @@ -168,9 +175,9 @@ export const Layout: FC = ({ '@context': 'https://schema.org', '@id': `${url}`, '@type': 'WebSite', - name: name, + name, description: baseline, - url: url, + url, author: { '@id': `${url}/#branding` }, copyrightYear: Number(copyright.start), creator: { '@id': `${url}/#branding` }, @@ -183,10 +190,10 @@ export const Layout: FC = ({ '@context': 'https://schema.org', '@type': 'Person', '@id': `${url}/#branding`, - name: name, - url: url, + name, + url, jobTitle: baseline, - image: photo.src, + image: '/armand-philippot.jpg', subjectOf: { '@id': `${url}` }, }; @@ -194,48 +201,56 @@ export const Layout: FC = ({ styles['back-to-top--hidden'] ); const updateBackToTopClassName = () => { + const visibleBreakpoint = 300; setBackToTopClassName( - window.scrollY > 300 + window.scrollY > visibleBreakpoint ? styles['back-to-top--visible'] : styles['back-to-top--hidden'] ); }; useScrollPosition(updateBackToTopClassName); + const topRef = useRef(null); const giveFocusToTopRef = () => { if (topRef.current) topRef.current.focus(); }; + useRouteChange(giveFocusToTopRef); return ( <>