@@ -12,9 +12,13 @@ import {
12
12
13
13
interface ProxyContextValue {
14
14
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.
15
19
isLoading : boolean
16
20
error ?: Error | unknown
17
- setProxy : ( proxies : Region [ ] , selectedProxy : Region | undefined ) => void
21
+ setProxy : ( selectedProxy : Region ) => void
18
22
}
19
23
20
24
interface PreferredProxy {
@@ -30,6 +34,8 @@ interface PreferredProxy {
30
34
preferredWildcardHostname : string
31
35
}
32
36
37
+ export const ProxyContext = createContext < ProxyContextValue | undefined > ( undefined )
38
+
33
39
/**
34
40
* ProxyProvider interacts with local storage to indicate the preferred workspace proxy.
35
41
*/
@@ -44,33 +50,37 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
44
50
}
45
51
46
52
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
+
58
54
59
55
const dashboard = useDashboard ( )
60
56
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 ( {
63
59
queryKey,
64
60
queryFn : getWorkspaceProxies ,
65
61
// This onSuccess ensures the local storage is synchronized with the
66
62
// regions returned by coderd. If the selected region is not in the list,
67
63
// then the user selection is removed.
68
- onSuccess : ( data ) => {
69
- setAndSaveProxy ( data . regions , proxy . selectedProxy )
64
+ onSuccess : ( ) => {
65
+ setAndSaveProxy ( proxy . selectedProxy )
70
66
} ,
71
67
enabled : experimentEnabled ,
72
68
} )
73
69
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
+
74
84
// ******************************* //
75
85
// ** This code can be removed **
76
86
// ** when the experimental is **
@@ -80,6 +90,7 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
80
90
data : applicationHostResult ,
81
91
error : appHostError ,
82
92
isLoading : appHostLoading ,
93
+ isFetched : appsFetched ,
83
94
} = useQuery ( {
84
95
queryKey : appHostQueryKey ,
85
96
queryFn : getApplicationsHost ,
@@ -94,13 +105,15 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
94
105
return (
95
106
< ProxyContext . Provider
96
107
value = { {
108
+ proxies : [ ] ,
97
109
proxy : {
98
110
...value ,
99
111
preferredWildcardHostname :
100
112
applicationHostResult ?. host || value . preferredWildcardHostname ,
101
113
} ,
102
114
isLoading : appHostLoading ,
103
115
error : appHostError ,
116
+ isFetched : appsFetched ,
104
117
setProxy : ( ) => {
105
118
// Does a noop
106
119
} ,
@@ -118,9 +131,11 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
118
131
return (
119
132
< ProxyContext . Provider
120
133
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 ,
124
139
// A function that takes the new regions and selected region and updates
125
140
// the state with the appropriate urls.
126
141
setProxy : setAndSaveProxy ,
@@ -146,57 +161,49 @@ export const useProxy = (): ProxyContextValue => {
146
161
* assumed no proxy is configured and relative paths should be used.
147
162
* Exported for testing.
148
163
*
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.
151
166
*/
152
167
export const getPreferredProxy = (
153
- regions : Region [ ] ,
154
- selectedRegion ?: Region ,
168
+ proxies : Region [ ] ,
169
+ selectedProxy ?: Region ,
155
170
) : PreferredProxy => {
156
171
// By default we set the path app to relative and disable wildcard hostnames.
157
172
// We will set these values if we find a proxy we can use that supports them.
158
173
let pathAppURL = ""
159
174
let wildcardHostname = ""
160
175
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
162
177
// 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 ,
165
180
)
166
181
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" )
170
185
}
171
186
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 .
175
190
// 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
178
193
}
179
- wildcardHostname = selectedRegion . wildcard_hostname
194
+ wildcardHostname = selectedProxy . wildcard_hostname
180
195
}
181
196
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?
183
198
184
199
return {
185
- selectedProxy : selectedRegion ,
200
+ selectedProxy : selectedProxy ,
186
201
// Trim trailing slashes to be consistent
187
202
preferredPathAppURL : pathAppURL . replace ( / \/ $ / , "" ) ,
188
203
preferredWildcardHostname : wildcardHostname ,
189
204
}
190
205
}
191
206
192
- export const ProxyContext = createContext < ProxyContextValue > ( {
193
- proxy : getPreferredProxy ( [ ] ) ,
194
- isLoading : false ,
195
- setProxy : ( ) => {
196
- // Does a noop
197
- } ,
198
- } )
199
-
200
207
// Local storage functions
201
208
202
209
export const savePreferredProxy = ( saved : PreferredProxy ) : void => {
@@ -209,9 +216,5 @@ const loadPreferredProxy = (): PreferredProxy | undefined => {
209
216
return undefined
210
217
}
211
218
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 )
217
220
}
0 commit comments