2023-09-03 05:35:08 +08:00
|
|
|
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 }) => {
|
2023-09-03 05:35:08 +08:00
|
|
|
const [t] = useI18n()
|
|
|
|
const { latencyMap } = useProxies()
|
2023-09-02 12:34:23 +08:00
|
|
|
const [textClassName, setTextClassName] = createSignal('')
|
2023-09-03 05:35:08 +08:00
|
|
|
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
|
|
|
|
2023-09-03 05:35:08 +08:00
|
|
|
if (latency() > latencyQualityMap().HIGH) {
|
2023-09-02 12:34:23 +08:00
|
|
|
setTextClassName('text-error')
|
2023-09-03 05:35:08 +08:00
|
|
|
} 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 (
|
|
|
|
<>
|
|
|
|
<Show
|
2023-09-03 03:56:04 +08:00
|
|
|
when={
|
2023-09-03 05:35:08 +08:00
|
|
|
typeof latency() === 'number' &&
|
|
|
|
latency() !== LATENCY_QUALITY_MAP_HTTP.NOT_CONNECTED
|
2023-09-03 03:56:04 +08:00
|
|
|
}
|
2023-09-02 12:34:23 +08:00
|
|
|
>
|
2023-09-03 05:35:08 +08:00
|
|
|
<span class={`whitespace-nowrap ${textClassName()}`}>
|
|
|
|
{latency()}
|
|
|
|
{t('ms')}
|
|
|
|
</span>
|
2023-09-02 12:34:23 +08:00
|
|
|
</Show>
|
|
|
|
</>
|
|
|
|
)
|
2023-09-02 11:52:51 +08:00
|
|
|
}
|