|
| 1 | +import Tooltip from '@corvu/tooltip' |
| 2 | +import { IconCircleCheckFilled } from '@tabler/icons-solidjs' |
| 3 | +import dayjs from 'dayjs' |
| 4 | +import { twMerge } from 'tailwind-merge' |
| 5 | +import { Latency } from '~/components' |
| 6 | +import { |
| 7 | + filterSpecialProxyType, |
| 8 | + formatProxyType, |
| 9 | + getLatencyClassName, |
| 10 | +} from '~/helpers' |
| 11 | +import { useI18n } from '~/i18n' |
| 12 | +import { rootElement, useProxies } from '~/signals' |
| 13 | + |
| 14 | +export const ProxyNodeListItem = (props: { |
| 15 | + proxyName: string |
| 16 | + testUrl: string | null |
| 17 | + timeout: number | null |
| 18 | + isSelected?: boolean |
| 19 | + onClick?: () => void |
| 20 | +}) => { |
| 21 | + const { proxyName, isSelected, onClick } = props |
| 22 | + const [t] = useI18n() |
| 23 | + const { |
| 24 | + proxyNodeMap, |
| 25 | + proxyLatencyTest, |
| 26 | + proxyLatencyTestingMap, |
| 27 | + getLatencyHistoryByName, |
| 28 | + } = useProxies() |
| 29 | + const proxyNode = createMemo(() => proxyNodeMap()[proxyName]) |
| 30 | + |
| 31 | + const specialTypes = createMemo(() => { |
| 32 | + if (!filterSpecialProxyType(proxyNode()?.type)) return null |
| 33 | + |
| 34 | + return [ |
| 35 | + proxyNode().xudp && 'xudp', |
| 36 | + proxyNode().udp && 'udp', |
| 37 | + proxyNode().tfo && 'TFO', |
| 38 | + ] |
| 39 | + .filter(Boolean) |
| 40 | + .join(' / ') |
| 41 | + }) |
| 42 | + |
| 43 | + const isUDP = createMemo(() => proxyNode().xudp || proxyNode().udp) |
| 44 | + |
| 45 | + const latencyTestHistory = getLatencyHistoryByName( |
| 46 | + props.proxyName, |
| 47 | + props.testUrl, |
| 48 | + ) |
| 49 | + |
| 50 | + return ( |
| 51 | + <Tooltip |
| 52 | + placement="top" |
| 53 | + group="proxy-node" |
| 54 | + openDelay={300} |
| 55 | + floatingOptions={{ |
| 56 | + flip: true, |
| 57 | + shift: { padding: 8 }, |
| 58 | + offset: 8, |
| 59 | + arrow: 8, |
| 60 | + size: { fitViewPort: true }, |
| 61 | + }} |
| 62 | + > |
| 63 | + <Tooltip.Anchor |
| 64 | + as="div" |
| 65 | + class={twMerge( |
| 66 | + 'rounded-lg bg-neutral text-neutral-content', |
| 67 | + isSelected && 'bg-primary text-primary-content', |
| 68 | + )} |
| 69 | + > |
| 70 | + <Tooltip.Trigger as="div"> |
| 71 | + <div |
| 72 | + class={twMerge( |
| 73 | + 'flex items-center gap-2 px-3 py-1.5', |
| 74 | + onClick && 'cursor-pointer hover:opacity-80', |
| 75 | + )} |
| 76 | + onClick={onClick} |
| 77 | + > |
| 78 | + {/* Selected indicator */} |
| 79 | + <Show when={isSelected}> |
| 80 | + <IconCircleCheckFilled class="size-4 shrink-0" /> |
| 81 | + </Show> |
| 82 | + |
| 83 | + {/* Proxy name */} |
| 84 | + <span class="min-w-0 flex-1 truncate text-sm font-medium"> |
| 85 | + {proxyName} |
| 86 | + </span> |
| 87 | + |
| 88 | + {/* UDP indicator */} |
| 89 | + <Show when={isUDP()}> |
| 90 | + <span class="badge shrink-0 badge-xs badge-info">U</span> |
| 91 | + </Show> |
| 92 | + |
| 93 | + {/* Special types */} |
| 94 | + <Show when={specialTypes()}> |
| 95 | + <span class="hidden text-xs uppercase opacity-60 sm:inline"> |
| 96 | + {specialTypes()} |
| 97 | + </span> |
| 98 | + </Show> |
| 99 | + |
| 100 | + {/* Proxy type */} |
| 101 | + <span class="hidden text-xs uppercase opacity-75 sm:inline"> |
| 102 | + {formatProxyType(proxyNode()?.type)} |
| 103 | + </span> |
| 104 | + |
| 105 | + {/* Latency */} |
| 106 | + <Latency |
| 107 | + proxyName={props.proxyName} |
| 108 | + testUrl={props.testUrl || null} |
| 109 | + class={twMerge( |
| 110 | + 'shrink-0', |
| 111 | + proxyLatencyTestingMap()[proxyName] && 'animate-pulse', |
| 112 | + )} |
| 113 | + onClick={(e) => { |
| 114 | + e.stopPropagation() |
| 115 | + |
| 116 | + void proxyLatencyTest( |
| 117 | + proxyName, |
| 118 | + proxyNode().provider, |
| 119 | + props.testUrl, |
| 120 | + props.timeout, |
| 121 | + ) |
| 122 | + }} |
| 123 | + /> |
| 124 | + </div> |
| 125 | + </Tooltip.Trigger> |
| 126 | + |
| 127 | + <Tooltip.Portal mount={rootElement()}> |
| 128 | + <Tooltip.Content class="z-50"> |
| 129 | + <Tooltip.Arrow class="text-primary [&>svg]:-translate-y-px [&>svg]:fill-current" /> |
| 130 | + |
| 131 | + <div class="flex max-h-[70vh] flex-col items-center gap-2 overflow-y-auto rounded-box bg-primary p-2.5 text-primary-content shadow-lg [clip-path:inset(0_round_var(--radius-box))]"> |
| 132 | + <h2 class="text-lg font-bold">{proxyName}</h2> |
| 133 | + |
| 134 | + <Show when={specialTypes()}> |
| 135 | + <div class="w-full text-xs uppercase">{specialTypes()}</div> |
| 136 | + </Show> |
| 137 | + |
| 138 | + <Show |
| 139 | + when={latencyTestHistory.length > 0} |
| 140 | + fallback={ |
| 141 | + <div class="text-sm opacity-75">{t('noLatencyHistory')}</div> |
| 142 | + } |
| 143 | + > |
| 144 | + <ul class="timeline timeline-vertical timeline-compact timeline-snap-icon"> |
| 145 | + <For each={latencyTestHistory}> |
| 146 | + {(latencyTestResult, index) => ( |
| 147 | + <li> |
| 148 | + <Show when={index() > 0}> |
| 149 | + <hr /> |
| 150 | + </Show> |
| 151 | + |
| 152 | + <div class="timeline-start space-y-2"> |
| 153 | + <time class="text-sm italic"> |
| 154 | + {dayjs(latencyTestResult.time).format( |
| 155 | + 'YYYY-MM-DD HH:mm:ss', |
| 156 | + )} |
| 157 | + </time> |
| 158 | + |
| 159 | + <div |
| 160 | + class={twMerge( |
| 161 | + 'badge block', |
| 162 | + getLatencyClassName(latencyTestResult.delay), |
| 163 | + )} |
| 164 | + > |
| 165 | + {latencyTestResult.delay || '---'} |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + |
| 169 | + <div class="timeline-middle"> |
| 170 | + <IconCircleCheckFilled class="size-4" /> |
| 171 | + </div> |
| 172 | + |
| 173 | + <Show when={index() !== latencyTestHistory.length - 1}> |
| 174 | + <hr /> |
| 175 | + </Show> |
| 176 | + </li> |
| 177 | + )} |
| 178 | + </For> |
| 179 | + </ul> |
| 180 | + </Show> |
| 181 | + </div> |
| 182 | + </Tooltip.Content> |
| 183 | + </Tooltip.Portal> |
| 184 | + </Tooltip.Anchor> |
| 185 | + </Tooltip> |
| 186 | + ) |
| 187 | +} |
0 commit comments