metacubexd/src/pages/Proxies.tsx

117 lines
3.3 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-05 15:38:42 +08:00
import { Show } from 'solid-js'
import { twMerge } from 'tailwind-merge'
import {
Button,
Collapse,
ForTwoColumns,
ProxyCardGroups,
ProxyNodePreview,
} from '~/components'
2023-09-05 15:38:42 +08:00
import { sortProxiesByOrderingType, useStringBooleanMap } from '~/helpers'
import {
proxiesOrderingType,
renderProxiesInSamePage,
useProxies,
} from '~/signals'
2023-08-29 20:20:01 +08:00
import type { Proxy } from '~/types'
import ProxyProvider from './ProxyProvider'
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
2023-09-05 15:38:42 +08:00
const { map: collapsedMap, set: setCollapsedMap } = useStringBooleanMap()
const { map: speedTestingMap, setWithCallback: setSpeedTestingMap } =
useStringBooleanMap()
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
2023-09-05 15:38:42 +08:00
const onSpeedTestClick = async (e: MouseEvent, name: string) => {
e.stopPropagation()
setSpeedTestingMap(
name,
async () => await latencyTestByProxyGroupName(name),
)
2023-08-30 11:01:19 +08:00
}
2023-08-24 04:20:53 +08:00
return (
<>
<div class="flex flex-col gap-2">
<h1 class="flex h-8 items-center pb-2 text-lg font-semibold">
{t('proxies')}
</h1>
<ForTwoColumns
subChild={proxies().map((proxy) => {
const sortedProxyNames = sortProxiesByOrderingType(
proxy.all ?? [],
latencyMap(),
proxiesOrderingType(),
)
2023-09-03 06:08:53 +08:00
const title = (
<>
2023-09-05 16:13:11 +08:00
<div class="mr-8 flex items-center justify-between">
<span>{proxy.name}</span>
<Button
class="btn-circle btn-sm"
onClick={(e) => onSpeedTestClick(e, proxy.name)}
>
2023-09-05 15:38:42 +08:00
<IconBrandSpeedtest
class={twMerge(
speedTestingMap()[proxy.name] && 'animate-pulse',
)}
/>
</Button>
</div>
<div class="text-sm text-slate-500">
{proxy.type} {proxy.now?.length > 0 && ` :: ${proxy.now}`}
</div>
<Show when={!collapsedMap()[`group-${proxy.name}`]}>
<ProxyNodePreview
proxyNameList={sortedProxyNames}
now={proxy.now}
/>
</Show>
</>
)
2023-09-02 16:58:22 +08:00
const content = (
<ProxyCardGroups
proxyNames={sortedProxyNames}
now={proxy.now}
onClick={(name) => {
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) =>
2023-09-05 15:38:42 +08:00
setCollapsedMap(`group-${proxy.name}`, val)
}
/>
)
})}
/>
</div>
<Show when={renderProxiesInSamePage()}>
<div class="divider"></div>
<ProxyProvider />
</Show>
</>
2023-08-24 04:20:53 +08:00
)
}