From 6b0802023d3eeb41a76e7a3ee1087c5321a19100 Mon Sep 17 00:00:00 2001 From: Zephyruso <127948745+Zephyruso@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:20:01 +0800 Subject: [PATCH] feat: proxy selectable (#19) --- src/pages/Overview.tsx | 4 +- src/pages/Proxies.tsx | 92 +++++++++++++++++++++++++----------------- src/signals/proxies.ts | 58 ++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 39 deletions(-) create mode 100644 src/signals/proxies.ts diff --git a/src/pages/Overview.tsx b/src/pages/Overview.tsx index b166f15..18e128c 100644 --- a/src/pages/Overview.tsx +++ b/src/pages/Overview.tsx @@ -76,7 +76,7 @@ export default () => { onCleanup(() => clearInterval(preventLeakTimer)) const trafficWS = createReconnectingWS( - `${wsEndpointURL()}/traffic?token=${secret()}}`, + `${wsEndpointURL()}/traffic?token=${secret()}`, ) const trafficWSMessageEvent = createEventSignal<{ @@ -148,7 +148,7 @@ export default () => { ]) const connectionsWS = createReconnectingWS( - `${wsEndpointURL()}/connections?token=${secret()}}`, + `${wsEndpointURL()}/connections?token=${secret()}`, ) const connectionsWSMessageEvent = createEventSignal<{ diff --git a/src/pages/Proxies.tsx b/src/pages/Proxies.tsx index 0977f38..6bed76c 100644 --- a/src/pages/Proxies.tsx +++ b/src/pages/Proxies.tsx @@ -1,13 +1,19 @@ import { For, createSignal, onMount } from 'solid-js' import { twMerge } from 'tailwind-merge' -import { useRequest } from '~/signals' -import type { Proxy, ProxyProvider } from '~/types' +import { useProxies } from '~/signals/proxies' +import type { Proxy } from '~/types' export default () => { - const request = useRequest() - const [proxies, setProxies] = createSignal([]) - const [delayMap, setDelayMap] = createSignal>({}) - const [proxyProviders, setProxyProviders] = createSignal([]) + const { + proxies, + proxyProviders, + delayMap, + updateProxy, + setProxiesByProxyName, + } = useProxies() + const [collapseMap, setCollapseMap] = createSignal>( + {}, + ) const renderDelay = (proxyname: string) => { const delay = delayMap()[proxyname] @@ -20,27 +26,24 @@ export default () => { } onMount(async () => { - const { providers } = await request - .get('providers/proxies') - .json<{ providers: Record }>() - const delay = delayMap() - - Object.values(providers).forEach((provider) => { - provider.proxies.forEach((proxy) => { - delay[proxy.name] = proxy.history[proxy.history.length - 1]?.delay - }) - }) - - setDelayMap(delay) - setProxyProviders(Object.values(providers)) - - const { proxies } = await request - .get('proxies') - .json<{ proxies: Record }>() - - setProxies(Object.values(proxies)) + await updateProxy() }) + const onProxyNodeClick = async (proxy: Proxy, proxyName: string) => { + setProxiesByProxyName(proxy, proxyName) + } + + const onCollapseTitleClick = (name: string) => { + const cMap = collapseMap() + + cMap[name] = !cMap[name] + setCollapseMap({ ...cMap }) + } + + const getCollapseClassName = (name: string) => { + return collapseMap()[name] ? 'collapse-open' : 'collapse-close' + } + return (
@@ -49,23 +52,31 @@ export default () => {
{(proxy) => ( -
- -
+
+
onCollapseTitleClick(proxy.name)} + > {proxy.name} {proxy.type}
- {(proxyPoint) => ( + {(proxyName) => (
onProxyNodeClick(proxy, proxyName)} > - {proxyPoint} {renderDelay(proxyPoint)} + {proxyName} {renderDelay(proxyName)}
)}
@@ -82,16 +93,23 @@ export default () => {
{(proxy) => ( -
- -
+
+
onCollapseTitleClick(proxy.name)} + > {proxy.name}
- {(proxyPoint) => ( + {(proxyNode) => (
- {proxyPoint.name} {renderDelay(proxyPoint.name)} + {proxyNode.name} {renderDelay(proxyNode.name)}
)}
diff --git a/src/signals/proxies.ts b/src/signals/proxies.ts new file mode 100644 index 0000000..5ccc2e9 --- /dev/null +++ b/src/signals/proxies.ts @@ -0,0 +1,58 @@ +import { createSignal } from 'solid-js' +import { useRequest } from '~/signals' +import type { Proxy, ProxyProvider } from '~/types' + +export function useProxies() { + const request = useRequest() + const [proxies, setProxies] = createSignal([]) + const [proxyProviders, setProxyProviders] = createSignal([]) + const [delayMap, setDelayMap] = createSignal>({}) + + const updateProxy = async () => { + const { providers } = await request + .get('providers/proxies') + .json<{ providers: Record }>() + const delay = delayMap() + + Object.values(providers).forEach((provider) => { + provider.proxies.forEach((proxy) => { + delay[proxy.name] = proxy.history[proxy.history.length - 1]?.delay + }) + }) + + setDelayMap(delay) + setProxyProviders( + Object.values(providers).filter( + (provider) => + provider.name !== 'default' && provider.vehicleType !== 'Compatible', + ), + ) + + const { proxies } = await request + .get('proxies') + .json<{ proxies: Record }>() + + setProxies( + Object.values(proxies).filter( + (proxy) => proxy.all && proxy.all.length > 0, + ), + ) + } + + const setProxiesByProxyName = async (proxy: Proxy, proxyName: string) => { + await request.put(`proxies/${proxy.name}`, { + body: JSON.stringify({ + name: proxyName, + }), + }) + await updateProxy() + } + + return { + proxies, + proxyProviders, + delayMap, + updateProxy, + setProxiesByProxyName, + } +}