Skip to content

Commit 91d99c3

Browse files
author
pompurin404
committed
proxy order
1 parent 3730d09 commit 91d99c3

File tree

5 files changed

+107
-39
lines changed

5 files changed

+107
-39
lines changed

src/main/utils/template.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const defaultConfig: IAppConfig = {
22
core: 'mihomo',
33
silentStart: false,
44
proxyDisplayMode: 'simple',
5+
proxyDisplayOrder: 'default',
56
sysProxy: { enable: false, mode: 'manual' }
67
}
78

src/renderer/src/components/proxies/proxy-item.tsx

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Button, Card, CardBody } from '@nextui-org/react'
2-
import React, { useEffect, useState } from 'react'
2+
import React, { useEffect, useMemo, useState } from 'react'
33
import PubSub from 'pubsub-js'
44

55
interface Props {
@@ -14,12 +14,14 @@ interface Props {
1414

1515
const ProxyItem: React.FC<Props> = (props) => {
1616
const { mutateProxies, proxyDisplayMode, group, proxy, selected, onSelect, onProxyDelay } = props
17-
const [delay, setDelay] = useState(() => {
17+
18+
const delay = useMemo(() => {
1819
if (proxy.history.length > 0) {
1920
return proxy.history[proxy.history.length - 1].delay
2021
}
2122
return -1
22-
})
23+
}, [proxy])
24+
2325
const [loading, setLoading] = useState(false)
2426

2527
function delayColor(delay: number): 'primary' | 'success' | 'warning' | 'danger' {
@@ -37,19 +39,11 @@ const ProxyItem: React.FC<Props> = (props) => {
3739

3840
const onDelay = (): void => {
3941
setLoading(true)
40-
onProxyDelay(proxy.name, group.testUrl).then(
41-
(delay) => {
42-
setDelay(delay.delay || 0)
43-
mutateProxies()
44-
setLoading(false)
45-
},
46-
() => {
47-
setDelay(0)
48-
setLoading(false)
49-
}
50-
)
42+
onProxyDelay(proxy.name, group.testUrl).finally(() => {
43+
mutateProxies()
44+
setLoading(false)
45+
})
5146
}
52-
console.log(delay)
5347

5448
useEffect(() => {
5549
const token = PubSub.subscribe(`${group.name}-delay`, onDelay)
@@ -58,6 +52,7 @@ const ProxyItem: React.FC<Props> = (props) => {
5852
PubSub.unsubscribe(token)
5953
}
6054
}, [])
55+
6156
return (
6257
<Card
6358
onPress={() => onSelect(group.name, proxy.name)}
@@ -66,12 +61,16 @@ const ProxyItem: React.FC<Props> = (props) => {
6661
className={`${selected ? 'bg-primary/30' : ''}`}
6762
radius="sm"
6863
>
69-
<CardBody className="p-1">
64+
<CardBody className="p-2">
7065
<div className="flex justify-between items-center">
7166
<div>
72-
<div className="inline">{proxy.name}</div>
67+
<div className="inline text-ellipsis whitespace-nowrap overflow-hidden">
68+
{proxy.name}
69+
</div>
7370
{proxyDisplayMode === 'full' && (
74-
<div className="inline ml-2 text-default-500">{proxy.type}</div>
71+
<div className="inline ml-2 text-ellipsis whitespace-nowrap overflow-hidden text-default-500">
72+
{proxy.type}
73+
</div>
7574
)}
7675
</div>
7776
<Button

src/renderer/src/pages/proxies.tsx

+88-20
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ import BasePage from '@renderer/components/base/base-page'
33
import { useAppConfig } from '@renderer/hooks/use-app-config'
44
import { mihomoChangeProxy, mihomoProxies, mihomoProxyDelay } from '@renderer/utils/ipc'
55
import { CgDetailsLess, CgDetailsMore } from 'react-icons/cg'
6-
import { useMemo, useState } from 'react'
6+
import { FaBoltLightning } from 'react-icons/fa6'
7+
import { TbCircleLetterD } from 'react-icons/tb'
8+
import { FaLocationCrosshairs } from 'react-icons/fa6'
9+
import { RxLetterCaseCapitalize } from 'react-icons/rx'
10+
import { useMemo, useRef, useState } from 'react'
711
import useSWR from 'swr'
8-
import { GroupedVirtuoso } from 'react-virtuoso'
12+
import { GroupedVirtuoso, GroupedVirtuosoHandle } from 'react-virtuoso'
913
import ProxyItem from '@renderer/components/proxies/proxy-item'
1014
import { IoIosArrowBack } from 'react-icons/io'
1115
import { MdOutlineSpeed } from 'react-icons/md'
1216

1317
const Proxies: React.FC = () => {
1418
const { data: proxies, mutate } = useSWR('mihomoProxies', mihomoProxies)
1519
const { appConfig, patchAppConfig } = useAppConfig()
16-
const { proxyDisplayMode = 'simple' } = appConfig || {}
20+
const { proxyDisplayMode = 'simple', proxyDisplayOrder = 'default' } = appConfig || {}
1721

1822
const groups = useMemo(() => {
1923
const groups: IMihomoGroup[] = []
@@ -36,20 +40,33 @@ const Proxies: React.FC = () => {
3640
}, [proxies])
3741

3842
const [isOpen, setIsOpen] = useState(Array(groups.length).fill(false))
39-
43+
const virtuosoRef = useRef<GroupedVirtuosoHandle>(null)
4044
const { groupCounts, allProxies } = useMemo(() => {
4145
const groupCounts = groups.map((group, index) => {
4246
return isOpen[index] ? group.all.length : 0
4347
})
4448
const allProxies: (IMihomoProxy | IMihomoGroup)[] = []
4549
groups.forEach((group, index) => {
4650
if (isOpen[index] && proxies) {
47-
allProxies.push(...group.all.map((name) => proxies.proxies[name]))
51+
let groupProxies = group.all.map((name) => proxies.proxies[name])
52+
if (proxyDisplayOrder === 'delay') {
53+
groupProxies = groupProxies.sort((a, b) => {
54+
if (a.history.length === 0) return -1
55+
if (b.history.length === 0) return 1
56+
if (a.history[a.history.length - 1].delay === 0) return 1
57+
if (b.history[b.history.length - 1].delay === 0) return -1
58+
return a.history[a.history.length - 1].delay - b.history[b.history.length - 1].delay
59+
})
60+
}
61+
if (proxyDisplayOrder === 'name') {
62+
groupProxies = groupProxies.sort((a, b) => a.name.localeCompare(b.name))
63+
}
64+
allProxies.push(...groupProxies)
4865
}
4966
})
5067

5168
return { groupCounts, allProxies }
52-
}, [groups, isOpen])
69+
}, [groups, isOpen, proxyDisplayOrder])
5370

5471
const onChangeProxy = (group: string, proxy: string): void => {
5572
mihomoChangeProxy(group, proxy).then(() => {
@@ -69,22 +86,50 @@ const Proxies: React.FC = () => {
6986
<BasePage
7087
title="代理组"
7188
header={
72-
<Button
73-
size="sm"
74-
isIconOnly
75-
onPress={() => {
76-
patchAppConfig({ proxyDisplayMode: proxyDisplayMode === 'simple' ? 'full' : 'simple' })
77-
}}
78-
>
79-
{proxyDisplayMode === 'simple' ? (
80-
<CgDetailsMore size={20} />
81-
) : (
82-
<CgDetailsLess size={20} />
83-
)}
84-
</Button>
89+
<div>
90+
<Button
91+
size="sm"
92+
isIconOnly
93+
onPress={() => {
94+
patchAppConfig({
95+
proxyDisplayOrder:
96+
proxyDisplayOrder === 'default'
97+
? 'delay'
98+
: proxyDisplayOrder === 'delay'
99+
? 'name'
100+
: 'default'
101+
})
102+
}}
103+
>
104+
{proxyDisplayOrder === 'default' ? (
105+
<TbCircleLetterD size={20} title="默认" />
106+
) : proxyDisplayOrder === 'delay' ? (
107+
<FaBoltLightning size={20} title="延迟" />
108+
) : (
109+
<RxLetterCaseCapitalize size={20} title="名称" />
110+
)}
111+
</Button>
112+
<Button
113+
size="sm"
114+
isIconOnly
115+
className="ml-2"
116+
onPress={() => {
117+
patchAppConfig({
118+
proxyDisplayMode: proxyDisplayMode === 'simple' ? 'full' : 'simple'
119+
})
120+
}}
121+
>
122+
{proxyDisplayMode === 'simple' ? (
123+
<CgDetailsMore size={20} title="详细信息" />
124+
) : (
125+
<CgDetailsLess size={20} title="简洁信息" />
126+
)}
127+
</Button>
128+
</div>
85129
}
86130
>
87131
<GroupedVirtuoso
132+
ref={virtuosoRef}
88133
style={{ height: 'calc(100vh - 50px)' }}
89134
groupCounts={groupCounts}
90135
groupContent={(index) => {
@@ -112,7 +157,7 @@ const Proxies: React.FC = () => {
112157
src={groups[index].icon}
113158
/>
114159
) : null}
115-
<div className="h-[32px] text-md leading-[32px]">
160+
<div className="h-[32px] text-ellipsis whitespace-nowrap overflow-hidden text-md leading-[32px]">
116161
{groups[index].name}
117162
{proxyDisplayMode === 'full' && (
118163
<>
@@ -128,6 +173,29 @@ const Proxies: React.FC = () => {
128173
</div>
129174
<div className="flex">
130175
<Button
176+
title="定位到当前节点"
177+
variant="light"
178+
size="sm"
179+
isIconOnly
180+
onPress={() => {
181+
if (!isOpen[index]) return
182+
let i = 0
183+
for (let j = 0; j < index; j++) {
184+
i += groupCounts[j]
185+
}
186+
for (let j = 0; j < groupCounts[index]; j++) {
187+
if (allProxies[i + j].name === groups[index].now) {
188+
i += j
189+
break
190+
}
191+
}
192+
virtuosoRef.current?.scrollToIndex({ index: i, align: 'start' })
193+
}}
194+
>
195+
<FaLocationCrosshairs className="text-lg text-default-500" />
196+
</Button>
197+
<Button
198+
title="延迟测试"
131199
variant="light"
132200
size="sm"
133201
isIconOnly

src/renderer/src/utils/ipc.ts

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export async function mihomoChangeProxy(group: string, proxy: string): Promise<I
3232

3333
export async function mihomoProxyDelay(proxy: string, url?: string): Promise<IMihomoDelay> {
3434
const res = await window.electron.ipcRenderer.invoke('mihomoProxyDelay', proxy, url)
35-
console.log(res)
3635
return res
3736
}
3837

src/shared/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ interface ISysProxyConfig {
131131
interface IAppConfig {
132132
core: 'mihomo' | 'mihomo-alpha'
133133
proxyDisplayMode: 'simple' | 'full'
134+
proxyDisplayOrder: 'default' | 'delay' | 'name'
134135
silentStart: boolean
135136
sysProxy: ISysProxyConfig
136137
userAgent?: string

0 commit comments

Comments
 (0)