Feature: provider order

This commit is contained in:
Dreamacro 2020-02-18 19:33:00 +08:00
parent 9cfd15b40a
commit 38dbc341c8
2 changed files with 8 additions and 2 deletions

View File

@ -1,9 +1,11 @@
import * as React from 'react'
import { useMemo } from 'react'
import { Card, Tag, Icon, Loading, useLoading } from '@components'
import { containers } from '@stores'
import { fromNow } from '@lib/date'
import { Provider as IProvider, Proxy as IProxy, updateProvider, healthCheckProvider } from '@lib/request'
import { Proxy } from '../Proxy'
import { compareDesc } from '../../'
import './style.scss'
interface ProvidersProps {
@ -29,6 +31,10 @@ export function Provider (props: ProvidersProps) {
updateProvider(provider.name).then(() => fetch()).finally(() => hide())
}
const proxies = useMemo(() => {
return (provider.proxies as IProxy[]).slice().sort((a, b) => -1 * compareDesc(a, b))
}, [provider.proxies])
return (
<Card className="proxy-provider">
<Loading visible={visible} />
@ -48,7 +54,7 @@ export function Provider (props: ProvidersProps) {
</div>
<ul className="proxies-list">
{
provider.proxies.map((p: IProxy) => (
proxies.map((p: IProxy) => (
<li key={p.name}>
<Proxy className="proxy-provider-item" config={p} />
</li>

View File

@ -21,7 +21,7 @@ const sortMap = {
[sortType.Desc]: 'sort-descending'
}
function compareDesc (a: API.Proxy, b: API.Proxy) {
export function compareDesc (a: API.Proxy, b: API.Proxy) {
const lastDelayA = a.history.length ? a.history.slice(-1)[0].delay : 0
const lastDelayB = b.history.length ? b.history.slice(-1)[0].delay : 0
return (lastDelayB || Number.MAX_SAFE_INTEGER) - (lastDelayA || Number.MAX_SAFE_INTEGER)