feat: short time ago

zio/stable
Anthony Fu 2022-12-02 16:16:06 +08:00
parent 9b207de718
commit a9af7e4a09
4 changed files with 56 additions and 40 deletions

View File

@ -41,7 +41,7 @@ function go(evt: MouseEvent | KeyboardEvent) {
} }
const createdAt = useFormattedDateTime(status.createdAt) const createdAt = useFormattedDateTime(status.createdAt)
const timeAgoOptions = useTimeAgoOptions() const timeAgoOptions = useTimeAgoOptions(true)
const timeago = useTimeAgo(() => status.createdAt, timeAgoOptions) const timeago = useTimeAgo(() => status.createdAt, timeAgoOptions)
</script> </script>

View File

@ -12,28 +12,30 @@ export const useFormattedDateTime = (
}) })
} }
export const useTimeAgoOptions = (): UseTimeAgoOptions<false> => { export const useTimeAgoOptions = (short = false): UseTimeAgoOptions<false> => {
const { d, t } = useI18n() const { d, t } = useI18n()
const prefix = short ? 'short_' : ''
return { return {
showSecond: true, showSecond: !short,
updateInterval: 1_000, updateInterval: short ? 60_000 : 1_000,
messages: { messages: {
justNow: t('time_ago_options.just_now'), justNow: t('time_ago_options.just_now'),
// just return the value // just return the value
past: n => n, past: n => n,
// just return the value // just return the value
future: n => n, future: n => n,
second: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_second`, n), second: (n, p) => t(`time_ago_options.${prefix}second_${p ? 'past' : 'future'}`, n),
minute: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_minute`, n), minute: (n, p) => t(`time_ago_options.${prefix}minute_${p ? 'past' : 'future'}`, n),
hour: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_hour`, n), hour: (n, p) => t(`time_ago_options.${prefix}hour_${p ? 'past' : 'future'}`, n),
day: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_day`, n), day: (n, p) => t(`time_ago_options.${prefix}day_${p ? 'past' : 'future'}`, n),
week: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_week`, n), week: (n, p) => t(`time_ago_options.${prefix}week_${p ? 'past' : 'future'}`, n),
month: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_month`, n), month: (n, p) => t(`time_ago_options.${prefix}month_${p ? 'past' : 'future'}`, n),
year: (n, p) => t(`time_ago_options.${p ? 'past' : 'future'}_year`, n), year: (n, p) => t(`time_ago_options.${prefix}year_${p ? 'past' : 'future'}`, n),
}, },
fullDateFormatter(date) { fullDateFormatter(date) {
return d(date, 'long') return d(date, short ? 'short' : 'long')
}, },
} }
} }

View File

