Skip to content

Commit 58b7d85

Browse files
committed
feat: enhance SegmentedBarChart with hideLegend prop and update types
1 parent ef7d8c0 commit 58b7d85

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

src/Common/SegmentedBarChart/SegmentedBarChart.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Entity, SegmentedBarChartProps } from './types'
2323
import './styles.scss'
2424

2525
const SegmentedBarChart: React.FC<SegmentedBarChartProps> = ({
26+
hideLegend,
2627
entities: userEntities = [FALLBACK_ENTITY],
2728
rootClassName,
2829
countClassName,
@@ -36,7 +37,7 @@ const SegmentedBarChart: React.FC<SegmentedBarChartProps> = ({
3637
const total = entities.reduce((sum, entity) => entity.value + sum, 0)
3738
const filteredEntities = entities.filter((entity) => !!entity.value)
3839

39-
const calcSegmentWidth = (entity: Entity) => `${(entity.value / total) * 100}%`
40+
const calcSegmentWidth = (entityValue: Entity['value']) => `${(entityValue / total) * 100}%`
4041

4142
const renderLabel = (label: Entity['label']) =>
4243
isLoading ? (
@@ -88,11 +89,17 @@ const SegmentedBarChart: React.FC<SegmentedBarChartProps> = ({
8889
))
8990
}
9091

91-
const renderLegend = () => (
92-
<div className={`flexbox flex-wrap dc__row-gap-4 ${isProportional ? 'dc__gap-24' : 'dc__gap-16'}`}>
93-
{renderContent()}
94-
</div>
95-
)
92+
const renderLegend = () => {
93+
if (hideLegend) {
94+
return null
95+
}
96+
97+
return (
98+
<div className={`flexbox flex-wrap dc__row-gap-4 ${isProportional ? 'dc__gap-24' : 'dc__gap-16'}`}>
99+
{renderContent()}
100+
</div>
101+
)
102+
}
96103

97104
const renderBar = () => (
98105
<motion.div
@@ -112,7 +119,7 @@ const SegmentedBarChart: React.FC<SegmentedBarChartProps> = ({
112119
className={`h-8 ${index === 0 ? 'dc__left-radius-4' : ''} ${
113120
index === map.length - 1 ? 'dc__right-radius-4' : ''
114121
} ${isLoading ? 'shimmer' : ''}`}
115-
style={{ backgroundColor: entity.color, width: calcSegmentWidth(entity) }}
122+
style={{ backgroundColor: entity.color, width: calcSegmentWidth(entity.value) }}
116123
/>
117124
))}
118125
</motion.div>

src/Common/SegmentedBarChart/types.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ export type Entity = {
2020
value: number
2121
}
2222

23-
export interface SegmentedBarChartProps {
24-
entities: NonNullable<Entity[]>
23+
type EntityPropType =
24+
| {
25+
hideLegend?: false
26+
entities: NonNullable<Entity[]>
27+
}
28+
| {
29+
hideLegend: true
30+
entities: NonNullable<Omit<Entity, 'label'> & { label?: never }>[]
31+
}
32+
33+
export type SegmentedBarChartProps = {
2534
rootClassName?: string
2635
countClassName?: string
2736
labelClassName?: string
2837
isProportional?: boolean
2938
swapLegendAndBar?: boolean
3039
showAnimationOnBar?: boolean
3140
isLoading?: boolean
32-
}
41+
} & EntityPropType

src/Pages/ResourceBrowser/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import { Dispatch, ReactElement, SetStateAction } from 'react'
1818

19+
import { TabProps } from '@Shared/Components'
20+
1921
import { NodeActionRequest } from './ResourceBrowser.Types'
2022

2123
export enum ClusterFiltersType {
@@ -99,3 +101,5 @@ export interface AdditionalConfirmationModalOptionsProps<T = unknown> {
99101
setOptionsData: Dispatch<SetStateAction<T>>
100102
children?: ReactElement
101103
}
104+
105+
export type NodeDetailTabsInfoType = (Pick<TabProps, 'label' | 'icon'> & { id: string; node: () => JSX.Element })[]

src/Shared/Components/Security/SecurityModal/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import React from 'react'
1818

19-
import { SegmentedBarChartProps } from '@Common/SegmentedBarChart'
19+
import { Entity } from '@Common/SegmentedBarChart/types'
2020
import { ServerErrors } from '@Common/ServerError'
2121
import { GenericEmptyStateType } from '@Common/Types'
2222
import { LastExecutionResultType, Nodes, NodeType } from '@Shared/types'
@@ -103,7 +103,7 @@ export interface StatusType {
103103
}
104104

105105
export interface InfoCardPropsType extends Pick<StatusType, 'scanToolName' | 'scanToolUrl'> {
106-
entities: SegmentedBarChartProps['entities']
106+
entities: NonNullable<Entity[]>
107107
lastScanTimeString?: string
108108
}
109109

0 commit comments

Comments
 (0)