diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-04-25 12:57:12 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-04-25 12:57:12 +0200 |
| commit | 8a6f09b564d5d2f02d0a2605f6b52070a910aaa3 (patch) | |
| tree | cd9e2b6ae6be75f4595b9823e67ebb6bc76df8e8 /src/utils | |
| parent | 782a5a1e794a9a8ef6b0b892cd3f386ed583c680 (diff) | |
chore: add a PageLayout component
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/helpers/format.ts | 28 | ||||
| -rw-r--r-- | src/utils/hooks/use-is-mounted.tsx | 19 |
2 files changed, 44 insertions, 3 deletions
diff --git a/src/utils/helpers/format.ts b/src/utils/helpers/format.ts index dd35868..47a7b57 100644 --- a/src/utils/helpers/format.ts +++ b/src/utils/helpers/format.ts @@ -14,6 +14,7 @@ import { TopicPreview, Thematic, } from '@ts/types/taxonomies'; +import { settings } from '@utils/config'; /** * Format a post preview from RawArticlePreview to ArticlePreview type. @@ -269,11 +270,14 @@ export const getFormattedPost = (rawPost: RawArticle): Article => { /** * Converts a date to a string by using the specified locale. - * @param {string} date The date. - * @param {string} locale A locale. + * @param {string} date - The date. + * @param {string} [locale] - A locale. * @returns {string} The formatted date to locale date string. */ -export const getFormattedDate = (date: string, locale: string) => { +export const getFormattedDate = ( + date: string, + locale: string = settings.locales.defaultLocale +): string => { const dateOptions: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'long', @@ -284,6 +288,24 @@ export const getFormattedDate = (date: string, locale: string) => { }; /** + * Converts a date to a time string by using the specified locale. + * @param {string} date - The date. + * @param {string} [locale] - A locale. + * @returns {string} The formatted time to locale date string. + */ +export const getFormattedTime = ( + date: string, + locale: string = settings.locales.defaultLocale +): string => { + const time = new Date(date).toLocaleTimeString(locale, { + hour: 'numeric', + minute: 'numeric', + }); + + return locale === 'fr' ? time.replace(':', 'h') : time; +}; + +/** * Convert an array of slugs to an array of params with slug. * @param {Slug} array - An array of object with slug. * @returns {ParamsSlug} An array of params with slug. diff --git a/src/utils/hooks/use-is-mounted.tsx b/src/utils/hooks/use-is-mounted.tsx new file mode 100644 index 0000000..ca79afb --- /dev/null +++ b/src/utils/hooks/use-is-mounted.tsx @@ -0,0 +1,19 @@ +import { RefObject, useEffect, useState } from 'react'; + +/** + * Check if an HTML element is mounted. + * + * @param {RefObject<HTMLElement>} ref - A React reference to an HTML element. + * @returns {boolean} True if the HTML element is mounted. + */ +const useIsMounted = (ref: RefObject<HTMLElement>) => { + const [isMounted, setIsMounted] = useState<boolean>(false); + + useEffect(() => { + if (ref.current) setIsMounted(true); + }, [ref]); + + return isMounted; +}; + +export default useIsMounted; |
