Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8c07ff

Browse files
authored
feat: Auto select workspace proxy based on lowest latency (#7515)
* feat: Proxy auto select and user selection state * chore: Auto select based on latency * Add extra test for unknown latencies * Mock latencies for unit tests
1 parent f9a97c2 commit b8c07ff

File tree

12 files changed

+589
-144
lines changed

12 files changed

+589
-144
lines changed

site/jest.setup.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Blob } from "buffer"
88
import jestFetchMock from "jest-fetch-mock"
99
import { ProxyLatencyReport } from "contexts/useProxyLatency"
1010
import { RegionsResponse } from "api/typesGenerated"
11+
import { useMemo } from "react"
1112

1213
jestFetchMock.enableMocks()
1314

@@ -16,20 +17,25 @@ jestFetchMock.enableMocks()
1617
// actual network requests. So just globally mock this hook.
1718
jest.mock("contexts/useProxyLatency", () => ({
1819
useProxyLatency: (proxies?: RegionsResponse) => {
19-
if (!proxies) {
20-
return {} as Record<string, ProxyLatencyReport>
21-
}
22-
23-
return proxies.regions.reduce((acc, proxy) => {
24-
acc[proxy.id] = {
25-
accurate: true,
26-
// Return a constant latency of 8ms.
27-
// If you make this random it could break stories.
28-
latencyMS: 8,
29-
at: new Date(),
20+
// Must use `useMemo` here to avoid infinite loop.
21+
// Mocking the hook with a hook.
22+
const latencies = useMemo(() => {
23+
if (!proxies) {
24+
return {} as Record<string, ProxyLatencyReport>
3025
}
31-
return acc
32-
}, {} as Record<string, ProxyLatencyReport>)
26+
return proxies.regions.reduce((acc, proxy) => {
27+
acc[proxy.id] = {
28+
accurate: true,
29+
// Return a constant latency of 8ms.
30+
// If you make this random it could break stories.
31+
latencyMS: 8,
32+
at: new Date(),
33+
}
34+
return acc
35+
}, {} as Record<string, ProxyLatencyReport>)
36+
}, [proxies])
37+
38+
return latencies
3339
},
3440
}))
3541

site/src/components/AppLink/AppLink.stories.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const Template: Story<AppLinkProps> = (args) => (
2626
setProxy: () => {
2727
return
2828
},
29+
clearProxy: () => {
30+
return
31+
},
2932
}}
3033
>
3134
<AppLink {...args} />

site/src/components/Resources/AgentRow.stories.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ const TemplateFC = (
6565
setProxy: () => {
6666
return
6767
},
68+
clearProxy: () => {
69+
return
70+
},
6871
}}
6972
>
7073
<AgentRow {...args} />

site/src/components/Resources/ResourceCard.stories.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Example.args = {
3030
setProxy: () => {
3131
return
3232
},
33+
clearProxy: () => {
34+
return
35+
},
3336
}}
3437
>
3538
<AgentRow
@@ -97,6 +100,9 @@ BunchOfMetadata.args = {
97100
setProxy: () => {
98101
return
99102
},
103+
clearProxy: () => {
104+
return
105+
},
100106
}}
101107
>
102108
<AgentRow

site/src/components/Workspace/Workspace.stories.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const Template: Story<WorkspaceProps> = (args) => (
4747
proxies: [],
4848
isLoading: false,
4949
isFetched: true,
50+
clearProxy: () => {
51+
return
52+
},
5053
setProxy: () => {
5154
return
5255
},

site/src/contexts/ProxyContext.test.ts

-62
This file was deleted.

0 commit comments

Comments
 (0)