Skip to content

fix: minor fixes for ProxyConfiguration #5203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Button from '../../Button'

import styles from './styles.module.scss'
import { Proxy } from '../../Settings/DefaultCredentials'
import { debounceWithInit } from '../../chat/ChatListHelpers'
import { debounce } from 'debounce'

import { getLogger } from '@deltachat-desktop/shared/logger'
import { unknownErrorToString } from '../../helpers/unknownErrorToString'
Expand Down Expand Up @@ -57,7 +57,6 @@ export default function ProxyConfiguration(
const [newProxyUrl, setNewProxyUrl] = useState('')

const [showNewProxyForm, setShowNewProxyForm] = useState(false)
const [showEnableSwitch, setShowEnableSwitch] = useState(false)

// updated on connectivity change
const [connectivityStatus, setConnectivityStatus] = useState(
Expand Down Expand Up @@ -209,37 +208,33 @@ export default function ProxyConfiguration(
[proxyState.activeProxy, proxyState.proxies, updateProxyState]
)

// show/hide the enable switch
useEffect(() => {
if (proxyState.enabled) {
setShowEnableSwitch(proxyState.proxies.length > 0)
} else {
setShowEnableSwitch(proxyState.proxies.length > 0)
let outdated = false

if (!configured) {
return
}
}, [showEnableSwitch, proxyState.enabled, proxyState.proxies])

useEffect(() => {
let removeConnectivityListener = () => {}
const checkConnectivity = async () => {
if (configured) {
const connectivity = await BackendRemote.rpc.getConnectivity(accountId)
setConnectivityStatus(connectivity)
removeConnectivityListener = onDCEvent(
accountId,
'ConnectivityChanged',
() =>
debounceWithInit(async () => {
const connectivity =
await BackendRemote.rpc.getConnectivity(accountId)
setConnectivityStatus(connectivity)
}, 300)()
)
const update = async () => {
const connectivity = await BackendRemote.rpc.getConnectivity(accountId)

if (outdated) {
return
}

setConnectivityStatus(connectivity)
}
checkConnectivity()
return () => {
removeConnectivityListener()
}

const debouncedUpdate = debounce(update, 300)
debouncedUpdate()
debouncedUpdate.flush()

const cleanup = [
onDCEvent(accountId, 'ConnectivityChanged', debouncedUpdate),
() => debouncedUpdate.clear(),
() => (outdated = true),
]
return () => cleanup.forEach(off => off())
}, [accountId, configured])

/**
Expand Down Expand Up @@ -344,7 +339,7 @@ export default function ProxyConfiguration(
/>
<DialogBody className={styles.proxyDialogBody}>
<div className={styles.container}>
{showEnableSwitch && (
{proxyState.proxies.length > 0 && (
<SettingsSwitch
label={tx('proxy_use_proxy')}
value={proxyState.enabled}
Expand Down
Loading