fix: set proxy both from proxies and provider

This commit is contained in:
Zephyruso 2023-09-01 12:10:34 +08:00
parent fb7f07d962
commit 2549bfe89c
2 changed files with 43 additions and 21 deletions

View File

@ -8,12 +8,10 @@ export default (props: {
onClick?: () => void
}) => {
const { proxyName, isSelected, onClick } = props
const { delayMap, proxyNodeMap } = useProxies()
const { proxyNodeMap } = useProxies()
const proxyNode = createMemo(() => proxyNodeMap()[proxyName])
const Delay = (proxyname: string) => {
const delay = delayMap()[proxyname]
const Delay = (delay: number | undefined) => {
if (typeof delay !== 'number' || delay === 0) {
return ''
}
@ -29,7 +27,7 @@ export default (props: {
return <span class={textClassName}>{delay}ms</span>
}
const formatProxyType = (type: string) => {
const formatProxyType = (type = '') => {
const t = type.toLowerCase()
if (t.includes('shadowsocks')) {
@ -57,10 +55,10 @@ export default (props: {
isSelected && 'text-primary',
)}
>
{formatProxyType(proxyNode().type)}
{proxyNode().udp && ' :: udp'}
{formatProxyType(proxyNode()?.type)}
{proxyNode()?.udp && ' :: udp'}
</div>
<div class="text-xs">{Delay(proxyName)}</div>
<div class="text-xs">{Delay(proxyNode()?.delay)}</div>
</div>
</div>
)

View File

@ -1,22 +1,45 @@
import { createSignal } from 'solid-js'
import { useRequest } from '~/signals'
import type { Proxy, ProxyProvider } from '~/types'
import type { Proxy, ProxyNode, ProxyProvider } from '~/types'
type ProxyInfo = {
name: string
udp: boolean
type: string
delay?: number
}
// these signals should be global state
const [proxies, setProxies] = createSignal<Proxy[]>([])
const [proxyProviders, setProxyProviders] = createSignal<ProxyProvider[]>([])
const [delayMap, setDelayMap] = createSignal<Record<string, number>>({})
const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, Proxy>>({})
const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, ProxyInfo>>(
{},
)
export function useProxies() {
const request = useRequest()
const setProxyInfoByProixes = (proxies: Proxy[] | ProxyNode[]) => {
proxies.forEach((proxy) => {
setProxyNodeMap({
...proxyNodeMap(),
[proxy.name]: {
udp: proxy.udp,
type: proxy.type,
delay: proxy.history.at(-1)?.delay ?? 0,
name: proxy.name,
},
})
})
}
const updateProxy = async () => {
const { providers } = await request
.get('providers/proxies')
.json<{ providers: Record<string, ProxyProvider> }>()
Object.values(providers).forEach((provider) => {
setProxyInfoByProixes(provider.proxies)
})
setProxyProviders(
Object.values(providers).filter(
(provider) =>
@ -28,14 +51,8 @@ export function useProxies() {
.get('proxies')
.json<{ proxies: Record<string, Proxy> }>()
const sortIndex = [...(proxies['GLOBAL'].all ?? []), 'GLOBAL']
const delay = delayMap()
Object.values(proxies).forEach((proxy) => {
setProxyNodeMap({ ...proxyNodeMap(), [proxy.name]: proxy })
delay[proxy.name] = proxy.history.at(-1)?.delay ?? 0
})
setDelayMap(delay)
setProxyInfoByProixes(Object.values(proxies))
setProxies(
Object.values(proxies)
.filter((proxy) => proxy.all?.length)
@ -62,7 +79,15 @@ export function useProxies() {
)
.json()
setDelayMap({ ...delayMap(), ...data })
Object.entries(data).forEach(([name, delay]) => {
setProxyNodeMap({
...proxyNodeMap(),
[name]: {
...proxyNodeMap()[name],
delay: delay,
},
})
})
}
const updateProviderByProviderName = async (proxyProviderName: string) => {
@ -82,7 +107,6 @@ export function useProxies() {
proxyProviders,
delayTestByProxyGroupName,
proxyNodeMap,
delayMap,
updateProxy,
setProxyGroupByProxyName,
updateProviderByProviderName,