Skip to content

Commit

Permalink
fix: bar and delay reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
Zephyruso authored and Zephyruso committed Sep 2, 2023
1 parent 5952425 commit 9ed0540
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
38 changes: 25 additions & 13 deletions src/components/Delay.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { Show, createEffect, createMemo, createSignal } from 'solid-js'
import { DELAY } from '~/config/enum'
import { useProxies } from '~/signals/proxies'

const Delay = (props: { delay: number | undefined }) => {
const delay = props.delay
const Delay = (props: { name?: string }) => {
const { proxyNodeMap } = useProxies()
const [textClassName, setTextClassName] = createSignal('')
const delay = createMemo(() => {
return proxyNodeMap()[props.name!]?.delay!
})

if (typeof delay !== 'number' || delay === DELAY.NOT_CONNECTED) {
return ''
}
createEffect(() => {
setTextClassName('text-success')

let textClassName = 'text-success'
if (delay() > DELAY.HIGH) {
setTextClassName('text-error')
} else if (delay() > DELAY.MEDIUM) {
setTextClassName('text-warning')
}
})

if (delay > DELAY.HIGH) {
textClassName = 'text-error'
} else if (delay > DELAY.MEDIUM) {
textClassName = 'text-warning'
}

return <span class={textClassName}>{delay}ms</span>
return (
<>
<Show
when={typeof delay() === 'number' && delay() !== DELAY.NOT_CONNECTED}
>
<span class={textClassName()}>{delay()}ms</span>
</Show>
</>
)
}

export default Delay
2 changes: 1 addition & 1 deletion src/components/ProxyNodeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default (props: {
{proxyNode()?.udp && ' :: udp'}
</div>
<div class="text-xs">
<Delay delay={proxyNode()?.delay} />
<Delay name={props.proxyName} />
</div>
</div>
</div>
Expand Down
51 changes: 33 additions & 18 deletions src/components/ProxyPreviewBar.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
import { createMemo } from 'solid-js'
import { twMerge } from 'tailwind-merge'
import Delay from '~/components/Delay'
import { DELAY } from '~/config/enum'
import { useProxies } from '~/signals/proxies'

export default (props: { proxyNameList: string[]; now?: string }) => {
const { proxyNodeMap } = useProxies()
const allNodesDelay = props.proxyNameList.map((i) => proxyNodeMap()[i].delay!)
const all = allNodesDelay.length
const good = allNodesDelay.filter(
(delay) => delay > DELAY.NOT_CONNECTED && delay <= DELAY.MEDIUM,
).length
const middle = allNodesDelay.filter(
(delay) => delay > DELAY.MEDIUM && delay <= DELAY.HIGH,
).length
const slow = allNodesDelay.filter((delay) => delay > DELAY.HIGH).length
const notConnected = allNodesDelay.filter(
(delay) => delay === DELAY.NOT_CONNECTED || typeof delay !== 'number',
).length
const delayList = createMemo(() =>
props.proxyNameList.map((i) => proxyNodeMap()[i].delay!),
)
const all = createMemo(() => delayList().length)
const good = createMemo(
() =>
delayList().filter(
(delay) => delay > DELAY.NOT_CONNECTED && delay <= DELAY.MEDIUM,
).length,
)
const middle = createMemo(
() =>
delayList().filter((delay) => delay > DELAY.MEDIUM && delay <= DELAY.HIGH)
.length,
)
const slow = createMemo(
() => delayList().filter((delay) => delay > DELAY.HIGH).length,
)
const notConnected = createMemo(
() =>
delayList().filter(
(delay) => delay === DELAY.NOT_CONNECTED || typeof delay !== 'number',
).length,
)

console.log(good())

return (
<div class="flex w-full items-center">
<div class="flex flex-1 overflow-hidden rounded-2xl">
<div
class={twMerge('h-2 bg-success')}
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
class={twMerge('h-2 bg-warning')}
style={{
width: `${(middle * 100) / all}%`,
width: `${(middle() * 100) / all()}%`,
}}
></div>
<div
class={twMerge('h-2 bg-error')}
style={{
width: `${(slow * 100) / all}%`,
width: `${(slow() * 100) / all()}%`,
}}
></div>
<div
class={twMerge('h-2 bg-neutral')}
style={{
width: `${(notConnected * 100) / all}%`,
width: `${(notConnected() * 100) / all()}%`,
}}
></div>
</div>
<div class="ml-4 text-xs">
<Delay delay={proxyNodeMap()[props.now!]?.delay} />
<div class="ml-3 w-8 text-xs">
<Delay name={props.now} />
</div>
</div>
)
Expand Down

0 comments on commit 9ed0540

Please sign in to comment.