refactor: update proxies and btn click animate

This commit is contained in:
Zephyruso 2023-09-04 17:57:20 +08:00
parent 9d7f08c01b
commit a17be36d92
5 changed files with 77 additions and 93 deletions

View File

@ -5,14 +5,21 @@ export const formatTimeFromNow = (time: number | string) => {
return dayjs(time).fromNow() return dayjs(time).fromNow()
} }
export const getBtnElFromClickEvent = (event: MouseEvent) => { export const handlerBtnClickWithAnimate = async (
event: MouseEvent,
cb: () => void,
className = 'animate-spin',
) => {
let el = event.target as HTMLElement let el = event.target as HTMLElement
event.stopPropagation()
while (el && !el.classList.contains('btn')) { while (el && !el.classList.contains('btn')) {
el = el.parentElement! el = el.parentElement!
} }
return el el.classList.add(className)
await cb()
el.classList.remove(className)
} }
export const formatProxyType = (type = '') => { export const formatProxyType = (type = '') => {

View File

@ -8,7 +8,10 @@ import {
ProxyCardGroups, ProxyCardGroups,
ProxyNodePreview, ProxyNodePreview,
} from '~/components' } from '~/components'
import { getBtnElFromClickEvent, sortProxiesByOrderingType } from '~/helpers' import {
handlerBtnClickWithAnimate,
sortProxiesByOrderingType,
} from '~/helpers'
import { proxiesOrderingType, useProxies } from '~/signals' import { proxiesOrderingType, useProxies } from '~/signals'
import type { Proxy } from '~/types' import type { Proxy } from '~/types'
@ -29,13 +32,12 @@ export default () => {
void setProxyGroupByProxyName(proxy, proxyName) void setProxyGroupByProxyName(proxy, proxyName)
} }
const onSpeedTestClick = async (e: MouseEvent, name: string) => { const onSpeedTestClick = (e: MouseEvent, name: string) => {
const el = getBtnElFromClickEvent(e) handlerBtnClickWithAnimate(
e,
el.classList.add('animate-pulse') latencyTestByProxyGroupName.bind(null, name),
e.stopPropagation() 'animate-pulse',
await latencyTestByProxyGroupName(name) )
el.classList.remove('animate-pulse')
} }
return ( return (

View File

@ -11,7 +11,7 @@ import {
} from '~/components' } from '~/components'
import { import {
formatTimeFromNow, formatTimeFromNow,
getBtnElFromClickEvent, handlerBtnClickWithAnimate,
sortProxiesByOrderingType, sortProxiesByOrderingType,
} from '~/helpers' } from '~/helpers'
import { proxiesOrderingType, useProxies } from '~/signals' import { proxiesOrderingType, useProxies } from '~/signals'
@ -30,31 +30,20 @@ export default () => {
{}, {},
) )
const onHealthCheckClick = async (e: MouseEvent, name: string) => { const onHealthCheckClick = (e: MouseEvent, name: string) => {
const el = getBtnElFromClickEvent(e) handlerBtnClickWithAnimate(
e,
el.classList.add('animate-pulse') healthCheckByProviderName.bind(null, name),
e.stopPropagation() 'animate-pulse',
await healthCheckByProviderName(name) )
el.classList.remove('animate-pulse')
} }
const onUpdateProviderClick = async (e: MouseEvent, name: string) => { const onUpdateProviderClick = (e: MouseEvent, name: string) => {
const el = getBtnElFromClickEvent(e) handlerBtnClickWithAnimate(e, updateProviderByProviderName.bind(null, name))
el.classList.add('animate-spin')
e.stopPropagation()
await updateProviderByProviderName(name)
el.classList.remove('animate-spin')
} }
const onUpdateAllProviderClick = async (e: MouseEvent) => { const onUpdateAllProviderClick = (e: MouseEvent) => {
const el = getBtnElFromClickEvent(e) handlerBtnClickWithAnimate(e, updateAllProvider)
el.classList.add('animate-spin')
e.stopPropagation()
await updateAllProvider()
el.classList.remove('animate-spin')
} }
return ( return (

View File

@ -3,7 +3,7 @@ import { IconReload } from '@tabler/icons-solidjs'
import InfiniteScroll from 'solid-infinite-scroll' import InfiniteScroll from 'solid-infinite-scroll'
import { For, Show, createMemo, createSignal, onMount } from 'solid-js' import { For, Show, createMemo, createSignal, onMount } from 'solid-js'
import { Button } from '~/components' import { Button } from '~/components'
import { formatTimeFromNow, getBtnElFromClickEvent } from '~/helpers' import { formatTimeFromNow, handlerBtnClickWithAnimate } from '~/helpers'
import { useRules } from '~/signals' import { useRules } from '~/signals'
export default () => { export default () => {
@ -20,22 +20,12 @@ export default () => {
onMount(updateRules) onMount(updateRules)
const onUpdateProviderClick = async (e: MouseEvent, name: string) => { const onUpdateProviderClick = (e: MouseEvent, name: string) => {
const el = getBtnElFromClickEvent(e) handlerBtnClickWithAnimate(e, updateRuleProviderByName.bind(null, name))
el.classList.add('animate-spin')
e.stopPropagation()
await updateRuleProviderByName(name)
el.classList.remove('animate-spin')
} }
const onUpdateAllProviderClick = async (e: MouseEvent) => { const onUpdateAllProviderClick = (e: MouseEvent) => {
const el = getBtnElFromClickEvent(e) handlerBtnClickWithAnimate(e, updateAllRuleProvider)
el.classList.add('animate-spin')
e.stopPropagation()
await updateAllRuleProvider()
el.classList.remove('animate-spin')
} }
return ( return (

View File

@ -16,59 +16,55 @@ const [proxyNodeMap, setProxyNodeMap] = createSignal<Record<string, ProxyInfo>>(
{}, {},
) )
export const useProxies = () => { const setProxiesInfo = (proxies: (Proxy | ProxyNode)[]) => {
const request = useRequest() const newProxyNodeMap = { ...proxyNodeMap() }
const newLatencyMap = { ...latencyMap() }
const setProxyInfoByProxies = (proxies: Proxy[] | ProxyNode[]) => {
proxies.forEach((proxy) => { proxies.forEach((proxy) => {
const latency = proxy.history.at(-1)?.delay || -1 const latency = proxy.history.at(-1)?.delay || -1
setProxyNodeMap({ newProxyNodeMap[proxy.name] = {
...proxyNodeMap(),
[proxy.name]: {
udp: proxy.udp, udp: proxy.udp,
type: proxy.type, type: proxy.type,
name: proxy.name, name: proxy.name,
}, }
}) newLatencyMap[proxy.name] = latency
setLatencyMap({
...latencyMap(),
[proxy.name]: latency,
})
}) })
setProxyNodeMap(newProxyNodeMap)
setLatencyMap(newLatencyMap)
} }
export const useProxies = () => {
const request = useRequest()
const updateProxies = async () => { const updateProxies = async () => {
const { providers } = await request const [{ providers }, { proxies }] = await Promise.all([
request
.get('providers/proxies') .get('providers/proxies')
.json<{ providers: Record<string, ProxyProvider> }>() .json<{ providers: Record<string, ProxyProvider> }>(),
request.get('proxies').json<{ proxies: Record<string, Proxy> }>(),
])
Object.values(providers).forEach((provider) => {
setProxyInfoByProxies(provider.proxies)
})
setProxyProviders(
Object.values(providers).filter(
(provider) =>
provider.name !== 'default' && provider.vehicleType !== 'Compatible',
),
)
const { proxies } = await request
.get('proxies')
.json<{ proxies: Record<string, Proxy> }>()
const sortIndex = [...(proxies['GLOBAL'].all ?? []), 'GLOBAL'] const sortIndex = [...(proxies['GLOBAL'].all ?? []), 'GLOBAL']
const proxiesArray = Object.values(proxies) const sortedProxies = Object.values(proxies)
setProxyInfoByProxies(proxiesArray)
setProxies(
proxiesArray
.filter((proxy) => proxy.all?.length) .filter((proxy) => proxy.all?.length)
.sort( .sort(
(prev, next) => (prev, next) =>
sortIndex.indexOf(prev.name) - sortIndex.indexOf(next.name), sortIndex.indexOf(prev.name) - sortIndex.indexOf(next.name),
),
) )
const sortedProviders = Object.values(providers).filter(
(provider) =>
provider.name !== 'default' && provider.vehicleType !== 'Compatible',
)
const allProxies: (Proxy | ProxyNode)[] = [
...Object.values(proxies),
...sortedProviders.flatMap((provider) => provider.proxies),
]
setProxies(sortedProxies)
setProxyProviders(sortedProviders)
setProxiesInfo(allProxies)
} }
const setProxyGroupByProxyName = async (proxy: Proxy, proxyName: string) => { const setProxyGroupByProxyName = async (proxy: Proxy, proxyName: string) => {