Skip to content

Commit 3fed785

Browse files
committed
Rename getUrls
1 parent 6b19118 commit 3fed785

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
MockWorkspaceApp,
88
} from "testHelpers/entities"
99
import { AppLink, AppLinkProps } from "./AppLink"
10-
import { ProxyContext, getURLs } from "contexts/ProxyContext"
10+
import { ProxyContext, getPreferredProxy } from "contexts/ProxyContext"
1111

1212
export default {
1313
title: "components/AppLink",
@@ -17,7 +17,7 @@ export default {
1717
const Template: Story<AppLinkProps> = (args) => (
1818
<ProxyContext.Provider
1919
value={{
20-
proxy: getURLs(MockRegions, MockPrimaryRegion),
20+
proxy: getPreferredProxy(MockRegions, MockPrimaryRegion),
2121
isLoading: false,
2222
setProxy: () => {
2323
return

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
MockWorkspaceApp,
1919
} from "testHelpers/entities"
2020
import { AgentRow, AgentRowProps } from "./AgentRow"
21-
import { ProxyContext, getURLs } from "contexts/ProxyContext"
21+
import { ProxyContext, getPreferredProxy } from "contexts/ProxyContext"
2222
import { Region } from "api/typesGenerated"
2323

2424
export default {
@@ -56,7 +56,7 @@ const TemplateFC = (
5656
return (
5757
<ProxyContext.Provider
5858
value={{
59-
proxy: getURLs(regions, selectedRegion),
59+
proxy: getPreferredProxy(regions, selectedRegion),
6060
isLoading: false,
6161
setProxy: () => {
6262
return

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Story } from "@storybook/react"
33
import { MockWorkspace, MockWorkspaceResource } from "testHelpers/entities"
44
import { AgentRow } from "./AgentRow"
55
import { ResourceCard, ResourceCardProps } from "./ResourceCard"
6-
import { ProxyContext, getURLs } from "contexts/ProxyContext"
6+
import { ProxyContext, getPreferredProxy } from "contexts/ProxyContext"
77

88
export default {
99
title: "components/ResourceCard",
@@ -18,7 +18,7 @@ Example.args = {
1818
agentRow: (agent) => (
1919
<ProxyContext.Provider
2020
value={{
21-
proxy: getURLs([], undefined),
21+
proxy: getPreferredProxy([], undefined),
2222
isLoading: false,
2323
setProxy: () => {
2424
return
@@ -82,7 +82,7 @@ BunchOfMetadata.args = {
8282
agentRow: (agent) => (
8383
<ProxyContext.Provider
8484
value={{
85-
proxy: getURLs([], undefined),
85+
proxy: getPreferredProxy([], undefined),
8686
isLoading: false,
8787
setProxy: () => {
8888
return

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as Mocks from "../../testHelpers/entities"
66
import { Workspace, WorkspaceErrors, WorkspaceProps } from "./Workspace"
77
import { withReactContext } from "storybook-react-context"
88
import EventSource from "eventsourcemock"
9-
import { ProxyContext, getURLs } from "contexts/ProxyContext"
9+
import { ProxyContext, getPreferredProxy } from "contexts/ProxyContext"
1010

1111
export default {
1212
title: "components/Workspace",
@@ -26,7 +26,7 @@ export default {
2626
const Template: Story<WorkspaceProps> = (args) => (
2727
<ProxyContext.Provider
2828
value={{
29-
proxy: getURLs([], undefined),
29+
proxy: getPreferredProxy([], undefined),
3030
isLoading: false,
3131
setProxy: () => {
3232
return

site/src/contexts/ProxyContext.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
MockRegions,
44
MockHealthyWildRegion,
55
} from "testHelpers/entities"
6-
import { getURLs } from "./ProxyContext"
6+
import { getPreferredProxy } from "./ProxyContext"
77

88
describe("ProxyContextGetURLs", () => {
99
it.each([
@@ -43,7 +43,7 @@ describe("ProxyContextGetURLs", () => {
4343
])(
4444
`%p`,
4545
(_, regions, selected, preferredPathAppURL, preferredWildcardHostname) => {
46-
const preferred = getURLs(regions, selected)
46+
const preferred = getPreferredProxy(regions, selected)
4747
expect(preferred.preferredPathAppURL).toBe(preferredPathAppURL)
4848
expect(preferred.preferredWildcardHostname).toBe(
4949
preferredWildcardHostname,

site/src/contexts/ProxyContext.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
4141
// Try to load the preferred proxy from local storage.
4242
let savedProxy = loadPreferredProxy()
4343
if (!savedProxy) {
44-
savedProxy = getURLs([])
44+
// If no preferred proxy is saved, then default to using relative paths
45+
// and no subdomain support until the regions are properly loaded.
46+
// This is the same as a user not selecting any proxy.
47+
savedProxy = getPreferredProxy([])
4548
}
4649

47-
// The initial state is no regions and no selected region.
4850
const [proxy, setProxy] = useState<PreferredProxy>(savedProxy)
4951
const setAndSaveProxy = (
5052
regions: Region[],
5153
selectedRegion: Region | undefined,
5254
) => {
53-
const preferred = getURLs(regions, selectedRegion)
55+
const preferred = getPreferredProxy(regions, selectedRegion)
5456
// Save to local storage to persist the user's preference across reloads
5557
// and other tabs.
5658
savePreferredProxy(preferred)
@@ -88,7 +90,7 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
8890
// If the experiment is disabled, then make the setState do a noop.
8991
// This preserves an empty state, which is the default behavior.
9092
if (!dashboard?.experiments.includes("moons")) {
91-
const value = getURLs([])
93+
const value = getPreferredProxy([])
9294

9395
return (
9496
<ProxyContext.Provider
@@ -148,7 +150,7 @@ export const useProxy = (): ProxyContextValue => {
148150
* @param regions Is the list of regions returned by coderd. If this is empty, default behavior is used.
149151
* @param selectedRegion Is the region the user has selected. If this is undefined, default behavior is used.
150152
*/
151-
export const getURLs = (
153+
export const getPreferredProxy = (
152154
regions: Region[],
153155
selectedRegion?: Region,
154156
): PreferredProxy => {
@@ -194,18 +196,15 @@ export const savePreferredProxy = (saved: PreferredProxy): void => {
194196
window.localStorage.setItem("preferred-proxy", JSON.stringify(saved))
195197
}
196198

197-
export const loadPreferredProxy = (): PreferredProxy | undefined => {
199+
const loadPreferredProxy = (): PreferredProxy | undefined => {
198200
const str = localStorage.getItem("preferred-proxy")
199-
if (str === undefined || str === null) {
201+
if (!str) {
200202
return undefined
201203
}
204+
202205
const proxy: PreferredProxy = JSON.parse(str)
203206
if (proxy.selectedRegion === undefined || proxy.selectedRegion === null) {
204207
return undefined
205208
}
206209
return proxy
207210
}
208-
209-
export const clearPreferredProxy = (): void => {
210-
localStorage.removeItem("preferred-proxy")
211-
}

0 commit comments

Comments
 (0)