From 30d2b034b32f98a35790d852a200d50b11b9872f Mon Sep 17 00:00:00 2001 From: Jesse Chan Date: Thu, 17 Sep 2020 23:29:35 +0800 Subject: [PATCH] dependency: migrate from moment to dayjs --- .../modals/feeds-modal/FeedsTab.tsx | 4 +- .../sidebar/TransferRateDetails.tsx | 9 ++- client/src/javascript/i18n/languages.tsx | 39 ++++++++++ client/src/javascript/util/detectLocale.tsx | 2 +- package-lock.json | 10 +-- package.json | 2 +- shared/util/formatUtil.js | 71 ------------------- shared/util/formatUtil.ts | 29 ++++++++ 8 files changed, 83 insertions(+), 83 deletions(-) delete mode 100644 shared/util/formatUtil.js create mode 100644 shared/util/formatUtil.ts diff --git a/client/src/javascript/components/modals/feeds-modal/FeedsTab.tsx b/client/src/javascript/components/modals/feeds-modal/FeedsTab.tsx index d65e5f53..7ea39cf4 100644 --- a/client/src/javascript/components/modals/feeds-modal/FeedsTab.tsx +++ b/client/src/javascript/components/modals/feeds-modal/FeedsTab.tsx @@ -1,5 +1,4 @@ import {defineMessages, FormattedMessage, injectIntl, WrappedComponentProps} from 'react-intl'; -import formatUtil from '@shared/util/formatUtil'; import React from 'react'; import throttle from 'lodash/throttle'; @@ -18,6 +17,7 @@ import { import Edit from '../../icons/Edit'; import Close from '../../icons/Close'; import FeedsStore, {FeedsStoreClass} from '../../../stores/FeedsStore'; +import {minToHumanReadable} from '../../../i18n/languages'; import ModalFormSectionHeader from '../ModalFormSectionHeader'; import * as validators from '../../../util/validators'; import UIActions from '../../../actions/UIActions'; @@ -284,7 +284,7 @@ class FeedsTab extends React.Component {
  • - {formatUtil.minToHumanReadable(feed.interval)} + {minToHumanReadable(feed.interval)}
  • { - const years = Math.floor(cumSeconds / 31536000); - const weeks = Math.floor((cumSeconds % 31536000) / 604800); - const days = Math.floor(((cumSeconds % 31536000) % 604800) / 86400); - const hours = Math.floor((((cumSeconds % 31536000) % 604800) % 86400) / 3600); - const minutes = Math.floor(((((cumSeconds % 31536000) % 604800) % 86400) % 3600) / 60); - const seconds = Math.floor(cumSeconds - minutes * 60); - let timeRemaining = null; - - if (years > 0) { - timeRemaining = {years, weeks, cumSeconds}; - } else if (weeks > 0) { - timeRemaining = {weeks, days, cumSeconds}; - } else if (days > 0) { - timeRemaining = {days, hours, cumSeconds}; - } else if (hours > 0) { - timeRemaining = {hours, minutes, cumSeconds}; - } else if (minutes > 0) { - timeRemaining = {minutes, seconds, cumSeconds}; - } else { - timeRemaining = {seconds, cumSeconds}; - } - - return timeRemaining; - }, - - minToHumanReadable: (min) => moment.duration(min * 60 * 1000).humanize(), - - parsePeers: (string) => { - // This lovely delimiter is defined in clientResponseUtil. - const markerPosition = string.indexOf('@!@'); - return string.substr(0, markerPosition); - }, - - status: (isHashing, isComplete, isOpen, uploadRate, downloadRate, state, message) => { - const torrentStatus = []; - - if (isHashing !== '0') { - torrentStatus.push('ch'); // checking - } else if (isComplete === '1' && isOpen === '1' && state === '1') { - torrentStatus.push('sd'); // seeding - } else if (isComplete === '1' && isOpen === '1' && state === '0') { - torrentStatus.push('p'); // paused - } else if (isComplete === '1' && isOpen === '0') { - torrentStatus.push('c'); // complete - } else if (isComplete === '0' && isOpen === '1' && state === '1') { - torrentStatus.push('d'); // downloading - } else if (isComplete === '0' && isOpen === '1' && state === '0') { - torrentStatus.push('p'); // paused - } else if (isComplete === '0' && isOpen === '0') { - torrentStatus.push('s'); // stopped - } - - if (message.length) { - torrentStatus.push('e'); // error - } - - if (uploadRate === '0' && downloadRate === '0') { - torrentStatus.push('i'); - } else { - torrentStatus.push('a'); - } - - return torrentStatus; - }, -}; - -module.exports = formatUtil; diff --git a/shared/util/formatUtil.ts b/shared/util/formatUtil.ts new file mode 100644 index 00000000..4d86c6a8 --- /dev/null +++ b/shared/util/formatUtil.ts @@ -0,0 +1,29 @@ +const formatUtil = { + secondsToDuration: (cumSeconds: number) => { + const years = Math.floor(cumSeconds / 31536000); + const weeks = Math.floor((cumSeconds % 31536000) / 604800); + const days = Math.floor(((cumSeconds % 31536000) % 604800) / 86400); + const hours = Math.floor((((cumSeconds % 31536000) % 604800) % 86400) / 3600); + const minutes = Math.floor(((((cumSeconds % 31536000) % 604800) % 86400) % 3600) / 60); + const seconds = Math.floor(cumSeconds - minutes * 60); + let timeRemaining = null; + + if (years > 0) { + timeRemaining = {years, weeks, cumSeconds}; + } else if (weeks > 0) { + timeRemaining = {weeks, days, cumSeconds}; + } else if (days > 0) { + timeRemaining = {days, hours, cumSeconds}; + } else if (hours > 0) { + timeRemaining = {hours, minutes, cumSeconds}; + } else if (minutes > 0) { + timeRemaining = {minutes, seconds, cumSeconds}; + } else { + timeRemaining = {seconds, cumSeconds}; + } + + return timeRemaining; + }, +}; + +export default formatUtil;