From 855b5d4cc80162a694c4ca8570b01847a783352e Mon Sep 17 00:00:00 2001 From: naiba Date: Sun, 15 Dec 2024 20:26:35 +0800 Subject: [PATCH] chore: refactor --- src/components/install-commands.tsx | 6 ++-- src/routes/root.tsx | 32 ++------------------ src/routes/settings.tsx | 46 ++++++++++++++--------------- 3 files changed, 29 insertions(+), 55 deletions(-) diff --git a/src/components/install-commands.tsx b/src/components/install-commands.tsx index b0446ab..42552ec 100644 --- a/src/components/install-commands.tsx +++ b/src/components/install-commands.tsx @@ -22,15 +22,15 @@ enum OSTypes { export const InstallCommandsMenu = forwardRef((props, ref) => { const [copy, setCopy] = useState(false) - const settings = useSettings() + const {data: settings} = useSettings() const { t } = useTranslation() const switchState = async (type: number) => { if (!copy) { try { setCopy(true) - if (!settings.data) throw new Error("Settings is not found.") - await copyToClipboard(generateCommand(type, settings.data) || "") + if (!settings) throw new Error("Settings is not found.") + await copyToClipboard(generateCommand(type, settings) || "") } catch (e: Error | any) { console.error(e) toast(t("Error"), { diff --git a/src/routes/root.tsx b/src/routes/root.tsx index 2cf0416..a138414 100644 --- a/src/routes/root.tsx +++ b/src/routes/root.tsx @@ -16,37 +16,11 @@ export default function Root() { }, [settingData]) const InjectContext = useCallback((content: string) => { + document.getElementById("nezha-custom-code")?.remove() const tempDiv = document.createElement("div") + tempDiv.id = "nezha-custom-code" tempDiv.innerHTML = content - - const handlers: { [key: string]: (element: HTMLElement) => void } = { - SCRIPT: (element) => { - const script = document.createElement("script") - if ((element as HTMLScriptElement).src) { - script.src = (element as HTMLScriptElement).src - } else { - script.textContent = element.textContent - } - document.body.appendChild(script) - }, - STYLE: (element) => { - const style = document.createElement("style") - style.textContent = element.textContent - document.head.appendChild(style) - }, - DEFAULT: (element) => { - document.body.appendChild(element) - }, - } - - Array.from(tempDiv.childNodes).forEach((node) => { - if (node.nodeType === Node.ELEMENT_NODE) { - const element = node as HTMLElement - ;(handlers[element.tagName] || handlers.DEFAULT)(element) - } else if (node.nodeType === Node.TEXT_NODE) { - document.body.appendChild(document.createTextNode(node.textContent || "")) - } - }) + document.body.appendChild(tempDiv) }, []) if (error) { diff --git a/src/routes/settings.tsx b/src/routes/settings.tsx index 6e53105..aa9eb4c 100644 --- a/src/routes/settings.tsx +++ b/src/routes/settings.tsx @@ -57,21 +57,21 @@ export default function SettingsPage() { resolver: zodResolver(settingFormSchema), defaultValues: config ? { - ...config, - language: config.language, - site_name: config.site_name || "", - user_template: - config.user_template || - Object.keys(config.frontend_templates.filter((t) => !t.is_admin) || {})[0] || - "user-dist", - } + ...config, + language: config.language, + site_name: config.site_name || "", + user_template: + config.user_template || + Object.keys(config.frontend_templates.filter((t) => !t.is_admin) || {})[0] || + "user-dist", + } : { - ip_change_notification_group_id: 0, - cover: 1, - site_name: "", - language: "", - user_template: "user-dist", - }, + ip_change_notification_group_id: 0, + cover: 1, + site_name: "", + language: "", + user_template: "user-dist", + }, resetOptions: { keepDefaultValues: false, }, @@ -86,8 +86,8 @@ export default function SettingsPage() { const onSubmit = async (values: z.infer) => { try { await updateSettings(values) - await mutate() form.reset() + await mutate() } catch (e) { toast(t("Error"), { description: t("Results.ErrorFetchingResource", { @@ -219,15 +219,15 @@ export default function SettingsPage() { {!config?.frontend_templates?.find( (t) => t.path === field.value, )?.is_official && ( -
-
- {t("CommunityThemeWarning")} +
+
+ {t("CommunityThemeWarning")} +
+
+ {t("CommunityThemeDescription")} +
-
- {t("CommunityThemeDescription")} -
-
- )} + )} )} />