diff --git a/ui/src/components/certificate/CertificateDetail.tsx b/ui/src/components/certificate/CertificateDetail.tsx index e3ffc1fd..c877ddd9 100644 --- a/ui/src/components/certificate/CertificateDetail.tsx +++ b/ui/src/components/certificate/CertificateDetail.tsx @@ -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; diff --git a/ui/src/components/certimate/AccessKubernetesForm.tsx b/ui/src/components/certimate/AccessKubernetesForm.tsx index 254281e5..d15a430f 100644 --- a/ui/src/components/certimate/AccessKubernetesForm.tsx +++ b/ui/src/components/certimate/AccessKubernetesForm.tsx @@ -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"; diff --git a/ui/src/components/certimate/AccessSSHForm.tsx b/ui/src/components/certimate/AccessSSHForm.tsx index ed9f3cb3..44d376f0 100644 --- a/ui/src/components/certimate/AccessSSHForm.tsx +++ b/ui/src/components/certimate/AccessSSHForm.tsx @@ -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"; diff --git a/ui/src/components/notify/Bark.tsx b/ui/src/components/notify/Bark.tsx index 0b651cb6..678f5919 100644 --- a/ui/src/components/notify/Bark.tsx +++ b/ui/src/components/notify/Bark.tsx @@ -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"), diff --git a/ui/src/components/notify/DingTalk.tsx b/ui/src/components/notify/DingTalk.tsx index ab9ff9d1..8f733dc9 100644 --- a/ui/src/components/notify/DingTalk.tsx +++ b/ui/src/components/notify/DingTalk.tsx @@ -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"), diff --git a/ui/src/components/notify/Email.tsx b/ui/src/components/notify/Email.tsx index f778e73c..72734869 100644 --- a/ui/src/components/notify/Email.tsx +++ b/ui/src/components/notify/Email.tsx @@ -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"), diff --git a/ui/src/components/notify/Lark.tsx b/ui/src/components/notify/Lark.tsx index 89a6eadd..9b42219c 100644 --- a/ui/src/components/notify/Lark.tsx +++ b/ui/src/components/notify/Lark.tsx @@ -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"), diff --git a/ui/src/components/notify/ServerChan.tsx b/ui/src/components/notify/ServerChan.tsx index ee9b23eb..0029d00b 100644 --- a/ui/src/components/notify/ServerChan.tsx +++ b/ui/src/components/notify/ServerChan.tsx @@ -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"), diff --git a/ui/src/components/notify/Telegram.tsx b/ui/src/components/notify/Telegram.tsx index 63f6079c..b11c8b8e 100644 --- a/ui/src/components/notify/Telegram.tsx +++ b/ui/src/components/notify/Telegram.tsx @@ -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"), diff --git a/ui/src/components/notify/Webhook.tsx b/ui/src/components/notify/Webhook.tsx index 70c8d052..f8b632da 100644 --- a/ui/src/components/notify/Webhook.tsx +++ b/ui/src/components/notify/Webhook.tsx @@ -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"), diff --git a/ui/src/lib/error.ts b/ui/src/lib/error.ts deleted file mode 100644 index 4a5c2b37..00000000 --- a/ui/src/lib/error.ts +++ /dev/null @@ -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"; - } -}; diff --git a/ui/src/lib/time.ts b/ui/src/lib/time.ts deleted file mode 100644 index 0d737f65..00000000 --- a/ui/src/lib/time.ts +++ /dev/null @@ -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; -}; diff --git a/ui/src/pages/settings/Account.tsx b/ui/src/pages/settings/Account.tsx index c12b6d94..3d1555bf 100644 --- a/ui/src/pages/settings/Account.tsx +++ b/ui/src/pages/settings/Account.tsx @@ -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, diff --git a/ui/src/pages/settings/Password.tsx b/ui/src/pages/settings/Password.tsx index 0868ecbb..d500f561 100644 --- a/ui/src/pages/settings/Password.tsx +++ b/ui/src/pages/settings/Password.tsx @@ -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, diff --git a/ui/src/pages/settings/SSLProvider.tsx b/ui/src/pages/settings/SSLProvider.tsx index fd4abeab..bd14af22 100644 --- a/ui/src/pages/settings/SSLProvider.tsx +++ b/ui/src/pages/settings/SSLProvider.tsx @@ -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, diff --git a/ui/src/pages/workflows/WorkflowDetail.tsx b/ui/src/pages/workflows/WorkflowDetail.tsx index 9d7dc66a..9df43bbe 100644 --- a/ui/src/pages/workflows/WorkflowDetail.tsx +++ b/ui/src/pages/workflows/WorkflowDetail.tsx @@ -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", }); } diff --git a/ui/src/utils/error.ts b/ui/src/utils/error.ts new file mode 100644 index 00000000..f046c559 --- /dev/null +++ b/ui/src/utils/error.ts @@ -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"; +}; diff --git a/ui/src/lib/file.ts b/ui/src/utils/file.ts similarity index 100% rename from ui/src/lib/file.ts rename to ui/src/utils/file.ts diff --git a/ui/src/lib/url.ts b/ui/src/utils/url.ts similarity index 100% rename from ui/src/lib/url.ts rename to ui/src/utils/url.ts