fc69ae30ab
* feat: depot api downloads * feat: frontend fixes and experimental webview store * feat: sync downloader * feat: cleanup and fixes * feat: encrypted database and fixed resuming * feat: launch option selector * fix: autostart when no options * fix: clippy * fix: clippy x2 * feat: executor launch * feat: executor launch * feat: not installed error handling * feat: better offline handling * feat: dependency popup * fix: cancelation and resuming issues * feat: dedup by platform * feat: new ui for additional components and fix dl manager clog * feat: auto-queue dependencies * feat: depot scanning and ranking * feat: new library fetching stack * In-app store page (Windows + macOS) (#176) * feat: async store loading * feat: fix overscroll behaviour * fix: query params in server protocol * fix: clippy
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { listen } from "@tauri-apps/api/event";
|
|
import type { DownloadableMetadata } from "~/types";
|
|
|
|
export type QueueState = {
|
|
queue: Array<{
|
|
meta: DownloadableMetadata;
|
|
status: string;
|
|
progress: number | null;
|
|
current: number;
|
|
max: number;
|
|
}>;
|
|
status: string;
|
|
};
|
|
|
|
export type StatsState = {
|
|
speed: number; // Bytes per second
|
|
time: number; // Seconds,
|
|
};
|
|
|
|
export const useQueueState = () =>
|
|
useState<QueueState>("queue", () => ({ queue: [], status: "Unknown" }));
|
|
|
|
export const useStatsState = () =>
|
|
useState<StatsState>("stats", () => ({ speed: 0, time: 0 }));
|
|
|
|
listen("update_queue", (event) => {
|
|
const queue = useQueueState();
|
|
queue.value = event.payload as QueueState;
|
|
});
|
|
|
|
listen("update_stats", (event) => {
|
|
const stats = useStatsState();
|
|
stats.value = event.payload as StatsState;
|
|
});
|
|
|
|
export const useDownloadHistory = () => useState<Array<number>>('history', () => []);
|
|
|
|
export function formatKilobytes(bytes: number): string {
|
|
const units = ["K", "M", "G", "T", "P"];
|
|
let value = bytes;
|
|
let unitIndex = 0;
|
|
const scalar = 1000;
|
|
|
|
while (value >= scalar && unitIndex < units.length - 1) {
|
|
value /= scalar;
|
|
unitIndex++;
|
|
}
|
|
|
|
return `${value.toFixed(1)} ${units[unitIndex]}`;
|
|
} |