2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
2024-02-13 16:59:27 +01:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2018-02-05 06:25:19 +01:00
|
|
|
<template>
|
2023-10-20 04:51:01 +02:00
|
|
|
<time :title="absolute" :class="{ [$style.old1]: colored && (ago > 60 * 60 * 24 * 90), [$style.old2]: colored && (ago > 60 * 60 * 24 * 180) }">
|
2023-02-24 08:21:49 +01:00
|
|
|
<template v-if="invalid">{{ i18n.ts._ago.invalid }}</template>
|
|
|
|
<template v-else-if="mode === 'relative'">{{ relative }}</template>
|
2022-01-15 23:47:28 +01:00
|
|
|
<template v-else-if="mode === 'absolute'">{{ absolute }}</template>
|
|
|
|
<template v-else-if="mode === 'detail'">{{ absolute }} ({{ relative }})</template>
|
2018-02-13 05:49:48 +01:00
|
|
|
</time>
|
2018-02-05 06:25:19 +01:00
|
|
|
</template>
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-01-15 23:47:28 +01:00
|
|
|
<script lang="ts" setup>
|
2023-04-13 16:23:11 +02:00
|
|
|
import isChromatic from 'chromatic/isChromatic';
|
2023-12-07 06:42:09 +01:00
|
|
|
import { onMounted, onUnmounted, ref, computed } from 'vue';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { i18n } from '@/i18n.js';
|
|
|
|
import { dateTimeFormat } from '@/scripts/intl-const.js';
|
2018-02-09 10:28:06 +01:00
|
|
|
|
2022-01-15 23:47:28 +01:00
|
|
|
const props = withDefaults(defineProps<{
|
2023-02-24 08:21:49 +01:00
|
|
|
time: Date | string | number | null;
|
2023-04-04 02:38:34 +02:00
|
|
|
origin?: Date | null;
|
2022-01-15 23:47:28 +01:00
|
|
|
mode?: 'relative' | 'absolute' | 'detail';
|
2023-10-20 04:51:01 +02:00
|
|
|
colored?: boolean;
|
2022-01-15 23:47:28 +01:00
|
|
|
}>(), {
|
2024-01-30 11:53:53 +01:00
|
|
|
origin: isChromatic() ? () => new Date('2023-04-01T00:00:00Z') : null,
|
2022-01-15 23:47:28 +01:00
|
|
|
mode: 'relative',
|
|
|
|
});
|
|
|
|
|
2023-11-23 11:49:45 +01:00
|
|
|
function getDateSafe(n: Date | string | number) {
|
|
|
|
try {
|
|
|
|
if (n instanceof Date) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
return new Date(n);
|
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
getTime: () => NaN,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line vue/no-setup-props-destructure
|
|
|
|
const _time = props.time == null ? NaN : getDateSafe(props.time).getTime();
|
2023-02-24 08:21:49 +01:00
|
|
|
const invalid = Number.isNaN(_time);
|
|
|
|
const absolute = !invalid ? dateTimeFormat.format(_time) : i18n.ts._ago.invalid;
|
2018-06-10 01:03:02 +02:00
|
|
|
|
2023-11-23 11:49:45 +01:00
|
|
|
// eslint-disable-next-line vue/no-setup-props-destructure
|
2023-12-07 06:42:09 +01:00
|
|
|
const now = ref((props.origin ?? new Date()).getTime());
|
|
|
|
const ago = computed(() => (now.value - _time) / 1000/*ms*/);
|
2023-07-04 15:48:39 +02:00
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
const relative = computed<string>(() => {
|
2023-02-24 08:21:49 +01:00
|
|
|
if (props.mode === 'absolute') return ''; // absoluteではrelativeを使わないので計算しない
|
|
|
|
if (invalid) return i18n.ts._ago.invalid;
|
|
|
|
|
2022-01-15 23:47:28 +01:00
|
|
|
return (
|
2024-01-20 00:11:59 +01:00
|
|
|
ago.value >= 31536000 ? i18n.tsx._ago.yearsAgo({ n: Math.round(ago.value / 31536000).toString() }) :
|
|
|
|
ago.value >= 2592000 ? i18n.tsx._ago.monthsAgo({ n: Math.round(ago.value / 2592000).toString() }) :
|
|
|
|
ago.value >= 604800 ? i18n.tsx._ago.weeksAgo({ n: Math.round(ago.value / 604800).toString() }) :
|
|
|
|
ago.value >= 86400 ? i18n.tsx._ago.daysAgo({ n: Math.round(ago.value / 86400).toString() }) :
|
|
|
|
ago.value >= 3600 ? i18n.tsx._ago.hoursAgo({ n: Math.round(ago.value / 3600).toString() }) :
|
|
|
|
ago.value >= 60 ? i18n.tsx._ago.minutesAgo({ n: (~~(ago.value / 60)).toString() }) :
|
|
|
|
ago.value >= 10 ? i18n.tsx._ago.secondsAgo({ n: (~~(ago.value % 60)).toString() }) :
|
2023-12-07 06:42:09 +01:00
|
|
|
ago.value >= -3 ? i18n.ts._ago.justNow :
|
2024-01-20 00:11:59 +01:00
|
|
|
ago.value < -31536000 ? i18n.tsx._timeIn.years({ n: Math.round(-ago.value / 31536000).toString() }) :
|
|
|
|
ago.value < -2592000 ? i18n.tsx._timeIn.months({ n: Math.round(-ago.value / 2592000).toString() }) :
|
|
|
|
ago.value < -604800 ? i18n.tsx._timeIn.weeks({ n: Math.round(-ago.value / 604800).toString() }) :
|
|
|
|
ago.value < -86400 ? i18n.tsx._timeIn.days({ n: Math.round(-ago.value / 86400).toString() }) :
|
|
|
|
ago.value < -3600 ? i18n.tsx._timeIn.hours({ n: Math.round(-ago.value / 3600).toString() }) :
|
|
|
|
ago.value < -60 ? i18n.tsx._timeIn.minutes({ n: (~~(-ago.value / 60)).toString() }) :
|
|
|
|
i18n.tsx._timeIn.seconds({ n: (~~(-ago.value % 60)).toString() })
|
2023-11-17 07:05:37 +01:00
|
|
|
);
|
2018-02-13 05:49:48 +01:00
|
|
|
});
|
2022-01-15 23:47:28 +01:00
|
|
|
|
2023-01-06 08:58:43 +01:00
|
|
|
let tickId: number;
|
2023-07-04 15:48:39 +02:00
|
|
|
let currentInterval: number;
|
2023-01-06 08:58:43 +01:00
|
|
|
|
2022-01-15 23:47:28 +01:00
|
|
|
function tick() {
|
2023-12-07 06:42:09 +01:00
|
|
|
now.value = (new Date()).getTime();
|
|
|
|
const nextInterval = ago.value < 60 ? 10000 : ago.value < 3600 ? 60000 : 180000;
|
2022-01-15 23:47:28 +01:00
|
|
|
|
2023-07-04 15:48:39 +02:00
|
|
|
if (currentInterval !== nextInterval) {
|
|
|
|
if (tickId) window.clearInterval(tickId);
|
|
|
|
currentInterval = nextInterval;
|
|
|
|
tickId = window.setInterval(tick, nextInterval);
|
|
|
|
}
|
2022-01-15 23:47:28 +01:00
|
|
|
}
|
|
|
|
|
2023-07-04 15:48:39 +02:00
|
|
|
if (!invalid && props.origin === null && (props.mode === 'relative' || props.mode === 'detail')) {
|
|
|
|
onMounted(() => {
|
|
|
|
tick();
|
|
|
|
});
|
2022-01-15 23:47:28 +01:00
|
|
|
onUnmounted(() => {
|
2023-07-04 15:48:39 +02:00
|
|
|
if (tickId) window.clearInterval(tickId);
|
2022-01-15 23:47:28 +01:00
|
|
|
});
|
|
|
|
}
|
2018-02-05 06:25:19 +01:00
|
|
|
</script>
|
2023-10-20 04:51:01 +02:00
|
|
|
|
|
|
|
<style lang="scss" module>
|
|
|
|
.old1 {
|
|
|
|
color: var(--warn);
|
|
|
|
}
|
|
|
|
|
|
|
|
.old1.old2 {
|
|
|
|
color: var(--error);
|
|
|
|
}
|
|
|
|
</style>
|