@@ -19,21 +19,15 @@ import type { SetStateAction } from 'react';
1919import { useCallback , useEffect , useState } from 'react' ;
2020
2121import { ErrorPage , Progress } from '@backstage/core-components' ;
22- import {
23- alertApiRef ,
24- useApi ,
25- useRouteRef ,
26- useRouteRefParams ,
27- } from '@backstage/core-plugin-api' ;
22+ import { useRouteRef , useRouteRefParams } from '@backstage/core-plugin-api' ;
2823
2924import yaml from 'yaml' ;
3025import { useNavigate } from 'react-router-dom' ;
3126
3227import {
3328 MarketplacePackage ,
34- MarketplacePackageSpecAppConfigExample ,
29+ ExtensionsPackageAppConfigExamples ,
3530} from '@red-hat-developer-hub/backstage-plugin-marketplace-common' ;
36- import { JsonObject } from '@backstage/types' ;
3731
3832import Box from '@mui/material/Box' ;
3933import Grid from '@mui/material/Grid' ;
@@ -51,94 +45,19 @@ import {
5145 pluginInstallRouteRef ,
5246 pluginRouteRef ,
5347} from '../routes' ;
54- import { applyContent , getExampleAsMarkdown } from '../utils' ;
5548
56- import {
57- CodeEditorContextProvider ,
58- CodeEditor ,
59- useCodeEditor ,
60- } from './CodeEditor' ;
61- import { Markdown } from './Markdown' ;
49+ import { CodeEditorContextProvider , useCodeEditor } from './CodeEditor' ;
6250import { usePackage } from '../hooks/usePackage' ;
51+ import { CodeEditorCard } from './CodeEditorCard' ;
52+ import { TabPanel } from './TabPanel' ;
6353
6454interface TabItem {
6555 label : string ;
66- content : string | MarketplacePackageSpecAppConfigExample [ ] ;
56+ content : string | ExtensionsPackageAppConfigExamples [ ] ;
6757 key : string ;
6858 others ?: { [ key : string ] : any } ;
6959}
7060
71- interface TabPanelProps {
72- markdownContent : string | MarketplacePackageSpecAppConfigExample [ ] ;
73- index : number ;
74- value : number ;
75- others ?: { [ key : string ] : any } ;
76- }
77-
78- const TabPanel = ( { markdownContent, index, value, others } : TabPanelProps ) => {
79- const alertApi = useApi ( alertApiRef ) ;
80- const codeEditor = useCodeEditor ( ) ;
81- if ( value !== index ) return null ;
82-
83- const handleApplyContent = ( content : string | JsonObject ) => {
84- try {
85- const codeEditorContent = codeEditor . getValue ( ) ;
86- const newContent = applyContent (
87- codeEditorContent || '' ,
88- others ?. packageName ,
89- content ,
90- ) ;
91- const selection = codeEditor . getSelection ( ) ;
92- const position = codeEditor . getPosition ( ) ;
93- if ( newContent ) {
94- codeEditor . setValue ( newContent ) ;
95- if ( selection ) {
96- codeEditor . setSelection ( selection ) ;
97- }
98- if ( position ) {
99- codeEditor . setPosition ( position ) ;
100- }
101- }
102- } catch ( error ) {
103- alertApi . post ( {
104- display : 'transient' ,
105- severity : 'warning' ,
106- message : `Could not apply YAML: ${ error } ` ,
107- } ) ;
108- }
109- } ;
110-
111- return (
112- < Box
113- role = "tabpanel"
114- sx = { { flex : 1 , overflow : 'auto' , p : 2 , scrollbarWidth : 'thin' } }
115- >
116- < Typography component = "div" >
117- { Array . isArray ( markdownContent ) ? (
118- markdownContent . map ( ( item , idx ) => (
119- < Box key = { idx } sx = { { mb : 3 } } >
120- < Typography variant = "h6" sx = { { fontWeight : 'bold' , mb : 1 } } >
121- { item . title }
122- { item . content !== 'string' && (
123- < Button
124- sx = { { float : 'right' } }
125- onClick = { ( ) => handleApplyContent ( item . content ) }
126- >
127- Apply
128- </ Button >
129- ) }
130- </ Typography >
131- < Markdown content = { getExampleAsMarkdown ( item . content ) } />
132- </ Box >
133- ) )
134- ) : (
135- < Markdown content = { markdownContent } />
136- ) }
137- </ Typography >
138- </ Box >
139- ) ;
140- } ;
141-
14261export const MarketplacePackageInstallContent = ( {
14362 pkg,
14463} : {
@@ -176,13 +95,20 @@ export const MarketplacePackageInstallContent = ({
17695 } , [ codeEditor , pkg ] ) ;
17796
17897 const navigate = useNavigate ( ) ;
179- const examples = pkg ?. spec ?. appConfigExamples ;
98+ const examples = [
99+ {
100+ [ `${ pkg . metadata . name } ` ] : pkg . spec ?. appConfigExamples ,
101+ } ,
102+ ] ;
103+ const packageDynamicArtifacts = {
104+ [ `${ pkg . metadata . name } ` ] : pkg . spec ?. dynamicArtifact ,
105+ } ;
180106 const availableTabs = [
181- examples && {
107+ ! ! Object . values ( examples [ 0 ] ) [ 0 ] && {
182108 label : 'Examples' ,
183109 content : examples ,
184110 key : 'examples' ,
185- others : { packageName : pkg . spec ?. dynamicArtifact } ,
111+ others : { packageNames : packageDynamicArtifacts } ,
186112 } ,
187113 ] . filter ( tab => tab ) as TabItem [ ] ;
188114
@@ -207,34 +133,7 @@ export const MarketplacePackageInstallContent = ({
207133 spacing = { 3 }
208134 sx = { { flex : 1 , overflow : 'hidden' , height : '100%' , pb : 1 } }
209135 >
210- < Grid
211- item
212- xs = { 12 }
213- md = { 6.5 }
214- sx = { { display : 'flex' , flexDirection : 'column' , height : '100%' } }
215- >
216- < Card
217- sx = { {
218- flex : 1 ,
219- display : 'flex' ,
220- flexDirection : 'column' ,
221- overflow : 'hidden' ,
222- borderRadius : 0 ,
223- } }
224- >
225- < CardContent
226- sx = { {
227- flex : 1 ,
228- display : 'flex' ,
229- flexDirection : 'column' ,
230- overflow : 'auto' ,
231- scrollbarWidth : 'thin' ,
232- } }
233- >
234- < CodeEditor defaultLanguage = "yaml" onLoaded = { onLoaded } />
235- </ CardContent >
236- </ Card >
237- </ Grid >
136+ < CodeEditorCard onLoad = { onLoaded } />
238137
239138 { showRightCard && (
240139 < Grid
0 commit comments