import { CopyrightIcon } from '@components/Icons'; import { settings } from '@utils/config'; import styles from './Copyright.module.scss'; const Copyright = () => { return (

{settings.name} {settings.copyright.startYear} - {settings.copyright.endYear}

); }; export default Copyright; ndphilippot.com' title='www.armandphilippot.com Git repository'/>
summaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-reading-time.tsx
blob: fb54135c0f4358d5eaf12bdbccadb96bb6dac6ff (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
import { useIntl } from 'react-intl';

/**
 * Retrieve the estimated reading time by words count.
 *
 * @param {number} wordsCount - The number of words.
 * @returns {string} The estimated reading time.
 */
const useReadingTime = (
  wordsCount: number,
  onlyMinutes: boolean = false
): string => {
  const intl = useIntl();
  const wordsPerMinute = 245;
  const wordsPerSecond = wordsPerMinute / 60;
  const estimatedTimeInSeconds = wordsCount / wordsPerSecond;

  if (onlyMinutes) {
    const estimatedTimeInMinutes = Math.round(estimatedTimeInSeconds / 60);

    return intl.formatMessage(
      {
        defaultMessage: '{minutesCount} minutes',
        description: 'useReadingTime: rounded minutes count',
        id: 's1i43J',
      },
      { minutesCount: estimatedTimeInMinutes }
    );
  } else {
    const estimatedTimeInMinutes = Math.floor(estimatedTimeInSeconds / 60);

    if (estimatedTimeInMinutes <= 0) {
      return intl.formatMessage(
        {
          defaultMessage: '{count} seconds',
          description: 'useReadingTime: seconds count',
          id: 'i7Wq3G',
        },
        { count: estimatedTimeInSeconds.toFixed(0) }
      );
    }

    const remainingSeconds = Math.round(
      estimatedTimeInSeconds - estimatedTimeInMinutes * 60
    ).toFixed(0);

    return intl.formatMessage(
      {
        defaultMessage: '{minutesCount} minutes {secondsCount} seconds',
        description: 'useReadingTime: minutes + seconds count',
        id: 'OevMeU',
      },
      { minutesCount: estimatedTimeInMinutes, secondsCount: remainingSeconds }
    );
  }
};

export default useReadingTime;