metacubexd/src/components/Latency.tsx

38 lines
1.0 KiB
TypeScript
Raw Normal View History

import { useI18n } from '@solid-primitives/i18n'
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 [t] = useI18n()
const { latencyMap } = useProxies()
2023-09-02 12:34:23 +08:00
const [textClassName, setTextClassName] = createSignal('')
const latency = createMemo(() => {
return latencyMap()[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
if (latency() > latencyQualityMap().HIGH) {
2023-09-02 12:34:23 +08:00
setTextClassName('text-error')
} else if (latency() > 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 (
2023-09-03 06:08:53 +08:00
<Show
when={
typeof latency() === 'number' &&
latency() !== LATENCY_QUALITY_MAP_HTTP.NOT_CONNECTED
}
>
<span class={`whitespace-nowrap text-xs ${textClassName()}`}>
{latency()}
{t('ms')}
</span>
</Show>
2023-09-02 12:34:23 +08:00
)
2023-09-02 11:52:51 +08:00
}