Skip to content

Commit

Permalink
fix: filesize in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelncui committed Oct 13, 2023
1 parent 4550992 commit 3f8bc85
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"react-dnd": "^11.1.3",
"react-dnd-html5-backend": "^11.1.3",
"react-dom": "^18.2.0",
"react-intl": "^6.4.7",
"react-is": "^18.2.0",
"react-router-dom": "^6.4.5",
"react-virtuoso": "^4.6.1",
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/pages/backup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { cli, convertSourceFiles } from "../api";
import { Root } from "../api";
import { AddFileAction, RefreshListAction, CreateBackupJobAction } from "../actions";
import { JobArchiveParam, JobCreateRequest, Source } from "../entity";
import { chonkyI18n } from "../tools";

const useBackupSourceBrowser = (source: RefObject<FileBrowserHandle>) => {
const [files, setFiles] = useState<FileArray>(Array(1).fill(null));
Expand Down Expand Up @@ -64,6 +65,7 @@ const useBackupSourceBrowser = (source: RefObject<FileBrowserHandle>) => {
fileActions,
defaultFileViewActionId: ChonkyActions.EnableListView.id,
doubleClickDelay: 300,
i18n: chonkyI18n,
};
};

Expand Down Expand Up @@ -139,6 +141,7 @@ const useBackupTargetBrowser = () => {
fileActions,
defaultFileViewActionId: ChonkyActions.EnableListView.id,
doubleClickDelay: 300,
i18n: chonkyI18n,
};
};

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/pages/file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ToobarInfo } from "../components/toolbarInfo";

import { useDetailModal, DetailModal } from "./file-detail";
import { FileGetReply } from "../entity";
import { chonkyI18n } from "../tools";

const useDualSide = () => {
const left = useRef<FileBrowserHandle>(null);
Expand Down Expand Up @@ -165,6 +166,7 @@ const useFileBrowser = (storageKey: string, refreshAll: () => Promise<void>, ope
defaultFileViewActionId: ChonkyActions.EnableListView.id,
doubleClickDelay: 300,
totalSize,
i18n: chonkyI18n,
};
};

Expand Down
8 changes: 5 additions & 3 deletions frontend/src/pages/restore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { useState, useEffect, useMemo, useCallback, FC, useRef, RefObject } from

import Grid from "@mui/material/Grid";
import Box from "@mui/material/Box";
import { FileBrowser, FileNavbar, FileToolbar, FileList, FileContextMenu, FileArray, FileBrowserHandle } from "@samuelncui/chonky";
import { FileBrowser, FileNavbar, FileToolbar, FileList, FileContextMenu, FileArray, FileBrowserHandle, defaultFormatters } from "@samuelncui/chonky";
import { ChonkyActions, ChonkyFileActionData, FileData } from "@samuelncui/chonky";

import { ToobarInfo } from "../components/toolbarInfo";

import { cli, convertFiles } from "../api";
import { Root } from "../api";
import { Root, cli, convertFiles } from "../api";
import { AddFileAction, RefreshListAction, CreateRestoreJobAction } from "../actions";
import { JobCreateRequest, JobRestoreParam, Source } from "../entity";
import { chonkyI18n } from "../tools";

const useRestoreSourceBrowser = (target: RefObject<FileBrowserHandle>) => {
const [files, setFiles] = useState<FileArray>(Array(1).fill(null));
Expand Down Expand Up @@ -82,6 +82,7 @@ const useRestoreSourceBrowser = (target: RefObject<FileBrowserHandle>) => {
fileActions,
defaultFileViewActionId: ChonkyActions.EnableListView.id,
doubleClickDelay: 300,
i18n: chonkyI18n,
};
};

Expand Down Expand Up @@ -132,6 +133,7 @@ const useRestoreTargetBrowser = () => {
fileActions,
defaultFileViewActionId: ChonkyActions.EnableListView.id,
doubleClickDelay: 300,
i18n: chonkyI18n,
};
};

Expand Down
20 changes: 15 additions & 5 deletions frontend/src/tools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { IntlShape } from "react-intl";
import { Nullable } from "tsdef";

import { filesize } from "filesize";
import { I18nConfig, FileData, defaultFormatters } from "@samuelncui/chonky";

export const hexEncode = (buf: string) => {
var str = "";
Expand All @@ -8,11 +12,7 @@ export const hexEncode = (buf: string) => {
return str;
};

export const formatFilesize = (size: number | bigint): string =>
filesize(size as any as number, {
base: 2,
standard: "jedec",
}) as string;
export const formatFilesize = (size: number | bigint): string => filesize(size as any as number, { standard: "iec" }) as string;

export const download = (buf: Uint8Array, filename: string, contentType: string) => {
const blob = new Blob([buf], { type: contentType });
Expand All @@ -27,3 +27,13 @@ export const sleep = (ms: number): Promise<null> =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});

export const chonkyI18n: I18nConfig = {
formatters: {
...defaultFormatters,
formatFileSize: (_intl: IntlShape, file: Nullable<FileData>): Nullable<string> => {
if (!file || typeof file.size !== "number") return null;
return filesize(file.size, { standard: "iec" }) as string;
},
},
};

0 comments on commit 3f8bc85

Please sign in to comment.