Compare commits

...

4 Commits

Author SHA1 Message Date
naskya b3d1be457b Merge branch 'fix/MkTime' into 'develop'
refactor MkTime: replace the awful ?: chain with if-else; fix: force update ticker when props.time changed

Co-authored-by: Lhcfl <Lhcfl@outlook.com>

See merge request firefish/firefish!10797
2024-05-08 12:11:31 +00:00
Hosted Weblate 347851d6bb
Merge branch 'origin/develop' into Weblate 2024-05-08 10:21:46 +02:00
jolupa abec71074b
locale: update translations (Catalan)
Currently translated at 100.0% (1930 of 1930 strings)

Translation: Firefish/locales
Translate-URL: https://hosted.weblate.org/projects/firefish/locales/ca/
2024-05-08 10:21:45 +02:00
Lhcfl 272e30be0c refactor: replace the awful ?: chain with if-else; fix: force update ticker when props.time changed
related: ed6f866a4f

Co-authored-by: kakkokari-gtyih <kakkokari-gtyih@users.noreply.github.com>
2024-05-08 10:52:32 +08:00
2 changed files with 64 additions and 40 deletions

View File

@ -2301,3 +2301,6 @@ getQrCode: Mostrar el codi QR
copyRemoteFollowUrl: Còpia la adreça URL del seguidor remot
foldNotification: Agrupar les notificacions similars
slashQuote: Cita encadenada
i18nServerInfo: Els nous clients els trobares en {language} per defecte.
i18nServerChange: Fes servir {language} en comptes.
i18nServerSet: Fes servir {language} per els nous clients.

View File

@ -25,15 +25,21 @@ const props = withDefaults(
},
);
function getDateSafe(n: Date | string | number) {
try {
if (n instanceof Date) {
return n;
}
return new Date(n);
} catch (err) {
return {
getTime: () => Number.NaN,
};
}
}
const _time = computed(() =>
props.time == null
? Number.NaN
: typeof props.time === "number"
? props.time
: (props.time instanceof Date
? props.time
: new Date(props.time)
).getTime(),
props.time == null ? Number.NaN : getDateSafe(props.time).getTime(),
);
const invalid = computed(() => Number.isNaN(_time.value));
const absolute = computed(() =>
@ -41,45 +47,57 @@ const absolute = computed(() =>
);
const now = ref(props.origin?.getTime() ?? Date.now());
const relative = computed<string>(() => {
if (props.mode === "absolute") return ""; // absoluterelative使
if (invalid.value) return i18n.ts._ago.invalid;
const ago = (now.value - _time.value) / 1000; /* ms */
return ago >= 31536000
? i18n.t("_ago.yearsAgo", { n: Math.floor(ago / 31536000).toString() })
: ago >= 2592000
? i18n.t("_ago.monthsAgo", {
n: Math.floor(ago / 2592000).toString(),
})
: ago >= 604800
? i18n.t("_ago.weeksAgo", {
n: Math.floor(ago / 604800).toString(),
})
: ago >= 86400
? i18n.t("_ago.daysAgo", {
n: Math.floor(ago / 86400).toString(),
})
: ago >= 3600
? i18n.t("_ago.hoursAgo", {
n: Math.floor(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
: i18n.ts._ago.future;
if (ago >= 31536000) {
return i18n.t("_ago.yearsAgo", {
n: Math.floor(ago / 31536000).toString(),
});
}
if (ago >= 2592000) {
return i18n.t("_ago.monthsAgo", {
n: Math.floor(ago / 2592000).toString(),
});
}
if (ago >= 604800) {
return i18n.t("_ago.weeksAgo", {
n: Math.floor(ago / 604800).toString(),
});
}
if (ago >= 86400) {
return i18n.t("_ago.daysAgo", {
n: Math.floor(ago / 86400).toString(),
});
}
if (ago >= 3600) {
return i18n.t("_ago.hoursAgo", {
n: Math.floor(ago / 3600).toString(),
});
}
if (ago >= 60) {
return i18n.t("_ago.minutesAgo", {
n: (~~(ago / 60)).toString(),
});
}
if (ago >= 10) {
return i18n.t("_ago.secondsAgo", {
n: (~~(ago % 60)).toString(),
});
}
if (ago >= -1) {
return i18n.ts._ago.justNow;
}
return i18n.ts._ago.future;
});
let tickId: number | undefined;
function tick() {
function tick(forceUpdateTicker = false) {
if (
invalid.value ||
props.origin ||
@ -101,13 +119,16 @@ function tick() {
if (!tickId) {
tickId = window.setInterval(tick, next);
} else if (prev < next) {
} else if (prev < next || forceUpdateTicker) {
window.clearInterval(tickId);
tickId = window.setInterval(tick, next);
}
}
watch(() => props.time, tick);
watch(
() => props.time,
() => tick(true),
);
onMounted(() => {
tick();