From 2c5c654b666e4fdf921d5737d7bf0bb9d23949fb Mon Sep 17 00:00:00 2001 From: ChunkyProgrammer <78101139+ChunkyProgrammer@users.noreply.github.com> Date: Thu, 20 Oct 2022 02:03:31 -0400 Subject: [PATCH] move `toLocalePublicationString` to`utils/helper` (#2748) * move toLocalePublicationString to helper * remove unnecessary assignments Co-Authored-By: absidue <48293849+absidue@users.noreply.github.com> * Update src/renderer/components/ft-list-video/ft-list-video.js Co-authored-by: PikachuEXE * fix linter issue Co-authored-by: absidue <48293849+absidue@users.noreply.github.com> Co-authored-by: PikachuEXE --- .../components/ft-list-video/ft-list-video.js | 14 ++-- .../watch-video-comments.js | 15 ++-- src/renderer/helpers/utils.js | 49 +++++++++++++ src/renderer/store/modules/utils.js | 70 ------------------- 4 files changed, 60 insertions(+), 88 deletions(-) diff --git a/src/renderer/components/ft-list-video/ft-list-video.js b/src/renderer/components/ft-list-video/ft-list-video.js index c0d90bb9..dfe822f9 100644 --- a/src/renderer/components/ft-list-video/ft-list-video.js +++ b/src/renderer/components/ft-list-video/ft-list-video.js @@ -2,7 +2,12 @@ import Vue from 'vue' import FtIconButton from '../ft-icon-button/ft-icon-button.vue' import { mapActions } from 'vuex' import i18n from '../../i18n/index' -import { copyToClipboard, openExternalLink, showToast } from '../../helpers/utils' +import { + copyToClipboard, + openExternalLink, + showToast, + toLocalePublicationString +} from '../../helpers/utils' export default Vue.extend({ name: 'FtListVideo', @@ -392,15 +397,11 @@ export default Vue.extend({ if (typeof (this.data.publishedText) !== 'undefined' && this.data.publishedText !== null && !this.isLive) { // produces a string according to the template in the locales string - this.toLocalePublicationString({ + this.uploadedTime = toLocalePublicationString({ publishText: this.publishedText, isLive: this.isLive, isUpcoming: this.isUpcoming, isRSS: this.data.isRSS - }).then((data) => { - this.uploadedTime = data - }).catch((error) => { - console.error(error) }) } @@ -503,7 +504,6 @@ export default Vue.extend({ }, ...mapActions([ - 'toLocalePublicationString', 'openInExternalPlayer', 'updateHistory', 'removeFromHistory', diff --git a/src/renderer/components/watch-video-comments/watch-video-comments.js b/src/renderer/components/watch-video-comments/watch-video-comments.js index bab022e2..95a373c2 100644 --- a/src/renderer/components/watch-video-comments/watch-video-comments.js +++ b/src/renderer/components/watch-video-comments/watch-video-comments.js @@ -6,7 +6,7 @@ import FtSelect from '../../components/ft-select/ft-select.vue' import FtTimestampCatcher from '../../components/ft-timestamp-catcher/ft-timestamp-catcher.vue' import autolinker from 'autolinker' import ytcm from '@freetube/yt-comment-scraper' -import { copyToClipboard, showToast } from '../../helpers/utils' +import { copyToClipboard, showToast, toLocalePublicationString } from '../../helpers/utils' export default Vue.extend({ name: 'WatchVideoComments', @@ -204,16 +204,10 @@ export default Vue.extend({ comment.authorThumb = comment.authorThumb[0].url comment.replies = [] comment.dataType = 'local' - this.toLocalePublicationString({ - publishText: (comment.time + ' ago'), - isLive: false, - isUpcoming: false, - isRSS: false - }).then((data) => { - comment.time = data - }).catch((error) => { - console.error(error) + comment.time = toLocalePublicationString({ + publishText: (comment.time + ' ago') }) + if (this.hideCommentLikes) { comment.likes = null } @@ -352,7 +346,6 @@ export default Vue.extend({ }, ...mapActions([ - 'toLocalePublicationString', 'invidiousAPICall' ]) } diff --git a/src/renderer/helpers/utils.js b/src/renderer/helpers/utils.js index 6994ab54..7f4686c5 100644 --- a/src/renderer/helpers/utils.js +++ b/src/renderer/helpers/utils.js @@ -102,6 +102,55 @@ export function calculatePublishedDate(publishedText) { return date.getTime() - timeSpan } +export function toLocalePublicationString ({ publishText, isLive = false, isUpcoming = false, isRSS = false }) { + if (isLive) { + return '0' + i18n.t('Video.Watching') + } else if (isUpcoming || publishText === null) { + // the check for null is currently just an inferring of knowledge, because there is no other possibility left + return `${i18n.t('Video.Published.Upcoming')}: ${publishText}` + } else if (isRSS) { + return publishText + } + const strings = publishText.split(' ') + // filters out the streamed x hours ago and removes the streamed in order to keep the rest of the code working + if (strings[0].toLowerCase() === 'streamed') { + strings.shift() + } + const singular = (strings[0] === '1') + let translationKey = '' + switch (strings[1].substring(0, 2)) { + case 'se': + translationKey = 'Video.Published.Second' + break + case 'mi': + translationKey = 'Video.Published.Minute' + break + case 'ho': + translationKey = 'Video.Published.Hour' + break + case 'da': + translationKey = 'Video.Published.Day' + break + case 'we': + translationKey = 'Video.Published.Week' + break + case 'mo': + translationKey = 'Video.Published.Month' + break + case 'ye': + translationKey = 'Video.Published.Year' + break + default: + return publishText + } + if (!singular) { + translationKey += 's' + } + + const unit = i18n.t(translationKey) + return i18n.t('Video.Publicationtemplate', { number: strings[0], unit }) +} + export function buildVTTFileLocally(storyboard) { let vttString = 'WEBVTT\n\n' // how many images are in one image diff --git a/src/renderer/store/modules/utils.js b/src/renderer/store/modules/utils.js index db2fb953..61c62613 100644 --- a/src/renderer/store/modules/utils.js +++ b/src/renderer/store/modules/utils.js @@ -697,76 +697,6 @@ const actions = { } }, - toLocalePublicationString ({ dispatch }, payload) { - if (payload.isLive) { - return '0' + i18n.t('Video.Watching') - } else if (payload.isUpcoming || payload.publishText === null) { - // the check for null is currently just an inferring of knowledge, because there is no other possibility left - return `${i18n.t('Video.Published.Upcoming')}: ${payload.publishText}` - } else if (payload.isRSS) { - return payload.publishText - } - const strings = payload.publishText.split(' ') - // filters out the streamed x hours ago and removes the streamed in order to keep the rest of the code working - if (strings[0].toLowerCase() === 'streamed') { - strings.shift() - } - const singular = (strings[0] === '1') - let unit - switch (strings[1].substring(0, 2)) { - case 'se': - if (singular) { - unit = i18n.t('Video.Published.Second') - } else { - unit = i18n.t('Video.Published.Seconds') - } - break - case 'mi': - if (singular) { - unit = i18n.t('Video.Published.Minute') - } else { - unit = i18n.t('Video.Published.Minutes') - } - break - case 'ho': - if (singular) { - unit = i18n.t('Video.Published.Hour') - } else { - unit = i18n.t('Video.Published.Hours') - } - break - case 'da': - if (singular) { - unit = i18n.t('Video.Published.Day') - } else { - unit = i18n.t('Video.Published.Days') - } - break - case 'we': - if (singular) { - unit = i18n.t('Video.Published.Week') - } else { - unit = i18n.t('Video.Published.Weeks') - } - break - case 'mo': - if (singular) { - unit = i18n.t('Video.Published.Month') - } else { - unit = i18n.t('Video.Published.Months') - } - break - case 'ye': - if (singular) { - unit = i18n.t('Video.Published.Year') - } else { - unit = i18n.t('Video.Published.Years') - } - break - } - return i18n.t('Video.Publicationtemplate', { number: strings[0], unit }) - }, - clearSessionSearchHistory ({ commit }) { commit('setSessionSearchHistory', []) },