mirror of
https://github.com/nicotsx/zerobyte.git
synced 2026-04-18 05:47:31 -04:00
* feat: extend snapshot details with more info Closes #385 * refactor: centralize restic backup schemas * refactor: pr feedbacks
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import type React from "react";
|
|
import { formatBytes } from "~/utils/format-bytes";
|
|
|
|
type ByteSizeProps = {
|
|
bytes: number;
|
|
base?: 1000 | 1024; // 1000 = SI (KB, MB, ...), 1024 = IEC (KiB, MiB, ...)
|
|
maximumFractionDigits?: number; // default: 2
|
|
smartRounding?: boolean; // dynamically reduces decimals for big numbers (default: true)
|
|
locale?: string | string[]; // e.g., 'en', 'de', or navigator.languages
|
|
space?: boolean; // space between number and unit (default: true)
|
|
className?: string;
|
|
style?: React.CSSProperties;
|
|
fallback?: string; // shown if bytes is not a finite number (default: '—')
|
|
};
|
|
|
|
export function ByteSize(props: ByteSizeProps) {
|
|
const {
|
|
bytes,
|
|
base = 1000,
|
|
maximumFractionDigits = 2,
|
|
smartRounding = true,
|
|
locale,
|
|
space = true,
|
|
className,
|
|
style,
|
|
fallback = "—",
|
|
} = props;
|
|
|
|
const { text, unit } = formatBytes(bytes, {
|
|
base,
|
|
maximumFractionDigits,
|
|
smartRounding,
|
|
locale,
|
|
fallback,
|
|
});
|
|
|
|
if (text === fallback) {
|
|
return (
|
|
<span className={className} style={style}>
|
|
{fallback}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<span className={className} style={style}>
|
|
{text}
|
|
{space ? " " : ""}
|
|
{unit}
|
|
</span>
|
|
);
|
|
}
|