Skip to content
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

Add keyboard listener on dimensions popover #193

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/components/line/popover/dimension.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react"
import styled from "styled-components"
import { Flex } from "@netdata/netdata-ui"
import { Flex, TextMicro } from "@netdata/netdata-ui"
import Color, { ColorBar } from "@/components/line/dimensions/color"
import Name from "@/components/line/dimensions/name"
import Value, { Value as ValuePart } from "@/components/line/dimensions/value"
Expand Down Expand Up @@ -64,7 +64,7 @@ const AnnotationsValue = ({ children: annotations, showFull, ...rest }) => (
</Flex>
)

const Dimension = ({ id, strong, rowFlavour }) => {
const Dimension = ({ id, index, strong, rowFlavour }) => {
const visible = useVisibleDimensionId(id)

const chart = useChart()
Expand All @@ -73,6 +73,7 @@ const Dimension = ({ id, strong, rowFlavour }) => {

return (
<GridRow opacity={visible ? null : "weak"}>
<TextMicro>{index < 9 ? index + 1 : ""}</TextMicro>
<Flex alignItems="center" gap={1} position="relative">
<ColorBackground
id={id}
Expand Down
11 changes: 8 additions & 3 deletions src/components/line/popover/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Units from "@/components/line/dimensions/units"
import UpdateEvery from "./updateEvery"
import Timestamp from "./timestamp"
import Dimension from "./dimension"
import useKeyboardListener from "./useKeyboardListener"

const Container = styled(Flex).attrs({
round: true,
Expand All @@ -23,8 +24,9 @@ const Container = styled(Flex).attrs({
const Grid = styled.div`
display: grid;
width: 100%;
grid-template-columns: minmax(150px, max-content) 60px 60px minmax(80px, auto);
grid-template-columns: 30px minmax(150px, max-content) 60px 60px minmax(80px, auto);
align-items: center;
justify-content: center;
`

const GridHeader = styled.div`
Expand Down Expand Up @@ -97,6 +99,8 @@ const Dimensions = () => {

const rowFlavour = rowFlavours[row] || rowFlavours.default

useKeyboardListener({ chart, ids })

return (
<Container data-testid="chartPopover-dimensions" gap={2}>
<Flex column gap={1}>
Expand All @@ -108,6 +112,7 @@ const Dimensions = () => {
</Flex>
<Grid gap={1} column>
<GridHeader>
<TextMicro strong>Key</TextMicro>
<TextMicro strong>Dimension</TextMicro>
<TextMicro
color={rowFlavour === rowFlavours.default ? "text" : "textLite"}
Expand Down Expand Up @@ -140,8 +145,8 @@ const Dimensions = () => {
Info
</TextMicro>
</GridHeader>
{ids.map(id => (
<Dimension key={id} id={id} strong={row === id} rowFlavour={rowFlavour} />
{ids.map((id, index) => (
<Dimension key={id} id={id} index={index} strong={row === id} rowFlavour={rowFlavour} />
))}
</Grid>
<Flex flex={false} height={3}>
Expand Down
6 changes: 5 additions & 1 deletion src/components/line/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const Popover = ({ uiName }) => {
targetRef.current = target
updatePositionRef.current = useMakeUpdatePosition(target, dropRef, align, stretch)

const [dimKey, setDimKey] = useState(0)

chart.onAttributeChange("selectedLegendDimensions", () => setDimKey(prev => prev + 1))

useEffect(() => {
return unregister(
chart.getUI(uiName).on("mousemove", event => {
Expand Down Expand Up @@ -79,7 +83,7 @@ const Popover = ({ uiName }) => {
data-testid="drop"
sx={{ pointerEvents: "none" }}
>
<Dimensions uiName={uiName} data-testid="chartPopover" />
<Dimensions key={dimKey} uiName={uiName} data-testid="chartPopover" />
</DropContainer>,
el
)}
Expand Down
28 changes: 28 additions & 0 deletions src/components/line/popover/useKeyboardListener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useCallback, useEffect } from "react"

const digitRegexp = new RegExp(/^Digit\d+$/)

const useKeyboardListener = ({ chart, ids }) => {
const keyDownHandler = useCallback(
e => {
const isDigit = digitRegexp.test(e.code)
if (!isDigit) return
const digit = e.code.replace(/^Digit/, "")
if (digit === null || isNaN(digit)) return
const id = ids[parseInt(digit) - 1]
if (!id) return
const merge = e.shiftKey || e.ctrlKey || e.metaKey
chart.toggleDimensionId(id, { merge })
},
[chart, ids]
)

useEffect(() => {
window.addEventListener("keydown", keyDownHandler)
return () => {
window.removeEventListener("keydown", keyDownHandler)
}
}, [chart, ids])
}

export default useKeyboardListener