metacubexd/src/pages/Proxies.tsx

108 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-08-24 04:20:53 +08:00
import { For, createSignal, onMount } from 'solid-js'
2023-08-29 12:28:52 +08:00
import { twMerge } from 'tailwind-merge'
2023-08-24 04:20:53 +08:00
import { useRequest } from '~/signals'
import type { Proxy, ProxyProvider } from '~/types'
2023-08-24 04:20:53 +08:00
export const Proxies = () => {
const request = useRequest()
const [proxies, setProxies] = createSignal<Proxy[]>([])
2023-08-29 12:28:52 +08:00
const [delayMap, setDelayMap] = createSignal<Record<string, number>>({})
2023-08-24 04:20:53 +08:00
const [proxyProviders, setProxyProviders] = createSignal<ProxyProvider[]>([])
2023-08-29 12:28:52 +08:00
const renderDelay = (proxyname: string) => {
const delay = delayMap()[proxyname]
2023-08-24 04:20:53 +08:00
2023-08-29 12:28:52 +08:00
if (typeof delay !== 'number' || delay === 0) {
return ''
}
return <span>{delay}ms</span>
}
2023-08-24 04:20:53 +08:00
2023-08-29 12:28:52 +08:00
onMount(async () => {
2023-08-24 04:20:53 +08:00
const { providers } = await request
.get('providers/proxies')
.json<{ providers: Record<string, ProxyProvider> }>()
2023-08-29 12:28:52 +08:00
const delay = delayMap()
Object.values(providers).forEach((provider) => {
provider.proxies.forEach((proxy) => {
delay[proxy.name] = proxy.history[proxy.history.length - 1]?.delay
})
})
2023-08-24 04:20:53 +08:00
2023-08-29 12:28:52 +08:00
setDelayMap(delay)
2023-08-24 04:20:53 +08:00
setProxyProviders(Object.values(providers))
2023-08-29 12:28:52 +08:00
const { proxies } = await request
.get('proxies')
.json<{ proxies: Record<string, Proxy> }>()
setProxies(Object.values(proxies))
2023-08-24 04:20:53 +08:00
})
return (
2023-08-28 00:51:10 +08:00
<div class="flex flex-col gap-4">
2023-08-24 04:20:53 +08:00
<div>
2023-08-28 00:51:10 +08:00
<h1 class="pb-4 text-lg font-semibold">Proxies</h1>
2023-08-24 04:20:53 +08:00
2023-08-29 12:28:52 +08:00
<div class="grid grid-cols-1 gap-2 sm:grid-cols-1">
2023-08-24 04:20:53 +08:00
<For each={proxies()}>
{(proxy) => (
2023-08-29 12:28:52 +08:00
<div class="collapse collapse-arrow border-secondary bg-base-200 p-4">
<input type="checkbox" />
<div class="collapse-title text-xl font-medium">
{proxy.name} {proxy.type}
</div>
<div class="collapse-content grid grid-cols-1 gap-2 sm:grid-cols-3 lg:grid-cols-5">
<For each={proxy.all}>
{(proxyPoint) => (
<div
class={twMerge(
proxy.now === proxyPoint
? 'border-primary bg-success-content text-success'
: 'border-secondary',
'card card-bordered card-compact m-1 flex-row justify-between p-4',
)}
>
{proxyPoint} {renderDelay(proxyPoint)}
</div>
)}
</For>
</div>
2023-08-24 04:20:53 +08:00
</div>
)}
</For>
</div>
</div>
<div>
2023-08-28 00:51:10 +08:00
<h1 class="pb-4 text-lg font-semibold">Proxy Providers</h1>
2023-08-24 04:20:53 +08:00
2023-08-29 12:28:52 +08:00
<div class="grid grid-cols-1 gap-2 sm:grid-cols-1">
2023-08-24 04:20:53 +08:00
<For each={proxyProviders()}>
{(proxy) => (
2023-08-29 12:28:52 +08:00
<div class="collapse-arrow collapse border-secondary bg-base-200 p-4">
<input type="checkbox" />
<div class="collapse-title text-xl font-medium">
{proxy.name}
</div>
<div class="collapse-content grid grid grid-cols-1 gap-2 sm:grid-cols-3 lg:grid-cols-5">
<For each={proxy.proxies}>
{(proxyPoint) => (
<div
class={`card card-bordered card-compact m-1 flex-row justify-between border-secondary p-4`}
>
{proxyPoint.name} {renderDelay(proxyPoint.name)}
</div>
)}
</For>
</div>
2023-08-24 04:20:53 +08:00
</div>
)}
</For>
</div>
</div>
</div>
)
}