Skip to content

feat(site): Add proxy menu into navbar #7715

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 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion site/src/components/Navbar/Navbar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render, screen, waitFor } from "@testing-library/react"
import { App } from "app"
import { Language } from "components/NavbarView/NavbarView"
import { Language } from "./NavbarView"
import { rest } from "msw"
import {
MockEntitlementsWithAuditLog,
Expand Down
8 changes: 7 additions & 1 deletion site/src/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { useFeatureVisibility } from "hooks/useFeatureVisibility"
import { useMe } from "hooks/useMe"
import { usePermissions } from "hooks/usePermissions"
import { FC } from "react"
import { NavbarView } from "../NavbarView/NavbarView"
import { NavbarView } from "./NavbarView"
import { useProxy } from "contexts/ProxyContext"

export const Navbar: FC = () => {
const { appearance, buildInfo } = useDashboard()
Expand All @@ -16,6 +17,8 @@ export const Navbar: FC = () => {
featureVisibility["audit_log"] && Boolean(permissions.viewAuditLog)
const canViewDeployment = Boolean(permissions.viewDeploymentValues)
const onSignOut = () => authSend("SIGN_OUT")
const proxyContextValue = useProxy()
const dashboard = useDashboard()

return (
<NavbarView
Expand All @@ -26,6 +29,9 @@ export const Navbar: FC = () => {
onSignOut={onSignOut}
canViewAuditLog={canViewAuditLog}
canViewDeployment={canViewDeployment}
proxyContextValue={
dashboard.experiments.includes("moons") ? proxyContextValue : undefined
}
/>
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import { screen } from "@testing-library/react"
import { MockUser, MockUser2 } from "../../testHelpers/entities"
import {
MockPrimaryWorkspaceProxy,
MockUser,
MockUser2,
} from "../../testHelpers/entities"
import { render } from "../../testHelpers/renderHelpers"
import { Language as navLanguage, NavbarView } from "./NavbarView"
import { ProxyContextValue } from "contexts/ProxyContext"
import { action } from "@storybook/addon-actions"

const proxyContextValue: ProxyContextValue = {
proxy: {
preferredPathAppURL: "",
preferredWildcardHostname: "",
proxy: MockPrimaryWorkspaceProxy,
},
isLoading: false,
isFetched: true,
setProxy: jest.fn(),
clearProxy: action("clearProxy"),
proxyLatencies: {},
}

describe("NavbarView", () => {
const noop = () => {
Expand All @@ -23,6 +42,7 @@ describe("NavbarView", () => {
it("workspaces nav link has the correct href", async () => {
render(
<NavbarView
proxyContextValue={proxyContextValue}
user={MockUser}
onSignOut={noop}
canViewAuditLog
Expand All @@ -36,6 +56,7 @@ describe("NavbarView", () => {
it("templates nav link has the correct href", async () => {
render(
<NavbarView
proxyContextValue={proxyContextValue}
user={MockUser}
onSignOut={noop}
canViewAuditLog
Expand All @@ -49,6 +70,7 @@ describe("NavbarView", () => {
it("users nav link has the correct href", async () => {
render(
<NavbarView
proxyContextValue={proxyContextValue}
user={MockUser}
onSignOut={noop}
canViewAuditLog
Expand All @@ -70,6 +92,7 @@ describe("NavbarView", () => {
// When
render(
<NavbarView
proxyContextValue={proxyContextValue}
user={mockUser}
onSignOut={noop}
canViewAuditLog
Expand All @@ -86,6 +109,7 @@ describe("NavbarView", () => {
it("audit nav link has the correct href", async () => {
render(
<NavbarView
proxyContextValue={proxyContextValue}
user={MockUser}
onSignOut={noop}
canViewAuditLog
Expand All @@ -99,6 +123,7 @@ describe("NavbarView", () => {
it("audit nav link is hidden for members", async () => {
render(
<NavbarView
proxyContextValue={proxyContextValue}
user={MockUser2}
onSignOut={noop}
canViewAuditLog={false}
Expand All @@ -112,6 +137,7 @@ describe("NavbarView", () => {
it("deployment nav link has the correct href", async () => {
render(
<NavbarView
proxyContextValue={proxyContextValue}
user={MockUser}
onSignOut={noop}
canViewAuditLog
Expand All @@ -127,6 +153,7 @@ describe("NavbarView", () => {
it("deployment nav link is hidden for members", async () => {
render(
<NavbarView
proxyContextValue={proxyContextValue}
user={MockUser2}
onSignOut={noop}
canViewAuditLog={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ import Drawer from "@mui/material/Drawer"
import IconButton from "@mui/material/IconButton"
import List from "@mui/material/List"
import ListItem from "@mui/material/ListItem"
import { makeStyles } from "@mui/styles"
import { makeStyles, useTheme } from "@mui/styles"
import MenuIcon from "@mui/icons-material/Menu"
import { CoderIcon } from "components/Icons/CoderIcon"
import { useState } from "react"
import { NavLink, useLocation } from "react-router-dom"
import { FC, useRef, useState } from "react"
import { NavLink, useLocation, useNavigate } from "react-router-dom"
import { colors } from "theme/colors"
import * as TypesGen from "../../api/typesGenerated"
import { navHeight } from "../../theme/constants"
import { combineClasses } from "../../utils/combineClasses"
import { UserDropdown } from "../UserDropdown/UsersDropdown"
import Box from "@mui/material/Box"
import Menu from "@mui/material/Menu"
import Button from "@mui/material/Button"
import MenuItem from "@mui/material/MenuItem"
import KeyboardArrowDownOutlined from "@mui/icons-material/KeyboardArrowDownOutlined"
import { ProxyContextValue } from "contexts/ProxyContext"
import { displayError } from "components/GlobalSnackbar/utils"
import Divider from "@mui/material/Divider"
import HelpOutline from "@mui/icons-material/HelpOutline"
import Tooltip from "@mui/material/Tooltip"
import Skeleton from "@mui/material/Skeleton"

export const USERS_LINK = `/users?filter=${encodeURIComponent("status:active")}`

Expand All @@ -23,6 +34,7 @@ export interface NavbarViewProps {
onSignOut: () => void
canViewAuditLog: boolean
canViewDeployment: boolean
proxyContextValue?: ProxyContextValue
}

export const Language = {
Expand Down Expand Up @@ -83,14 +95,15 @@ const NavItems: React.FC<
</List>
)
}
export const NavbarView: React.FC<React.PropsWithChildren<NavbarViewProps>> = ({
export const NavbarView: FC<NavbarViewProps> = ({
user,
logo_url,
buildInfo,
supportLinks,
onSignOut,
canViewAuditLog,
canViewDeployment,
proxyContextValue,
}) => {
const styles = useStyles()
const [isDrawerOpen, setIsDrawerOpen] = useState(false)
Expand Down Expand Up @@ -145,7 +158,16 @@ export const NavbarView: React.FC<React.PropsWithChildren<NavbarViewProps>> = ({
canViewDeployment={canViewDeployment}
/>

<div className={styles.profileButton}>
<Box
display="flex"
marginLeft={{ lg: "auto" }}
gap={2}
alignItems="center"
paddingRight={2}
>
{proxyContextValue && (
<ProxyMenu proxyContextValue={proxyContextValue} />
)}
{user && (
<UserDropdown
user={user}
Expand All @@ -154,12 +176,163 @@ export const NavbarView: React.FC<React.PropsWithChildren<NavbarViewProps>> = ({
onSignOut={onSignOut}
/>
)}
</div>
</Box>
</div>
</nav>
)
}

const ProxyMenu: FC<{ proxyContextValue: ProxyContextValue }> = ({
proxyContextValue,
}) => {
const buttonRef = useRef<HTMLButtonElement>(null)
const [isOpen, setIsOpen] = useState(false)
const selectedProxy = proxyContextValue.proxy.proxy
const closeMenu = () => setIsOpen(false)
const navigate = useNavigate()

if (!proxyContextValue.isFetched) {
return (
<Skeleton
width="160px"
height={30}
sx={{ borderRadius: "4px", transform: "none" }}
/>
)
}

return (
<>
<Button
ref={buttonRef}
onClick={() => setIsOpen(true)}
size="small"
endIcon={<KeyboardArrowDownOutlined />}
sx={{
borderRadius: "4px",
"& .MuiSvgIcon-root": { fontSize: 14 },
}}
>
{selectedProxy ? (
<Box display="flex" gap={2} alignItems="center">
<Box width={14} height={14} lineHeight={0}>
<Box
component="img"
src={selectedProxy.icon_url}
alt=""
sx={{ objectFit: "contain" }}
width="100%"
height="100%"
/>
</Box>
{selectedProxy.display_name}
<ProxyStatusLatency
proxy={selectedProxy}
latency={
proxyContextValue.proxyLatencies?.[selectedProxy.id]?.latencyMS
}
/>
</Box>
) : (
"Select Proxy"
)}
</Button>
<Menu
open={isOpen}
anchorEl={buttonRef.current}
onClick={closeMenu}
onClose={closeMenu}
sx={{ "& .MuiMenu-paper": { py: 1 } }}
>
{proxyContextValue.proxies?.map((proxy) => (
<MenuItem
onClick={() => {
if (!proxy.healthy) {
displayError("Please select a healthy workspace proxy.")
closeMenu()
return
}

proxyContextValue.setProxy(proxy)
closeMenu()
}}
key={proxy.id}
selected={proxy.id === selectedProxy?.id}
sx={{
fontSize: 14,
}}
>
<Box display="flex" gap={3} alignItems="center" width="100%">
<Box width={14} height={14} lineHeight={0}>
<Box
component="img"
src={proxy.icon_url}
alt=""
sx={{ objectFit: "contain" }}
width="100%"
height="100%"
/>
</Box>
{proxy.display_name}
<ProxyStatusLatency
proxy={proxy}
latency={
proxyContextValue.proxyLatencies?.[proxy.id]?.latencyMS
}
/>
</Box>
</MenuItem>
))}
<Divider sx={{ borderColor: (theme) => theme.palette.divider }} />
<MenuItem
sx={{ fontSize: 14 }}
onClick={() => {
navigate("/settings/workspace-proxies")
}}
>
Proxy settings
</MenuItem>
</Menu>
</>
)
}

const ProxyStatusLatency: FC<{ proxy: TypesGen.Region; latency?: number }> = ({
proxy,
latency,
}) => {
const theme = useTheme()
let color = theme.palette.success.light

if (!latency) {
return (
<Tooltip title="Latency not available">
<HelpOutline
sx={{
ml: "auto",
fontSize: "14px !important",
color: (theme) => theme.palette.text.secondary,
}}
/>
</Tooltip>
)
}

if (latency >= 300) {
color = theme.palette.error.light
}

if (!proxy.healthy || latency >= 100) {
color = theme.palette.warning.light
}

return (
<Box sx={{ color, fontSize: 13, marginLeft: "auto" }}>
{latency.toFixed(0)}ms
</Box>
)
}

const useStyles = makeStyles((theme) => ({
root: {
height: navHeight,
Expand Down Expand Up @@ -192,12 +365,6 @@ const useStyles = makeStyles((theme) => ({
display: "flex",
},
},
profileButton: {
paddingRight: theme.spacing(2),
[theme.breakpoints.up("md")]: {
marginLeft: "auto",
},
},
mobileMenuButton: {
[theme.breakpoints.up("md")]: {
display: "none",
Expand Down
2 changes: 1 addition & 1 deletion site/src/components/UsersLayout/UsersLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { makeStyles } from "@mui/styles"
import GroupAdd from "@mui/icons-material/GroupAddOutlined"
import PersonAdd from "@mui/icons-material/PersonAddOutlined"
import { useMachine } from "@xstate/react"
import { USERS_LINK } from "components/NavbarView/NavbarView"
import { USERS_LINK } from "components/Navbar/NavbarView"
import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader"
import { useFeatureVisibility } from "hooks/useFeatureVisibility"
import { usePermissions } from "hooks/usePermissions"
Expand Down
2 changes: 1 addition & 1 deletion site/src/contexts/ProxyContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
return (
<ProxyContext.Provider
value={{
proxyLatencies,
userProxy: userSavedProxy,
proxyLatencies: proxyLatencies,
proxy: experimentEnabled
? proxy
: {
Expand Down
Loading