Skip to content

Commit edbe6e4

Browse files
committed
Cleanup args to take just the selected proxy
Delete extra hook Renames regions -> proxies
1 parent c868fc9 commit edbe6e4

File tree

3 files changed

+59
-75
lines changed

3 files changed

+59
-75
lines changed

site/src/contexts/ProxyContext.tsx

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import {
1212

1313
interface ProxyContextValue {
1414
proxy: PreferredProxy
15+
proxies?: Region[]
16+
// isfetched is true when the proxy api call is complete.
17+
isFetched: boolean
18+
// isLoading is true if the proxy is in the process of being fetched.
1519
isLoading: boolean
1620
error?: Error | unknown
17-
setProxy: (proxies: Region[], selectedProxy: Region | undefined) => void
21+
setProxy: (selectedProxy: Region) => void
1822
}
1923

2024
interface PreferredProxy {
@@ -30,6 +34,8 @@ interface PreferredProxy {
3034
preferredWildcardHostname: string
3135
}
3236

37+
export const ProxyContext = createContext<ProxyContextValue | undefined>(undefined)
38+
3339
/**
3440
* ProxyProvider interacts with local storage to indicate the preferred workspace proxy.
3541
*/
@@ -44,33 +50,37 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
4450
}
4551

4652
const [proxy, setProxy] = useState<PreferredProxy>(savedProxy)
47-
const setAndSaveProxy = (
48-
proxies: Region[],
49-
selectedProxy: Region | undefined,
50-
) => {
51-
const preferred = getPreferredProxy(proxies, selectedProxy)
52-
// Save to local storage to persist the user's preference across reloads
53-
// and other tabs.
54-
savePreferredProxy(preferred)
55-
// Set the state for the current context.
56-
setProxy(preferred)
57-
}
53+
5854

5955
const dashboard = useDashboard()
6056
const experimentEnabled = !dashboard?.experiments.includes("moons")
61-
const queryKey = ["get-regions"]
62-
const { error: regionsError, isLoading: regionsLoading } = useQuery({
57+
const queryKey = ["get-proxies"]
58+
const { data: proxies, error: proxiesError, isLoading: proxiesLoading, isFetched: proxiesFetched } = useQuery({
6359
queryKey,
6460
queryFn: getWorkspaceProxies,
6561
// This onSuccess ensures the local storage is synchronized with the
6662
// regions returned by coderd. If the selected region is not in the list,
6763
// then the user selection is removed.
68-
onSuccess: (data) => {
69-
setAndSaveProxy(data.regions, proxy.selectedProxy)
64+
onSuccess: () => {
65+
setAndSaveProxy(proxy.selectedProxy)
7066
},
7167
enabled: experimentEnabled,
7268
})
7369

70+
const setAndSaveProxy = (
71+
selectedProxy?: Region,
72+
) => {
73+
if (!proxies) {
74+
throw new Error("proxies are not yet loaded, so selecting a region makes no sense. How did you get here?")
75+
}
76+
const preferred = getPreferredProxy(proxies.regions, selectedProxy)
77+
// Save to local storage to persist the user's preference across reloads
78+
// and other tabs.
79+
savePreferredProxy(preferred)
80+
// Set the state for the current context.
81+
setProxy(preferred)
82+
}
83+
7484
// ******************************* //
7585
// ** This code can be removed **
7686
// ** when the experimental is **
@@ -80,6 +90,7 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
8090
data: applicationHostResult,
8191
error: appHostError,
8292
isLoading: appHostLoading,
93+
isFetched: appsFetched,
8394
} = useQuery({
8495
queryKey: appHostQueryKey,
8596
queryFn: getApplicationsHost,
@@ -94,13 +105,15 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
94105
return (
95106
<ProxyContext.Provider
96107
value={{
108+
proxies: [],
97109
proxy: {
98110
...value,
99111
preferredWildcardHostname:
100112
applicationHostResult?.host || value.preferredWildcardHostname,
101113
},
102114
isLoading: appHostLoading,
103115
error: appHostError,
116+
isFetched: appsFetched,
104117
setProxy: () => {
105118
// Does a noop
106119
},
@@ -118,9 +131,11 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
118131
return (
119132
<ProxyContext.Provider
120133
value={{
121-
proxy: proxy,
122-
isLoading: regionsLoading,
123-
error: regionsError,
134+
proxy,
135+
proxies: proxies?.regions,
136+
isLoading: proxiesLoading,
137+
isFetched: proxiesFetched,
138+
error: proxiesError,
124139
// A function that takes the new regions and selected region and updates
125140
// the state with the appropriate urls.
126141
setProxy: setAndSaveProxy,
@@ -146,57 +161,49 @@ export const useProxy = (): ProxyContextValue => {
146161
* assumed no proxy is configured and relative paths should be used.
147162
* Exported for testing.
148163
*
149-
* @param regions Is the list of regions returned by coderd. If this is empty, default behavior is used.
150-
* @param selectedRegion Is the region the user has selected. If this is undefined, default behavior is used.
164+
* @param proxies Is the list of regions returned by coderd. If this is empty, default behavior is used.
165+
* @param selectedProxy Is the region the user has selected. If this is undefined, default behavior is used.
151166
*/
152167
export const getPreferredProxy = (
153-
regions: Region[],
154-
selectedRegion?: Region,
168+
proxies: Region[],
169+
selectedProxy?: Region,
155170
): PreferredProxy => {
156171
// By default we set the path app to relative and disable wildcard hostnames.
157172
// We will set these values if we find a proxy we can use that supports them.
158173
let pathAppURL = ""
159174
let wildcardHostname = ""
160175

161-
// If a region is selected, make sure it is in the list of regions. If it is not
176+
// If a proxy is selected, make sure it is in the list of proxies. If it is not
162177
// we should default to the primary.
163-
selectedRegion = regions.find(
164-
(region) => selectedRegion && region.id === selectedRegion.id,
178+
selectedProxy = proxies.find(
179+
(proxy) => selectedProxy && proxy.id === selectedProxy.id,
165180
)
166181

167-
if (!selectedRegion) {
168-
// If no region is selected, default to the primary region.
169-
selectedRegion = regions.find((region) => region.name === "primary")
182+
if (!selectedProxy) {
183+
// If no proxy is selected, default to the primary proxy.
184+
selectedProxy = proxies.find((proxy) => proxy.name === "primary")
170185
}
171186

172-
// Only use healthy regions.
173-
if (selectedRegion && selectedRegion.healthy) {
174-
// By default use relative links for the primary region.
187+
// Only use healthy proxies.
188+
if (selectedProxy && selectedProxy.healthy) {
189+
// By default use relative links for the primary proxy.
175190
// This is the default, and we should not change it.
176-
if (selectedRegion.name !== "primary") {
177-
pathAppURL = selectedRegion.path_app_url
191+
if (selectedProxy.name !== "primary") {
192+
pathAppURL = selectedProxy.path_app_url
178193
}
179-
wildcardHostname = selectedRegion.wildcard_hostname
194+
wildcardHostname = selectedProxy.wildcard_hostname
180195
}
181196

182-
// TODO: @emyrk Should we notify the user if they had an unhealthy region selected?
197+
// TODO: @emyrk Should we notify the user if they had an unhealthy proxy selected?
183198

184199
return {
185-
selectedProxy: selectedRegion,
200+
selectedProxy: selectedProxy,
186201
// Trim trailing slashes to be consistent
187202
preferredPathAppURL: pathAppURL.replace(/\/$/, ""),
188203
preferredWildcardHostname: wildcardHostname,
189204
}
190205
}
191206

192-
export const ProxyContext = createContext<ProxyContextValue>({
193-
proxy: getPreferredProxy([]),
194-
isLoading: false,
195-
setProxy: () => {
196-
// Does a noop
197-
},
198-
})
199-
200207
// Local storage functions
201208

202209
export const savePreferredProxy = (saved: PreferredProxy): void => {
@@ -209,9 +216,5 @@ const loadPreferredProxy = (): PreferredProxy | undefined => {
209216
return undefined
210217
}
211218

212-
const proxy: PreferredProxy = JSON.parse(str)
213-
if (proxy.selectedProxy === undefined || proxy.selectedProxy === null) {
214-
return undefined
215-
}
216-
return proxy
219+
return JSON.parse(str)
217220
}

site/src/pages/UserSettingsPage/WorkspaceProxyPage/WorkspaceProxyPage.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { FC, PropsWithChildren } from "react"
22
import { Section } from "components/SettingsLayout/Section"
33
import { WorkspaceProxyView } from "./WorkspaceProxyView"
44
import makeStyles from "@material-ui/core/styles/makeStyles"
5-
import { useWorkspaceProxiesData } from "./hooks"
65
import { displayError } from "components/GlobalSnackbar/utils"
76
import { useProxy } from "contexts/ProxyContext"
87

@@ -14,14 +13,8 @@ export const WorkspaceProxyPage: FC<PropsWithChildren<unknown>> = () => {
1413
"workspace. To get the best experience, choose the workspace proxy that is" +
1514
"closest located to you."
1615

17-
const { proxy, setProxy } = useProxy()
16+
const { proxies, error: proxiesError, isFetched: proxiesFetched, isLoading: proxiesLoading, proxy, setProxy } = useProxy()
1817

19-
const {
20-
data: proxiesResponse,
21-
error: getProxiesError,
22-
isFetching,
23-
isFetched,
24-
} = useWorkspaceProxiesData()
2518

2619
return (
2720
<Section
@@ -31,19 +24,18 @@ export const WorkspaceProxyPage: FC<PropsWithChildren<unknown>> = () => {
3124
layout="fluid"
3225
>
3326
<WorkspaceProxyView
34-
proxies={proxiesResponse?.regions}
35-
isLoading={isFetching}
36-
hasLoaded={isFetched}
37-
getWorkspaceProxiesError={getProxiesError}
27+
proxies={proxies}
28+
isLoading={proxiesLoading}
29+
hasLoaded={proxiesFetched}
30+
getWorkspaceProxiesError={proxiesError}
3831
preferredProxy={proxy.selectedProxy}
3932
onSelect={(proxy) => {
4033
if (!proxy.healthy) {
4134
displayError("Please select a healthy workspace proxy.")
4235
return
4336
}
4437

45-
// Set the fetched regions + the selected proxy
46-
setProxy(proxiesResponse ? proxiesResponse.regions : [], proxy)
38+
setProxy(proxy)
4739
}}
4840
/>
4941
</Section>

site/src/pages/UserSettingsPage/WorkspaceProxyPage/hooks.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)