Skip to content

Commit

Permalink
refactor: remove pretty bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEnx authored and JackEnx committed Apr 18, 2024
1 parent 91b1341 commit 18bb9e9
Show file tree
Hide file tree
Showing 8 changed files with 706 additions and 454 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"lodash": "^4.17.21",
"lottie-react": "^2.4.0",
"parse-torrent": "9.1.5",
"pretty-bytes": "^6.1.1",
"ps-list": "^8.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
6 changes: 2 additions & 4 deletions src/renderer/hooks/use-download.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { addMilliseconds } from "date-fns";
import prettyBytes from "pretty-bytes";

import { formatDownloadProgress } from "@renderer/helpers";
import { useLibrary } from "./use-library";
Expand All @@ -12,6 +11,7 @@ import {
} from "@renderer/features";
import type { GameShop, TorrentProgress } from "@types";
import { useDate } from "./use-date";
import { byteFormat } from "@renderer/utils";

export function useDownload() {
const { updateLibrary } = useLibrary();
Expand Down Expand Up @@ -113,9 +113,7 @@ export function useDownload() {
fileSize: lastPacket?.game.fileSize,
isVerifying,
gameId: lastPacket?.game.id,
downloadSpeed: `${prettyBytes(lastPacket?.downloadSpeed ?? 0, {
bits: true,
})}/s`,
downloadSpeed: `${byteFormat(lastPacket?.downloadSpeed ?? 0)}/s`,
isDownloading: Boolean(lastPacket),
progress: getProgress(),
numPeers: lastPacket?.numPeers,
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/pages/downloads/downloads.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import prettyBytes from "pretty-bytes";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";

Expand All @@ -11,6 +10,7 @@ import { useEffect, useMemo, useRef, useState } from "react";
import { BinaryNotFoundModal } from "../shared-modals/binary-not-found-modal";
import * as styles from "./downloads.css";
import { DeleteModal } from "./delete-modal";
import { byteFormat } from "@renderer/utils";

export function Downloads() {
const { library, updateLibrary } = useLibrary();
Expand Down Expand Up @@ -61,10 +61,10 @@ export function Downloads() {
const isGameDownloading = isDownloading && gameDownloading?.id === game?.id;

if (!game) return "N/A";
if (game.fileSize) return prettyBytes(game.fileSize);
if (game.fileSize) return byteFormat(game.fileSize);

if (gameDownloading?.fileSize && isGameDownloading)
return prettyBytes(gameDownloading.fileSize);
return byteFormat(gameDownloading.fileSize);

return game.repack?.fileSize ?? "N/A";
};
Expand All @@ -87,7 +87,7 @@ export function Downloads() {
) : (
<>
<p>
{prettyBytes(gameDownloading?.bytesDownloaded)} /{" "}
{byteFormat(gameDownloading?.bytesDownloaded)} /{" "}
{finalDownloadSize}
</p>
<p>
Expand Down
10 changes: 5 additions & 5 deletions src/renderer/pages/game-details/hero-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { format } from "date-fns";
import prettyBytes from "pretty-bytes";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";

Expand All @@ -12,6 +11,7 @@ import { NoEntryIcon, PlusCircleIcon } from "@primer/octicons-react";
import { BinaryNotFoundModal } from "../shared-modals/binary-not-found-modal";
import * as styles from "./hero-panel.css";
import { useDate } from "@renderer/hooks/use-date";
import { byteFormat } from "@renderer/utils";

export interface HeroPanelProps {
game: Game | null;
Expand Down Expand Up @@ -120,10 +120,10 @@ export function HeroPanel({

const finalDownloadSize = useMemo(() => {
if (!game) return "N/A";
if (game.fileSize) return prettyBytes(game.fileSize);
if (game.fileSize) return byteFormat(game.fileSize);

if (gameDownloading?.fileSize && isGameDownloading)
return prettyBytes(gameDownloading.fileSize);
return byteFormat(gameDownloading.fileSize);

return game.repack?.fileSize ?? "N/A";
}, [game, isGameDownloading, gameDownloading]);
Expand Down Expand Up @@ -170,7 +170,7 @@ export function HeroPanel({
</>
) : (
<p className={styles.downloadDetailsRow}>
{prettyBytes(gameDownloading?.bytesDownloaded)} /{" "}
{byteFormat(gameDownloading?.bytesDownloaded)} /{" "}
{finalDownloadSize}
<small>
{numPeers} peers / {numSeeds} seeds
Expand All @@ -190,7 +190,7 @@ export function HeroPanel({
})}
</p>
<p>
{prettyBytes(game.bytesDownloaded)} / {finalDownloadSize}
{byteFormat(game.bytesDownloaded)} / {finalDownloadSize}
</p>
</>
);
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/game-details/repacks-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import prettyBytes from "pretty-bytes";

import { Button, Modal, TextField } from "@renderer/components";
import type { GameRepack, ShopDetails } from "@types";
Expand All @@ -10,6 +9,7 @@ import * as styles from "./repacks-modal.css";
import type { DiskSpace } from "check-disk-space";
import { format } from "date-fns";
import { SPACING_UNIT } from "@renderer/theme.css";
import { byteFormat } from "@renderer/utils";

export interface RepacksModalProps {
visible: boolean;
Expand Down Expand Up @@ -66,7 +66,7 @@ export function RepacksModal({
visible={visible}
title={`${gameDetails.name} Repacks`}
description={t("space_left_on_disk", {
space: prettyBytes(diskFreeSpace?.free ?? 0),
space: byteFormat(diskFreeSpace?.free ?? 0),
})}
onClose={onClose}
>
Expand Down
15 changes: 15 additions & 0 deletions src/renderer/utils/byteFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const FORMAT = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];

export const byteFormat = (bytes: number): string => {
if (!Number.isFinite(bytes) || isNaN(bytes) || bytes < 0) {
return `N/A ${FORMAT[0]}`;
}

const byteKBase = 1024;

const base = Math.floor(Math.log(bytes) / Math.log(byteKBase));

const formatedByte = bytes / byteKBase ** base;

return `${Math.trunc(formatedByte * 10) / 10} ${FORMAT[base]}`;
};
1 change: 1 addition & 0 deletions src/renderer/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./byteFormat";
Loading

0 comments on commit 18bb9e9

Please sign in to comment.