Skip to content

Commit 017b3db

Browse files
committed
Fix stories
1 parent eb6493c commit 017b3db

File tree

6 files changed

+72
-86
lines changed

6 files changed

+72
-86
lines changed

site/src/components/AppLink/AppLink.stories.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const Template: Story<AppLinkProps> = (args) => (
1818
<ProxyContext.Provider
1919
value={{
2020
proxy: getPreferredProxy(MockWorkspaceProxies, MockPrimaryWorkspaceProxy),
21+
proxies: MockWorkspaceProxies,
2122
isLoading: false,
23+
isFetched: true,
2224
setProxy: () => {
2325
return
2426
},

site/src/components/Resources/AgentRow.stories.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ const TemplateFC = (
5757
<ProxyContext.Provider
5858
value={{
5959
proxy: getPreferredProxy(proxies, selectedProxy),
60+
proxies: proxies,
6061
isLoading: false,
62+
isFetched: true,
6163
setProxy: () => {
6264
return
6365
},

site/src/components/Resources/ResourceCard.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Example.args = {
1919
<ProxyContext.Provider
2020
value={{
2121
proxy: getPreferredProxy([], undefined),
22+
proxies: [],
2223
isLoading: false,
24+
isFetched: true,
2325
setProxy: () => {
2426
return
2527
},
@@ -83,7 +85,9 @@ BunchOfMetadata.args = {
8385
<ProxyContext.Provider
8486
value={{
8587
proxy: getPreferredProxy([], undefined),
88+
proxies: [],
8689
isLoading: false,
90+
isFetched: true,
8791
setProxy: () => {
8892
return
8993
},

site/src/components/Workspace/Workspace.stories.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const Template: Story<WorkspaceProps> = (args) => (
2727
<ProxyContext.Provider
2828
value={{
2929
proxy: getPreferredProxy([], undefined),
30+
proxies: [],
3031
isLoading: false,
32+
isFetched: true,
3133
setProxy: () => {
3234
return
3335
},

site/src/contexts/ProxyContext.tsx

Lines changed: 59 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,54 @@ interface PreferredProxy {
3434
preferredWildcardHostname: string
3535
}
3636

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+
3785
export const ProxyContext = createContext<ProxyContextValue | undefined>(undefined)
3886

3987
/**
@@ -90,59 +138,32 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
90138
data: applicationHostResult,
91139
error: appHostError,
92140
isLoading: appHostLoading,
93-
isFetched: appsFetched,
141+
isFetched: appHostFetched,
94142
} = useQuery({
95143
queryKey: appHostQueryKey,
96144
queryFn: getApplicationsHost,
97145
enabled: !experimentEnabled,
98146
})
99147

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-
131148
return (
132149
<ProxyContext.Provider
133150
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,
139160
// A function that takes the new proxies and selected proxy and updates
140161
// the state with the appropriate urls.
141162
setProxy: setAndSaveProxy,
142163
}}
143164
>
144165
{children}
145-
</ProxyContext.Provider>
166+
</ProxyContext.Provider >
146167
)
147168
}
148169

@@ -156,54 +177,6 @@ export const useProxy = (): ProxyContextValue => {
156177
return context
157178
}
158179

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-
207180
// Local storage functions
208181

209182
export const savePreferredProxy = (saved: PreferredProxy): void => {

site/src/pages/TerminalPage/TerminalPage.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
MockPrimaryWorkspaceProxy,
77
MockWorkspace,
88
MockWorkspaceAgent,
9+
MockWorkspaceProxies,
910
} from "testHelpers/entities"
1011
import { TextDecoder, TextEncoder } from "util"
1112
import { ReconnectingPTYRequest } from "../../api/types"
@@ -47,6 +48,8 @@ const renderTerminal = () => {
4748
preferredPathAppURL: "",
4849
preferredWildcardHostname: "",
4950
},
51+
proxies: MockWorkspaceProxies,
52+
isFetched: true,
5053
isLoading: false,
5154
setProxy: jest.fn(),
5255
}}

0 commit comments

Comments
 (0)