1
1
import React from 'react' ;
2
+ import useAsync from 'react-use/lib/useAsync' ;
2
3
3
4
import {
4
5
InfoCard as BSInfoCard ,
5
6
CopyTextButton ,
6
7
} from '@backstage/core-components' ;
8
+ import { useApi } from '@backstage/core-plugin-api' ;
7
9
8
10
import UnfoldLessIcon from '@mui/icons-material/UnfoldLess' ;
9
11
import UnfoldMoreIcon from '@mui/icons-material/UnfoldMore' ;
10
12
import IconButton from '@mui/material/IconButton' ;
11
13
import Typography from '@mui/material/Typography' ;
12
14
15
+ import { buildInfoApiRef } from '../../api/BuildInfoApiClient' ;
13
16
import buildMetadata from '../../build-metadata.json' ;
14
17
15
18
export const InfoCard = ( ) => {
19
+ const client = useApi ( buildInfoApiRef ) ;
20
+ const { value : buildInfo } = useAsync ( ( ) => {
21
+ return client . getBuildInfo ( ) ;
22
+ } ) ;
23
+
16
24
const [ showBuildInformation , setShowBuildInformation ] =
17
25
React . useState < boolean > (
18
26
( ) =>
@@ -32,24 +40,47 @@ export const InfoCard = () => {
32
40
}
33
41
} ;
34
42
35
- let clipboardText = buildMetadata . title ;
36
- if ( buildMetadata . card ?. length ) {
43
+ const buildDetailsTitle = ( ) => {
44
+ if ( buildInfo ?. title && Object . keys ( buildInfo ?. card ?? { } ) . length > 0 ) {
45
+ return buildInfo . title ;
46
+ }
47
+ if ( ! buildInfo ?. title && Object . keys ( buildInfo ?. card ?? { } ) . length > 0 ) {
48
+ // eslint-disable-next-line no-console
49
+ console . warn (
50
+ 'Set a custom title for your build details to display your preferred title.' ,
51
+ ) ;
52
+ return 'Build Metadata' ;
53
+ }
54
+ return buildMetadata . title ;
55
+ } ;
56
+
57
+ let clipboardText = buildDetailsTitle ( ) ;
58
+ const buildDetails = Object . entries (
59
+ buildInfo ?. card && Object . keys ( buildInfo . card ) . length > 0
60
+ ? buildInfo . card
61
+ : buildMetadata ?. card || { } ,
62
+ ) . map ( ( [ key , value ] ) => `${ key } : ${ value } ` ) ;
63
+ if ( buildDetails ?. length ) {
37
64
clipboardText += '\n\n' ;
38
- buildMetadata . card . forEach ( text => {
65
+ buildDetails . forEach ( text => {
39
66
clipboardText += `${ text } \n` ;
40
67
} ) ;
41
68
}
42
69
43
- const filteredCards = showBuildInformation
44
- ? buildMetadata . card
45
- : buildMetadata . card . filter (
46
- text =>
47
- text . startsWith ( 'RHDH Version' ) ||
48
- text . startsWith ( 'Backstage Version' ) ,
49
- ) ;
70
+ const filteredContent = ( ) => {
71
+ if ( Object . keys ( buildInfo ?. card ?? { } ) ?. length > 0 ) {
72
+ return buildDetails . slice ( 0 , 2 ) ;
73
+ }
74
+ return buildDetails . filter (
75
+ text =>
76
+ text . startsWith ( 'RHDH Version' ) || text . startsWith ( 'Backstage Version' ) ,
77
+ ) ;
78
+ } ;
79
+
80
+ const filteredCards = showBuildInformation ? buildDetails : filteredContent ( ) ;
50
81
// Ensure that we show always some information
51
82
const versionInfo =
52
- filteredCards . length > 0 ? filteredCards . join ( '\n' ) : buildMetadata . card [ 0 ] ;
83
+ filteredCards . length > 0 ? filteredCards . join ( '\n' ) : buildDetails [ 0 ] ;
53
84
54
85
/**
55
86
* Show all build information and automatically select them
@@ -80,7 +111,7 @@ export const InfoCard = () => {
80
111
81
112
return (
82
113
< BSInfoCard
83
- title = { buildMetadata . title }
114
+ title = { buildDetailsTitle ( ) }
84
115
action = {
85
116
// This is a workaround to ensure that the buttons doesn't increase the header size.
86
117
< div style = { { position : 'relative' } } >
0 commit comments