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

View File

@ -4,7 +4,7 @@ import { CopyToClipboard } from "react-copy-to-clipboard";
import { Clipboard as ClipboardIcon } from "lucide-react";
import { type Certificate } from "@/domain/certificate";
import { saveFiles2Zip } from "@/lib/file";
import { saveFiles2Zip } from "@/utils/file";
type CertificateDetailProps = {
data: Certificate;

View File

@ -8,7 +8,7 @@ import { ClientResponseError } from "pocketbase";
import { Button } from "@/components/ui/button";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { readFileContent } from "@/lib/file";
import { readFileContent } from "@/utils/file";
import { PbErrorData } from "@/domain/base";
import { accessProvidersMap, accessTypeFormSchema, type Access, type KubernetesConfig } from "@/domain/access";
import { save } from "@/repository/access";

View File

@ -8,7 +8,7 @@ import { ClientResponseError } from "pocketbase";
import { Button } from "@/components/ui/button";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { readFileContent } from "@/lib/file";
import { readFileContent } from "@/utils/file";
import { PbErrorData } from "@/domain/base";
import { accessProvidersMap, accessTypeFormSchema, type Access, type SSHConfig } from "@/domain/access";
import { save } from "@/repository/access";

View File

@ -6,7 +6,7 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { NotifyChannels, NotifyChannelBark } from "@/domain/settings";
import { update } from "@/repository/settings";
import { useNotifyContext } from "@/providers/notify";
@ -113,7 +113,7 @@ const Bark = () => {
description: t("settings.notification.config.saved.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),
@ -137,7 +137,7 @@ const Bark = () => {
description: t("settings.notification.push_test_message.succeeded.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("settings.notification.push_test_message.failed.message"),
@ -173,7 +173,7 @@ const Bark = () => {
setChannels(resp);
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),

View File

@ -6,7 +6,7 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { NotifyChannelDingTalk, NotifyChannels } from "@/domain/settings";
import { useNotifyContext } from "@/providers/notify";
import { update } from "@/repository/settings";
@ -113,7 +113,7 @@ const DingTalk = () => {
description: t("settings.notification.config.saved.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),
@ -139,7 +139,7 @@ const DingTalk = () => {
description: t("settings.notification.push_test_message.succeeded.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("settings.notification.push_test_message.failed.message"),
@ -173,7 +173,7 @@ const DingTalk = () => {
setChannels(resp);
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),

View File

@ -6,7 +6,7 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { NotifyChannelEmail, NotifyChannels } from "@/domain/settings";
import { useNotifyContext } from "@/providers/notify";
import { update } from "@/repository/settings";
@ -136,7 +136,7 @@ const Mail = () => {
description: t("settings.notification.config.saved.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),
@ -160,7 +160,7 @@ const Mail = () => {
description: t("settings.notification.push_test_message.succeeded.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("settings.notification.push_test_message.failed.message"),
@ -196,7 +196,7 @@ const Mail = () => {
setChannels(resp);
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),

View File

@ -6,7 +6,7 @@ import { useNotifyContext } from "@/providers/notify";
import { NotifyChannelLark, NotifyChannels } from "@/domain/settings";
import { useEffect, useState } from "react";
import { update } from "@/repository/settings";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { useToast } from "@/components/ui/use-toast";
import { useTranslation } from "react-i18next";
import { notifyTest } from "@/api/notify";
@ -109,7 +109,7 @@ const Lark = () => {
description: t("settings.notification.config.saved.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),
@ -135,7 +135,7 @@ const Lark = () => {
description: t("settings.notification.push_test_message.succeeded.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("settings.notification.push_test_message.failed.message"),
@ -169,7 +169,7 @@ const Lark = () => {
setChannels(resp);
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),

View File

@ -6,8 +6,8 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { isValidURL } from "@/lib/url";
import { getErrMsg } from "@/utils/error";
import { isValidURL } from "@/utils/url";
import { NotifyChannels, NotifyChannelServerChan } from "@/domain/settings";
import { update } from "@/repository/settings";
import { useNotifyContext } from "@/providers/notify";
@ -120,7 +120,7 @@ const ServerChan = () => {
description: t("settings.notification.config.saved.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),
@ -144,7 +144,7 @@ const ServerChan = () => {
description: t("settings.notification.push_test_message.succeeded.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("settings.notification.push_test_message.failed.message"),
@ -180,7 +180,7 @@ const ServerChan = () => {
setChannels(resp);
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),

View File

@ -6,7 +6,7 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { NotifyChannels, NotifyChannelTelegram } from "@/domain/settings";
import { update } from "@/repository/settings";
import { useNotifyContext } from "@/providers/notify";
@ -113,7 +113,7 @@ const Telegram = () => {
description: t("settings.notification.config.saved.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),
@ -137,7 +137,7 @@ const Telegram = () => {
description: t("settings.notification.push_test_message.succeeded.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("settings.notification.push_test_message.failed.message"),
@ -173,7 +173,7 @@ const Telegram = () => {
setChannels(resp);
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),

View File

@ -6,8 +6,8 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { isValidURL } from "@/lib/url";
import { getErrMsg } from "@/utils/error";
import { isValidURL } from "@/utils/url";
import { NotifyChannels, NotifyChannelWebhook } from "@/domain/settings";
import { update } from "@/repository/settings";
import { useNotifyContext } from "@/providers/notify";
@ -120,7 +120,7 @@ const Webhook = () => {
description: t("settings.notification.config.saved.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),
@ -144,7 +144,7 @@ const Webhook = () => {
description: t("settings.notification.push_test_message.succeeded.message"),
});
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("settings.notification.push_test_message.failed.message"),
@ -180,7 +180,7 @@ const Webhook = () => {
setChannels(resp);
} catch (e) {
const msg = getErrMessage(e);
const msg = getErrMsg(e);
toast({
title: t("common.save.failed.message"),

View File

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

View File

@ -1,35 +0,0 @@
export const convertZulu2Beijing = (zuluTime: string) => {
const utcDate = new Date(zuluTime);
// Format the Beijing time
const formattedBeijingTime = new Intl.DateTimeFormat("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
timeZone: "Asia/Shanghai",
}).format(utcDate);
return formattedBeijingTime;
};
export const getLeftDays = (zuluTime: string) => {
const time = convertZulu2Beijing(zuluTime);
const date = time.split(" ")[0];
const now = new Date();
const target = new Date(date);
const diff = target.getTime() - now.getTime();
const days = Math.floor(diff / (1000 * 60 * 60 * 24));
return days;
};
export const diffDays = (date1: string, date2: string) => {
const target1 = new Date(date1);
const target2 = new Date(date2);
const diff = target1.getTime() - target2.getTime();
const days = Math.ceil(diff / (1000 * 60 * 60 * 24));
return days;
};

View File

@ -9,7 +9,7 @@ import { Button } from "@/components/ui/button";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { getPocketBase } from "@/repository/pocketbase";
const formSchema = z.object({
@ -45,7 +45,7 @@ const Account = () => {
navigate("/login");
}, 500);
} catch (e) {
const message = getErrMessage(e);
const message = getErrMsg(e);
toast({
title: t("settings.account.email.failed.message"),
description: message,

View File

@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button";
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { getPocketBase } from "@/repository/pocketbase";
const formSchema = z
@ -46,7 +46,7 @@ const Password = () => {
try {
await getPocketBase().admins.authWithPassword(getPocketBase().authStore.model?.email, values.oldPassword);
} catch (e) {
const message = getErrMessage(e);
const message = getErrMsg(e);
form.setError("oldPassword", { message });
}
@ -65,7 +65,7 @@ const Password = () => {
navigate("/login");
}, 500);
} catch (e) {
const message = getErrMessage(e);
const message = getErrMsg(e);
toast({
title: t("settings.password.failed.message"),
description: message,

View File

@ -11,7 +11,7 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import { useToast } from "@/components/ui/use-toast";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { SSLProvider as SSLProviderType, SSLProviderSetting, Setting } from "@/domain/settings";
import { getSetting, update } from "@/repository/settings";
import { produce } from "immer";
@ -84,7 +84,7 @@ const SSLProvider = () => {
description: t("common.update.succeeded.message"),
});
} catch (e) {
const message = getErrMessage(e);
const message = getErrMsg(e);
toast({
title: t("common.update.failed.message"),
description: message,

View File

@ -13,7 +13,7 @@ import WorkflowLog from "@/components/workflow/WorkflowLog";
import { cn } from "@/components/ui/utils";
import WorkflowProvider from "@/components/workflow/WorkflowProvider";
import { allNodesValidated, WorkflowNode } from "@/domain/workflow";
import { getErrMessage } from "@/lib/error";
import { getErrMsg } from "@/utils/error";
import { useWorkflowStore, WorkflowState } from "@/stores/workflow";
import { ArrowLeft } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
@ -128,7 +128,7 @@ const WorkflowDetail = () => {
} catch (e) {
toast({
title: t("workflow.detail.action.run.failed"),
description: getErrMessage(e),
description: getErrMsg(e),
variant: "destructive",
});
}

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";
};