chore: refactor

This commit is contained in:
naiba
2024-12-15 20:26:35 +08:00
parent 3ec7e7a784
commit 855b5d4cc8
3 changed files with 29 additions and 55 deletions

View File

@@ -22,15 +22,15 @@ enum OSTypes {
export const InstallCommandsMenu = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => { export const InstallCommandsMenu = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
const [copy, setCopy] = useState(false) const [copy, setCopy] = useState(false)
const settings = useSettings() const {data: settings} = useSettings()
const { t } = useTranslation() const { t } = useTranslation()
const switchState = async (type: number) => { const switchState = async (type: number) => {
if (!copy) { if (!copy) {
try { try {
setCopy(true) setCopy(true)
if (!settings.data) throw new Error("Settings is not found.") if (!settings) throw new Error("Settings is not found.")
await copyToClipboard(generateCommand(type, settings.data) || "") await copyToClipboard(generateCommand(type, settings) || "")
} catch (e: Error | any) { } catch (e: Error | any) {
console.error(e) console.error(e)
toast(t("Error"), { toast(t("Error"), {

View File

@@ -16,37 +16,11 @@ export default function Root() {
}, [settingData]) }, [settingData])
const InjectContext = useCallback((content: string) => { const InjectContext = useCallback((content: string) => {
document.getElementById("nezha-custom-code")?.remove()
const tempDiv = document.createElement("div") const tempDiv = document.createElement("div")
tempDiv.id = "nezha-custom-code"
tempDiv.innerHTML = content tempDiv.innerHTML = content
document.body.appendChild(tempDiv)
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 || ""))
}
})
}, []) }, [])
if (error) { if (error) {

View File

@@ -57,21 +57,21 @@ export default function SettingsPage() {
resolver: zodResolver(settingFormSchema), resolver: zodResolver(settingFormSchema),
defaultValues: config defaultValues: config
? { ? {
...config, ...config,
language: config.language, language: config.language,
site_name: config.site_name || "", site_name: config.site_name || "",
user_template: user_template:
config.user_template || config.user_template ||
Object.keys(config.frontend_templates.filter((t) => !t.is_admin) || {})[0] || Object.keys(config.frontend_templates.filter((t) => !t.is_admin) || {})[0] ||
"user-dist", "user-dist",
} }
: { : {
ip_change_notification_group_id: 0, ip_change_notification_group_id: 0,
cover: 1, cover: 1,
site_name: "", site_name: "",
language: "", language: "",
user_template: "user-dist", user_template: "user-dist",
}, },
resetOptions: { resetOptions: {
keepDefaultValues: false, keepDefaultValues: false,
}, },
@@ -86,8 +86,8 @@ export default function SettingsPage() {
const onSubmit = async (values: z.infer<typeof settingFormSchema>) => { const onSubmit = async (values: z.infer<typeof settingFormSchema>) => {
try { try {
await updateSettings(values) await updateSettings(values)
await mutate()
form.reset() form.reset()
await mutate()
} catch (e) { } catch (e) {
toast(t("Error"), { toast(t("Error"), {
description: t("Results.ErrorFetchingResource", { description: t("Results.ErrorFetchingResource", {
@@ -219,15 +219,15 @@ export default function SettingsPage() {
{!config?.frontend_templates?.find( {!config?.frontend_templates?.find(
(t) => t.path === field.value, (t) => t.path === field.value,
)?.is_official && ( )?.is_official && (
<div className="mt-2 text-sm text-yellow-700 dark:text-yellow-200 bg-yellow-100 dark:bg-yellow-900 border border-yellow-200 dark:border-yellow-700 rounded-md p-2"> <div className="mt-2 text-sm text-yellow-700 dark:text-yellow-200 bg-yellow-100 dark:bg-yellow-900 border border-yellow-200 dark:border-yellow-700 rounded-md p-2">
<div className="font-medium text-lg mb-1"> <div className="font-medium text-lg mb-1">
{t("CommunityThemeWarning")} {t("CommunityThemeWarning")}
</div>
<div className="text-yellow-700 dark:text-yellow-200">
{t("CommunityThemeDescription")}
</div>
</div> </div>
<div className="text-yellow-700 dark:text-yellow-200"> )}
{t("CommunityThemeDescription")}
</div>
</div>
)}
</FormItem> </FormItem>
)} )}
/> />