From 7ecc646408556be9983d90579677b33b14c47121 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Mon, 17 Oct 2022 20:06:01 -0700 Subject: [PATCH] refactor: move moment stuff to utils --- src/components/Timeline.vue | 5 +---- src/services/Utils.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/components/Timeline.vue b/src/components/Timeline.vue index 435d7d6a..e93ae1bc 100644 --- a/src/components/Timeline.vue +++ b/src/components/Timeline.vue @@ -100,7 +100,6 @@ import { showError } from '@nextcloud/dialogs' import { getCanonicalLocale } from '@nextcloud/l10n'; import { NcEmptyContent } from '@nextcloud/vue'; import GlobalMixin from '../mixins/GlobalMixin'; -import moment from 'moment'; import { ViewerManager } from "../services/Viewer"; import { getLayout } from "../services/Layout"; @@ -584,9 +583,7 @@ export default class Timeline extends Mixins(GlobalMixin, UserConfig) { } else if (this.$route.name === 'thisday' && (!prevDay || Math.abs(prevDay.dayid - day.dayid) > 30)) { // thisday view with new year title head.size = 67; - const dateTaken = moment(utils.dayIdToDate(day.dayid)); - const text = dateTaken.locale(getCanonicalLocale()).fromNow(); - head.super = text.charAt(0).toUpperCase() + text.slice(1); + head.super = utils.getFromNowStr(utils.dayIdToDate(day.dayid)); } // Add header to list diff --git a/src/services/Utils.ts b/src/services/Utils.ts index 80f9bfc2..c8335b13 100644 --- a/src/services/Utils.ts +++ b/src/services/Utils.ts @@ -2,6 +2,7 @@ import { getCanonicalLocale } from "@nextcloud/l10n"; import { getCurrentUser } from '@nextcloud/auth' import { loadState } from '@nextcloud/initial-state' import { IPhoto } from "../types"; +import moment from 'moment'; // Memoize the result of short date conversions // These operations are surprisingly expensive @@ -45,6 +46,15 @@ export function getLongDateStr(date: Date, skipYear=false, time=false) { }); } +/** Get text like "5 years ago" from a date */ +export function getFromNowStr(date: Date) { + // Get fromNow in correct locale + const text = moment(date).locale(getCanonicalLocale()).fromNow(); + + // Title case + return text.charAt(0).toUpperCase() + text.slice(1); +} + /** * Returns a hash code from a string * @param {String} str The string to hash.