From 12e7eb63575e5faa419415c075ab27ac85c9f346 Mon Sep 17 00:00:00 2001 From: Jannes Vandepitte Date: Fri, 26 Aug 2022 21:38:28 +0200 Subject: [PATCH] Address PR comments --- public/locales/en/common.json | 9 ++++++-- public/locales/en/modules/usenet.json | 10 +++++++-- src/modules/usenet/UsenetHistoryList.tsx | 27 +++++++++++++----------- src/modules/usenet/UsenetQueueList.tsx | 10 +++++++-- src/tools/parseDuration.ts | 20 ++++++++++++++++++ 5 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 src/tools/parseDuration.ts diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 9a0b9a9d9..64232fcf5 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -2,5 +2,10 @@ "actions": { "save": "Save" }, - "tip": "Tip: " -} \ No newline at end of file + "tip": "Tip: ", + "time": { + "seconds": "seconds", + "minutes": "minutes", + "hours": "hours" + } +} diff --git a/public/locales/en/modules/usenet.json b/public/locales/en/modules/usenet.json index 30231fb80..efdd2adfb 100644 --- a/public/locales/en/modules/usenet.json +++ b/public/locales/en/modules/usenet.json @@ -27,7 +27,10 @@ "progress": "Progress" }, "empty": "Queue is empty.", - "error": "Some error has occured while fetching data:", + "error": { + "title": "Error!", + "message": "Some error has occured while fetching data:" + }, "paused": "Paused" }, "history": { @@ -37,7 +40,10 @@ "duration": "Download Duration" }, "empty": "Queue is empty.", - "error": "Some error has occured while fetching data:", + "error": { + "title": "Error!", + "message": "Some error has occured while fetching data:" + }, "paused": "Paused" } } diff --git a/src/modules/usenet/UsenetHistoryList.tsx b/src/modules/usenet/UsenetHistoryList.tsx index ba5a29601..f886e5ad3 100644 --- a/src/modules/usenet/UsenetHistoryList.tsx +++ b/src/modules/usenet/UsenetHistoryList.tsx @@ -18,6 +18,7 @@ import { useTranslation } from 'next-i18next'; import { FunctionComponent, useState } from 'react'; import { useGetUsenetHistory } from '../../tools/hooks/api'; import { humanFileSize } from '../../tools/humanFileSize'; +import { parseDuration } from '../../tools/parseDuration'; dayjs.extend(duration); @@ -29,7 +30,7 @@ const PAGE_SIZE = 10; export const UsenetHistoryList: FunctionComponent = ({ serviceId }) => { const [page, setPage] = useState(1); - const { t } = useTranslation('modules/usenet'); + const { t } = useTranslation(['modules/usenet', 'common']); const { data, isLoading, isError, error } = useGetUsenetHistory({ limit: PAGE_SIZE, @@ -51,8 +52,14 @@ export const UsenetHistoryList: FunctionComponent = ({ s if (isError) { return ( - } my="lg" title="Error!" color="red" radius="md"> - {t('history.error')} + } + my="lg" + title={t('modules/usenet:history.error.title')} + color="red" + radius="md" + > + {t('modules/usenet:history.error.message')} {(error as AxiosError)?.response?.data as string} @@ -64,7 +71,7 @@ export const UsenetHistoryList: FunctionComponent = ({ s if (!data || data.items.length <= 0) { return (
- {t('history.empty')} + {t('modules/usenet:history.empty')}
); } @@ -79,9 +86,9 @@ export const UsenetHistoryList: FunctionComponent = ({ s - {t('history.header.name')} - {t('history.header.size')} - {t('history.header.duration')} + {t('modules/usenet:history.header.name')} + {t('modules/usenet:history.header.size')} + {t('modules/usenet:history.header.duration')} @@ -105,11 +112,7 @@ export const UsenetHistoryList: FunctionComponent = ({ s {humanFileSize(history.size)} - - {dayjs - .duration(history.time, 's') - .format(history.time < 60 ? 's [seconds]' : 'm [minutes] s [seconds] ')} - + {parseDuration(history.time, t)} ))} diff --git a/src/modules/usenet/UsenetQueueList.tsx b/src/modules/usenet/UsenetQueueList.tsx index bc6d5ea0c..e1e4d59f2 100644 --- a/src/modules/usenet/UsenetQueueList.tsx +++ b/src/modules/usenet/UsenetQueueList.tsx @@ -55,8 +55,14 @@ export const UsenetQueueList: FunctionComponent = ({ servi if (isError) { return ( - } my="lg" title="Error!" color="red" radius="md"> - {t('queue.error')} + } + my="lg" + title={t('queue.error.title')} + color="red" + radius="md" + > + {t('queue.error.message')} {(error as AxiosError)?.response?.data as string} diff --git a/src/tools/parseDuration.ts b/src/tools/parseDuration.ts new file mode 100644 index 000000000..d59de519a --- /dev/null +++ b/src/tools/parseDuration.ts @@ -0,0 +1,20 @@ +import dayjs from 'dayjs'; +import duration from 'dayjs/plugin/duration'; +import { TFunction } from 'next-i18next'; + +dayjs.extend(duration); + +export const parseDuration = (time: number, t: TFunction): string => { + const etaDuration = dayjs.duration(time, 's'); + + let eta = etaDuration.format(`s [${t('common:time.seconds')}]`); + + if (etaDuration.asMinutes() > 1) { + eta = etaDuration.format(`m [${t('common:time.minutes')}] `) + eta; + } + if (etaDuration.asHours() > 1) { + eta = etaDuration.format(`H [${t('common:time.hours')}] `) + eta; + } + + return eta; +};