-
Notifications
You must be signed in to change notification settings - Fork 894
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
feat: UI/UX for regions #7283
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 3203ad7
Begin work on FE to add workspace proxy options to account settings
Emyrk a9ad485
Take origin file
Emyrk bde8870
Remove excess diffs
Emyrk 69ce5f0
fixup! Remove excess diffs
Emyrk 1daa32f
Update proxy page for regions endpoint
Emyrk b2e3efb
Some basic selector for proxies
Emyrk f78935f
Make hook for preferred proxy
Emyrk 7a2e78e
Make fmt
Emyrk 9b598e8
Typo
Emyrk 17f9b00
Create workspace proxy context
Emyrk 0683412
Use new context
Emyrk 69c5734
fixup! Use new context
Emyrk 9879476
WorkspaceProxy context syncs with coderd on region responses
Emyrk 7d163fd
Make fmt
Emyrk e400810
Move dashboard provider
Emyrk 02bcb84
Fix authenticated providers
Emyrk f9446c2
Fix authenticated renders
Emyrk 9281333
Merge remote-tracking branch 'origin/main' into stevenmasley/regions
Emyrk 8cc227f
Make fmt
Emyrk 63dc985
Use auth render
Emyrk f4b6921
Fix terminal test render
Emyrk 322fda6
Make fmt
Emyrk 89efc57
Fix local storage load
Emyrk 48a0beb
Fix terminals on the frontend to use proxies
Emyrk 77d943f
Remove CSP hole
Emyrk 75b8fd4
Add comment on origin patterns
Emyrk 3391e84
Add unit test for getURLs
Emyrk 4075b92
remove some TODOs
Emyrk b79b460
Add another store
Emyrk e879160
Update site/src/components/TerminalLink/TerminalLink.tsx
Emyrk 27ef4a9
Fix stories
Emyrk eb38e95
Move providers into requrie auth
Emyrk 1f8cae4
Fix imports
Emyrk 6a22181
Fix 2 stories
Emyrk 51bdaa2
Stories did not have subdomains on
Emyrk 909801c
Merge remote-tracking branch 'origin/main' into stevenmasley/regions
Emyrk 836c5a4
Fmt after merge
Emyrk 0d0ed87
Fix port forward story
Emyrk 6ed5fe4
ProxyPageView -> ProxyView
Emyrk 163bbff
PR comment cleanup
Emyrk 7dee309
Fix moon feature flag panic
Emyrk b7cfb39
Make fmt
Emyrk 6b19118
Fix typo
Emyrk 3fed785
Rename getUrls
Emyrk 5bb44e8
Rename regions to proxies
Emyrk c868fc9
Only do 1 api call based on experiment
Emyrk edbe6e4
Cleanup args to take just the selected proxy
Emyrk eb6493c
Renames regions -> proxies
Emyrk 017b3db
Fix stories
Emyrk c59a6ba
Move funciton back to bottom
Emyrk 87e0b6d
Fix onSuccess of proxy provider
Emyrk fef5b00
Make fmt
Emyrk d33371d
Simplify some ts
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fixup! Use new context
- Loading branch information
commit 69c573443265e018ec5d76cd2087de3d823fb2a3
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 }) => { | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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. | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 => { | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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") | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.