@@ -34,54 +34,6 @@ 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
-
85
37
export const ProxyContext = createContext < ProxyContextValue | undefined > ( undefined )
86
38
87
39
/**
@@ -177,6 +129,55 @@ export const useProxy = (): ProxyContextValue => {
177
129
return context
178
130
}
179
131
132
+ /**
133
+ * getURLs is a helper function to calculate the urls to use for a given proxy configuration. By default, it is
134
+ * assumed no proxy is configured and relative paths should be used.
135
+ * Exported for testing.
136
+ *
137
+ * @param proxies Is the list of proxies returned by coderd. If this is empty, default behavior is used.
138
+ * @param selectedProxy Is the proxy the user has selected. If this is undefined, default behavior is used.
139
+ */
140
+ export const getPreferredProxy = (
141
+ proxies : Region [ ] ,
142
+ selectedProxy ?: Region ,
143
+ ) : PreferredProxy => {
144
+ // By default we set the path app to relative and disable wildcard hostnames.
145
+ // We will set these values if we find a proxy we can use that supports them.
146
+ let pathAppURL = ""
147
+ let wildcardHostname = ""
148
+
149
+ // If a proxy is selected, make sure it is in the list of proxies. If it is not
150
+ // we should default to the primary.
151
+ selectedProxy = proxies . find (
152
+ ( proxy ) => selectedProxy && proxy . id === selectedProxy . id ,
153
+ )
154
+
155
+ if ( ! selectedProxy ) {
156
+ // If no proxy is selected, default to the primary proxy.
157
+ selectedProxy = proxies . find ( ( proxy ) => proxy . name === "primary" )
158
+ }
159
+
160
+ // Only use healthy proxies.
161
+ if ( selectedProxy && selectedProxy . healthy ) {
162
+ // By default use relative links for the primary proxy.
163
+ // This is the default, and we should not change it.
164
+ if ( selectedProxy . name !== "primary" ) {
165
+ pathAppURL = selectedProxy . path_app_url
166
+ }
167
+ wildcardHostname = selectedProxy . wildcard_hostname
168
+ }
169
+
170
+ // TODO: @emyrk Should we notify the user if they had an unhealthy proxy selected?
171
+
172
+ return {
173
+ selectedProxy : selectedProxy ,
174
+ // Trim trailing slashes to be consistent
175
+ preferredPathAppURL : pathAppURL . replace ( / \/ $ / , "" ) ,
176
+ preferredWildcardHostname : wildcardHostname ,
177
+ }
178
+ }
179
+
180
+
180
181
// Local storage functions
181
182
182
183
export const savePreferredProxy = ( saved : PreferredProxy ) : void => {
0 commit comments