From c5ec08893e13b6ac23c1a9db59c443076ff91491 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Tue, 3 Sep 2024 15:34:16 +0800 Subject: [PATCH] fix style --- .../src/components/base/collapse-input.tsx | 3 +- src/renderer/src/pages/dns.tsx | 86 +++++++++++-------- src/renderer/src/pages/sniffer.tsx | 57 +++++++----- 3 files changed, 84 insertions(+), 62 deletions(-) diff --git a/src/renderer/src/components/base/collapse-input.tsx b/src/renderer/src/components/base/collapse-input.tsx index 8662674..6651567 100644 --- a/src/renderer/src/components/base/collapse-input.tsx +++ b/src/renderer/src/components/base/collapse-input.tsx @@ -18,8 +18,7 @@ const CollapseInput: React.FC = (props) => { style={{ paddingInlineEnd: 0 }} classNames={{ inputWrapper: 'cursor-pointer bg-transparent p-0 data-[hover=true]:bg-content2', - input: 'w-0 focus:w-[150px] transition-all duration-200', - innerWrapper: 'cursor-pointer p-0' + input: 'w-0 focus:w-[150px] focus:ml-2 transition-all duration-200' }} endContent={
{ 'https://dns.alidns.com/dns-query' ] } = dns || {} - - const [values, setValues] = useState({ + const [changed, setChanged] = useState(false) + const [values, originSetValues] = useState({ ipv6, useHosts, enhancedMode, @@ -53,6 +53,11 @@ const DNS: React.FC = () => { hosts: Object.entries(hosts || {}).map(([domain, value]) => ({ domain, value })) }) + const setValues = (v: typeof values): void => { + originSetValues(v) + setChanged(true) + } + const handleListChange = (type: string, value: string, index: number): void => { const list = [...values[type]] if (value.trim()) { @@ -112,48 +117,55 @@ const DNS: React.FC = () => { ), useNameserverPolicy: values.useNameserverPolicy }) - await patchControledMihomoConfig(patch) - await restartCore() + try { + setChanged(false) + await patchControledMihomoConfig(patch) + await restartCore() + } catch (e) { + alert(e) + } } return ( { - const hostsObject = Object.fromEntries( - values.hosts.map(({ domain, value }) => [domain, value]) - ) - const dnsConfig = { - ipv6: values.ipv6, - 'fake-ip-range': values.fakeIPRange, - 'fake-ip-filter': values.fakeIPFilter, - 'enhanced-mode': values.enhancedMode, - 'use-hosts': values.useHosts, - 'use-system-hosts': values.useSystemHosts, - 'respect-rules': values.respectRules, - nameserver: values.nameserver, - 'proxy-server-nameserver': values.proxyServerNameserver, - fallback: [], - 'fallback-filter': {} - } - if (values.useNameserverPolicy) { - dnsConfig['nameserver-policy'] = Object.fromEntries( - values.nameserverPolicy.map(({ domain, value }) => [domain, value]) + changed && ( + + const dnsConfig = { + ipv6: values.ipv6, + 'fake-ip-range': values.fakeIPRange, + 'fake-ip-filter': values.fakeIPFilter, + 'enhanced-mode': values.enhancedMode, + 'use-hosts': values.useHosts, + 'use-system-hosts': values.useSystemHosts, + 'respect-rules': values.respectRules, + nameserver: values.nameserver, + 'proxy-server-nameserver': values.proxyServerNameserver, + fallback: [], + 'fallback-filter': {} + } + if (values.useNameserverPolicy) { + dnsConfig['nameserver-policy'] = Object.fromEntries( + values.nameserverPolicy.map(({ domain, value }) => [domain, value]) + ) + } + onSave({ + dns: dnsConfig, + hosts: hostsObject + }) + }} + > + 保存 + + ) } > diff --git a/src/renderer/src/pages/sniffer.tsx b/src/renderer/src/pages/sniffer.tsx index adfe8f4..7fc8335 100644 --- a/src/renderer/src/pages/sniffer.tsx +++ b/src/renderer/src/pages/sniffer.tsx @@ -22,8 +22,8 @@ const Sniffer: React.FC = () => { 'skip-domain': skipDomain = ['+.push.apple.com'], 'force-domain': forceDomain = [] } = sniffer || {} - - const [values, setValues] = useState({ + const [changed, setChanged] = useState(false) + const [values, originSetValues] = useState({ parsePureIP, forceDNSMapping, overrideDestination, @@ -31,10 +31,19 @@ const Sniffer: React.FC = () => { skipDomain, forceDomain }) + const setValues = (v: typeof values): void => { + originSetValues(v) + setChanged(true) + } const onSave = async (patch: Partial): Promise => { - await patchControledMihomoConfig(patch) - await restartCore() + try { + setChanged(false) + await patchControledMihomoConfig(patch) + await restartCore() + } catch (e) { + alert(e) + } } const handleSniffPortChange = (protocol: keyof typeof sniff, value: string): void => { @@ -69,25 +78,27 @@ const Sniffer: React.FC = () => { - onSave({ - sniffer: { - 'parse-pure-ip': values.parsePureIP, - 'force-dns-mapping': values.forceDNSMapping, - 'override-destination': values.overrideDestination, - sniff: values.sniff, - 'skip-domain': values.skipDomain, - 'force-domain': values.forceDomain - } - }) - } - > - 保存 - + changed && ( + + ) } >