| 1 |
import { Fragment } from 'react'; |
| 2 |
import { |
| 3 |
Card, |
| 4 |
CardBody, |
| 5 |
CardTitle, |
| 6 |
Divider, |
| 7 |
Flex, |
| 8 |
FlexItem, |
| 9 |
Gallery, |
| 10 |
Grid, |
| 11 |
GridItem, |
| 12 |
Stack, |
| 13 |
Icon |
| 14 |
} from '@patternfly/react-core'; |
| 15 |
import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/js/icons/rh-ui-check-circle-fill-icon'; |
| 16 |
import RhUiErrorFillIcon from '@patternfly/react-icons/dist/js/icons/rh-ui-error-fill-icon'; |
| 17 |
import RhUiWarningFillIcon from '@patternfly/react-icons/dist/js/icons/rh-ui-warning-fill-icon'; |
| 18 |
import cssGridTemplateColumnsMin from '@patternfly/react-tokens/dist/esm/l_gallery_GridTemplateColumns_min'; |
| 19 |
|
| 20 |
interface ContentType { |
| 21 |
icon?: React.ReactNode; |
| 22 |
count?: number; |
| 23 |
status?: string; |
| 24 |
subtitle?: string; |
| 25 |
} |
| 26 |
interface CardData { |
| 27 |
[attribute: string]: { |
| 28 |
title: string; |
| 29 |
content: { |
| 30 |
icon: React.JSX.Element; |
| 31 |
count?: number; |
| 32 |
status?: string; |
| 33 |
subtitle?: string; |
| 34 |
}[]; |
| 35 |
layout: string; |
| 36 |
}[]; |
| 37 |
} |
| 38 |
|
| 39 |
const cardData: CardData = { |
| 40 |
iconOnly: [ |
| 41 |
{ |
| 42 |
title: '5 Clusters', |
| 43 |
content: [ |
| 44 |
{ |
| 45 |
icon: ( |
| 46 |
<Icon status="success"> |
| 47 |
<RhUiCheckCircleFillIcon /> |
| 48 |
</Icon> |
| 49 |
) |
| 50 |
} |
| 51 |
], |
| 52 |
layout: 'icon' |
| 53 |
}, |
| 54 |
{ |
| 55 |
title: '15 Clusters', |
| 56 |
content: [ |
| 57 |
{ |
| 58 |
icon: ( |
| 59 |
<Icon status="warning"> |
| 60 |
<RhUiWarningFillIcon /> |
| 61 |
</Icon> |
| 62 |
) |
| 63 |
} |
| 64 |
], |
| 65 |
layout: 'icon' |
| 66 |
}, |
| 67 |
{ |
| 68 |
title: '3 Clusters', |
| 69 |
content: [ |
| 70 |
{ |
| 71 |
icon: ( |
| 72 |
<Icon status="danger"> |
| 73 |
<RhUiErrorFillIcon /> |
| 74 |
</Icon> |
| 75 |
) |
| 76 |
} |
| 77 |
], |
| 78 |
layout: 'icon' |
| 79 |
} |
| 80 |
], |
| 81 |
iconWithCount: [ |
| 82 |
{ |
| 83 |
title: '10 Hosts', |
| 84 |
content: [ |
| 85 |
{ |
| 86 |
icon: ( |
| 87 |
<Icon status="success"> |
| 88 |
<RhUiCheckCircleFillIcon /> |
| 89 |
</Icon> |
| 90 |
), |
| 91 |
count: 2 |
| 92 |
}, |
| 93 |
{ |
| 94 |
icon: ( |
| 95 |
<Icon status="warning"> |
| 96 |
<RhUiWarningFillIcon /> |
| 97 |
</Icon> |
| 98 |
), |
| 99 |
count: 1 |
| 100 |
} |
| 101 |
], |
| 102 |
layout: 'multiIcon' |
| 103 |
}, |
| 104 |
{ |
| 105 |
title: '50 Hosts', |
| 106 |
content: [ |
| 107 |
{ |
| 108 |
icon: ( |
| 109 |
<Icon status="success"> |
| 110 |
<RhUiCheckCircleFillIcon /> |
| 111 |
</Icon> |
| 112 |
), |
| 113 |
count: 5 |
| 114 |
}, |
| 115 |
{ |
| 116 |
icon: ( |
| 117 |
<Icon status="danger"> |
| 118 |
<RhUiErrorFillIcon /> |
| 119 |
</Icon> |
| 120 |
), |
| 121 |
count: 12 |
| 122 |
} |
| 123 |
], |
| 124 |
layout: 'multiIcon' |
| 125 |
}, |
| 126 |
{ |
| 127 |
title: '12 Hosts', |
| 128 |
content: [ |
| 129 |
{ |
| 130 |
icon: ( |
| 131 |
<Icon status="warning"> |
| 132 |
<RhUiWarningFillIcon /> |
| 133 |
</Icon> |
| 134 |
), |
| 135 |
count: 3 |
| 136 |
}, |
| 137 |
{ |
| 138 |
icon: ( |
| 139 |
<Icon status="danger"> |
| 140 |
<RhUiErrorFillIcon /> |
| 141 |
</Icon> |
| 142 |
), |
| 143 |
count: 7 |
| 144 |
} |
| 145 |
], |
| 146 |
layout: 'multiIcon' |
| 147 |
} |
| 148 |
], |
| 149 |
withSubtitle: [ |
| 150 |
{ |
| 151 |
title: '13 Hosts', |
| 152 |
content: [ |
| 153 |
{ |
| 154 |
icon: ( |
| 155 |
<Icon status="danger"> |
| 156 |
<RhUiErrorFillIcon /> |
| 157 |
</Icon> |
| 158 |
), |
| 159 |
status: '2 errors', |
| 160 |
subtitle: 'subtitle' |
| 161 |
}, |
| 162 |
{ |
| 163 |
icon: ( |
| 164 |
<Icon status="warning"> |
| 165 |
<RhUiWarningFillIcon /> |
| 166 |
</Icon> |
| 167 |
), |
| 168 |
status: '1 warning', |
| 169 |
subtitle: 'subtitle' |
| 170 |
} |
| 171 |
], |
| 172 |
layout: 'withSubtitle' |
| 173 |
}, |
| 174 |
{ |
| 175 |
title: '3 Hosts', |
| 176 |
content: [ |
| 177 |
{ |
| 178 |
icon: ( |
| 179 |
<Icon status="success"> |
| 180 |
<RhUiCheckCircleFillIcon /> |
| 181 |
</Icon> |
| 182 |
), |
| 183 |
status: '2 successes', |
| 184 |
subtitle: 'subtitle' |
| 185 |
}, |
| 186 |
{ |
| 187 |
icon: ( |
| 188 |
<Icon status="warning"> |
| 189 |
<RhUiWarningFillIcon /> |
| 190 |
</Icon> |
| 191 |
), |
| 192 |
status: '3 warnings', |
| 193 |
subtitle: 'subtitle' |
| 194 |
} |
| 195 |
], |
| 196 |
layout: 'withSubtitle' |
| 197 |
}, |
| 198 |
{ |
| 199 |
title: '50 Hosts', |
| 200 |
content: [ |
| 201 |
{ |
| 202 |
icon: ( |
| 203 |
<Icon status="warning"> |
| 204 |
<RhUiWarningFillIcon /> |
| 205 |
</Icon> |
| 206 |
), |
| 207 |
status: '7 warnings', |
| 208 |
subtitle: 'subtitle' |
| 209 |
}, |
| 210 |
{ |
| 211 |
icon: ( |
| 212 |
<Icon status="danger"> |
| 213 |
<RhUiErrorFillIcon /> |
| 214 |
</Icon> |
| 215 |
), |
| 216 |
status: '1 error', |
| 217 |
subtitle: 'subtitle' |
| 218 |
} |
| 219 |
], |
| 220 |
layout: 'withSubtitle' |
| 221 |
} |
| 222 |
] |
| 223 |
}; |
| 224 |
|
| 225 |
export const CardAggregateStatus: React.FunctionComponent = () => { |
| 226 |
const renderContent = (content: ContentType[], layout: string) => { |
| 227 |
if (layout === 'icon') { |
| 228 |
return content[0].icon; |
| 229 |
} |
| 230 |
if (layout === 'multiIcon') { |
| 231 |
return ( |
| 232 |
<Flex display={{ default: 'inlineFlex' }}> |
| 233 |
{content.map(({ icon, count }, index: number) => ( |
| 234 |
<Fragment key={index}> |
| 235 |
<Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 236 |
<FlexItem>{icon}</FlexItem> |
| 237 |
<FlexItem> |
| 238 |
<a href="#">{count}</a> |
| 239 |
</FlexItem> |
| 240 |
</Flex> |
| 241 |
{content.length > 1 && index === 0 && ( |
| 242 |
<Divider |
| 243 |
key={`${index}_d`} |
| 244 |
orientation={{ |
| 245 |
default: 'vertical' |
| 246 |
}} |
| 247 |
/> |
| 248 |
)} |
| 249 |
</Fragment> |
| 250 |
))} |
| 251 |
</Flex> |
| 252 |
); |
| 253 |
} |
| 254 |
if (layout === 'withSubtitle') { |
| 255 |
return ( |
| 256 |
<Flex justifyContent={{ default: 'justifyContentSpaceAround' }}> |
| 257 |
{content.map(({ icon, status, subtitle }, index: number) => ( |
| 258 |
<Flex key={index}> |
| 259 |
<FlexItem>{icon}</FlexItem> |
| 260 |
<Stack> |
| 261 |
<a href="#">{status}</a> |
| 262 |
<span>{subtitle}</span> |
| 263 |
</Stack> |
| 264 |
</Flex> |
| 265 |
))} |
| 266 |
</Flex> |
| 267 |
); |
| 268 |
} |
| 269 |
}; |
| 270 |
return ( |
| 271 |
<Grid hasGutter> |
| 272 |
{Object.keys(cardData).map((cardGroup, groupIndex) => { |
| 273 |
let galleryWidth: string = ''; |
| 274 |
let cardAlign: string; |
| 275 |
let titleAlign: string; |
| 276 |
if (cardGroup === 'withSubtitle') { |
| 277 |
galleryWidth = '260px'; |
| 278 |
cardAlign = ''; |
| 279 |
titleAlign = 'center'; |
| 280 |
} else { |
| 281 |
cardAlign = 'center'; |
| 282 |
} |
| 283 |
return ( |
| 284 |
<GridItem key={groupIndex}> |
| 285 |
<Gallery hasGutter style={{ [cssGridTemplateColumnsMin.name]: galleryWidth } as React.CSSProperties}> |
| 286 |
{cardData[cardGroup].map(({ title, content, layout }, cardIndex: number) => ( |
| 287 |
<Card |
| 288 |
style={{ textAlign: cardAlign } as React.CSSProperties} |
| 289 |
key={`${groupIndex}${cardIndex}`} |
| 290 |
component="div" |
| 291 |
> |
| 292 |
<CardTitle style={{ textAlign: titleAlign } as React.CSSProperties}>{title}</CardTitle> |
| 293 |
<CardBody>{renderContent(content, layout)}</CardBody> |
| 294 |
</Card> |
| 295 |
))} |
| 296 |
</Gallery> |
| 297 |
</GridItem> |
| 298 |
); |
| 299 |
})} |
| 300 |
</Grid> |
| 301 |
); |
| 302 |
}; |