summaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/pages.ts
blob: 93582f0fdefa10a695a1aacda9b48f6ddcdc15a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { type Post } from '@components/organisms/layout/posts-list';
import { type LinksListItems } from '@components/organisms/widgets/links-list-widget';
import { type Meta, type PageLink } from '@ts/types/app';
import {
  type RawThematicPreview,
  type RawTopicPreview,
} from '@ts/types/raw-data';

/**
 * Convert raw data to a Link object.
 *
 * @param data - An object.
 * @param {number} data.databaseId - The data id.
 * @param {string} data.slug - The data slug.
 * @param {string} data.title - The data name.
 * @returns {PageLink} The link data (id, slug and title).
 */
export const getPageLinkFromRawData = (
  data: RawThematicPreview | RawTopicPreview
): PageLink => {
  const { databaseId, slug, title } = data;

  return {
    id: databaseId,
    name: title,
    slug,
  };
};

/**
 * Convert page link data to an array of links items.
 *
 * @param {PageLink[]} links - An array of page links.
 * @param {'thematic'|'topic'} kind - The page links kind.
 * @returns {LinksListItem[]} An array of links items.
 */
export const getLinksListItems = (
  links: PageLink[],
  kind: 'thematic' | 'topic'
): LinksListItems[] => {
  const baseUrl = kind === 'thematic' ? '/thematique/' : '/sujet/';

  return links.map((link) => {
    return {
      name: link.name,
      url: `${baseUrl}${link.slug}`,
    };
  });
};

/**
 * Retrieve the formatted meta.
 *
 * @param {Meta<'article'>} meta - The article meta.
 * @returns {Post['meta']} The formatted meta.
 */
export const getPostMeta = (meta: Meta<'article'>): Post['meta'] => {
  const { commentsCount, dates, thematics, topics, wordsCount } = meta;

  return {
    commentsCount,
    dates,
    readingTime: { wordsCount: wordsCount || 0, onlyMinutes: true },
    thematics: thematics?.map((thematic) => {
      return { ...thematic, url: `/thematique/${thematic.slug}` };
    }),
    topics: topics?.map((topic) => {
      return { ...topic, url: `/sujet/${topic.slug}` };
    }),
  };
};