import Toastify from "toastify-js"; export function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); } //join string and dedup slashes and make sure it doesn't start with a slash export function joinPath(...paths: string[]): string { return paths.join("/").replace(/\/+/g, "/").replace(/^\//, ""); } export function formatTime(seconds: number): string { const mins = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60); return `${mins}:${secs < 10 ? "0" : ""}${secs}`; } export const LocalStorageValues = { options: "Options", volume: "Volume", playlist: "Playlist", shuffle: "Shuffle", repeat: "Repeat", offline: "Offline", path: "Path", }; export function generateUUID() { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0; const v = c === "x" ? r : (r & 0x3) | 0x8; return v.toString(16); }); } function stringErr(e: unknown) { return e && typeof e === "object" && "message" in e ? e.message : e; } export function toast(text: string, type: "info" | "error" | "success", e?: unknown) { if (e) console.error(e); const thisToast = Toastify({ text: e ? `${text}:\n${stringErr(e)}` : text, duration: 20000, close: true, style: { "border-radius": "0", display: "flex", "align-items": "center", background: type === "success" ? "linear-gradient(135deg,rgb(38, 194, 27),rgb(5, 144, 52))" : type === "error" ? "linear-gradient(135deg,rgb(255, 24, 24),rgb(220, 0, 0))" : "linear-gradient(135deg,rgb(50, 60, 255),rgb(34, 37, 209))", }, }); thisToast.showToast(); return thisToast; } export function toastError() {} export function formatBytes(bytes: number): string { if (bytes === 0) return "0 Bytes"; const k = 1024; const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); const value = bytes / k ** i; return `${value.toFixed(2)} ${sizes[i]}`; }