2018-02-05 14:25:19 +09:00
|
|
|
<template>
|
2020-10-27 18:11:41 +09:00
|
|
|
<time :title="absolute">
|
2023-02-24 16:21:49 +09:00
|
|
|
<template v-if="invalid">{{ i18n.ts._ago.invalid }}</template>
|
|
|
|
<template v-else-if="mode === 'relative'">{{ relative }}</template>
|
2022-01-16 07:47:28 +09:00
|
|
|
<template v-else-if="mode === 'absolute'">{{ absolute }}</template>
|
|
|
|
<template v-else-if="mode === 'detail'">{{ absolute }} ({{ relative }})</template>
|
2018-02-13 13:49:48 +09:00
|
|
|
</time>
|
2018-02-05 14:25:19 +09:00
|
|
|
</template>
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-01-16 07:47:28 +09:00
|
|
|
<script lang="ts" setup>
|
2023-04-13 12:20:39 +09:00
|
|
|
import isChromatic from 'chromatic/isChromatic';
|
2022-01-16 07:47:28 +09:00
|
|
|
import { onUnmounted } from 'vue';
|
|
|
|
import { i18n } from '@/i18n';
|
2023-01-01 17:11:33 +09:00
|
|
|
import { dateTimeFormat } from '@/scripts/intl-const';
|
2018-02-09 18:28:06 +09:00
|
|
|
|
2022-01-16 07:47:28 +09:00
|
|
|
const props = withDefaults(defineProps<{
|
2023-02-24 16:21:49 +09:00
|
|
|
time: Date | string | number | null;
|
2023-04-04 09:38:34 +09:00
|
|
|
origin?: Date | null;
|
2022-01-16 07:47:28 +09:00
|
|
|
mode?: 'relative' | 'absolute' | 'detail';
|
|
|
|
}>(), {
|
2023-04-13 12:20:39 +09:00
|
|
|
origin: isChromatic() ? new Date('2023-04-01T00:00:00Z') : null,
|
2022-01-16 07:47:28 +09:00
|
|
|
mode: 'relative',
|
|
|
|
});
|
|
|
|
|
2023-02-24 16:21:49 +09:00
|
|
|
const _time = props.time == null ? NaN :
|
|
|
|
typeof props.time === 'number' ? props.time :
|
|
|
|
(props.time instanceof Date ? props.time : new Date(props.time)).getTime();
|
|
|
|
const invalid = Number.isNaN(_time);
|
|
|
|
const absolute = !invalid ? dateTimeFormat.format(_time) : i18n.ts._ago.invalid;
|
2018-06-10 08:03:02 +09:00
|
|
|
|
2023-04-04 09:38:34 +09:00
|
|
|
let now = $ref((props.origin ?? new Date()).getTime());
|
2023-02-24 16:21:49 +09:00
|
|
|
const relative = $computed<string>(() => {
|
|
|
|
if (props.mode === 'absolute') return ''; // absoluteではrelativeを使わないので計算しない
|
|
|
|
if (invalid) return i18n.ts._ago.invalid;
|
|
|
|
|
|
|
|
const ago = (now - _time) / 1000/*ms*/;
|
2022-01-16 07:47:28 +09:00
|
|
|
return (
|
2022-06-26 03:12:58 +09:00
|
|
|
ago >= 31536000 ? i18n.t('_ago.yearsAgo', { n: Math.round(ago / 31536000).toString() }) :
|
|
|
|
ago >= 2592000 ? i18n.t('_ago.monthsAgo', { n: Math.round(ago / 2592000).toString() }) :
|
|
|
|
ago >= 604800 ? i18n.t('_ago.weeksAgo', { n: Math.round(ago / 604800).toString() }) :
|
|
|
|
ago >= 86400 ? i18n.t('_ago.daysAgo', { n: Math.round(ago / 86400).toString() }) :
|
|
|
|
ago >= 3600 ? i18n.t('_ago.hoursAgo', { n: Math.round(ago / 3600).toString() }) :
|
|
|
|
ago >= 60 ? i18n.t('_ago.minutesAgo', { n: (~~(ago / 60)).toString() }) :
|
|
|
|
ago >= 10 ? i18n.t('_ago.secondsAgo', { n: (~~(ago % 60)).toString() }) :
|
|
|
|
ago >= -1 ? i18n.ts._ago.justNow :
|
2022-05-29 15:15:52 +09:00
|
|
|
i18n.ts._ago.future);
|
2018-02-13 13:49:48 +09:00
|
|
|
});
|
2022-01-16 07:47:28 +09:00
|
|
|
|
2023-01-06 16:58:43 +09:00
|
|
|
let tickId: number;
|
|
|
|
|
2022-01-16 07:47:28 +09:00
|
|
|
function tick() {
|
2023-04-04 09:38:34 +09:00
|
|
|
now = props.origin ?? (new Date()).getTime();
|
2023-02-24 16:21:49 +09:00
|
|
|
const ago = (now - _time) / 1000/*ms*/;
|
2023-01-04 08:54:54 +09:00
|
|
|
const next = ago < 60 ? 10000 : ago < 3600 ? 60000 : 180000;
|
2022-01-16 07:47:28 +09:00
|
|
|
|
2023-01-06 16:58:43 +09:00
|
|
|
tickId = window.setTimeout(tick, next);
|
2022-01-16 07:47:28 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (props.mode === 'relative' || props.mode === 'detail') {
|
2023-01-06 16:58:43 +09:00
|
|
|
tick();
|
2022-01-16 07:47:28 +09:00
|
|
|
|
|
|
|
onUnmounted(() => {
|
2023-01-06 16:58:43 +09:00
|
|
|
window.clearTimeout(tickId);
|
2022-01-16 07:47:28 +09:00
|
|
|
});
|
|
|
|
}
|
2018-02-05 14:25:19 +09:00
|
|
|
</script>
|