|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: syuilo and misskey-project |
| 3 | +SPDX-License-Identifier: AGPL-3.0-only |
| 4 | +--> |
| 5 | + |
| 6 | +<template> |
| 7 | +<time :title="absolute"> |
| 8 | + <template v-if="invalid">{{ $t('_mk._ago.invalid') }}</template> |
| 9 | + <template v-else-if="mode === 'relative'">{{ relative }}</template> |
| 10 | + <template v-else-if="mode === 'absolute'">{{ absolute }}</template> |
| 11 | + <template v-else-if="mode === 'detail'">{{ absolute }} ({{ relative }})</template> |
| 12 | +</time> |
| 13 | +</template> |
| 14 | + |
| 15 | +<script lang="ts" setup> |
| 16 | +import { computed } from 'vue'; |
| 17 | +const { locale } = useI18n(); |
| 18 | +
|
| 19 | +const props = withDefaults(defineProps<{ |
| 20 | + time: Date | string | number | null; |
| 21 | + mode?: 'relative' | 'absolute' | 'detail'; |
| 22 | +}>(), { |
| 23 | + mode: 'relative', |
| 24 | +}); |
| 25 | +
|
| 26 | +function getDateSafe(n: Date | string | number) { |
| 27 | + try { |
| 28 | + if (n instanceof Date) { |
| 29 | + return n; |
| 30 | + } |
| 31 | + return new Date(n); |
| 32 | + } catch (err) { |
| 33 | + return { |
| 34 | + getTime: () => NaN, |
| 35 | + }; |
| 36 | + } |
| 37 | +} |
| 38 | +
|
| 39 | +// eslint-disable-next-line vue/no-setup-props-reactivity-loss |
| 40 | +const _time = props.time == null ? NaN : getDateSafe(props.time).getTime(); |
| 41 | +const invalid = Number.isNaN(_time); |
| 42 | +const dateTimeFormat = new Intl.DateTimeFormat(locale.value, { |
| 43 | + year: 'numeric', |
| 44 | + month: 'numeric', |
| 45 | + day: 'numeric', |
| 46 | + hour: 'numeric', |
| 47 | + minute: 'numeric', |
| 48 | + second: 'numeric', |
| 49 | +}); |
| 50 | +const absolute = !invalid ? dateTimeFormat.format(_time) : $t('_mk._ago.invalid'); |
| 51 | +const now = ref(Date.now()); |
| 52 | +
|
| 53 | +// eslint-disable-next-line vue/no-setup-props-reactivity-loss |
| 54 | +const ago = computed(() => (now.value - _time) / 1000/*ms*/); |
| 55 | +
|
| 56 | +const relative = computed<string>(() => { |
| 57 | + if (props.mode === 'absolute') return ''; // absoluteではrelativeを使わないので計算しない |
| 58 | + if (invalid) return $t('_mk._ago.invalid'); |
| 59 | +
|
| 60 | + return ( |
| 61 | + ago.value >= 31536000 ? $t('_mk._ago.yearsAgo', { n: Math.round(ago.value / 31536000).toString() }) : |
| 62 | + ago.value >= 2592000 ? $t('_mk._ago.monthsAgo', { n: Math.round(ago.value / 2592000).toString() }) : |
| 63 | + ago.value >= 604800 ? $t('_mk._ago.weeksAgo', { n: Math.round(ago.value / 604800).toString() }) : |
| 64 | + ago.value >= 86400 ? $t('_mk._ago.daysAgo', { n: Math.round(ago.value / 86400).toString() }) : |
| 65 | + ago.value >= 3600 ? $t('_mk._ago.hoursAgo', { n: Math.round(ago.value / 3600).toString() }) : |
| 66 | + ago.value >= 60 ? $t('_mk._ago.minutesAgo', { n: (~~(ago.value / 60)).toString() }) : |
| 67 | + ago.value >= 10 ? $t('_mk._ago.secondsAgo', { n: (~~(ago.value % 60)).toString() }) : |
| 68 | + ago.value >= -3 ? $t('_mk._ago.justNow') : |
| 69 | + ago.value < -31536000 ? $t('_mk._timeIn.years', { n: Math.round(-ago.value / 31536000).toString() }) : |
| 70 | + ago.value < -2592000 ? $t('_mk._timeIn.months', { n: Math.round(-ago.value / 2592000).toString() }) : |
| 71 | + ago.value < -604800 ? $t('_mk._timeIn.weeks', { n: Math.round(-ago.value / 604800).toString() }) : |
| 72 | + ago.value < -86400 ? $t('_mk._timeIn.days', { n: Math.round(-ago.value / 86400).toString() }) : |
| 73 | + ago.value < -3600 ? $t('_mk._timeIn.hours', { n: Math.round(-ago.value / 3600).toString() }) : |
| 74 | + ago.value < -60 ? $t('_mk._timeIn.minutes', { n: (~~(-ago.value / 60)).toString() }) : |
| 75 | + $t('_mk._timeIn.seconds', { n: (~~(-ago.value % 60)).toString() }) |
| 76 | + ); |
| 77 | +}); |
| 78 | +
|
| 79 | +let tickId: number; |
| 80 | +let currentInterval: number; |
| 81 | +
|
| 82 | +function tick() { |
| 83 | + now.value = Date.now(); |
| 84 | + const nextInterval = ago.value < 60 ? 10000 : ago.value < 3600 ? 60000 : 180000; |
| 85 | +
|
| 86 | + if (currentInterval !== nextInterval) { |
| 87 | + if (tickId) window.clearInterval(tickId); |
| 88 | + currentInterval = nextInterval; |
| 89 | + tickId = window.setInterval(tick, nextInterval); |
| 90 | + } |
| 91 | +} |
| 92 | +
|
| 93 | +if (!invalid && (props.mode === 'relative' || props.mode === 'detail')) { |
| 94 | + onMounted(() => { |
| 95 | + tick(); |
| 96 | + }); |
| 97 | + onUnmounted(() => { |
| 98 | + if (tickId) window.clearInterval(tickId); |
| 99 | + }); |
| 100 | +} |
| 101 | +</script> |
0 commit comments