Skip to content

Commit a23006a

Browse files
committed
feat: support new benchmark api
1 parent 15b5f6f commit a23006a

File tree

12 files changed

+121
-22
lines changed

12 files changed

+121
-22
lines changed

src/components/VersionSupport.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/** @jsx jsx */
22
import { jsx } from '@emotion/core'
33
import React from 'react'
4-
import gte from 'semver/functions/gte'
54

6-
import { usePlatform, usePlatformVersion } from '../models/profile'
5+
import { useVersionSupport } from '../hooks'
76

87
interface VersionSupportProps {
98
macos?: string
@@ -15,16 +14,9 @@ const VersionSupport: React.FC<VersionSupportProps> = ({
1514
ios,
1615
children,
1716
}) => {
18-
const platform = usePlatform()
19-
const platformVersion = usePlatformVersion()
17+
const isSupported = useVersionSupport({ macos, ios })
2018

21-
if (!platform || !platformVersion) return null
22-
23-
if (macos && platform === 'macos' && gte(platformVersion, macos)) {
24-
return <React.Fragment>{children}</React.Fragment>
25-
}
26-
27-
if (ios && platform === 'ios' && gte(platformVersion, ios)) {
19+
if (isSupported) {
2820
return <React.Fragment>{children}</React.Fragment>
2921
}
3022

src/hooks/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './useSetState'
2+
export * from './useVersionSupport'

src/hooks/use-set-state.test.ts renamed to src/hooks/useSetState.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { renderHook, act } from '@testing-library/react-hooks'
22

3-
import useSetState from './use-set-state'
3+
import { useSetState } from './useSetState'
44

55
describe('useSetState', () => {
66
type State = { a: string; b: string }

src/hooks/use-set-state.ts renamed to src/hooks/useSetState.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useState } from 'react'
22

3-
const useSetState = <State = any>(initialState: State | (() => State)) => {
3+
export const useSetState = <State = any>(
4+
initialState: State | (() => State),
5+
) => {
46
const [state, setState] = useState<State>(initialState)
57

68
const getState = async (): Promise<State> => {
@@ -21,5 +23,3 @@ const useSetState = <State = any>(initialState: State | (() => State)) => {
2123
typeof getState,
2224
]
2325
}
24-
25-
export default useSetState

src/hooks/useVersionSupport.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import gte from 'semver/functions/gte'
2+
3+
import { usePlatform, usePlatformVersion } from '../models/profile'
4+
5+
interface VersionSupportProps {
6+
macos?: string
7+
ios?: string
8+
}
9+
10+
export const useVersionSupport = ({
11+
macos,
12+
ios,
13+
}: VersionSupportProps): boolean => {
14+
const platform = usePlatform()
15+
const platformVersion = usePlatformVersion()
16+
17+
if (!platform || !platformVersion) {
18+
return false
19+
}
20+
21+
if (macos && platform === 'macos' && gte(platformVersion, macos)) {
22+
return true
23+
}
24+
25+
if (ios && platform === 'ios' && gte(platformVersion, ios)) {
26+
return true
27+
}
28+
29+
return false
30+
}

src/pages/Index/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ const Page: React.FC = () => {
109109
>
110110
{profile && (
111111
<div tw="w-full flex justify-between items-center">
112-
<div
113-
tw="w-2/3"
114-
onDoubleClick={() => window.location.reload(true)}
115-
>
112+
<div tw="w-2/3" onDoubleClick={() => window.location.reload()}>
116113
<HostInfo />
117114
</div>
118115

src/pages/Landing/Regular.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { useTranslation } from 'react-i18next'
1414
import ChangeLanguage from '../../components/ChangeLanguage'
1515
import ProfileCell from '../../components/ProfileCell'
1616
import Ad from '../../components/Ad'
17-
import useSetState from '../../hooks/use-set-state'
17+
import { useSetState } from '../../hooks'
1818
import { useProfileDispatch } from '../../models/profile'
1919
import { Profile } from '../../types'
2020
import { ExistingProfiles, LastUsedProfile } from '../../utils/constant'

src/pages/Landing/Surge.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { find } from 'lodash-es'
1212
import { useHistory } from 'react-router-dom'
1313

1414
import ChangeLanguage from '../../components/ChangeLanguage'
15-
import useSetState from '../../hooks/use-set-state'
15+
import { useSetState } from '../../hooks'
1616
import { useProfile, useProfileDispatch } from '../../models/profile'
1717
import { Profile } from '../../types'
1818
import { ExistingProfiles, LastUsedProfile } from '../../utils/constant'

src/pages/Policies/components/PolicyGroup.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import {
1313
Policy,
1414
SelectPolicyTestResult,
1515
UrlTestPolicyTestResult,
16+
PolicyBenchmarkResults,
1617
} from '../../../types'
1718
import fetcher from '../../../utils/fetcher'
19+
import { mutatePolicyPerformanceResults } from '../usePolicyPerformance'
1820

1921
interface PolicyGroupProps {
2022
policyGroupName: string
2123
policyGroup: Policy[]
24+
policyPerformanceResults?: PolicyBenchmarkResults
2225
}
2326

2427
const LoadingOverlay = styled.div`
@@ -42,6 +45,7 @@ const latencyResultStyle = (latency: number) => {
4245
const PolicyGroup: React.FC<PolicyGroupProps> = ({
4346
policyGroupName,
4447
policyGroup,
48+
policyPerformanceResults,
4549
}) => {
4650
const { t } = useTranslation()
4751
const [isInViewport, targetRef] = useIsInViewport({ threshold: 10 })
@@ -97,6 +101,11 @@ const PolicyGroup: React.FC<PolicyGroupProps> = ({
97101
[name: string]: number
98102
} = {}
99103

104+
if (policyPerformanceResults) {
105+
mutatePolicyPerformanceResults()
106+
return
107+
}
108+
100109
if (res.winner) {
101110
const testResult = (res as UrlTestPolicyTestResult).results[0].data
102111
Object.keys(testResult).forEach((key) => {
@@ -134,6 +143,31 @@ const PolicyGroup: React.FC<PolicyGroupProps> = ({
134143
}).then((res) => res.policy)
135144
}, [policyGroupName])
136145

146+
useEffect(() => {
147+
if (!policyPerformanceResults) {
148+
return
149+
}
150+
151+
const latencies: {
152+
[name: string]: number
153+
} = {}
154+
155+
policyGroup.forEach((policy) => {
156+
Object.keys(policyPerformanceResults).forEach((key) => {
157+
if (policy.lineHash === key) {
158+
latencies[policy.name] =
159+
policyPerformanceResults[key].lastTestScoreInMS === 0
160+
? -1
161+
: Number(
162+
policyPerformanceResults[key].lastTestScoreInMS.toFixed(0),
163+
)
164+
}
165+
})
166+
})
167+
168+
setLatencies(latencies)
169+
}, [policyGroup, policyPerformanceResults])
170+
137171
useEffect(() => {
138172
let isMounted = true
139173

@@ -152,7 +186,7 @@ const PolicyGroup: React.FC<PolicyGroupProps> = ({
152186

153187
return (
154188
<div ref={targetRef}>
155-
<Card shadow="single" tw="relative overflow-hidden px-3 md:px-4">
189+
<Card tw="relative overflow-hidden px-3 md:px-4">
156190
{isLoading && (
157191
<LoadingOverlay>
158192
<Spinner />

src/pages/Policies/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import PageContainer from '../../components/PageContainer'
1414
import { Policies, PolicyGroups } from '../../types'
1515
import fetcher from '../../utils/fetcher'
1616
import PolicyGroup from './components/PolicyGroup'
17+
import { usePolicyPerformance } from './usePolicyPerformance'
1718

1819
const PolicyNameItem = styled.div`
1920
${tw`flex-shrink-0 bg-gray-200 rounded-md px-3 py-2 mr-3 overflow-hidden cursor-pointer hover:bg-gray-300 transition-colors ease-in-out duration-200`}
@@ -34,6 +35,7 @@ const Page: React.FC = () => {
3435
return createRef<HTMLDivElement>()
3536
})
3637
const headerRef = useRef<HTMLDivElement>(null)
38+
const { data: policyPerformanceResults } = usePolicyPerformance()
3739

3840
const getRefTop = (ref: RefObject<HTMLDivElement>): number => {
3941
const ele = ref.current
@@ -107,6 +109,7 @@ const Page: React.FC = () => {
107109
<PolicyGroup
108110
policyGroupName={policy}
109111
policyGroup={policyGroups[policy]}
112+
policyPerformanceResults={policyPerformanceResults}
110113
/>
111114
</div>
112115
)

0 commit comments

Comments
 (0)