Refactor the rqbit-web file by breaking it down into components and helper functions. Improve code organization and maintainability
This commit is contained in:
parent
a5e7a5a5f5
commit
f978ad02fe
25 changed files with 1722 additions and 926 deletions
29
crates/librqbit/webui/src/helper/customSetInterval.ts
Normal file
29
crates/librqbit/webui/src/helper/customSetInterval.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Run a function with initial interval, then run it forever with the interval that the
|
||||
// callback returns.
|
||||
// Returns a callback to clear it.
|
||||
|
||||
export function customSetInterval(
|
||||
asyncCallback: () => Promise<number>,
|
||||
initialInterval: number
|
||||
): () => void {
|
||||
let timeoutId: number;
|
||||
let currentInterval: number = initialInterval;
|
||||
|
||||
const executeCallback = async () => {
|
||||
currentInterval = await asyncCallback();
|
||||
if (currentInterval === null || currentInterval === undefined) {
|
||||
throw "asyncCallback returned null or undefined";
|
||||
}
|
||||
scheduleNext();
|
||||
};
|
||||
|
||||
let scheduleNext = () => {
|
||||
timeoutId = setTimeout(executeCallback, currentInterval);
|
||||
};
|
||||
|
||||
scheduleNext();
|
||||
|
||||
return () => {
|
||||
clearTimeout(timeoutId);
|
||||
};
|
||||
}
|
||||
10
crates/librqbit/webui/src/helper/formatBytes.ts
Normal file
10
crates/librqbit/webui/src/helper/formatBytes.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
export function formatBytes(bytes: number): string {
|
||||
if (bytes === 0) return "0 Bytes";
|
||||
|
||||
const k = 1024;
|
||||
const sizes = ["Bytes", "KB", "MB", "GB"];
|
||||
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
|
||||
}
|
||||
19
crates/librqbit/webui/src/helper/formatSecondsToTime.ts
Normal file
19
crates/librqbit/webui/src/helper/formatSecondsToTime.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
export function formatSecondsToTime(seconds: number): string {
|
||||
const hours = Math.floor(seconds / 3600);
|
||||
const minutes = Math.floor((seconds % 3600) / 60);
|
||||
const remainingSeconds = seconds % 60;
|
||||
|
||||
const formatUnit = (value: number, unit: string) =>
|
||||
value > 0 ? `${value}${unit}` : "";
|
||||
|
||||
if (hours > 0) {
|
||||
return `${formatUnit(hours, "h")} ${formatUnit(minutes, "m")}`.trim();
|
||||
} else if (minutes > 0) {
|
||||
return `${formatUnit(minutes, "m")} ${formatUnit(
|
||||
remainingSeconds,
|
||||
"s"
|
||||
)}`.trim();
|
||||
} else {
|
||||
return `${formatUnit(remainingSeconds, "s")}`.trim();
|
||||
}
|
||||
}
|
||||
10
crates/librqbit/webui/src/helper/getCompletionETA.ts
Normal file
10
crates/librqbit/webui/src/helper/getCompletionETA.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { TorrentStats } from "../api-types";
|
||||
import { formatSecondsToTime } from "./formatSecondsToTime";
|
||||
|
||||
export function getCompletionETA(stats: TorrentStats): string {
|
||||
let duration = stats?.live?.time_remaining?.duration?.secs;
|
||||
if (duration == null) {
|
||||
return "N/A";
|
||||
}
|
||||
return formatSecondsToTime(duration);
|
||||
}
|
||||
10
crates/librqbit/webui/src/helper/getLargestFileName.ts
Normal file
10
crates/librqbit/webui/src/helper/getLargestFileName.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { TorrentDetails } from "../api-types";
|
||||
|
||||
export function getLargestFileName(torrentDetails: TorrentDetails): string {
|
||||
const largestFile = torrentDetails.files
|
||||
.filter((f) => f.included)
|
||||
.reduce((prev: any, current: any) =>
|
||||
prev.length > current.length ? prev : current
|
||||
);
|
||||
return largestFile.name;
|
||||
}
|
||||
27
crates/librqbit/webui/src/helper/loopUntilSuccess.ts
Normal file
27
crates/librqbit/webui/src/helper/loopUntilSuccess.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
export function loopUntilSuccess<T>(
|
||||
callback: () => Promise<T>,
|
||||
interval: number
|
||||
): () => void {
|
||||
let timeoutId: number;
|
||||
|
||||
const executeCallback = async () => {
|
||||
let retry = await callback().then(
|
||||
() => false,
|
||||
() => true
|
||||
);
|
||||
if (retry) {
|
||||
scheduleNext();
|
||||
}
|
||||
};
|
||||
|
||||
let scheduleNext = (overrideInterval?: number) => {
|
||||
timeoutId = setTimeout(
|
||||
executeCallback,
|
||||
overrideInterval !== undefined ? overrideInterval : interval
|
||||
);
|
||||
};
|
||||
|
||||
scheduleNext(0);
|
||||
|
||||
return () => clearTimeout(timeoutId);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue