diff --git a/src/pages/Overview.tsx b/src/pages/Overview.tsx index b166f151..18e128ca 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 0977f385..6bed76ca 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,26 +26,23 @@ export default () => { } onMount(async () => { - const { providers } = await request - .get('providers/proxies') - .json<{ providers: Record }>() - const delay = delayMap() + await updateProxy() + }) - Object.values(providers).forEach((provider) => { - provider.proxies.forEach((proxy) => { - delay[proxy.name] = proxy.history[proxy.history.length - 1]?.delay - }) - }) + const onProxyNodeClick = async (proxy: Proxy, proxyName: string) => { + setProxiesByProxyName(proxy, proxyName) + } - setDelayMap(delay) - setProxyProviders(Object.values(providers)) + const onCollapseTitleClick = (name: string) => { + const cMap = collapseMap() - const { proxies } = await request - .get('proxies') - .json<{ proxies: Record }>() + cMap[name] = !cMap[name] + setCollapseMap({ ...cMap }) + } - setProxies(Object.values(proxies)) - }) + 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 00000000..5ccc2e9c --- /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, + } +}