blob: 553b66cafb2c7cda8733e6549ba4f1a83f7be2a4 [file] [log] [blame]
idilloncab81d72022-11-08 12:20:00 -05001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import { filesize } from 'filesize';
19import { useMemo } from 'react';
20import { useTranslation } from 'react-i18next';
21
22// 'filesize.js' library requires us to define the symbols ourselves for localization:
23// https://github.com/avoidwork/filesize.js/issues/64
24// https://github.com/avoidwork/filesize.js/issues/96
25// Could not find a library doing it by itself
26const dataSizeSymbols: Record<string, Record<string, string>> = {
27 fr: { B: 'o', kB: 'ko', MB: 'Mo', GB: 'Go', TB: 'To', PB: 'Po', EB: 'Eo', ZB: 'Zo', YB: 'Yo' },
28 ru: { B: 'Б', kB: 'кБ', MB: 'МБ', GB: 'ГБ', TB: 'ТБ', PB: 'ПБ', EB: 'ЭБ', ZB: 'ЗБ', YB: 'ЙБ' },
29 default: { B: 'B', kB: 'kB', MB: 'MB', GB: 'GB', TB: 'TB', PB: 'PB', EB: 'EB', ZB: 'ZB', YB: 'YB' },
30};
31
32export const useDataSizeUnits = (nbBytes: number) => {
33 const { i18n } = useTranslation();
34 return useMemo(() => {
35 const options = {
36 symbols: dataSizeSymbols[i18n.language] || dataSizeSymbols['default'], // undefined is not supported
37 locale: i18n.language,
38 };
39 return filesize(nbBytes, options);
40 }, [i18n, nbBytes]);
41};