utils.ts
⎇
Raw
1import Toastify from "toastify-js";
2
3export function sleep(ms: number) {
4 return new Promise((resolve) => setTimeout(resolve, ms));
5}
6
7//join string and dedup slashes and make sure it doesn't start with a slash
8export function joinPath(...paths: string[]): string {
9 return paths.join("/").replace(/\/+/g, "/").replace(/^\//, "");
10}
11
12export function formatTime(seconds: number): string {
13 const mins = Math.floor(seconds / 60);
14 const secs = Math.floor(seconds % 60);
15 return `${mins}:${secs < 10 ? "0" : ""}${secs}`;
16}
17
18export const LocalStorageValues = {
19 options: "Options",
20 volume: "Volume",
21 playlist: "Playlist",
22 shuffle: "Shuffle",
23 repeat: "Repeat",
24 offline: "Offline",
25 path: "Path",
26};
27
28export function generateUUID() {
29 return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
30 const r = (Math.random() * 16) | 0;
31 const v = c === "x" ? r : (r & 0x3) | 0x8;
32 return v.toString(16);
33 });
34}
35
36function stringErr(e: unknown) {
37 return e && typeof e === "object" && "message" in e ? e.message : e;
38}
39
40export function toast(text: string, type: "info" | "error" | "success", e?: unknown) {
41 if (e) {
42 console.error(e);
43 }
44 const thisToast = Toastify({
45 text: e ? `${text}:\n${stringErr(e)}` : text,
46 duration: 2000,
47 close: true,
48 style: {
49 "border-radius": "0",
50 display: "flex",
51 "align-items": "center",
52 background:
53 type === "success"
54 ? "linear-gradient(135deg,rgb(38, 194, 27),rgb(5, 144, 52))"
55 : type === "error"
56 ? "linear-gradient(135deg,rgb(255, 24, 24),rgb(220, 0, 0))"
57 : "linear-gradient(135deg,rgb(50, 60, 255),rgb(34, 37, 209))",
58 },
59 });
60 thisToast.showToast();
61 return thisToast;
62}
63
64export function remoteLog(e: unknown) {
65 fetch("remote-log", { body: JSON.stringify(e?.toString()), method: "POST" });
66}
67