metacubexd/src/components/Latency.tsx

35 lines
914 B
TypeScript
Raw Normal View History

2023-09-02 12:34:23 +08:00
import { Show, createEffect, createMemo, createSignal } from 'solid-js'
2023-09-03 03:56:04 +08:00
import { LATENCY_QUALITY_MAP_HTTP } from '~/constants'
import { latencyQualityMap, useProxies } from '~/signals'
2023-09-02 11:52:51 +08:00
2023-09-03 03:56:04 +08:00
export const Latency = (props: { name?: string }) => {
const { delayMap } = useProxies()
2023-09-02 12:34:23 +08:00
const [textClassName, setTextClassName] = createSignal('')
const delay = createMemo(() => {
return delayMap()[props.name!]
2023-09-02 12:34:23 +08:00
})
2023-09-02 11:52:51 +08:00
2023-09-02 12:34:23 +08:00
createEffect(() => {
setTextClassName('text-success')
2023-09-02 11:52:51 +08:00
2023-09-03 03:56:04 +08:00
if (delay() > latencyQualityMap().HIGH) {
2023-09-02 12:34:23 +08:00
setTextClassName('text-error')
2023-09-03 03:56:04 +08:00
} else if (delay() > latencyQualityMap().MEDIUM) {
2023-09-02 12:34:23 +08:00
setTextClassName('text-warning')
}
})
2023-09-02 11:52:51 +08:00
2023-09-02 12:34:23 +08:00
return (
<>
<Show
2023-09-03 03:56:04 +08:00
when={
typeof delay() === 'number' &&
delay() !== LATENCY_QUALITY_MAP_HTTP.NOT_CONNECTED
}
2023-09-02 12:34:23 +08:00
>
<span class={textClassName()}>{delay()}ms</span>
</Show>
</>
)
2023-09-02 11:52:51 +08:00
}