peer selector improvements

This commit is contained in:
mrfry 2023-04-10 10:59:30 +02:00
parent b46180c25a
commit d9e5c8bdb0

73
user.js
View file

@ -1749,7 +1749,9 @@
clearAllMessages()
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = 'none'
updatePeerSelector(elem)
})
GetXHRInfos()
.then((inf) => {
try {
@ -1789,15 +1791,7 @@
})
if (skipAvailablePeerFind || !getVal('lastp2pinfo')) {
SafeGetElementById('retryContainer', (elem) => {
elem.style.display = 'flex'
})
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = ''
})
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
connectionErrorAction()
} else {
tryAnotherPeer()
}
@ -1855,33 +1849,11 @@
)
connectToPeer(suitablePeer)
} else {
SafeGetElementById('retryContainer', (elem) => {
elem.style.display = 'flex'
})
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = ''
})
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = texts.noPeersOnline
})
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
connectionErrorAction(texts.noPeersOnline)
debugLog('None of the peers are online!')
}
} catch (e) {
SafeGetElementById('retryContainer', (elem) => {
elem.style.display = 'flex'
})
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = ''
})
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = texts.peerTryingError
})
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
connectionErrorAction(texts.peerTryingError)
warn('Error ocurred during trying to connect to peers!')
warn(e)
}
@ -1909,6 +1881,21 @@
ConnectToServer()
}
function connectionErrorAction(infoText) {
SafeGetElementById('infoMainDiv', (elem) => {
elem.innerText = infoText || texts.noServer
})
SafeGetElementById('retryContainer', (elem) => {
elem.style.display = 'flex'
})
SafeGetElementById('peerSelector', (elem) => {
elem.style.display = ''
})
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = 'red'
})
}
function NoUserAction() {
SafeGetElementById('scriptMenuDiv', (elem) => {
elem.style.backgroundColor = '#44cc00'
@ -2817,13 +2804,7 @@
resetMenu()
})
try {
const selectedPeer = JSON.parse(getVal('peerToUse'))
const selectedIndex = peers.findIndex((x) => {
return getPeerUrl(x) === getPeerUrl(selectedPeer)
})
peerSelector.value = selectedIndex
} catch (e) {}
updatePeerSelector(peerSelector)
return peerSelector
} catch (e) {
@ -2904,6 +2885,18 @@
}
}
function updatePeerSelector(selector) {
try {
const peers = [originalServer, ...JSON.parse(getVal('lastp2pinfo'))]
const selectedPeer = JSON.parse(getVal('peerToUse'))
const selectedIndex = peers.findIndex((x) => {
return getPeerUrl(x) === getPeerUrl(selectedPeer)
})
selector.value = selectedIndex
} catch (e) {}
}
// : }}}
// : Generic utils {{{