mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-19 02:12:10 +08:00
perf(home): render proxy dropdown options on demand
This commit is contained in:
@@ -77,6 +77,7 @@
|
|||||||
<details>
|
<details>
|
||||||
<summary><strong> 🚀 优化改进 </strong></summary>
|
<summary><strong> 🚀 优化改进 </strong></summary>
|
||||||
|
|
||||||
|
- 优化首页节点菜单关闭时的刷新性能
|
||||||
- 优化 TUN 配置优先级:界面设置优先,其他选项仍可通过订阅、覆写和脚本调整
|
- 优化 TUN 配置优先级:界面设置优先,其他选项仍可通过订阅、覆写和脚本调整
|
||||||
- 优化系统代理失败后的恢复流程,保持系统设置和界面状态一致
|
- 优化系统代理失败后的恢复流程,保持系统设置和界面状态一致
|
||||||
- 优化错误提示,支持复制完整原因,托盘和快捷键操作失败时也会提醒
|
- 优化错误提示,支持复制完整原因,托盘和快捷键操作失败时也会提醒
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/* eslint-disable @eslint-react/set-state-in-effect */
|
/* eslint-disable @eslint-react/set-state-in-effect */
|
||||||
import {
|
import {
|
||||||
AccessTimeRounded,
|
AccessTimeRounded,
|
||||||
|
ArrowDropDown,
|
||||||
ChevronRight,
|
ChevronRight,
|
||||||
NetworkCheckRounded,
|
NetworkCheckRounded,
|
||||||
WifiOff as SignalError,
|
WifiOff as SignalError,
|
||||||
@@ -15,24 +16,22 @@ import {
|
|||||||
import {
|
import {
|
||||||
Box,
|
Box,
|
||||||
Button,
|
Button,
|
||||||
|
ButtonBase,
|
||||||
Chip,
|
Chip,
|
||||||
ClickAwayListener,
|
ClickAwayListener,
|
||||||
FormControl,
|
|
||||||
IconButton,
|
IconButton,
|
||||||
InputLabel,
|
|
||||||
MenuItem,
|
MenuItem,
|
||||||
MenuList,
|
MenuList,
|
||||||
Paper,
|
Paper,
|
||||||
Popper,
|
Popper,
|
||||||
type PopperProps,
|
type PopperProps,
|
||||||
Select,
|
|
||||||
Tooltip,
|
Tooltip,
|
||||||
Typography,
|
Typography,
|
||||||
alpha,
|
alpha,
|
||||||
useTheme,
|
useTheme,
|
||||||
} from '@mui/material'
|
} from '@mui/material'
|
||||||
import { useLockFn } from 'ahooks'
|
import { useLockFn } from 'ahooks'
|
||||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
import { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import { useNavigate } from 'react-router'
|
import { useNavigate } from 'react-router'
|
||||||
|
|
||||||
@@ -223,34 +222,28 @@ const sortProxyOptions = (
|
|||||||
|
|
||||||
interface PersistentSelectProps {
|
interface PersistentSelectProps {
|
||||||
label: string
|
label: string
|
||||||
labelId: string
|
displayValue: React.ReactNode
|
||||||
listboxId: string
|
|
||||||
value: string
|
|
||||||
selectedName: string
|
|
||||||
open: boolean
|
open: boolean
|
||||||
disabled: boolean
|
disabled: boolean
|
||||||
keepOpenRef: React.RefObject<HTMLElement | null>
|
keepOpenRef: React.RefObject<HTMLElement | null>
|
||||||
onOpen: () => void
|
onOpenChange: (open: boolean) => void
|
||||||
onClose: () => void
|
renderOptions: () => React.ReactNode
|
||||||
renderValue?: () => React.ReactNode
|
|
||||||
children: React.ReactNode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const PersistentSelect = ({
|
const PersistentSelect = ({
|
||||||
label,
|
label,
|
||||||
labelId,
|
displayValue,
|
||||||
listboxId,
|
|
||||||
value,
|
|
||||||
selectedName,
|
|
||||||
open,
|
open,
|
||||||
disabled,
|
disabled,
|
||||||
keepOpenRef,
|
keepOpenRef,
|
||||||
onOpen,
|
onOpenChange,
|
||||||
onClose,
|
renderOptions,
|
||||||
renderValue,
|
|
||||||
children,
|
|
||||||
}: PersistentSelectProps) => {
|
}: PersistentSelectProps) => {
|
||||||
const anchorRef = useRef<HTMLDivElement>(null)
|
const id = useId()
|
||||||
|
const labelId = `${id}-label`
|
||||||
|
const valueId = `${id}-value`
|
||||||
|
const listboxId = `${id}-listbox`
|
||||||
|
const anchorRef = useRef<HTMLButtonElement>(null)
|
||||||
const listRef = useRef<HTMLUListElement>(null)
|
const listRef = useRef<HTMLUListElement>(null)
|
||||||
|
|
||||||
const popperOptions = useMemo<PopperProps['popperOptions']>(
|
const popperOptions = useMemo<PopperProps['popperOptions']>(
|
||||||
@@ -283,17 +276,15 @@ const PersistentSelect = ({
|
|||||||
|
|
||||||
const closeFromKeyboard = (event: KeyboardEvent) => {
|
const closeFromKeyboard = (event: KeyboardEvent) => {
|
||||||
if (event.key === 'Tab') {
|
if (event.key === 'Tab') {
|
||||||
onClose()
|
onOpenChange(false)
|
||||||
} else if (event.key === 'Escape') {
|
} else if (event.key === 'Escape') {
|
||||||
onClose()
|
onOpenChange(false)
|
||||||
anchorRef.current
|
anchorRef.current?.focus({ preventScroll: true })
|
||||||
?.querySelector<HTMLElement>('[role="combobox"]')
|
|
||||||
?.focus()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
document.addEventListener('keydown', closeFromKeyboard)
|
document.addEventListener('keydown', closeFromKeyboard)
|
||||||
return () => document.removeEventListener('keydown', closeFromKeyboard)
|
return () => document.removeEventListener('keydown', closeFromKeyboard)
|
||||||
}, [onClose, open])
|
}, [onOpenChange, open])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ClickAwayListener
|
<ClickAwayListener
|
||||||
@@ -303,31 +294,92 @@ const PersistentSelect = ({
|
|||||||
if (target instanceof Node && keepOpenRef.current?.contains(target)) {
|
if (target instanceof Node && keepOpenRef.current?.contains(target)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
onClose()
|
onOpenChange(false)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Box>
|
<Box>
|
||||||
<FormControl ref={anchorRef} fullWidth variant="outlined" size="small">
|
<Box
|
||||||
<InputLabel id={labelId}>{label}</InputLabel>
|
sx={{
|
||||||
<Select
|
position: 'relative',
|
||||||
labelId={labelId}
|
borderRadius: 1,
|
||||||
value={value}
|
'& fieldset': {
|
||||||
open={false}
|
borderColor: (theme) =>
|
||||||
onOpen={(event) => {
|
alpha(theme.palette.text.primary, disabled ? 0.12 : 0.23),
|
||||||
if (open && event.type === 'mousedown') onClose()
|
},
|
||||||
else onOpen()
|
...(!disabled && {
|
||||||
}}
|
'&:hover fieldset': { borderColor: 'text.primary' },
|
||||||
label={label}
|
'&:focus-within fieldset': {
|
||||||
|
borderColor: 'primary.main',
|
||||||
|
borderWidth: 2,
|
||||||
|
},
|
||||||
|
'&:focus-within legend': { color: 'primary.main' },
|
||||||
|
}),
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<ButtonBase
|
||||||
|
ref={anchorRef}
|
||||||
|
type="button"
|
||||||
|
role="combobox"
|
||||||
|
aria-labelledby={`${labelId} ${valueId}`}
|
||||||
|
aria-haspopup="listbox"
|
||||||
|
aria-controls={open ? listboxId : undefined}
|
||||||
|
aria-expanded={open}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
renderValue={renderValue}
|
onClick={() => onOpenChange(!open)}
|
||||||
SelectDisplayProps={{
|
onKeyDown={(event) => {
|
||||||
'aria-controls': open ? listboxId : undefined,
|
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
||||||
'aria-expanded': open ? 'true' : 'false',
|
event.preventDefault()
|
||||||
|
onOpenChange(true)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
sx={{
|
||||||
|
width: '100%',
|
||||||
|
height: 40,
|
||||||
|
px: 1.75,
|
||||||
|
borderRadius: 'inherit',
|
||||||
|
justifyContent: 'space-between',
|
||||||
|
textAlign: 'left',
|
||||||
|
color: disabled ? 'text.disabled' : 'text.primary',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<MenuItem value={value}>{selectedName}</MenuItem>
|
<Box id={valueId} sx={{ minWidth: 0, flex: 1 }}>
|
||||||
</Select>
|
{displayValue}
|
||||||
</FormControl>
|
</Box>
|
||||||
|
<ArrowDropDown
|
||||||
|
sx={{
|
||||||
|
ml: 1,
|
||||||
|
color: disabled ? 'action.disabled' : 'action.active',
|
||||||
|
transform: open ? 'rotate(180deg)' : undefined,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</ButtonBase>
|
||||||
|
<Box
|
||||||
|
component="fieldset"
|
||||||
|
sx={{
|
||||||
|
position: 'absolute',
|
||||||
|
inset: '-5px 0 0',
|
||||||
|
m: 0,
|
||||||
|
px: 1,
|
||||||
|
minWidth: 0,
|
||||||
|
border: '1px solid',
|
||||||
|
borderRadius: 'inherit',
|
||||||
|
pointerEvents: 'none',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Box
|
||||||
|
component="legend"
|
||||||
|
id={labelId}
|
||||||
|
sx={{
|
||||||
|
px: 0.5,
|
||||||
|
typography: 'caption',
|
||||||
|
lineHeight: '11px',
|
||||||
|
color: disabled ? 'text.disabled' : 'text.secondary',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
|
||||||
<Popper
|
<Popper
|
||||||
open={open}
|
open={open}
|
||||||
@@ -354,7 +406,7 @@ const PersistentSelect = ({
|
|||||||
aria-labelledby={labelId}
|
aria-labelledby={labelId}
|
||||||
variant="selectedMenu"
|
variant="selectedMenu"
|
||||||
>
|
>
|
||||||
{children}
|
{open && renderOptions()}
|
||||||
</MenuList>
|
</MenuList>
|
||||||
</Paper>
|
</Paper>
|
||||||
</Popper>
|
</Popper>
|
||||||
@@ -364,10 +416,9 @@ const PersistentSelect = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface PersistentProxySelectProps
|
interface PersistentProxySelectProps
|
||||||
extends Omit<
|
extends Omit<PersistentSelectProps, 'displayValue' | 'renderOptions'> {
|
||||||
PersistentSelectProps,
|
value: string
|
||||||
'labelId' | 'listboxId' | 'renderValue' | 'children'
|
selectedName: string
|
||||||
> {
|
|
||||||
groupName: string
|
groupName: string
|
||||||
fixed?: string
|
fixed?: string
|
||||||
selectedDelay: number
|
selectedDelay: number
|
||||||
@@ -390,11 +441,7 @@ const PersistentProxySelect = ({
|
|||||||
return (
|
return (
|
||||||
<PersistentSelect
|
<PersistentSelect
|
||||||
{...props}
|
{...props}
|
||||||
labelId="proxy-select-label"
|
displayValue={
|
||||||
listboxId="current-proxy-node-listbox"
|
|
||||||
value={value}
|
|
||||||
selectedName={selectedName}
|
|
||||||
renderValue={() => (
|
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
@@ -421,67 +468,68 @@ const PersistentProxySelect = ({
|
|||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
)}
|
}
|
||||||
>
|
renderOptions={() =>
|
||||||
{options.map((option) => {
|
options.map((option) => {
|
||||||
const selected = option.value === value
|
const selected = option.value === value
|
||||||
const isFixed = option.name === fixed
|
const isFixed = option.name === fixed
|
||||||
const delay = option.disabled
|
const delay = option.disabled
|
||||||
? -1
|
? -1
|
||||||
: delayManager.getDelayFix(option.member, groupName)
|
: delayManager.getDelayFix(option.member, groupName)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MenuItem
|
<MenuItem
|
||||||
key={option.value}
|
key={option.value}
|
||||||
role="option"
|
role="option"
|
||||||
aria-selected={selected}
|
aria-selected={selected}
|
||||||
selected={selected}
|
selected={selected}
|
||||||
disabled={option.disabled}
|
disabled={option.disabled}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (!option.disabled) onChange(option.value)
|
if (!option.disabled) onChange(option.value)
|
||||||
}}
|
}}
|
||||||
sx={{
|
sx={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
justifyContent: 'space-between',
|
justifyContent: 'space-between',
|
||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
width: '100%',
|
width: '100%',
|
||||||
pr: 1,
|
pr: 1,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{isFixed && (
|
{isFixed && (
|
||||||
<span
|
<span
|
||||||
style={{
|
style={{
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
fontSize: '12px',
|
fontSize: '12px',
|
||||||
top: '-5px',
|
top: '-5px',
|
||||||
right: '5px',
|
right: '5px',
|
||||||
...(!fixedProxyInUsed && {
|
...(!fixedProxyInUsed && {
|
||||||
filter: 'grayscale(1)',
|
filter: 'grayscale(1)',
|
||||||
}),
|
}),
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
📌
|
📌
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
<Typography noWrap sx={{ flex: 1, mr: 1 }}>
|
<Typography noWrap sx={{ flex: 1, mr: 1 }}>
|
||||||
{option.name}
|
{option.name}
|
||||||
</Typography>
|
</Typography>
|
||||||
{!option.disabled && (
|
{!option.disabled && (
|
||||||
<Chip
|
<Chip
|
||||||
size="small"
|
size="small"
|
||||||
label={delayManager.formatDelay(delay)}
|
label={delayManager.formatDelay(delay)}
|
||||||
color={convertDelayColor(delay)}
|
color={convertDelayColor(delay)}
|
||||||
sx={{
|
sx={{
|
||||||
minWidth: '60px',
|
minWidth: '60px',
|
||||||
height: '22px',
|
height: '22px',
|
||||||
flexShrink: 0,
|
flexShrink: 0,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
)
|
)
|
||||||
})}
|
})
|
||||||
</PersistentSelect>
|
}
|
||||||
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -863,7 +911,7 @@ export const CurrentProxyCard = () => {
|
|||||||
|
|
||||||
const proxyOptions = useMemo(
|
const proxyOptions = useMemo(
|
||||||
() =>
|
() =>
|
||||||
isDirectMode
|
isDirectMode || openSelect !== 'proxy'
|
||||||
? []
|
? []
|
||||||
: sortProxyOptions(
|
: sortProxyOptions(
|
||||||
unsortedProxyOptions,
|
unsortedProxyOptions,
|
||||||
@@ -874,6 +922,7 @@ export const CurrentProxyCard = () => {
|
|||||||
[
|
[
|
||||||
delays,
|
delays,
|
||||||
isDirectMode,
|
isDirectMode,
|
||||||
|
openSelect,
|
||||||
unsortedProxyOptions,
|
unsortedProxyOptions,
|
||||||
sortType,
|
sortType,
|
||||||
defaultLatencyTimeout,
|
defaultLatencyTimeout,
|
||||||
@@ -1039,28 +1088,25 @@ export const CurrentProxyCard = () => {
|
|||||||
<Box sx={{ mb: 1.5 }}>
|
<Box sx={{ mb: 1.5 }}>
|
||||||
<PersistentSelect
|
<PersistentSelect
|
||||||
label={t('home.components.currentProxy.labels.group')}
|
label={t('home.components.currentProxy.labels.group')}
|
||||||
labelId="proxy-group-select-label"
|
displayValue={<Typography noWrap>{selectedGroupName}</Typography>}
|
||||||
listboxId="current-proxy-group-listbox"
|
|
||||||
value={selectedGroupName}
|
|
||||||
selectedName={selectedGroupName}
|
|
||||||
open={openSelect === 'group'}
|
open={openSelect === 'group'}
|
||||||
disabled={isGlobalMode || isDirectMode}
|
disabled={isGlobalMode || isDirectMode}
|
||||||
keepOpenRef={delayButtonRef}
|
keepOpenRef={delayButtonRef}
|
||||||
onOpen={() => setOpenSelect('group')}
|
onOpenChange={(open) => setOpenSelect(open ? 'group' : null)}
|
||||||
onClose={() => setOpenSelect(null)}
|
renderOptions={() =>
|
||||||
>
|
selectableGroups.map((group) => (
|
||||||
{selectableGroups.map((group) => (
|
<MenuItem
|
||||||
<MenuItem
|
key={group.name}
|
||||||
key={group.name}
|
role="option"
|
||||||
role="option"
|
aria-selected={group.name === selectedGroupName}
|
||||||
aria-selected={group.name === selectedGroupName}
|
selected={group.name === selectedGroupName}
|
||||||
selected={group.name === selectedGroupName}
|
onClick={() => handleGroupChange(group.name)}
|
||||||
onClick={() => handleGroupChange(group.name)}
|
>
|
||||||
>
|
<Typography noWrap>{group.name}</Typography>
|
||||||
<Typography noWrap>{group.name}</Typography>
|
</MenuItem>
|
||||||
</MenuItem>
|
))
|
||||||
))}
|
}
|
||||||
</PersistentSelect>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<PersistentProxySelect
|
<PersistentProxySelect
|
||||||
@@ -1074,12 +1120,11 @@ export const CurrentProxyCard = () => {
|
|||||||
}
|
}
|
||||||
selectedName={selectedProxyName}
|
selectedName={selectedProxyName}
|
||||||
selectedDelay={currentDelay}
|
selectedDelay={currentDelay}
|
||||||
options={isDirectMode ? [] : proxyOptions}
|
options={proxyOptions}
|
||||||
open={openSelect === 'proxy'}
|
open={openSelect === 'proxy'}
|
||||||
disabled={isDirectMode}
|
disabled={isDirectMode}
|
||||||
keepOpenRef={delayButtonRef}
|
keepOpenRef={delayButtonRef}
|
||||||
onOpen={() => setOpenSelect('proxy')}
|
onOpenChange={(open) => setOpenSelect(open ? 'proxy' : null)}
|
||||||
onClose={() => setOpenSelect(null)}
|
|
||||||
onChange={handleProxyChange}
|
onChange={handleProxyChange}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
Reference in New Issue
Block a user