|
| 1 | +import { useRef, useState, FC } from "react" |
| 2 | +import { makeStyles, Theme, useTheme } from "@material-ui/core/styles" |
| 3 | +import { |
| 4 | + HelpTooltipText, |
| 5 | + HelpPopover, |
| 6 | + HelpTooltipTitle, |
| 7 | +} from "components/Tooltips/HelpTooltip" |
| 8 | +import { Stack } from "components/Stack/Stack" |
| 9 | +import { WorkspaceAgent, DERPRegion } from "api/typesGenerated" |
| 10 | + |
| 11 | +const getDisplayLatency = (theme: Theme, agent: WorkspaceAgent) => { |
| 12 | + // Find the right latency to display |
| 13 | + const latencyValues = Object.values(agent.latency ?? {}) |
| 14 | + const latency = |
| 15 | + latencyValues.find((derp) => derp.preferred) ?? |
| 16 | + // Accessing an array index can return undefined as well |
| 17 | + // for some reason TS does not handle that |
| 18 | + (latencyValues[0] as DERPRegion | undefined) |
| 19 | + |
| 20 | + if (!latency) { |
| 21 | + return undefined |
| 22 | + } |
| 23 | + |
| 24 | + // Get the color |
| 25 | + let color = theme.palette.success.light |
| 26 | + if (latency.latency_ms >= 150 && latency.latency_ms < 300) { |
| 27 | + color = theme.palette.warning.light |
| 28 | + } else if (latency.latency_ms >= 300) { |
| 29 | + color = theme.palette.error.light |
| 30 | + } |
| 31 | + |
| 32 | + return { |
| 33 | + ...latency, |
| 34 | + color, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export const AgentLatency: FC<{ agent: WorkspaceAgent }> = ({ agent }) => { |
| 39 | + const theme: Theme = useTheme() |
| 40 | + const anchorRef = useRef<HTMLButtonElement>(null) |
| 41 | + const [isOpen, setIsOpen] = useState(false) |
| 42 | + const id = isOpen ? "latency-popover" : undefined |
| 43 | + const latency = getDisplayLatency(theme, agent) |
| 44 | + const styles = useStyles() |
| 45 | + |
| 46 | + if (!latency || !agent.latency) { |
| 47 | + return null |
| 48 | + } |
| 49 | + |
| 50 | + return ( |
| 51 | + <> |
| 52 | + <span |
| 53 | + role="presentation" |
| 54 | + aria-label="latency" |
| 55 | + ref={anchorRef} |
| 56 | + onMouseEnter={() => setIsOpen(true)} |
| 57 | + className={styles.trigger} |
| 58 | + style={{ color: latency.color }} |
| 59 | + > |
| 60 | + {Math.round(Math.round(latency.latency_ms))}ms |
| 61 | + </span> |
| 62 | + <HelpPopover |
| 63 | + id={id} |
| 64 | + open={isOpen} |
| 65 | + anchorEl={anchorRef.current} |
| 66 | + onOpen={() => setIsOpen(true)} |
| 67 | + onClose={() => setIsOpen(false)} |
| 68 | + > |
| 69 | + <HelpTooltipTitle>Latency</HelpTooltipTitle> |
| 70 | + <HelpTooltipText> |
| 71 | + Latency from relay servers, used when connections cannot connect |
| 72 | + peer-to-peer. Star indicates the preferred relay. |
| 73 | + </HelpTooltipText> |
| 74 | + |
| 75 | + <HelpTooltipText> |
| 76 | + <Stack direction="column" spacing={1} className={styles.regions}> |
| 77 | + {Object.keys(agent.latency).map((regionName) => { |
| 78 | + if (!agent.latency) { |
| 79 | + throw new Error("No latency found on agent") |
| 80 | + } |
| 81 | + |
| 82 | + const region = agent.latency[regionName] |
| 83 | + |
| 84 | + return ( |
| 85 | + <Stack |
| 86 | + direction="row" |
| 87 | + key={regionName} |
| 88 | + spacing={0.5} |
| 89 | + justifyContent="space-between" |
| 90 | + className={region.preferred ? styles.preferred : undefined} |
| 91 | + > |
| 92 | + <strong>{regionName}</strong> |
| 93 | + {Math.round(region.latency_ms)}ms |
| 94 | + </Stack> |
| 95 | + ) |
| 96 | + })} |
| 97 | + </Stack> |
| 98 | + </HelpTooltipText> |
| 99 | + </HelpPopover> |
| 100 | + </> |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +const useStyles = makeStyles((theme) => ({ |
| 105 | + trigger: { |
| 106 | + cursor: "pointer", |
| 107 | + }, |
| 108 | + regions: { |
| 109 | + marginTop: theme.spacing(2), |
| 110 | + }, |
| 111 | + preferred: { |
| 112 | + color: theme.palette.text.primary, |
| 113 | + }, |
| 114 | +})) |
0 commit comments