@@ -34,6 +34,54 @@ interface PreferredProxy {
34
34
preferredWildcardHostname : string
35
35
}
36
36
37
+ /**
38
+ * getURLs is a helper function to calculate the urls to use for a given proxy configuration. By default, it is
39
+ * assumed no proxy is configured and relative paths should be used.
40
+ * Exported for testing.
41
+ *
42
+ * @param proxies Is the list of proxies returned by coderd. If this is empty, default behavior is used.
43
+ * @param selectedProxy Is the proxy the user has selected. If this is undefined, default behavior is used.
44
+ */
45
+ export const getPreferredProxy = (
46
+ proxies : Region [ ] ,
47
+ selectedProxy ?: Region ,
48
+ ) : PreferredProxy => {
49
+ // By default we set the path app to relative and disable wildcard hostnames.
50
+ // We will set these values if we find a proxy we can use that supports them.
51
+ let pathAppURL = ""
52
+ let wildcardHostname = ""
53
+
54
+ // If a proxy is selected, make sure it is in the list of proxies. If it is not
55
+ // we should default to the primary.
56
+ selectedProxy = proxies . find (
57
+ ( proxy ) => selectedProxy && proxy . id === selectedProxy . id ,
58
+ )
59
+
60
+ if ( ! selectedProxy ) {
61
+ // If no proxy is selected, default to the primary proxy.
62
+ selectedProxy = proxies . find ( ( proxy ) => proxy . name === "primary" )
63
+ }
64
+
65
+ // Only use healthy proxies.
66
+ if ( selectedProxy && selectedProxy . healthy ) {
67
+ // By default use relative links for the primary proxy.
68
+ // This is the default, and we should not change it.
69
+ if ( selectedProxy . name !== "primary" ) {
70
+ pathAppURL = selectedProxy . path_app_url
71
+ }
72
+ wildcardHostname = selectedProxy . wildcard_hostname
73
+ }
74
+
75
+ // TODO: @emyrk Should we notify the user if they had an unhealthy proxy selected?
76
+
77
+ return {
78
+ selectedProxy : selectedProxy ,
79
+ // Trim trailing slashes to be consistent
80
+ preferredPathAppURL : pathAppURL . replace ( / \/ $ / , "" ) ,
81
+ preferredWildcardHostname : wildcardHostname ,
82
+ }
83
+ }
84
+
37
85
export const ProxyContext = createContext < ProxyContextValue | undefined > ( undefined )
38
86
39
87
/**
@@ -90,59 +138,32 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
90
138
data : applicationHostResult ,
91
139
error : appHostError ,
92
140
isLoading : appHostLoading ,
93
- isFetched : appsFetched ,
141
+ isFetched : appHostFetched ,
94
142
} = useQuery ( {
95
143
queryKey : appHostQueryKey ,
96
144
queryFn : getApplicationsHost ,
97
145
enabled : ! experimentEnabled ,
98
146
} )
99
147
100
- // If the experiment is disabled, then make the setState do a noop.
101
- // This preserves an empty state, which is the default behavior.
102
- if ( ! experimentEnabled ) {
103
- const value = getPreferredProxy ( [ ] )
104
-
105
- return (
106
- < ProxyContext . Provider
107
- value = { {
108
- proxies : [ ] ,
109
- proxy : {
110
- ...value ,
111
- preferredWildcardHostname :
112
- applicationHostResult ?. host || value . preferredWildcardHostname ,
113
- } ,
114
- isLoading : appHostLoading ,
115
- error : appHostError ,
116
- isFetched : appsFetched ,
117
- setProxy : ( ) => {
118
- // Does a noop
119
- } ,
120
- } }
121
- >
122
- { children }
123
- </ ProxyContext . Provider >
124
- )
125
- }
126
- // ******************************* //
127
-
128
- // TODO: @emyrk Should make an api call to /regions endpoint to update the
129
- // proxies list.
130
-
131
148
return (
132
149
< ProxyContext . Provider
133
150
value = { {
134
- proxy,
135
- proxies : proxies ?. regions ,
136
- isLoading : proxiesLoading ,
137
- isFetched : proxiesFetched ,
138
- error : proxiesError ,
151
+ proxy : experimentEnabled ? proxy : {
152
+ ...getPreferredProxy ( [ ] ) ,
153
+ preferredWildcardHostname :
154
+ applicationHostResult ?. host || "" ,
155
+ } ,
156
+ proxies : experimentEnabled ? proxies ?. regions : [ ] ,
157
+ isLoading : experimentEnabled ? proxiesLoading : appHostLoading ,
158
+ isFetched : experimentEnabled ? proxiesFetched : appHostFetched ,
159
+ error : experimentEnabled ? proxiesError : appHostError ,
139
160
// A function that takes the new proxies and selected proxy and updates
140
161
// the state with the appropriate urls.
141
162
setProxy : setAndSaveProxy ,
142
163
} }
143
164
>
144
165
{ children }
145
- </ ProxyContext . Provider >
166
+ </ ProxyContext . Provider >
146
167
)
147
168
}
148
169
@@ -156,54 +177,6 @@ export const useProxy = (): ProxyContextValue => {
156
177
return context
157
178
}
158
179
159
- /**
160
- * getURLs is a helper function to calculate the urls to use for a given proxy configuration. By default, it is
161
- * assumed no proxy is configured and relative paths should be used.
162
- * Exported for testing.
163
- *
164
- * @param proxies Is the list of proxies returned by coderd. If this is empty, default behavior is used.
165
- * @param selectedProxy Is the proxy the user has selected. If this is undefined, default behavior is used.
166
- */
167
- export const getPreferredProxy = (
168
- proxies : Region [ ] ,
169
- selectedProxy ?: Region ,
170
- ) : PreferredProxy => {
171
- // By default we set the path app to relative and disable wildcard hostnames.
172
- // We will set these values if we find a proxy we can use that supports them.
173
- let pathAppURL = ""
174
- let wildcardHostname = ""
175
-
176
- // If a proxy is selected, make sure it is in the list of proxies. If it is not
177
- // we should default to the primary.
178
- selectedProxy = proxies . find (
179
- ( proxy ) => selectedProxy && proxy . id === selectedProxy . id ,
180
- )
181
-
182
- if ( ! selectedProxy ) {
183
- // If no proxy is selected, default to the primary proxy.
184
- selectedProxy = proxies . find ( ( proxy ) => proxy . name === "primary" )
185
- }
186
-
187
- // Only use healthy proxies.
188
- if ( selectedProxy && selectedProxy . healthy ) {
189
- // By default use relative links for the primary proxy.
190
- // This is the default, and we should not change it.
191
- if ( selectedProxy . name !== "primary" ) {
192
- pathAppURL = selectedProxy . path_app_url
193
- }
194
- wildcardHostname = selectedProxy . wildcard_hostname
195
- }
196
-
197
- // TODO: @emyrk Should we notify the user if they had an unhealthy proxy selected?
198
-
199
- return {
200
- selectedProxy : selectedProxy ,
201
- // Trim trailing slashes to be consistent
202
- preferredPathAppURL : pathAppURL . replace ( / \/ $ / , "" ) ,
203
- preferredWildcardHostname : wildcardHostname ,
204
- }
205
- }
206
-
207
180
// Local storage functions
208
181
209
182
export const savePreferredProxy = ( saved : PreferredProxy ) : void => {
0 commit comments