Skip to content

feat: UI/UX for regions #7283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 54 commits into from
Apr 28, 2023
Merged
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
26d3497
chore: Allow regular users to query for all workspaces
Emyrk Apr 24, 2023
3203ad7
Begin work on FE to add workspace proxy options to account settings
Emyrk Apr 24, 2023
a9ad485
Take origin file
Emyrk Apr 25, 2023
bde8870
Remove excess diffs
Emyrk Apr 25, 2023
69ce5f0
fixup! Remove excess diffs
Emyrk Apr 25, 2023
1daa32f
Update proxy page for regions endpoint
Emyrk Apr 25, 2023
b2e3efb
Some basic selector for proxies
Emyrk Apr 25, 2023
f78935f
Make hook for preferred proxy
Emyrk Apr 25, 2023
7a2e78e
Make fmt
Emyrk Apr 25, 2023
9b598e8
Typo
Emyrk Apr 25, 2023
17f9b00
Create workspace proxy context
Emyrk Apr 26, 2023
0683412
Use new context
Emyrk Apr 26, 2023
69c5734
fixup! Use new context
Emyrk Apr 26, 2023
9879476
WorkspaceProxy context syncs with coderd on region responses
Emyrk Apr 26, 2023
7d163fd
Make fmt
Emyrk Apr 26, 2023
e400810
Move dashboard provider
Emyrk Apr 26, 2023
02bcb84
Fix authenticated providers
Emyrk Apr 26, 2023
f9446c2
Fix authenticated renders
Emyrk Apr 26, 2023
9281333
Merge remote-tracking branch 'origin/main' into stevenmasley/regions
Emyrk Apr 26, 2023
8cc227f
Make fmt
Emyrk Apr 26, 2023
63dc985
Use auth render
Emyrk Apr 26, 2023
f4b6921
Fix terminal test render
Emyrk Apr 26, 2023
322fda6
Make fmt
Emyrk Apr 26, 2023
89efc57
Fix local storage load
Emyrk Apr 27, 2023
48a0beb
Fix terminals on the frontend to use proxies
Emyrk Apr 27, 2023
77d943f
Remove CSP hole
Emyrk Apr 27, 2023
75b8fd4
Add comment on origin patterns
Emyrk Apr 27, 2023
3391e84
Add unit test for getURLs
Emyrk Apr 27, 2023
4075b92
remove some TODOs
Emyrk Apr 27, 2023
b79b460
Add another store
Emyrk Apr 27, 2023
e879160
Update site/src/components/TerminalLink/TerminalLink.tsx
Emyrk Apr 27, 2023
27ef4a9
Fix stories
Emyrk Apr 27, 2023
eb38e95
Move providers into requrie auth
Emyrk Apr 27, 2023
1f8cae4
Fix imports
Emyrk Apr 27, 2023
6a22181
Fix 2 stories
Emyrk Apr 27, 2023
51bdaa2
Stories did not have subdomains on
Emyrk Apr 27, 2023
909801c
Merge remote-tracking branch 'origin/main' into stevenmasley/regions
Emyrk Apr 27, 2023
836c5a4
Fmt after merge
Emyrk Apr 27, 2023
0d0ed87
Fix port forward story
Emyrk Apr 27, 2023
6ed5fe4
ProxyPageView -> ProxyView
Emyrk Apr 27, 2023
163bbff
PR comment cleanup
Emyrk Apr 27, 2023
7dee309
Fix moon feature flag panic
Emyrk Apr 27, 2023
b7cfb39
Make fmt
Emyrk Apr 27, 2023
6b19118
Fix typo
Emyrk Apr 27, 2023
3fed785
Rename getUrls
Emyrk Apr 28, 2023
5bb44e8
Rename regions to proxies
Emyrk Apr 28, 2023
c868fc9
Only do 1 api call based on experiment
Emyrk Apr 28, 2023
edbe6e4
Cleanup args to take just the selected proxy
Emyrk Apr 28, 2023
eb6493c
Renames regions -> proxies
Emyrk Apr 28, 2023
017b3db
Fix stories
Emyrk Apr 28, 2023
c59a6ba
Move funciton back to bottom
Emyrk Apr 28, 2023
87e0b6d
Fix onSuccess of proxy provider
Emyrk Apr 28, 2023
fef5b00
Make fmt
Emyrk Apr 28, 2023
d33371d
Simplify some ts
Emyrk Apr 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! Use new context
  • Loading branch information
Emyrk committed Apr 26, 2023
commit 69c573443265e018ec5d76cd2087de3d823fb2a3
159 changes: 159 additions & 0 deletions site/src/contexts/ProxyContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { Region } from "api/typesGenerated"
import { useDashboard } from "components/Dashboard/DashboardProvider"
import { createContext, FC, PropsWithChildren, useContext, useState } from "react"

