metacubexd/src/components/Latency.tsx
2023-09-03 06:08:53 +08:00

38 lines
1.0 KiB
TypeScript

import { useI18n } from '@solid-primitives/i18n'
import { Show, createEffect, createMemo, createSignal } from 'solid-js'
import { LATENCY_QUALITY_MAP_HTTP } from '~/constants'
import { latencyQualityMap, useProxies } from '~/signals'
export const Latency = (props: { name?: string }) => {
const [t] = useI18n()
const { latencyMap } = useProxies()
const [textClassName, setTextClassName] = createSignal('')
const latency = createMemo(() => {
return latencyMap()[props.name!]
})
createEffect(() => {
setTextClassName('text-success')
if (latency() > latencyQualityMap().HIGH) {
setTextClassName('text-error')
} else if (latency() > latencyQualityMap().MEDIUM) {
setTextClassName('text-warning')
}
})
return (
<Show
when={
typeof latency() === 'number' &&
latency() !== LATENCY_QUALITY_MAP_HTTP.NOT_CONNECTED
}
>
<span class={`whitespace-nowrap text-xs ${textClassName()}`}>
{latency()}
{t('ms')}
</span>
</Show>
)
}