aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-breadcrumb.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-12-12 18:50:03 +0100
committerArmand Philippot <git@armandphilippot.com>2023-12-12 18:50:03 +0100
commit85c4c42bd601270d7be0f34a0767a34bb85e29bb (patch)
tree16a07a89cf209139672592fd6988f0c028acb7e9 /src/utils/hooks/use-breadcrumb.ts
parent93f87c10783e3d76f1dec667779aedffcae33a39 (diff)
refactor(hooks): rewrite useBreadcrumbs hook
* use next/router to get the slug instead of using props * handle cases where the current page title is not provided * update JSON-LD schema to match the example in documentation * add tests
Diffstat (limited to 'src/utils/hooks/use-breadcrumb.ts')
-rw-r--r--src/utils/hooks/use-breadcrumb.ts124
1 files changed, 0 insertions, 124 deletions
diff --git a/src/utils/hooks/use-breadcrumb.ts b/src/utils/hooks/use-breadcrumb.ts
deleted file mode 100644
index 8b23ff2..0000000
--- a/src/utils/hooks/use-breadcrumb.ts
+++ /dev/null
@@ -1,124 +0,0 @@
-/* eslint-disable max-statements */
-import { useIntl } from 'react-intl';
-import type { BreadcrumbList } from 'schema-dts';
-import type { BreadcrumbsItem } from '../../components';
-import { CONFIG } from '../config';
-import { ROUTES } from '../constants';
-import { slugify } from '../helpers';
-
-const isArticle = (url: string) => url.startsWith(`${ROUTES.ARTICLE}/`);
-
-const isHome = (url: string) => url === '/';
-
-const isPageNumber = (url: string) => url.includes('/page/');
-
-const isProject = (url: string) => url.startsWith(`${ROUTES.PROJECTS}/`);
-
-const isSearch = (url: string) => url.startsWith(ROUTES.SEARCH);
-
-const isThematic = (url: string) => url.startsWith(`${ROUTES.THEMATICS}/`);
-
-const isTopic = (url: string) => url.startsWith(`${ROUTES.TOPICS}/`);
-
-const hasBlogAsParent = (url: string) =>
- isArticle(url) ||
- isPageNumber(url) ||
- isSearch(url) ||
- isThematic(url) ||
- isTopic(url);
-
-export type useBreadcrumbProps = {
- /**
- * The current page title.
- */
- title: string;
- /**
- * The current page url.
- */
- url: string;
-};
-
-export type useBreadcrumbReturn = {
- /**
- * The breadcrumb items.
- */
- items: BreadcrumbsItem[];
- /**
- * 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 labels = {
- home: intl.formatMessage({
- defaultMessage: 'Home',
- description: 'Breadcrumb: home label',
- id: 'j5k9Fe',
- }),
- blog: intl.formatMessage({
- defaultMessage: 'Blog',
- description: 'Breadcrumb: blog label',
- id: 'Es52wh',
- }),
- projects: intl.formatMessage({
- defaultMessage: 'Projects',
- description: 'Breadcrumb: projects label',
- id: '28GZdv',
- }),
- };
-
- const items: BreadcrumbsItem[] = [
- { id: 'home', name: labels.home, url: '/' },
- ];
- const schema: BreadcrumbList['itemListElement'][] = [
- {
- '@type': 'ListItem',
- position: 1,
- name: labels.home,
- item: CONFIG.url,
- },
- ];
-
- if (isHome(url)) return { items, schema };
-
- if (hasBlogAsParent(url)) {
- items.push({ id: 'blog', name: labels.blog, url: ROUTES.BLOG });
- schema.push({
- '@type': 'ListItem',
- position: 2,
- name: labels.blog,
- item: `${CONFIG.url}${ROUTES.BLOG}`,
- });
- }
-
- if (isProject(url)) {
- items.push({ id: 'projects', name: labels.projects, url: ROUTES.PROJECTS });
- schema.push({
- '@type': 'ListItem',
- position: 2,
- name: labels.projects,
- item: `${CONFIG.url}${ROUTES.PROJECTS}`,
- });
- }
-
- items.push({ id: slugify(title), name: title, url });
- schema.push({
- '@type': 'ListItem',
- position: schema.length + 1,
- name: title,
- item: `${CONFIG.url}${url}`,
- });
-
- return { items, schema };
-};