-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logic to trim label's to be exact two lines
- Loading branch information
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