Skip to content

Commit 171b7fd

Browse files
authored
fix(extensions): load all the package configurations (#1215)
* fix(extensions): load all the package configurations * remove examples tab if no appConfigExamples are present * refactor to remove duplicate code
1 parent 3fce49c commit 171b7fd

9 files changed

Lines changed: 243 additions & 246 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-marketplace': patch
3+
'@red-hat-developer-hub/backstage-plugin-marketplace-common': patch
4+
---
5+
6+
load all the package configurations

workspaces/marketplace/plugins/marketplace-common/report.api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ export const encodeGetEntityFacetsRequest: (request: GetEntityFacetsRequest) =>
8383
// @public (undocumented)
8484
export const EXTENSIONS_API_VERSION = "extensions.backstage.io/v1alpha1";
8585

86+
// @public (undocumented)
87+
export interface ExtensionsPackageAppConfigExamples {
88+
// (undocumented)
89+
[key: string]: MarketplacePackageSpecAppConfigExample[];
90+
}
91+
8692
// @public (undocumented)
8793
export const extensionsPermissions: ResourcePermission<"extensions-plugin">[];
8894

workspaces/marketplace/plugins/marketplace-common/src/types/MarketplacePackage.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ export interface MarketplacePackageSpecAppConfigExample extends JsonObject {
6969
title: string;
7070
content: string | JsonObject;
7171
}
72+
/**
73+
* @public
74+
*/
75+
export interface ExtensionsPackageAppConfigExamples {
76+
[key: string]: MarketplacePackageSpecAppConfigExample[];
77+
}
7278

7379
/**
7480
* @public
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright The Backstage Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Grid from '@mui/material/Grid';
18+
import Card from '@mui/material/Card';
19+
import CardContent from '@mui/material/CardContent';
20+
import { CodeEditor } from './CodeEditor';
21+
22+
export const CodeEditorCard = ({ onLoad }: { onLoad: () => void }) => {
23+
return (
24+
<Grid
25+
item
26+
xs={12}
27+
md={6.5}
28+
sx={{ display: 'flex', flexDirection: 'column', height: '100%' }}
29+
>
30+
<Card
31+
sx={{
32+
flex: 1,
33+
display: 'flex',
34+
flexDirection: 'column',
35+
overflow: 'hidden',
36+
borderRadius: 0,
37+
}}
38+
>
39+
<CardContent
40+
sx={{
41+
flex: 1,
42+
display: 'flex',
43+
flexDirection: 'column',
44+
overflow: 'auto',
45+
scrollbarWidth: 'thin',
46+
}}
47+
>
48+
<CodeEditor defaultLanguage="yaml" onLoaded={onLoad} />
49+
</CardContent>
50+
</Card>
51+
</Grid>
52+
);
53+
};

workspaces/marketplace/plugins/marketplace/src/components/MarketplacePackageInstallContent.tsx

Lines changed: 17 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,15 @@ import type { SetStateAction } from 'react';
1919
import { useCallback, useEffect, useState } from 'react';
2020

2121
import { 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

2924
import yaml from 'yaml';
3025
import { useNavigate } from 'react-router-dom';
3126

3227
import {
3328
MarketplacePackage,
34-
MarketplacePackageSpecAppConfigExample,
29+
ExtensionsPackageAppConfigExamples,
3530
} from '@red-hat-developer-hub/backstage-plugin-marketplace-common';
36-
import { JsonObject } from '@backstage/types';
3731

3832
import Box from '@mui/material/Box';
3933
import 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';
6250
import { usePackage } from '../hooks/usePackage';
51+
import { CodeEditorCard } from './CodeEditorCard';
52+
import { TabPanel } from './TabPanel';
6353

6454
interface 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-
14261
export 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

Comments
 (0)