import { DescriptionList, DescriptionListGroup, DescriptionListTerm, DescriptionListDescription, Stack, StackItem, } from '@patternfly/react-core'; import { isObject } from 'lodash-es'; import ExternalLink from '../../../../shared/components/links/ExternalLink'; import { getImageLink, isUrl } from './url'; /** * converts a key (e.g. `build_id`, `commitHash`) into a more readable label * by adding spaces and capitalizing only the first word. * * Examples: * - "build_id" -> "Build id" * - "commitHash" -> "Commit hash" * - "last_updated" -> "Last updated" */ function humanizeKey(key: string): string { const spaced = key .replace(/_{2,}/g, '_') // Replace multiple underscores with a single underscore .replace(/_/g, ' ') // Replace remaining underscores with spaces .replace(/([a-z0-9])([A-Z])/g, '$1 $2'); // add space between camelCase words return spaced.charAt(0).toUpperCase() + spaced.slice(1).toLocaleLowerCase(); // Capitalize only the first letter } function renderFinalValue(value: unknown): React.ReactNode { const str = String(value); if (isUrl(str)) { return ; } if (str.includes('quay.io')) { return ; } if (isObject(value)) { return JSON.stringify(value, null, 2); } return str; } function renderValue(value: unknown, level: number = 0): React.ReactNode { if (value === null || value === undefined) { return —; } if (Array.isArray(value)) { return value.length === 0 ? ( — ) : ( {value.map((item, idx) => ( {renderValue(item, level + 1)} ))} ); } if (typeof value === 'object') { const entries = Object.entries(value as Record); return entries.length === 0 ? ( — ) : ( 0 ? { paddingLeft: `var(--pf-t--global--spacer--${level > 1 ? 'xl' : 'md'})` } : undefined } > {entries.map(([k, v]) => ( {humanizeKey(k)}: {renderValue(v, level + 1)} ))} ); } return renderFinalValue(value); } export function renderKeyValueList(data: Record) { return ( {Object.entries(data).map(([key, value]) => ( {humanizeKey(key)} {renderValue(value)} ))} ); }