metacubexd/src/pages/Proxies.tsx

107 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-09-02 00:09:41 +08:00
import { useI18n } from '@solid-primitives/i18n'
2023-09-02 16:58:22 +08:00
import { IconBrandSpeedtest } from '@tabler/icons-solidjs'
2023-09-03 20:23:02 +08:00
import { createSignal, Show } from 'solid-js'
import {
Button,
Collapse,
ForTwoColumns,
ProxyCardGroups,
ProxyNodePreview,
} from '~/components'
import {
handlerBtnClickWithAnimate,
sortProxiesByOrderingType,
} from '~/helpers'
import { proxiesOrderingType, useProxies } from '~/signals'
2023-08-29 20:20:01 +08:00
import type { Proxy } from '~/types'
2023-08-24 04:20:53 +08:00
export default () => {
2023-09-02 00:09:41 +08:00
const [t] = useI18n()
const {
proxies,
setProxyGroupByProxyName,
2023-09-03 05:40:39 +08:00
latencyTestByProxyGroupName,
latencyMap,
} = useProxies()
2023-08-30 23:02:55 +08:00
const [collapsedMap, setCollapsedMap] = createSignal<Record<string, boolean>>(
2023-08-29 20:20:01 +08:00
{},
)
2023-08-24 04:20:53 +08:00
2023-08-29 20:20:01 +08:00
const onProxyNodeClick = async (proxy: Proxy, proxyName: string) => {
2023-09-03 20:23:02 +08:00
void setProxyGroupByProxyName(proxy, proxyName)
2023-08-29 20:20:01 +08:00
}
2023-08-24 04:20:53 +08:00
const onSpeedTestClick = (e: MouseEvent, name: string) => {
handlerBtnClickWithAnimate(
e,
latencyTestByProxyGroupName.bind(null, name),
'animate-pulse',
)
2023-08-30 11:01:19 +08:00
}
2023-08-24 04:20:53 +08:00
return (
2023-09-02 00:15:19 +08:00
<div class="flex flex-col gap-2">
2023-09-02 17:42:13 +08:00
<h1 class="flex h-8 items-center pb-2 text-lg font-semibold">
{t('proxies')}
</h1>
2023-09-02 21:00:20 +08:00
<ForTwoColumns
2023-09-02 16:58:22 +08:00
subChild={proxies().map((proxy) => {
2023-09-03 06:08:53 +08:00
const sortedProxyNames = sortProxiesByOrderingType(
proxy.all ?? [],
latencyMap(),
proxiesOrderingType(),
)
2023-09-02 16:58:22 +08:00
const title = (
<>
<div class="mr-10 flex items-center justify-between">
<span>{proxy.name}</span>
<Button
class="btn-circle btn-sm"
2023-09-02 16:58:22 +08:00
onClick={(e) => onSpeedTestClick(e, proxy.name)}
>
<IconBrandSpeedtest />
</Button>
2023-09-02 16:58:22 +08:00
</div>
<div class="text-sm text-slate-500">
{proxy.type} :: {proxy.now}
</div>
<Show when={!collapsedMap()[`group-${proxy.name}`]}>
<ProxyNodePreview
2023-09-03 06:08:53 +08:00
proxyNameList={sortedProxyNames}
2023-09-02 16:02:32 +08:00
now={proxy.now}
2023-09-02 00:15:19 +08:00
/>
2023-09-02 16:58:22 +08:00
</Show>
</>
)
const content = (
<ProxyCardGroups
2023-09-03 06:08:53 +08:00
proxyNames={sortedProxyNames}
2023-09-02 16:58:22 +08:00
now={proxy.now}
onClick={(name) => {
2023-09-03 20:23:02 +08:00
void onProxyNodeClick(proxy, name)
2023-09-02 16:58:22 +08:00
}}
/>
)
return (
<Collapse
isOpen={collapsedMap()[`group-${proxy.name}`]}
title={title}
content={content}
onCollapse={(val) =>
setCollapsedMap({
...collapsedMap(),
[`group-${proxy.name}`]: val,
})
}
2023-09-02 16:02:32 +08:00
/>
2023-09-02 16:58:22 +08:00
)
})}
/>
2023-08-24 04:20:53 +08:00
</div>
)
}