Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Clash Verge
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
many-archive
Clash Verge
Commits
6a8ffe16
There was an error fetching the commit references. Please try again later.
Unverified
Commit
6a8ffe16
authored
3 years ago
by
GyDi
Browse files
Options
Downloads
Patches
Plain Diff
feat: enable change proxy mode
parent
3a73868c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/pages/proxies.tsx
+87
-16
87 additions, 16 deletions
src/pages/proxies.tsx
with
87 additions
and
16 deletions
src/pages/proxies.tsx
+
87
−
16
View file @
6a8ffe16
import
useSWR
,
{
useSWRConfig
}
from
"
swr
"
;
import
{
useEffect
}
from
"
react
"
;
import
{
List
,
Paper
}
from
"
@mui/material
"
;
import
{
useEffect
,
useMemo
,
useRef
,
useState
}
from
"
react
"
;
import
{
Button
,
ButtonGroup
,
List
,
Paper
}
from
"
@mui/material
"
;
import
{
getClashConfig
,
updateConfigs
,
updateProxy
}
from
"
../services/api
"
;
import
{
patchClashConfig
}
from
"
../services/cmds
"
;
import
{
getProxies
}
from
"
../services/api
"
;
import
BasePage
from
"
../components/base-page
"
;
import
ProxyItem
from
"
../components/proxy-item
"
;
...
...
@@ -9,34 +11,103 @@ import ProxyGroup from "../components/proxy-group";
const
ProxyPage
=
()
=>
{
const
{
mutate
}
=
useSWRConfig
();
const
{
data
:
proxiesData
}
=
useSWR
(
"
getProxies
"
,
getProxies
);
const
{
groups
=
[],
proxies
=
[]
}
=
proxiesData
??
{};
const
{
data
:
clashConfig
}
=
useSWR
(
"
getClashConfig
"
,
getClashConfig
);
const
[
curProxy
,
setCurProxy
]
=
useState
<
string
>
(
"
DIRECT
"
);
const
curMode
=
clashConfig
?.
mode
.
toLowerCase
();
// proxy groups
const
{
groups
=
[]
}
=
proxiesData
??
{};
// proxies and sorted
const
filterProxies
=
useMemo
(()
=>
{
if
(
!
proxiesData
?.
proxies
)
return
[];
const
list
=
Object
.
values
(
proxiesData
.
proxies
);
const
retList
=
list
.
filter
(
(
p
)
=>
!
p
.
all
?.
length
&&
p
.
name
!==
"
DIRECT
"
&&
p
.
name
!==
"
REJECT
"
);
const
direct
=
list
.
filter
((
p
)
=>
p
.
name
===
"
DIRECT
"
);
const
reject
=
list
.
filter
((
p
)
=>
p
.
name
===
"
REJECT
"
);
return
direct
.
concat
(
retList
).
concat
(
reject
);
},
[
proxiesData
]);
const
modeList
=
[
"
rule
"
,
"
global
"
,
"
direct
"
];
const
asGroup
=
curMode
===
"
rule
"
||
!
groups
.
length
;
// make sure that fetch the proxies successfully
useEffect
(()
=>
{
if
(
(
curMode
===
"
rule
"
&&
!
groups
.
length
)
||
(
curMode
===
"
global
"
&&
filterProxies
.
length
<
4
)
)
{
setTimeout
(()
=>
mutate
(
"
getProxies
"
),
500
);
}
},
[
groups
,
filterProxies
,
curMode
]);
// update the current proxy
useEffect
(()
=>
{
// fix the empty proxies on the first sight
// this bud only show on the build version
// call twice to avoid something unknown or the delay of the clash startup
setTimeout
(()
=>
mutate
(
"
getProxies
"
),
250
);
setTimeout
(()
=>
mutate
(
"
getProxies
"
),
1000
);
},
[]);
if
(
curMode
===
"
direct
"
)
setCurProxy
(
"
DIRECT
"
);
if
(
curMode
===
"
global
"
)
{
const
globalNow
=
proxiesData
?.
proxies
?.
GLOBAL
?.
now
;
setCurProxy
(
globalNow
||
"
DIRECT
"
);
}
},
[
curMode
,
proxiesData
]);
const
changeLockRef
=
useRef
(
false
);
const
onChangeMode
=
async
(
mode
:
string
)
=>
{
if
(
changeLockRef
.
current
)
return
;
changeLockRef
.
current
=
true
;
try
{
// switch rapidly
await
updateConfigs
({
mode
});
await
patchClashConfig
({
mode
});
mutate
(
"
getClashConfig
"
);
}
finally
{
changeLockRef
.
current
=
false
;
}
};
const
onChangeProxy
=
async
(
name
:
string
)
=>
{
if
(
curMode
!==
"
global
"
)
return
;
await
updateProxy
(
"
GLOBAL
"
,
name
);
setCurProxy
(
name
);
};
return
(
<
BasePage
title
=
{
groups
.
length
?
"
Proxy Groups
"
:
"
Proxies
"
}
>
<
BasePage
title
=
{
asGroup
?
"
Proxy Groups
"
:
"
Proxies
"
}
header
=
{
<
ButtonGroup
size
=
"small"
>
{
modeList
.
map
((
mode
)
=>
(
<
Button
key
=
{
mode
}
variant
=
{
mode
===
curMode
?
"
contained
"
:
"
outlined
"
}
onClick
=
{
()
=>
onChangeMode
(
mode
)
}
sx
=
{
{
textTransform
:
"
capitalize
"
}
}
>
{
mode
}
</
Button
>
))
}
</
ButtonGroup
>
}
>
<
Paper
sx
=
{
{
borderRadius
:
1
,
boxShadow
:
2
,
mb
:
1
}
}
>
{
g
roup
s
.
length
>
0
&&
(
{
asG
roup
?
(
<
List
>
{
groups
.
map
((
group
)
=>
(
<
ProxyGroup
key
=
{
group
.
name
}
group
=
{
group
}
/>
))
}
</
List
>
)
}
{
!
groups
.
length
&&
(
)
:
(
// todo: virtual list
<
List
>
{
Object
.
values
(
p
roxies
)
.
map
((
proxy
)
=>
(
{
filterP
roxies
.
map
((
proxy
)
=>
(
<
ProxyItem
key
=
{
proxy
.
name
}
proxy
=
{
proxy
}
selected
=
{
false
}
selected
=
{
proxy
.
name
===
curProxy
}
onClick
=
{
onChangeProxy
}
sx
=
{
{
py
:
0
,
px
:
2
}
}
/>
))
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment