-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(DocPage): Add updatedAt meta #198
Changes from 3 commits
8b0dcab
098dcfa
7192ec5
8f35097
48b497f
241360e
e8e4b98
c0d8fc1
22a2abd
c3a675c
fa6dda6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, {memo, useMemo} from 'react'; | ||
|
||
import block from 'bem-cn-lite'; | ||
|
||
import {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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем давать возможность переопределять этот ключ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему то тоже самое мы позволяем делать в Contributors |
||
const {t} = useTranslation(translationName); | ||
|
||
const updatedAtFormatted = useMemo(() => { | ||
const {localeCode} = getConfig(); | ||
return format(updatedAt, 'longDateTime', localeCode); | ||
}, [updatedAt]); | ||
|
||
return ( | ||
<div className={b()}> | ||
<div className={b('title')}>{t<string>('title')}</div> | ||
{updatedAtFormatted} | ||
</div> | ||
); | ||
}; | ||
|
||
export default memo(UpdatedAtDate); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
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 = 'en'; | ||
|
||
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, | ||
) => { | ||
let result = getDateTimeFormatter(localeCode)[formatCode].format(new Date(date)); | ||
if (formatCode === 'longDate' && ['ru-RU', 'ru-KZ'].includes(localeCode)) { | ||
result = result.replace(/\sг\.$/, ''); | ||
} | ||
return result; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.