From 7a5bcf67c57b9677c135e322b9aa467a7aa32b7d Mon Sep 17 00:00:00 2001 From: Tunglies Date: Sat, 24 May 2025 04:21:45 +0800 Subject: [PATCH] refactor(ControllerViewer): replace manual core restart logic with service call to restartCore --- .../setting/mods/controller-viewer.tsx | 48 ++++--------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/src/components/setting/mods/controller-viewer.tsx b/src/components/setting/mods/controller-viewer.tsx index 8b8f5495..7973ea4e 100644 --- a/src/components/setting/mods/controller-viewer.tsx +++ b/src/components/setting/mods/controller-viewer.tsx @@ -1,5 +1,7 @@ import { BaseDialog, DialogRef } from "@/components/base"; import { useClashInfo } from "@/hooks/use-clash"; +import { useVerge } from "@/hooks/use-verge"; +import { patchClashConfig, restartCore } from "@/services/cmds"; import { showNotice } from "@/services/noticeService"; import { ContentCopy, @@ -53,49 +55,12 @@ export const ControllerViewer = forwardRef((props, ref) => { const [copySuccess, setCopySuccess] = useState(null); const [isSaving, setIsSaving] = useState(false); - const [isRestarting, setIsRestarting] = useState(false); const { clashInfo, patchInfo } = useClashInfo(); const [controller, setController] = useState(clashInfo?.server || ""); const [secret, setSecret] = useState(clashInfo?.secret || ""); - // 直接通过API重启内核 - const restartCoreDirectly = useLockFn(async () => { - try { - const controllerUrl = controller || clashInfo?.server || 'http://localhost:9090'; - - const headers: Record = { - 'Content-Type': 'application/json', - }; - - if (secret) { - headers['Authorization'] = `Bearer ${secret}`; - } - - const response = await fetch(`${controllerUrl}/restart`, { - method: 'POST', - headers, - }); - - if (!response.ok) { - const errorText = await response.text(); - throw new Error(errorText || 'Failed to restart core'); - } - - const contentType = response.headers.get('content-type'); - if (contentType && contentType.includes('application/json')) { - return await response.json(); - } else { - const text = await response.text(); - console.log('Non-JSON response:', text); - return { message: 'Restart request sent successfully' }; - } - } catch (err: any) { - console.error('Error restarting core:', err); - throw err; - } - }); // 生成随机配置并重启内核 const generateAndRestart = useLockFn(async () => { @@ -157,9 +122,14 @@ export const ControllerViewer = forwardRef((props, ref) => { try { setIsSaving(true); - await patchInfo({ "external-controller": controller, secret }); + await patchInfo({ + "external-controller": controller, + secret, + }); - showNotice('success', t("Configuration saved successfully"), 2000); + await restartCore(); + + showNotice('success', t("Configuration saved and core restarted successfully"), 2000); setOpen(false); } catch (err: any) { showNotice('error', err.message || t("Failed to save configuration"), 4000);