@@ -58,11 +58,11 @@ export interface ProxyContextValue {
5858}
5959
6060interface PreferredProxy {
61- //selectedProxy is the preferred proxy being used. It is provided for
61+ //proxy is the proxy being used. It is provided for
6262// getting the fields such as "display_name" and "id"
6363// Do not use the fields 'path_app_url' or 'wildcard_hostname' from this
6464// object. Use the preferred fields.
65- selectedProxy :Region | undefined
65+ proxy :Region | undefined
6666// PreferredPathAppURL is the URL of the proxy or it is the empty string
6767// to indicate using relative paths. To add a path to this:
6868// PreferredPathAppURL + "/path/to/app"
@@ -86,12 +86,9 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
8686
8787// As the proxies are being loaded, default to using the saved proxy.
8888// If the saved proxy is not valid when the async fetch happens, the
89- // selectedProxy will be updated accordingly.
90- let defaultPreferredProxy :PreferredProxy = {
91- selectedProxy :savedProxy ,
92- preferredPathAppURL :savedProxy ?. path_app_url . replace ( / \/ $ / , "" ) || "" ,
93- preferredWildcardHostname :savedProxy ?. wildcard_hostname || "" ,
94- }
89+ // proxy will be updated accordingly.
90+ let defaultPreferredProxy = computeUsableURLS ( savedProxy )
91+
9592if ( ! savedProxy ) {
9693// If no preferred proxy is saved, then default to using relative paths
9794// and no subdomain support until the proxies are properly loaded.
@@ -206,12 +203,12 @@ export const useProxy = (): ProxyContextValue => {
206203}
207204
208205/**
209- *getURLs is a helper function to calculate the urls to use for a given proxy configuration. By default, it is
206+ *getPreferredProxy is a helper function to calculate the urls to use for a given proxy configuration. By default, it is
210207 * assumed no proxy is configured and relative paths should be used.
211208 * Exported for testing.
212209 *
213210 *@param proxies Is the list of proxies returned by coderd. If this is empty, default behavior is used.
214- *@param selectedProxy Is the proxythe user has selected . If this is undefined, default behavior is used.
211+ *@param selectedProxy Is the proxysaved in local storage . If this is undefined, default behavior is used.
215212 *@param latencies If provided, this is used to determine the best proxy to default to.
216213 * If not, `primary` is always the best default.
217214 */
@@ -220,11 +217,6 @@ export const getPreferredProxy = (
220217selectedProxy ?:Region ,
221218latencies ?:Record < string , ProxyLatencyReport > ,
222219) :PreferredProxy => {
223- // By default we set the path app to relative and disable wildcard hostnames.
224- // We will set these values if we find a proxy we can use that supports them.
225- let pathAppURL = ""
226- let wildcardHostname = ""
227-
228220// If a proxy is selected, make sure it is in the list of proxies. If it is not
229221// we should default to the primary.
230222selectedProxy = proxies . find (
@@ -263,23 +255,31 @@ export const getPreferredProxy = (
263255}
264256}
265257
266- // Only use healthy proxies.
267- if ( selectedProxy && selectedProxy . healthy ) {
258+ return computeUsableURLS ( selectedProxy )
259+ }
260+
261+ const computeUsableURLS = ( proxy ?:Region ) :PreferredProxy => {
262+ if ( ! proxy ) {
268263// By default use relative links for the primary proxy.
269264// This is the default, and we should not change it.
270- if ( selectedProxy . name !== "primary" ) {
271- pathAppURL = selectedProxy . path_app_url
265+ return {
266+ proxy :undefined ,
267+ preferredPathAppURL :"" ,
268+ preferredWildcardHostname :"" ,
272269}
273- wildcardHostname = selectedProxy . wildcard_hostname
274270}
275271
276- // TODO:@emyrk Should we notify the user if they had an unhealthy proxy selected?
272+ let pathAppURL = proxy ?. path_app_url . replace ( / \/ $ / , "" )
273+ // Primary proxy uses relative paths. It's the only exception.
274+ if ( proxy . name === "primary" ) {
275+ pathAppURL = ""
276+ }
277277
278278return {
279- selectedProxy : selectedProxy ,
279+ proxy : proxy ,
280280// Trim trailing slashes to be consistent
281- preferredPathAppURL :pathAppURL . replace ( / \/ $ / , "" ) ,
282- preferredWildcardHostname :wildcardHostname ,
281+ preferredPathAppURL :pathAppURL ,
282+ preferredWildcardHostname :proxy . wildcard_hostname ,
283283}
284284}
285285