-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@import '../../styles/mixins'; | ||
|
||
.updated-at-date { | ||
display: flex; | ||
margin: 0 0 32px; | ||
|
||
&__title { | ||
@include contributors-text(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, {memo, useMemo} from 'react'; | ||
|
||
import block from 'bem-cn-lite'; | ||
|
||
import {configure, getConfig} from '../../config'; | ||
import {useTranslation} from '../../hooks'; | ||
import {format} from '../../utils/date'; | ||
|
||
import './UpdatedAtDate.scss'; | ||
|
||
const b = block('updated-at-date'); | ||
|
||
export interface UpdatedAtDateProps { | ||
updatedAt: string; | ||
translationName?: string; | ||
} | ||
|
||
const UpdatedAtDate: React.FC<UpdatedAtDateProps> = (props) => { | ||
const {updatedAt, translationName = 'updated-at-date'} = props; | ||
const {t} = useTranslation(translationName); | ||
|
||
const updatedAtFormatted = useMemo(() => { | ||
configure(); | ||
const config = getConfig(); | ||
return format(updatedAt, 'longDateTime', config.localeCode); | ||
}, [updatedAt]); | ||
|
||
return ( | ||
<div className={b()}> | ||
<div className={b('title')}>{t<string>('title')}</div> | ||
{updatedAtFormatted} | ||
</div> | ||
); | ||
}; | ||
|
||
export default memo(UpdatedAtDate); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
interface DateTimeFormatter { | ||
longDate: Intl.DateTimeFormat; | ||
shortDate: Intl.DateTimeFormat; | ||
longMonthDay: Intl.DateTimeFormat; | ||
shortMonthDay: Intl.DateTimeFormat; | ||
longDateTime: Intl.DateTimeFormat; | ||
shortDateTime: Intl.DateTimeFormat; | ||
shortTime: Intl.DateTimeFormat; | ||
nanoTime: Intl.DateTimeFormat; | ||
year: Intl.DateTimeFormat; | ||
} | ||
|
||
const defaultRegion = 'ru-RU'; | ||
|
||
const dateTimeFormatters: Map<string, DateTimeFormatter> = new Map(); | ||
|
||
function getDateTimeFormatter(localeCode: string): DateTimeFormatter { | ||
if (!dateTimeFormatters.has(localeCode)) { | ||
const formatters = { | ||
// December 20, 2012 | ||
longDate: new Intl.DateTimeFormat(localeCode, { | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
}), | ||
// 4/30/2021 | ||
shortDate: new Intl.DateTimeFormat(localeCode, { | ||
year: 'numeric', | ||
month: 'numeric', | ||
day: 'numeric', | ||
}), | ||
// December 20 | ||
longMonthDay: new Intl.DateTimeFormat(localeCode, {month: 'long', day: 'numeric'}), | ||
// 12/20 | ||
shortMonthDay: new Intl.DateTimeFormat(localeCode, {month: 'numeric', day: 'numeric'}), | ||
// April 27, 2021, 3:03 PM | ||
longDateTime: new Intl.DateTimeFormat(localeCode, { | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
}), | ||
// 4/30/2021, 2:30 PM | ||
shortDateTime: new Intl.DateTimeFormat(localeCode, { | ||
year: 'numeric', | ||
month: 'numeric', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
}), | ||
// 12:30 | ||
shortTime: new Intl.DateTimeFormat(localeCode, {hour: 'numeric', minute: 'numeric'}), | ||
// 30:58 | ||
nanoTime: new Intl.DateTimeFormat(localeCode, { | ||
minute: 'numeric', | ||
second: 'numeric', | ||
}), | ||
// 2023 | ||
year: new Intl.DateTimeFormat(localeCode, { | ||
year: 'numeric', | ||
}), | ||
}; | ||
dateTimeFormatters.set(localeCode, formatters); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- formatter will be always inserted above | ||
return dateTimeFormatters.get(localeCode)!; | ||
} | ||
|
||
export const format = ( | ||
date: string | number, | ||
formatCode: keyof DateTimeFormatter, | ||
localeCode = defaultRegion, | ||
) => getDateTimeFormatter(localeCode)[formatCode].format(new Date(date)); |