From 34e216546151eaf8a0a3cbb0bc8b65dae4c63bf2 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 23 May 2022 14:07:02 +0200 Subject: refactor: reduce the number of data transformation --- src/components/organisms/layout/summary.tsx | 73 +++++++++-------------------- 1 file changed, 23 insertions(+), 50 deletions(-) (limited to 'src/components/organisms/layout/summary.tsx') diff --git a/src/components/organisms/layout/summary.tsx b/src/components/organisms/layout/summary.tsx index 3831c0c..ca51cd6 100644 --- a/src/components/organisms/layout/summary.tsx +++ b/src/components/organisms/layout/summary.tsx @@ -6,7 +6,7 @@ import ResponsiveImage, { type ResponsiveImageProps, } from '@components/molecules/images/responsive-image'; import Meta, { type MetaData } from '@components/molecules/layout/meta'; -import { type Dates } from '@ts/types/app'; +import { type Article, type Meta as MetaType } from '@ts/types/app'; import useReadingTime from '@utils/hooks/use-reading-time'; import { FC, ReactNode } from 'react'; import { useIntl } from 'react-intl'; @@ -17,43 +17,22 @@ export type Cover = Pick< 'alt' | 'src' | 'width' | 'height' >; -export type SummaryMetaLink = { - id: number | string; - name: string; - url: string; -}; - -export type SummaryMetaReadingTime = { - wordsCount: number; - onlyMinutes?: boolean; -}; - -export type SummaryMeta = { - author?: string; - commentsCount?: number; - dates: Dates; - readingTime: SummaryMetaReadingTime; - thematics?: SummaryMetaLink[]; - topics?: SummaryMetaLink[]; -}; +export type SummaryMeta = Pick< + MetaType<'article'>, + | 'author' + | 'commentsCount' + | 'cover' + | 'dates' + | 'thematics' + | 'topics' + | 'wordsCount' +>; -export type SummaryProps = { +export type SummaryProps = Pick & { /** - * The post cover. - */ - cover?: Cover; - /** - * The post excerpt. - */ - excerpt: string; - /** - * The post meta. + * The post metadata. */ meta: SummaryMeta; - /** - * The post title. - */ - title: string; /** * The heading level (hn). */ @@ -70,8 +49,7 @@ export type SummaryProps = { * Render a page summary. */ const Summary: FC = ({ - cover, - excerpt, + intro, meta, title, titleLevel = 2, @@ -91,14 +69,13 @@ const Summary: FC = ({ ), } ); - const { wordsCount, onlyMinutes } = meta.readingTime; - const readingTime = useReadingTime(wordsCount, onlyMinutes); - - const getMeta = (data: SummaryMeta): MetaData => { - const { author, commentsCount, dates, thematics, topics } = data; + const { author, commentsCount, cover, dates, thematics, topics, wordsCount } = + meta; + const readingTime = useReadingTime(wordsCount, true); + const getMeta = (): MetaData => { return { - author, + author: author?.name, publication: { date: dates.publication }, update: dates.update && dates.publication !== dates.update @@ -106,12 +83,12 @@ const Summary: FC = ({ : undefined, readingTime, thematics: thematics?.map((thematic) => ( - + {thematic.name} )), topics: topics?.map((topic) => ( - + {topic.name} )), @@ -134,11 +111,7 @@ const Summary: FC = ({
- {typeof excerpt === 'string' ? ( -
- ) : ( - excerpt - )} +
<> {readMore} @@ -148,7 +121,7 @@ const Summary: FC = ({