mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-03 12:20:05 +08:00
refactor(react): replace avoidable effects with derived state and handlers
Also drops a duplicate WebDAV history request on open and the per-keystroke DNS YAML dump.
This commit is contained in:
@@ -21,6 +21,7 @@ import {
|
||||
memo,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useEffectEvent,
|
||||
useReducer,
|
||||
useRef,
|
||||
useState,
|
||||
@@ -113,7 +114,6 @@ const ProfileItemBase = (props: ProfileItemProps) => {
|
||||
const setProfileLoading = useSetProfileLoading()
|
||||
|
||||
const [showNextUpdate, setShowNextUpdate] = useState(false)
|
||||
const showNextUpdateRef = useRef(false)
|
||||
const [nextUpdateTime, setNextUpdateTime] = useState('')
|
||||
const refreshTimeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(
|
||||
undefined,
|
||||
@@ -203,10 +203,6 @@ const ProfileItemBase = (props: ProfileItemProps) => {
|
||||
setShowNextUpdate(!showNextUpdate)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
showNextUpdateRef.current = showNextUpdate
|
||||
}, [showNextUpdate])
|
||||
|
||||
useEffect(() => {
|
||||
if (showNextUpdate) {
|
||||
fetchNextUpdateTime()
|
||||
@@ -218,8 +214,10 @@ const ProfileItemBase = (props: ProfileItemProps) => {
|
||||
updated,
|
||||
])
|
||||
|
||||
const isNextUpdateShown = useEffectEvent(() => showNextUpdate)
|
||||
|
||||
useEffect(() => {
|
||||
if (timerUpdateRevision === 0 || !showNextUpdateRef.current) return
|
||||
if (timerUpdateRevision === 0 || !isNextUpdateShown()) return
|
||||
|
||||
if (refreshTimeoutRef.current !== undefined) {
|
||||
clearTimeout(refreshTimeoutRef.current)
|
||||
@@ -236,7 +234,7 @@ const ProfileItemBase = (props: ProfileItemProps) => {
|
||||
}, [fetchNextUpdateTime, timerUpdateRevision])
|
||||
|
||||
useEffect(() => {
|
||||
if (completedUpdateRevision === 0 || !showNextUpdateRef.current) return
|
||||
if (completedUpdateRevision === 0 || !isNextUpdateShown()) return
|
||||
fetchNextUpdateTime()
|
||||
}, [completedUpdateRevision, fetchNextUpdateTime])
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from '@mui/material'
|
||||
import { useLockFn } from 'ahooks'
|
||||
import type { Ref } from 'react'
|
||||
import { useEffect, useImperativeHandle, useRef, useState } from 'react'
|
||||
import { useImperativeHandle, useRef, useState } from 'react'
|
||||
import { Controller, useForm } from 'react-hook-form'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
@@ -77,17 +77,6 @@ export function ProfileViewer({ onChange, ref }: ProfileViewerProps) {
|
||||
},
|
||||
}))
|
||||
|
||||
const selfProxy = watch('option.self_proxy')
|
||||
const withProxy = watch('option.with_proxy')
|
||||
|
||||
useEffect(() => {
|
||||
if (selfProxy) setValue('option.with_proxy', false)
|
||||
}, [selfProxy, setValue])
|
||||
|
||||
useEffect(() => {
|
||||
if (withProxy) setValue('option.self_proxy', false)
|
||||
}, [setValue, withProxy])
|
||||
|
||||
const handleOk = useLockFn(
|
||||
handleSubmit(async (form) => {
|
||||
setLoading(true)
|
||||
@@ -419,7 +408,15 @@ export function ProfileViewer({ onChange, ref }: ProfileViewerProps) {
|
||||
<InputLabel>
|
||||
{t('profiles.modals.profileForm.fields.useSystemProxy')}
|
||||
</InputLabel>
|
||||
<Switch checked={field.value!} {...field} color="primary" />
|
||||
<Switch
|
||||
checked={field.value!}
|
||||
{...field}
|
||||
onChange={(_, checked) => {
|
||||
field.onChange(checked)
|
||||
if (checked) setValue('option.self_proxy', false)
|
||||
}}
|
||||
color="primary"
|
||||
/>
|
||||
</StyledBox>
|
||||
)}
|
||||
/>
|
||||
@@ -432,7 +429,15 @@ export function ProfileViewer({ onChange, ref }: ProfileViewerProps) {
|
||||
<InputLabel>
|
||||
{t('profiles.modals.profileForm.fields.useClashProxy')}
|
||||
</InputLabel>
|
||||
<Switch checked={field.value!} {...field} color="primary" />
|
||||
<Switch
|
||||
checked={field.value!}
|
||||
{...field}
|
||||
onChange={(_, checked) => {
|
||||
field.onChange(checked)
|
||||
if (checked) setValue('option.with_proxy', false)
|
||||
}}
|
||||
color="primary"
|
||||
/>
|
||||
</StyledBox>
|
||||
)}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user