forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cloud Security] Improve graph node label ellipsis logic (elastic#204580
) ## Summary This PR improves the logic of node's label truncation. It follows the guidelines from design: > - Set the maximum width for names to 160px > - Truncate names to max two lines > - I recommend truncating names in the middle, as entity IDs often differ only in their last characters while starting similarly. This approach will make it easier to identify differences between names > - Display the full name in a tooltip on hover for better visibility Before: <img width="150" src="https://github.com/user-attachments/assets/4bc18b98-1e1b-41df-ac69-79b2adf1f127" /> <img width="150" src="https://github.com/user-attachments/assets/69c335f6-3952-44e8-8836-19eab3bff28f" /> <img width="150" src="https://github.com/user-attachments/assets/4ba65932-5af5-4382-b721-41c6ee7388a8" /> After: https://github.com/user-attachments/assets/2ab04809-7599-45ab-9aaa-ae1fcabdf969 **How to test** To test this PR you can run ``` yarn storybook cloud_security_posture_packages ``` ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Sean Rathier <[email protected]> Co-authored-by: Brad White <[email protected]>
- Loading branch information
1 parent
dec31c5
commit f316612
Showing
8 changed files
with
234 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
117 changes: 117 additions & 0 deletions
117
.../security/packages/kbn-cloud-security-posture/graph/src/components/node/label.stories.tsx
This file contains 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,117 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { ThemeProvider } from '@emotion/react'; | ||
import { pick } from 'lodash'; | ||
import { ReactFlow, Controls, Background } from '@xyflow/react'; | ||
import { Story } from '@storybook/react'; | ||
import { NodeViewModel } from '../types'; | ||
import { HexagonNode, PentagonNode, EllipseNode, RectangleNode, DiamondNode, LabelNode } from '.'; | ||
|
||
import '@xyflow/react/dist/style.css'; | ||
|
||
export default { | ||
title: 'Components/Graph Components/Labels', | ||
description: 'CDR - Graph visualization', | ||
argTypes: { | ||
color: { | ||
options: ['primary', 'danger', 'warning'], | ||
control: { type: 'radio' }, | ||
}, | ||
shape: { | ||
options: ['ellipse', 'hexagon', 'pentagon', 'rectangle', 'diamond', 'label'], | ||
control: { type: 'radio' }, | ||
}, | ||
expandButtonClick: { action: 'expandButtonClick' }, | ||
}, | ||
}; | ||
|
||
const nodeTypes = { | ||
hexagon: HexagonNode, | ||
pentagon: PentagonNode, | ||
ellipse: EllipseNode, | ||
rectangle: RectangleNode, | ||
diamond: DiamondNode, | ||
label: LabelNode, | ||
}; | ||
|
||
const Template: Story<NodeViewModel> = (args: NodeViewModel) => ( | ||
<ThemeProvider theme={{ darkMode: false }}> | ||
<ReactFlow | ||
fitView | ||
attributionPosition={undefined} | ||
nodeTypes={nodeTypes} | ||
nodes={[ | ||
{ | ||
id: args.id, | ||
type: args.shape, | ||
data: pick(args, ['id', 'label', 'color', 'icon', 'interactive', 'expandButtonClick']), | ||
position: { x: 0, y: 0 }, | ||
}, | ||
]} | ||
> | ||
<Controls /> | ||
<Background /> | ||
</ReactFlow> | ||
</ThemeProvider> | ||
); | ||
|
||
export const ShortLabel = Template.bind({}); | ||
|
||
ShortLabel.args = { | ||
id: 'siem-windows', | ||
label: '', | ||
color: 'primary', | ||
shape: 'hexagon', | ||
icon: 'okta', | ||
interactive: true, | ||
}; | ||
|
||
export const ArnLabel = Template.bind({}); | ||
|
||
ArnLabel.args = { | ||
id: 'siem-windows', | ||
label: 'arn:aws:iam::1234567890:user/lorem-ipsumdol-sitamet-user-1234', | ||
color: 'primary', | ||
shape: 'hexagon', | ||
icon: 'okta', | ||
interactive: true, | ||
}; | ||
|
||
export const DashedLabel = Template.bind({}); | ||
|
||
DashedLabel.args = { | ||
id: 'siem-windows', | ||
label: 'lore-ipsumdol-sitameta-consectetu-adipis342', | ||
color: 'primary', | ||
shape: 'hexagon', | ||
icon: 'okta', | ||
interactive: true, | ||
}; | ||
|
||
export const NoSpacesLabel = Template.bind({}); | ||
|
||
NoSpacesLabel.args = { | ||
id: 'siem-windows', | ||
label: 'LoremIpsumDolorSitAmetConsectetur123', | ||
color: 'primary', | ||
shape: 'hexagon', | ||
icon: 'okta', | ||
interactive: true, | ||
}; | ||
|
||
export const NoSpacesAllLoweredLabel = Template.bind({}); | ||
|
||
NoSpacesAllLoweredLabel.args = { | ||
id: 'siem-windows', | ||
label: 'loremipsumdolorsitametconsectetur123', | ||
color: 'primary', | ||
shape: 'hexagon', | ||
icon: 'okta', | ||
interactive: true, | ||
}; |
104 changes: 104 additions & 0 deletions
104
...olutions/security/packages/kbn-cloud-security-posture/graph/src/components/node/label.tsx
This file contains 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,104 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import React, { memo, type PropsWithChildren } from 'react'; | ||
import { EuiText, EuiTextTruncate, EuiToolTip } from '@elastic/eui'; | ||
import { css } from '@emotion/react'; | ||
import styled from '@emotion/styled'; | ||
import { NODE_LABEL_WIDTH, NODE_WIDTH } from './styles'; | ||
|
||
const WORD_BOUNDARIES_REGEX = /\b/; | ||
const FORCE_BREAK_REGEX = /(.{10})/; | ||
|
||
/** | ||
* A component that renders an element with breaking opportunities (`<wbr>`s) | ||
* spliced into text children at word boundaries. | ||
* Copied from x-pack/plugins/security_solution/public/resolver/view/generated_text.tsx | ||
*/ | ||
const GeneratedText = memo<PropsWithChildren<{}>>(function ({ children }) { | ||
return <>{processedValue()}</>; | ||
|
||
function processedValue() { | ||
return React.Children.map(children, (child) => { | ||
if (typeof child === 'string') { | ||
let valueSplitByWordBoundaries = child.split(WORD_BOUNDARIES_REGEX); | ||
|
||
if (valueSplitByWordBoundaries.length < 2) { | ||
valueSplitByWordBoundaries = child.split(FORCE_BREAK_REGEX); | ||
|
||
if (valueSplitByWordBoundaries.length < 2) { | ||
return valueSplitByWordBoundaries[0]; | ||
} | ||
} | ||
|
||
return [ | ||
valueSplitByWordBoundaries[0], | ||
...valueSplitByWordBoundaries | ||
.splice(1) | ||
.reduce((generatedTextMemo: Array<string | JSX.Element>, value) => { | ||
if ( | ||
generatedTextMemo.length > 0 && | ||
typeof generatedTextMemo[generatedTextMemo.length - 1] === 'object' | ||
) { | ||
return [...generatedTextMemo, value]; | ||
} | ||
return [...generatedTextMemo, value, <wbr />]; | ||
}, []), | ||
]; | ||
} else { | ||
return child; | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
GeneratedText.displayName = 'GeneratedText'; | ||
|
||
export interface LabelProps { | ||
text?: string; | ||
} | ||
|
||
const LabelComponent: React.FC<LabelProps> = ({ text = '' }: LabelProps) => { | ||
const [isTruncated, setIsTruncated] = React.useState(false); | ||
|
||
return ( | ||
<EuiText | ||
size="xs" | ||
textAlign="center" | ||
css={css` | ||
width: ${NODE_LABEL_WIDTH}px; | ||
margin-left: ${-(NODE_LABEL_WIDTH - NODE_WIDTH) / 2}px; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
max-height: 32px; | ||
`} | ||
> | ||
<EuiToolTip content={isTruncated ? text : ''} position="bottom"> | ||
<EuiTextTruncate | ||
truncation="end" | ||
truncationOffset={20} | ||
text={text} | ||
width={NODE_LABEL_WIDTH * 1.5} | ||
> | ||
{(truncatedText) => ( | ||
<> | ||
{setIsTruncated(truncatedText.length !== text.length)} | ||
{<GeneratedText>{truncatedText}</GeneratedText>} | ||
</> | ||
)} | ||
</EuiTextTruncate> | ||
</EuiToolTip> | ||
</EuiText> | ||
); | ||
}; | ||
|
||
export const Label = styled(LabelComponent)` | ||
width: ${NODE_LABEL_WIDTH}px; | ||
margin-left: ${-(NODE_LABEL_WIDTH - NODE_WIDTH) / 2}px; | ||
text-overflow: ellipsis; | ||
overflow: hidden; | ||
`; |
This file contains 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
This file contains 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
This file contains 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