refactor(ui): clean code

This commit is contained in:
Fu Diwei
2024-12-09 17:48:44 +08:00
parent 588e89e8fe
commit 07a443f6c4
19 changed files with 57 additions and 88 deletions

15
ui/src/utils/error.ts Normal file
View File

@@ -0,0 +1,15 @@
export const getErrMsg = (error: unknown): string => {
if (error instanceof Error) {
return error.message;
} else if (typeof error === "object" && error != null) {
if ("message" in error) {
return String(error.message);
} else if ("msg" in error) {
return String(error.msg);
}
} else if (typeof error === "string") {
return error;
}
return "Something went wrong";
};

35
ui/src/utils/file.ts Normal file
View File

@@ -0,0 +1,35 @@
import JSZip from "jszip";
export function readFileContent(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (reader.result) {
resolve(reader.result.toString());
} else {
reject("No content found");
}
};
reader.onerror = () => reject(reader.error);
reader.readAsText(file);
});
}
export const saveFiles2Zip = async (zipName: string, files: { name: string; content: string }[]) => {
const zip = new JSZip();
files.forEach((file) => {
zip.file(file.name, file.content);
});
const content = await zip.generateAsync({ type: "blob" });
// Save the zip file to the local system
const link = document.createElement("a");
link.href = URL.createObjectURL(content);
link.download = zipName;
link.click();
};

8
ui/src/utils/url.ts Normal file
View File

@@ -0,0 +1,8 @@
export function isValidURL(url: string): boolean {
try {
new URL(url);
return true;
} catch (error) {
return false;
}
}