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
Show file tree
Hide file tree
Changes from 10 commits
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
8 changes: 8 additions & 0 deletions site/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const SSHKeysPage = lazy(
const TokensPage = lazy(
() => import("./pages/UserSettingsPage/TokensPage/TokensPage"),
)
const WorkspaceProxyPage = lazy(
() =>
import("./pages/UserSettingsPage/WorkspaceProxyPage/WorkspaceProxyPage"),
)
const CreateUserPage = lazy(
() => import("./pages/UsersPage/CreateUserPage/CreateUserPage"),
)
Expand Down Expand Up @@ -259,6 +263,10 @@ export const AppRouter: FC = () => {
<Route index element={<TokensPage />} />
<Route path="new" element={<CreateTokenPage />} />
</Route>
<Route
path="workspace-proxies"
element={<WorkspaceProxyPage />}
/>
</Route>

<Route path="/@:username">
Expand Down
9 changes: 9 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,15 @@ export const getFile = async (fileId: string): Promise<ArrayBuffer> => {
return response.data
}

export const getWorkspaceProxies =
async (): Promise<TypesGen.RegionsResponse> => {
const response = await axios.get<TypesGen.RegionsResponse>(
`/api/v2/regions`,
{},
)
return response.data
}

export const getAppearance = async (): Promise<TypesGen.AppearanceConfig> => {
try {
const response = await axios.get(`/api/v2/appearance`)
Expand Down
12 changes: 10 additions & 2 deletions site/src/components/AppLink/AppLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as TypesGen from "../../api/typesGenerated"
import { generateRandomString } from "../../utils/random"
import { BaseIcon } from "./BaseIcon"
import { ShareIcon } from "./ShareIcon"
import { usePreferredProxy } from "hooks/usePreferredProxy"

const Language = {
appTitle: (appName: string, identifier: string): string =>
Expand All @@ -29,6 +30,11 @@ export const AppLink: FC<AppLinkProps> = ({
workspace,
agent,
}) => {
const preferredProxy = usePreferredProxy()
const preferredPathBase = preferredProxy ? preferredProxy.path_app_url : ""
// Use the proxy host subdomain if it's configured.
appsHost = preferredProxy ? preferredProxy.wildcard_hostname : appsHost

const styles = useStyles()
const username = workspace.owner_name

Expand All @@ -43,14 +49,16 @@ export const AppLink: FC<AppLinkProps> = ({

// The backend redirects if the trailing slash isn't included, so we add it
// here to avoid extra roundtrips.
let href = `/@${username}/${workspace.name}.${
let href = `${preferredPathBase}/@${username}/${workspace.name}.${
agent.name
}/apps/${encodeURIComponent(appSlug)}/`
if (app.command) {
href = `/@${username}/${workspace.name}.${
href = `${preferredPathBase}/@${username}/${workspace.name}.${
agent.name
}/terminal?command=${encodeURIComponent(app.command)}`
}

// TODO: @emyrk handle proxy subdomains.
if (appsHost && app.subdomain) {
const subdomain = `${appSlug}--${agent.name}--${workspace.name}--${username}`
href = `${window.location.protocol}//${appsHost}/`.replace("*", subdomain)
Expand Down
23 changes: 23 additions & 0 deletions site/src/components/DeploySettingsLayout/Badges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ export const EntitledBadge: FC = () => {
)
}

export const HealthyBadge: FC = () => {
const styles = useStyles()
return (
<span className={combineClasses([styles.badge, styles.enabledBadge])}>
Healthy
</span>
)
}

export const NotHealthyBadge: FC = () => {
const styles = useStyles()
return (
<span className={combineClasses([styles.badge, styles.errorBadge])}>
Unhealthy
</span>
)
}

export const DisabledBadge: FC = () => {
const styles = useStyles()
return (
Expand Down Expand Up @@ -92,6 +110,11 @@ const useStyles = makeStyles((theme) => ({
backgroundColor: theme.palette.success.dark,
},

errorBadge: {
border: `1px solid ${theme.palette.error.light}`,
backgroundColor: theme.palette.error.dark,
},

disabledBadge: {
border: `1px solid ${theme.palette.divider}`,
backgroundColor: theme.palette.background.paper,
Expand Down
7 changes: 5 additions & 2 deletions site/src/components/PortForwardButton/PortForwardButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ export const portForwardURL = (

const TooltipView: React.FC<PortForwardButtonProps> = (props) => {
const { host, workspaceName, agentName, agentId, username } = props
const preferredProxy = usePreferredProxy()
const portHost = preferredProxy ? preferredProxy.wildcard_hostname : host

const styles = useStyles()
const [port, setPort] = useState("3000")
const urlExample = portForwardURL(
host,
portHost,
parseInt(port),
agentName,
workspaceName,
Expand Down Expand Up @@ -104,7 +107,7 @@ const TooltipView: React.FC<PortForwardButtonProps> = (props) => {
{ports &&
ports.map((p, i) => {
const url = portForwardURL(
host,
portHost,
p.port,
agentName,
workspaceName,
Expand Down
11 changes: 11 additions & 0 deletions site/src/components/SettingsLayout/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { NavLink } from "react-router-dom"
import { combineClasses } from "utils/combineClasses"
import AccountIcon from "@material-ui/icons/Person"
import SecurityIcon from "@material-ui/icons/LockOutlined"
import PublicIcon from "@material-ui/icons/Public"
import { useDashboard } from "components/Dashboard/DashboardProvider"

const SidebarNavItem: FC<
PropsWithChildren<{ href: string; icon: ReactNode }>
Expand Down Expand Up @@ -41,6 +43,7 @@ const SidebarNavItemIcon: React.FC<{ icon: ElementType }> = ({

export const Sidebar: React.FC<{ user: User }> = ({ user }) => {
const styles = useStyles()
const dashboard = useDashboard()

return (
<nav className={styles.sidebar}>
Expand Down Expand Up @@ -76,6 +79,14 @@ export const Sidebar: React.FC<{ user: User }> = ({ user }) => {
>
Tokens
</SidebarNavItem>
{dashboard.experiments.includes("moons") && (
<SidebarNavItem
href="workspace-proxies"
icon={<SidebarNavItemIcon icon={PublicIcon} />}
>
Workspace Proxy
</SidebarNavItem>
)}
</nav>
)
}
Expand Down
6 changes: 5 additions & 1 deletion site/src/components/TerminalLink/TerminalLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { SecondaryAgentButton } from "components/Resources/AgentButton"
import { FC } from "react"
import * as TypesGen from "../../api/typesGenerated"
import { generateRandomString } from "../../utils/random"
import { usePreferredProxy } from "hooks/usePreferredProxy"

export const Language = {
linkText: "Terminal",
Expand All @@ -27,7 +28,10 @@ export const TerminalLink: FC<React.PropsWithChildren<TerminalLinkProps>> = ({
userName = "me",
workspaceName,
}) => {
const href = `/@${userName}/${workspaceName}${
const preferredProxy = usePreferredProxy()
const preferredPathBase = preferredProxy ? preferredProxy.path_app_url : ""

const href = `${preferredPathBase}/@${userName}/${workspaceName}${
agentName ? `.${agentName}` : ""
}/terminal`

Expand Down
20 changes: 20 additions & 0 deletions site/src/hooks/usePreferredProxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Region } from "api/typesGenerated"
import { useDashboard } from "components/Dashboard/DashboardProvider"

export const usePreferredProxy = (): Region | undefined => {
const dashboard = useDashboard()
// Only use preferred proxy if the user has the moons experiment enabled
if (!dashboard?.experiments.includes("moons")) {
return 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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { FC, PropsWithChildren, useState } from "react"
import { Section } from "components/SettingsLayout/Section"
import { WorkspaceProxyPageView } from "./WorkspaceProxyView"
import makeStyles from "@material-ui/core/styles/makeStyles"
import { Trans } from "react-i18next"
import { useWorkspaceProxiesData } from "./hooks"
import { Region } from "api/typesGenerated"
import { displayError } from "components/GlobalSnackbar/utils"
// import { ConfirmDeleteDialog } from "./components"
// import { Stack } from "components/Stack/Stack"
// import Button from "@material-ui/core/Button"
// import { Link as RouterLink } from "react-router-dom"
// import AddIcon from "@material-ui/icons/AddOutlined"
// import { APIKeyWithOwner } from "api/typesGenerated"

export const WorkspaceProxyPage: FC<PropsWithChildren<unknown>> = () => {
const styles = useStyles()

const description = (
<Trans values={{}}>
Workspace proxies are used to reduce the latency of connections to a
workspace. To get the best experience, choose the workspace proxy that is
closest located to you.
</Trans>
)

const [preferred, setPreferred] = useState(getPreferredProxy())

const {
data: response,
error: getProxiesError,
isFetching,
isFetched,
} = useWorkspaceProxiesData()

return (
<>
<Section
title="Workspace Proxies"
className={styles.section}
description={description}
layout="fluid"
>
<WorkspaceProxyPageView
proxies={response ? response.regions : []}
isLoading={isFetching}
hasLoaded={isFetched}
getWorkspaceProxiesError={getProxiesError}
preferredProxy={preferred}
onSelect={(proxy) => {
if (!proxy.healthy) {
displayError("Please select a healthy workspace proxy.")
return
}
// normProxy is a normalized proxy to
const normProxy = {
...proxy,
// Trim trailing slashes to be consistent
path_app_url: proxy.path_app_url.replace(/\/$/, ""),
}

savePreferredProxy(normProxy)
setPreferred(normProxy)
}}
/>
</Section>
</>
)
}

const useStyles = makeStyles((theme) => ({
section: {
"& code": {
background: theme.palette.divider,
fontSize: 12,
padding: "2px 4px",
color: theme.palette.text.primary,
borderRadius: 2,
},
},
}))

export default WorkspaceProxyPage

// Exporting to be used in the tests
export const savePreferredProxy = (proxy: Region): void => {
window.localStorage.setItem("preferred-proxy", JSON.stringify(proxy))
}

export const getPreferredProxy = (): Region | 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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Region } from "api/typesGenerated"
import { AvatarData } from "components/AvatarData/AvatarData"
import { Avatar } from "components/Avatar/Avatar"
import { useClickableTableRow } from "hooks/useClickableTableRow"
import TableCell from "@material-ui/core/TableCell"
import TableRow from "@material-ui/core/TableRow"
import { FC } from "react"
import {
HealthyBadge,
NotHealthyBadge,
} from "components/DeploySettingsLayout/Badges"
import { makeStyles } from "@material-ui/core/styles"
import { combineClasses } from "utils/combineClasses"

export const ProxyRow: FC<{
proxy: Region
onSelectRegion: (proxy: Region) => void
preferred: boolean
}> = ({ proxy, onSelectRegion, preferred }) => {
const styles = useStyles()

const clickable = useClickableTableRow(() => {
onSelectRegion(proxy)
})

const classes = [clickable.className]

if (preferred) {
classes.push(styles.preferredrow)
}

return (
<TableRow
key={proxy.name}
data-testid={`${proxy.name}`}
{...clickable}
// Make sure to include our classname here.
className={combineClasses(classes)}
>
<TableCell>
<AvatarData
title={
proxy.display_name && proxy.display_name.length > 0
? proxy.display_name
: proxy.name
}
avatar={
proxy.icon_url !== "" && (
<Avatar src={proxy.icon_url} variant="square" fitImage />
)
}
/>
</TableCell>

<TableCell>{proxy.path_app_url}</TableCell>
<TableCell>
<ProxyStatus proxy={proxy} />
</TableCell>
</TableRow>
)
}

const ProxyStatus: FC<{
proxy: Region
}> = ({ proxy }) => {
let icon = <NotHealthyBadge />
if (proxy.healthy) {
icon = <HealthyBadge />
}

return icon
}

const useStyles = makeStyles((theme) => ({
preferredrow: {
// TODO: What is the best way to show what proxy is currently being used?
backgroundColor: theme.palette.secondary.main,
outline: `3px solid ${theme.palette.secondary.light}`,
outlineOffset: -3,
},
}))
Loading