Skip to content

Commit 0683412

Browse files
committed
Use new context
1 parent 17f9b00 commit 0683412

File tree

3 files changed

+11
-50
lines changed

3 files changed

+11
-50
lines changed

site/src/app.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ErrorBoundary } from "./components/ErrorBoundary/ErrorBoundary"
99
import { GlobalSnackbar } from "./components/GlobalSnackbar/GlobalSnackbar"
1010
import { dark } from "./theme"
1111
import "./theme/globalFonts"
12+
import { ProxyProvider } from "contexts/ProxyContext"
1213

1314
const queryClient = new QueryClient({
1415
defaultOptions: {
@@ -29,8 +30,10 @@ export const AppProviders: FC<PropsWithChildren> = ({ children }) => {
2930
<ErrorBoundary>
3031
<QueryClientProvider client={queryClient}>
3132
<AuthProvider>
32-
{children}
33-
<GlobalSnackbar />
33+
<ProxyProvider>
34+
{children}
35+
<GlobalSnackbar />
36+
</ProxyProvider>
3437
</AuthProvider>
3538
</QueryClientProvider>
3639
</ErrorBoundary>

site/src/pages/UserSettingsPage/WorkspaceProxyPage/WorkspaceProxyPage.tsx

+5-31
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Trans } from "react-i18next"
66
import { useWorkspaceProxiesData } from "./hooks"
77
import { Region } from "api/typesGenerated"
88
import { displayError } from "components/GlobalSnackbar/utils"
9+
import { useProxy } from "contexts/ProxyContext"
910
// import { ConfirmDeleteDialog } from "./components"
1011
// import { Stack } from "components/Stack/Stack"
1112
// import Button from "@material-ui/core/Button"
@@ -24,7 +25,7 @@ export const WorkspaceProxyPage: FC<PropsWithChildren<unknown>> = () => {
2425
</Trans>
2526
)
2627

27-
const [preferred, setPreferred] = useState(getPreferredProxy())
28+
const { value, setValue } = useProxy()
2829

2930
const {
3031
data: response,
@@ -46,21 +47,15 @@ export const WorkspaceProxyPage: FC<PropsWithChildren<unknown>> = () => {
4647
isLoading={isFetching}
4748
hasLoaded={isFetched}
4849
getWorkspaceProxiesError={getProxiesError}
49-
preferredProxy={preferred}
50+
preferredProxy={value.selectedRegion}
5051
onSelect={(proxy) => {
5152
if (!proxy.healthy) {
5253
displayError("Please select a healthy workspace proxy.")
5354
return
5455
}
55-
// normProxy is a normalized proxy to
56-
const normProxy = {
57-
...proxy,
58-
// Trim trailing slashes to be consistent
59-
path_app_url: proxy.path_app_url.replace(/\/$/, ""),
60-
}
6156

62-
savePreferredProxy(normProxy)
63-
setPreferred(normProxy)
57+
// Set the fetched regions + the selected proxy
58+
setValue(response ? response.regions : [], proxy)
6459
}}
6560
/>
6661
</Section>
@@ -81,24 +76,3 @@ const useStyles = makeStyles((theme) => ({
8176
}))
8277

8378
export default WorkspaceProxyPage
84-
85-
// Exporting to be used in the tests
86-
export const savePreferredProxy = (proxy: Region): void => {
87-
window.localStorage.setItem("preferred-proxy", JSON.stringify(proxy))
88-
}
89-
90-
export const getPreferredProxy = (): Region | undefined => {
91-
const str = localStorage.getItem("preferred-proxy")
92-
if (str === undefined || str === null) {
93-
return undefined
94-
}
95-
const proxy = JSON.parse(str)
96-
if (proxy.id === undefined || proxy.id === null) {
97-
return undefined
98-
}
99-
return proxy
100-
}
101-
102-
export const clearPreferredProxy = (): void => {
103-
localStorage.removeItem("preferred-proxy")
104-
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import {
22
useQuery,
3-
useMutation,
4-
useQueryClient,
5-
QueryKey,
63
} from "@tanstack/react-query"
7-
import { deleteToken, getWorkspaceProxies } from "api/api"
4+
import { getWorkspaceProxies } from "api/api"
85

96
// Loads all workspace proxies
107
export const useWorkspaceProxiesData = () => {
@@ -16,16 +13,3 @@ export const useWorkspaceProxiesData = () => {
1613
...result,
1714
}
1815
}
19-
20-
// Delete a token
21-
export const useDeleteToken = (queryKey: QueryKey) => {
22-
const queryClient = useQueryClient()
23-
24-
return useMutation({
25-
mutationFn: deleteToken,
26-
onSuccess: () => {
27-
// Invalidate and refetch
28-
void queryClient.invalidateQueries(queryKey)
29-
},
30-
})
31-
}

0 commit comments

Comments
 (0)