@ -142,20 +142,34 @@
}, },
"time_ago_options": { "time_ago_options": {
"just_now": "just now", "just_now": "just now",
"past_second": "just now|{n} second ago|{n} seconds ago", "second_past": "just now|{n} second ago|{n} seconds ago",
"future_second": "just now|in {n} second|in {n} seconds", "second_future": "just now|in {n} second|in {n} seconds",
"past_minute": "0 minutes ago|1 minute ago|{n} minutes ago", "minute_past": "0 minutes ago|1 minute ago|{n} minutes ago",
"future_minute": "in 0 minutes|in 1 minute|in {n} minutes", "minute_future": "in 0 minutes|in 1 minute|in {n} minutes",
"past_hour": "0 hours ago|1 hour ago|{n} hours ago", "hour_past": "0 hours ago|1 hour ago|{n} hours ago",
"future_hour": "in 0 hours|in 1 hour|in {n} hours", "hour_future": "in 0 hours|in 1 hour|in {n} hours",
"past_day": "0 days ago|yesterday|{n} days ago", "day_past": "0 days ago|yesterday|{n} days ago",
"future_day": "in 0 days|tomorrow|in {n} days", "day_future": "in 0 days|tomorrow|in {n} days",
"past_week": "0 weeks ago|last week|{n} weeks ago", "week_past": "0 weeks ago|last week|{n} weeks ago",
"future_week": "in 0 weeks|next week|in {n} weeks", "week_future": "in 0 weeks|next week|in {n} weeks",
"past_month": "0 months ago|last month|{n} months ago", "month_past": "0 months ago|last month|{n} months ago",
"future_month": "in 0 months|next month|in {n} months", "month_future": "in 0 months|next month|in {n} months",
"past_year": "0 years ago|last year|{n} years ago", "year_past": "0 years ago|last year|{n} years ago",
"future_year": "in 0 years|next year|in {n} years" "year_future": "in 0 years|next year|in {n} years",
"short_second_past": "{n}s",
"short_second_future": "in {n}s",
"short_minute_past": "{n}min",
"short_minute_future": "in {n}min",
"short_hour_past": "{n}h",
"short_hour_future": "in {n}h",
"short_day_past": "{n}d",
"short_day_future": "in {n}d",
"short_week_past": "{n}w",
"short_week_future": "in {n}w",
"short_month_past": "{n}mo",
"short_month_future": "in {n}mo",
"short_year_past": "{n}y",
"short_year_future": "in {n}y"
}, },
"timeline": { "timeline": {
"show_new_items": "Show {n} new items" "show_new_items": "Show {n} new items"

View File

@ -126,20 +126,20 @@
}, },
"time_ago_options": { "time_ago_options": {
"just_now": "ahora mismo", "just_now": "ahora mismo",
"past_second": "hace 0 segundos|hace {n} segundo|hace {n} segundos", "second_past": "hace 0 segundos|hace {n} segundo|hace {n} segundos",
"future_second": "dentro de 0 segundos|dentro de {n} segundo|dentro de {n} segundos", "second_future": "dentro de 0 segundos|dentro de {n} segundo|dentro de {n} segundos",
"past_minute": "hace 0 minutos|hace 1 minuto|hace {n} minutos", "minute_past": "hace 0 minutos|hace 1 minuto|hace {n} minutos",
"future_minute": "dentro de 0 minutos|dentro de 1 minuto|dentro de {n} minutos", "minute_future": "dentro de 0 minutos|dentro de 1 minuto|dentro de {n} minutos",
"past_hour": "hace 0 horas|hace 1 hora|hace {n} horas", "hour_past": "hace 0 horas|hace 1 hora|hace {n} horas",
"future_hour": "dentro de 0 horas|dentro de 1 hora|dentro {n} horas", "hour_future": "dentro de 0 horas|dentro de 1 hora|dentro {n} horas",
"past_day": "hace 0 días|ayer|hace {n} días", "day_past": "hace 0 días|ayer|hace {n} días",
"future_day": "dentro de 0 días|mañana|dentro de {n} días", "day_future": "dentro de 0 días|mañana|dentro de {n} días",
"past_week": "hace 0 semanas|la semana pasada|hace {n} semanas", "week_past": "hace 0 semanas|la semana pasada|hace {n} semanas",
"future_week": "dentro de 0 semanas|la próxima semana|dentro de {n} semanas", "week_future": "dentro de 0 semanas|la próxima semana|dentro de {n} semanas",
"past_month": "hace 0 meses|el mes pasado|hace {n} meses", "month_past": "hace 0 meses|el mes pasado|hace {n} meses",
"future_month": "dentro de 0 meses|el próximo mes|dentro de {n} meses", "month_future": "dentro de 0 meses|el próximo mes|dentro de {n} meses",
"past_year": "hace 0 años|el año pasado|hace {n} años", "year_past": "hace 0 años|el año pasado|hace {n} años",
"future_year": "dentro de 0 años|el próximo año|dentro de {n} años", "year_future": "dentro de 0 años|el próximo año|dentro de {n} años",
"tomorrow": "mañana", "tomorrow": "mañana",
"yesterday": "ayer" "yesterday": "ayer"
}, },