interface ProxyContextValue {
value: PreferredProxy
setValue: (regions: Region[], selectedRegion: Region | undefined) => void
}

interface PreferredProxy {
// Regions is a list of all the regions returned by coderd.
regions: Region[]
// SelectedRegion is the region the user has selected.
selectedRegion: Region | undefined
// PreferredPathAppURL is the URL of the proxy or it is the empty string
// to indicte using relative paths. To add a path to this:
// PreferredPathAppURL + "/path/to/app"
preferredPathAppURL: string
// PreferredWildcardHostname is a hostname that includes a wildcard.
// TODO: If the preferred proxy does not have this set, should we default to'
// the primary's?
// Example: "*.example.com"
preferredWildcardHostname: string
}

const ProxyContext = createContext<ProxyContextValue | undefined>(undefined)

/**
* ProxyProvider interacts with local storage to indicate the preferred workspace proxy.
*/
export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
// Try to load the preferred proxy from local storage.
let savedProxy = loadPreferredProxy()
if (!savedProxy) {
savedProxy = getURLs([], undefined)
}

// The initial state is no regions and no selected region.
const [state, setState] = useState<PreferredProxy>(savedProxy)

// ******************************* //
// ** This code can be removed **
// ** when the experimental is **
// ** dropped ** //
const dashboard = useDashboard()
// If the experiment is disabled, then make the setState do a noop.
// This preserves an empty state, which is the default behavior.
if (!dashboard?.experiments.includes("moons")) {
return (
<ProxyContext.Provider value={{
value: getURLs([], undefined),
setValue: () => {
// Does a noop
},
}}>
{children}
</ProxyContext.Provider >
)
}
// ******************************* //

// TODO: @emyrk Should make an api call to /regions endpoint to update the
// regions list.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to kick this to later.


return (
<ProxyContext.Provider value={{
value: state,
// A function that takes the new regions and selected region and updates
// the state with the appropriate urls.
setValue: (regions, selectedRegion) => {
const preferred = getURLs(regions, selectedRegion)
// Save to local storage to persist the user's preference across reloads
// and other tabs.
savePreferredProxy(preferred)
// Set the state for the current context.
setState(preferred)
},
}}>
{children}
</ProxyContext.Provider >
)
}

export const useProxy = (): ProxyContextValue => {
const context = useContext(ProxyContext)

if (!context) {
throw new Error("useProxy should be used inside of <ProxyProvider />")
}

return context
}


/**
* getURLs is a helper function to calculate the urls to use for a given proxy configuration. By default, it is
* assumed no proxy is configured and relative paths should be used.
*
* @param regions Is the list of regions returned by coderd. If this is empty, default behavior is used.
* @param selectedRegion Is the region the user has selected. If this is undefined, default behavior is used.
*/
const getURLs = (regions: Region[], selectedRegion: Region | undefined): PreferredProxy => {
// By default we set the path app to relative and disable wilcard hostnames.
// We will set these values if we find a proxy we can use that supports them.
let pathAppURL = ""
let wildcardHostname = ""

if (selectedRegion === undefined) {
// If no region is selected, default to the primary region.
selectedRegion = regions.find((region) => region.name === "primary")
} else {
// If a region is selected, make sure it is in the list of regions. If it is not
// we should default to the primary.
selectedRegion = regions.find((region) => region.id === selectedRegion?.id)
}

// Only use healthy regions.
if (selectedRegion && selectedRegion.healthy) {
// By default use relative links for the primary region.
// This is the default, and we should not change it.
if (selectedRegion.name !== "primary") {
pathAppURL = selectedRegion.path_app_url
}
wildcardHostname = selectedRegion.wildcard_hostname
}

// TODO: @emyrk Should we notify the user if they had an unhealthy region selected?


return {
regions: regions,
selectedRegion: selectedRegion,
// Trim trailing slashes to be consistent
preferredPathAppURL: pathAppURL.replace(/\/$/, ""),
preferredWildcardHostname: wildcardHostname,
}
}

// Local storage functions

export const savePreferredProxy = (saved: PreferredProxy): void => {
window.localStorage.setItem("preferred-proxy", JSON.stringify(saved))
}

export const loadPreferredProxy = (): PreferredProxy | undefined => {
const str = localStorage.getItem("preferred-proxy")
if (str === undefined || str === null) {
return undefined
}
const proxy = JSON.parse(str)
if (proxy.id === undefined || proxy.id === null) {
return undefined
}
return proxy
}

export const clearPreferredProxy = (): void => {
localStorage.removeItem("preferred-proxy")
}