fix: bar and delay reactive

This commit is contained in:
Zephyruso 2023-09-02 12:34:23 +08:00
parent 5952425621
commit 9ed05401a8
3 changed files with 59 additions and 32 deletions

View File

@ -1,21 +1,33 @@
import { Show, createEffect, createMemo, createSignal } from 'solid-js'
import { DELAY } from '~/config/enum' import { DELAY } from '~/config/enum'
import { useProxies } from '~/signals/proxies'
const Delay = (props: { delay: number | undefined }) => { const Delay = (props: { name?: string }) => {
const delay = props.delay const { proxyNodeMap } = useProxies()
const [textClassName, setTextClassName] = createSignal('')
const delay = createMemo(() => {
return proxyNodeMap()[props.name!]?.delay!
})
if (typeof delay !== 'number' || delay === DELAY.NOT_CONNECTED) { createEffect(() => {
return '' setTextClassName('text-success')
if (delay() > DELAY.HIGH) {
setTextClassName('text-error')
} else if (delay() > DELAY.MEDIUM) {
setTextClassName('text-warning')
} }
})
let textClassName = 'text-success' return (
<>
if (delay > DELAY.HIGH) { <Show
textClassName = 'text-error' when={typeof delay() === 'number' && delay() !== DELAY.NOT_CONNECTED}
} else if (delay > DELAY.MEDIUM) { >
textClassName = 'text-warning' <span class={textClassName()}>{delay()}ms</span>
} </Show>
</>
return <span class={textClassName}>{delay}ms</span> )
} }
export default Delay export default Delay

View File

@ -44,7 +44,7 @@ export default (props: {
{proxyNode()?.udp && ' :: udp'} {proxyNode()?.udp && ' :: udp'}
</div> </div>
<div class="text-xs"> <div class="text-xs">
<Delay delay={proxyNode()?.delay} /> <Delay name={props.proxyName} />
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,3 +1,4 @@
import { createMemo } from 'solid-js'
import { twMerge } from 'tailwind-merge' import { twMerge } from 'tailwind-merge'
import Delay from '~/components/Delay' import Delay from '~/components/Delay'
import { DELAY } from '~/config/enum' import { DELAY } from '~/config/enum'
@ -5,18 +6,32 @@ import { useProxies } from '~/signals/proxies'
export default (props: { proxyNameList: string[]; now?: string }) => { export default (props: { proxyNameList: string[]; now?: string }) => {
const { proxyNodeMap } = useProxies() const { proxyNodeMap } = useProxies()
const allNodesDelay = props.proxyNameList.map((i) => proxyNodeMap()[i].delay!) const delayList = createMemo(() =>
const all = allNodesDelay.length props.proxyNameList.map((i) => proxyNodeMap()[i].delay!),
const good = allNodesDelay.filter( )
const all = createMemo(() => delayList().length)
const good = createMemo(
() =>
delayList().filter(
(delay) => delay > DELAY.NOT_CONNECTED && delay <= DELAY.MEDIUM, (delay) => delay > DELAY.NOT_CONNECTED && delay <= DELAY.MEDIUM,
).length ).length,
const middle = allNodesDelay.filter( )
(delay) => delay > DELAY.MEDIUM && delay <= DELAY.HIGH, const middle = createMemo(
).length () =>
const slow = allNodesDelay.filter((delay) => delay > DELAY.HIGH).length delayList().filter((delay) => delay > DELAY.MEDIUM && delay <= DELAY.HIGH)
const notConnected = allNodesDelay.filter( .length,
)
const slow = createMemo(
() => delayList().filter((delay) => delay > DELAY.HIGH).length,
)
const notConnected = createMemo(
() =>
delayList().filter(
(delay) => delay === DELAY.NOT_CONNECTED || typeof delay !== 'number', (delay) => delay === DELAY.NOT_CONNECTED || typeof delay !== 'number',
).length ).length,
)
console.log(good())
return ( return (
<div class="flex w-full items-center"> <div class="flex w-full items-center">
@ -24,30 +39,30 @@ export default (props: { proxyNameList: string[]; now?: string }) => {
<div <div
class={twMerge('h-2 bg-success')} class={twMerge('h-2 bg-success')}
style={{ style={{
width: `${(good * 100) / all}%`, // cant use tw class cause dynamic classname wont import width: `${(good() * 100) / all()}%`, // cant use tw class cause dynamic classname wont import
}} }}
></div> ></div>
<div <div
class={twMerge('h-2 bg-warning')} class={twMerge('h-2 bg-warning')}
style={{ style={{
width: `${(middle * 100) / all}%`, width: `${(middle() * 100) / all()}%`,
}} }}
></div> ></div>
<div <div
class={twMerge('h-2 bg-error')} class={twMerge('h-2 bg-error')}
style={{ style={{
width: `${(slow * 100) / all}%`, width: `${(slow() * 100) / all()}%`,
}} }}
></div> ></div>
<div <div
class={twMerge('h-2 bg-neutral')} class={twMerge('h-2 bg-neutral')}
style={{ style={{
width: `${(notConnected * 100) / all}%`, width: `${(notConnected() * 100) / all()}%`,
}} }}
></div> ></div>
</div> </div>
<div class="ml-4 text-xs"> <div class="ml-3 w-8 text-xs">
<Delay delay={proxyNodeMap()[props.now!]?.delay} /> <Delay name={props.now} />
</div> </div>
</div> </div>
) )