@@ -2,7 +2,13 @@ import { useQuery } from "@tanstack/react-query"
2
2
import { getApplicationsHost , getWorkspaceProxies } from "api/api"
3
3
import { Region } from "api/typesGenerated"
4
4
import { useDashboard } from "components/Dashboard/DashboardProvider"
5
- import { createContext , FC , PropsWithChildren , useContext , useState } from "react"
5
+ import {
6
+ createContext ,
7
+ FC ,
8
+ PropsWithChildren ,
9
+ useContext ,
10
+ useState ,
11
+ } from "react"
6
12
7
13
interface ProxyContextValue {
8
14
proxy : PreferredProxy
@@ -17,7 +23,7 @@ interface PreferredProxy {
17
23
// object. Use the preferred fields.
18
24
selectedRegion : Region | undefined
19
25
// PreferredPathAppURL is the URL of the proxy or it is the empty string
20
- // to indicte using relative paths. To add a path to this:
26
+ // to indicate using relative paths. To add a path to this:
21
27
// PreferredPathAppURL + "/path/to/app"
22
28
preferredPathAppURL : string
23
29
// PreferredWildcardHostname is a hostname that includes a wildcard.
@@ -38,7 +44,10 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
38
44
39
45
// The initial state is no regions and no selected region.
40
46
const [ proxy , setProxy ] = useState < PreferredProxy > ( savedProxy )
41
- const setAndSaveProxy = ( regions : Region [ ] , selectedRegion : Region | undefined ) => {
47
+ const setAndSaveProxy = (
48
+ regions : Region [ ] ,
49
+ selectedRegion : Region | undefined ,
50
+ ) => {
42
51
const preferred = getURLs ( regions , selectedRegion )
43
52
// Save to local storage to persist the user's preference across reloads
44
53
// and other tabs.
@@ -51,7 +60,7 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
51
60
const { error : regionsError , isLoading : regionsLoading } = useQuery ( {
52
61
queryKey,
53
62
queryFn : getWorkspaceProxies ,
54
- // This onSucccess ensures the local storage is synchronized with the
63
+ // This onSuccess ensures the local storage is synchronized with the
55
64
// regions returned by coderd. If the selected region is not in the list,
56
65
// then the user selection is removed.
57
66
onSuccess : ( data ) => {
@@ -61,11 +70,15 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
61
70
62
71
// ******************************* //
63
72
// ** This code can be removed **
64
- // ** when the experimental is **
73
+ // ** when the experimental is **
65
74
// ** dropped ** //
66
75
const dashboard = useDashboard ( )
67
76
const appHostQueryKey = [ "get-application-host" ]
68
- const { data : applicationHostResult , error : appHostError , isLoading : appHostLoading } = useQuery ( {
77
+ const {
78
+ data : applicationHostResult ,
79
+ error : appHostError ,
80
+ isLoading : appHostLoading ,
81
+ } = useQuery ( {
69
82
queryKey : appHostQueryKey ,
70
83
queryFn : getApplicationsHost ,
71
84
} )
@@ -75,19 +88,22 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
75
88
const value = getURLs ( [ ] )
76
89
77
90
return (
78
- < ProxyContext . Provider value = { {
79
- proxy : {
80
- ...value ,
81
- preferredWildcardHostname : applicationHostResult ?. host || value . preferredWildcardHostname ,
82
- } ,
83
- isLoading : appHostLoading ,
84
- error : appHostError ,
85
- setProxy : ( ) => {
86
- // Does a noop
87
- } ,
88
- } } >
91
+ < ProxyContext . Provider
92
+ value = { {
93
+ proxy : {
94
+ ...value ,
95
+ preferredWildcardHostname :
96
+ applicationHostResult ?. host || value . preferredWildcardHostname ,
97
+ } ,
98
+ isLoading : appHostLoading ,
99
+ error : appHostError ,
100
+ setProxy : ( ) => {
101
+ // Does a noop
102
+ } ,
103
+ } }
104
+ >
89
105
{ children }
90
- </ ProxyContext . Provider >
106
+ </ ProxyContext . Provider >
91
107
)
92
108
}
93
109
// ******************************* //
@@ -96,16 +112,18 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
96
112
// regions list.
97
113
98
114
return (
99
- < ProxyContext . Provider value = { {
100
- proxy : proxy ,
101
- isLoading : regionsLoading ,
102
- error : regionsError ,
103
- // A function that takes the new regions and selected region and updates
104
- // the state with the appropriate urls.
105
- setProxy : setAndSaveProxy ,
106
- } } >
115
+ < ProxyContext . Provider
116
+ value = { {
117
+ proxy : proxy ,
118
+ isLoading : regionsLoading ,
119
+ error : regionsError ,
120
+ // A function that takes the new regions and selected region and updates
121
+ // the state with the appropriate urls.
122
+ setProxy : setAndSaveProxy ,
123
+ } }
124
+ >
107
125
{ children }
108
- </ ProxyContext . Provider >
126
+ </ ProxyContext . Provider >
109
127
)
110
128
}
111
129
@@ -119,23 +137,27 @@ export const useProxy = (): ProxyContextValue => {
119
137
return context
120
138
}
121
139
122
-
123
140
/**
124
141
* getURLs is a helper function to calculate the urls to use for a given proxy configuration. By default, it is
125
142
* assumed no proxy is configured and relative paths should be used.
126
- *
143
+ *
127
144
* @param regions Is the list of regions returned by coderd. If this is empty, default behavior is used.
128
145
* @param selectedRegion Is the region the user has selected. If this is undefined, default behavior is used.
129
146
*/
130
- const getURLs = ( regions : Region [ ] , selectedRegion ?: Region ) : PreferredProxy => {
147
+ const getURLs = (
148
+ regions : Region [ ] ,
149
+ selectedRegion ?: Region ,
150
+ ) : PreferredProxy => {
131
151
// By default we set the path app to relative and disable wilcard hostnames.
132
152
// We will set these values if we find a proxy we can use that supports them.
133
153
let pathAppURL = ""
134
154
let wildcardHostname = ""
135
155
136
156
// If a region is selected, make sure it is in the list of regions. If it is not
137
157
// we should default to the primary.
138
- selectedRegion = regions . find ( ( region ) => selectedRegion && region . id === selectedRegion . id )
158
+ selectedRegion = regions . find (
159
+ ( region ) => selectedRegion && region . id === selectedRegion . id ,
160
+ )
139
161
140
162
if ( ! selectedRegion ) {
141
163
// If no region is selected, default to the primary region.
0 commit comments