feat: polish release v2 experience (#6800)
This commit is contained in:
1 parent
925b0fb801
commit
a4a903d7c3
295 files changed
+10627
-7115
No files matched your search
@@ -5,51 +5,23 @@ import relativeTime from "dayjs/plugin/relativeTime";
|
||||
dayjs.extend(duration);
|
||||
dayjs.extend(relativeTime);
|
||||
|
||||
const byteUnits = ["B", "KB", "MB", "GB", "TB"] as const;
|
||||
const rateUnits = ["B/s", "KB/s", "MB/s", "GB/s"] as const;
|
||||
|
||||
const formatScaled = (value: number, units: readonly string[], zeroLabel: string): string => {
|
||||
if (!value || !Number.isFinite(value)) return zeroLabel;
|
||||
const i = Math.max(0, Math.min(Math.floor(Math.log(Math.abs(value)) / Math.log(1024)), units.length - 1));
|
||||
const scaled = value / 1024 ** i;
|
||||
const decimals = scaled < 10 ? 2 : 1;
|
||||
return `${scaled.toFixed(decimals)} ${units[i] ?? ""}`;
|
||||
const compactByteLabel = (value: number, formatter: (value: number) => string): string => {
|
||||
if (!value || !Number.isFinite(value)) return "0";
|
||||
return formatter(Number(value)).replace(".0 ", "").replace(" ", "");
|
||||
};
|
||||
|
||||
const formatScaledCompact = (value: number, units: readonly string[], zeroLabel: string): string => {
|
||||
if (!value || !Number.isFinite(value)) return zeroLabel;
|
||||
const i = Math.max(0, Math.min(Math.floor(Math.log(Math.abs(value)) / Math.log(1024)), units.length - 1));
|
||||
const scaled = value / 1024 ** i;
|
||||
return `${scaled < 10 ? scaled.toFixed(1) : Math.round(scaled)}${units[i] ?? ""}`;
|
||||
};
|
||||
|
||||
export const formatByteRate = (value: number): string => formatScaled(value, rateUnits, "0 B/s");
|
||||
|
||||
export const formatStorageBytes = (bytes: number): string => formatScaled(bytes, byteUnits, "0 B");
|
||||
|
||||
export const formatGB = (value: number): string => {
|
||||
if (!value || !Number.isFinite(value)) return "0 GB";
|
||||
if (Math.abs(value) >= 1024) return `${(value / 1024).toFixed(2)} TB`;
|
||||
return `${value.toFixed(2)} GB`;
|
||||
};
|
||||
export const createByteChartAxisFormatters = (
|
||||
formatBytes: (bytes: number) => string,
|
||||
formatByteRate: (bytes: number) => string,
|
||||
) => ({
|
||||
bytes: (value: number) => compactByteLabel(value, formatBytes),
|
||||
rate: (value: number) => compactByteLabel(value, formatByteRate),
|
||||
});
|
||||
|
||||
export const formatPercent = (value: number): string => `${value.toFixed(1)}%`;
|
||||
|
||||
export const getProgressTrackSize = (size: "xs" | "sm"): number => (size === "xs" ? 6 : 9);
|
||||
|
||||
export const chartAxisFormatters = {
|
||||
percent: formatPercent,
|
||||
gb: (value: number) => {
|
||||
const numericValue = Number(value);
|
||||
if (!numericValue) return "0";
|
||||
if (Math.abs(numericValue) >= 1024) return `${(numericValue / 1024).toFixed(1)}T`;
|
||||
return `${numericValue.toFixed(0)}G`;
|
||||
},
|
||||
mb: (value: number) => (!value ? "0" : `${Number(value).toFixed(0)}M`),
|
||||
bytes: (value: number) => formatScaledCompact(Number(value), byteUnits, "0"),
|
||||
rate: (value: number) => formatScaledCompact(Number(value), rateUnits, "0"),
|
||||
} as const;
|
||||
|
||||
export const formatUptime = (seconds: number): string => dayjs.duration(seconds, "seconds").humanize();
|
||||
|
||||
export const formatTemp = (celsius: number | null, fahrenheit: boolean): string => {
|
||||
|
||||
Reference in new issue
Block a user