diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-09-26 15:54:28 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-10-24 12:23:48 +0200 |
| commit | 70efcfeaa0603415dd992cb662d8efb960e6e49a (patch) | |
| tree | 5d37e98fae9aa7e5c3d8ef30a10db9fed9b63e36 /src/utils/hooks/use-breadcrumb.tsx | |
| parent | 31695306bfed44409f03006ea717fd2cceff8f87 (diff) | |
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.
Diffstat (limited to 'src/utils/hooks/use-breadcrumb.tsx')
| -rw-r--r-- | src/utils/hooks/use-breadcrumb.tsx | 105 |
1 files changed, 0 insertions, 105 deletions
diff --git a/src/utils/hooks/use-breadcrumb.tsx b/src/utils/hooks/use-breadcrumb.tsx deleted file mode 100644 index f4506d7..0000000 --- a/src/utils/hooks/use-breadcrumb.tsx +++ /dev/null @@ -1,105 +0,0 @@ -import { useIntl } from 'react-intl'; -import { BreadcrumbList } from 'schema-dts'; -import { BreadcrumbItem } from '../../components'; -import { slugify } from '../helpers'; -import { useSettings } from './use-settings'; - -export type useBreadcrumbProps = { - /** - * The current page title. - */ - title: string; - /** - * The current page url. - */ - url: string; -}; - -export type useBreadcrumbReturn = { - /** - * The breadcrumb items. - */ - items: BreadcrumbItem[]; - /** - * The breadcrumb JSON schema. - */ - schema: BreadcrumbList['itemListElement'][]; -}; - -/** - * Retrieve the breadcrumb items. - * - * @param {useBreadcrumbProps} props - An object (the current page title & url). - * @returns {useBreadcrumbReturn} The breadcrumb items and its JSON schema. - */ -export const useBreadcrumb = ({ - title, - url, -}: useBreadcrumbProps): useBreadcrumbReturn => { - const intl = useIntl(); - const { website } = useSettings(); - const isArticle = url.startsWith('/article/'); - const isHome = url === '/'; - const isPageNumber = url.includes('/page/'); - const isProject = url.startsWith('/projets/'); - const isSearch = url.startsWith('/recherche'); - const isThematic = url.startsWith('/thematique/'); - const isTopic = url.startsWith('/sujet/'); - - const homeLabel = intl.formatMessage({ - defaultMessage: 'Home', - description: 'Breadcrumb: home label', - id: 'j5k9Fe', - }); - const items: BreadcrumbItem[] = [{ id: 'home', name: homeLabel, url: '/' }]; - const schema: BreadcrumbList['itemListElement'][] = [ - { - '@type': 'ListItem', - position: 1, - name: homeLabel, - item: website.url, - }, - ]; - - if (isHome) return { items, schema }; - - if (isArticle || isPageNumber || isSearch || isThematic || isTopic) { - const blogLabel = intl.formatMessage({ - defaultMessage: 'Blog', - description: 'Breadcrumb: blog label', - id: 'Es52wh', - }); - items.push({ id: 'blog', name: blogLabel, url: '/blog' }); - schema.push({ - '@type': 'ListItem', - position: 2, - name: blogLabel, - item: `${website.url}/blog`, - }); - } - - if (isProject) { - const projectsLabel = intl.formatMessage({ - defaultMessage: 'Projects', - description: 'Breadcrumb: projects label', - id: '28GZdv', - }); - items.push({ id: 'blog', name: projectsLabel, url: '/projets' }); - schema.push({ - '@type': 'ListItem', - position: 2, - name: projectsLabel, - item: `${website.url}/projets`, - }); - } - - items.push({ id: slugify(title), name: title, url }); - schema.push({ - '@type': 'ListItem', - position: schema.length + 1, - name: title, - item: `${website.url}${url}`, - }); - - return { items, schema }; -}; |
