Skip to content

Commit 52f82ec

Browse files
authored
feat(homepage): update quickaccess card icon rendering logic (#1578)
1 parent c6e4f39 commit 52f82ec

File tree

6 files changed

+126
-11
lines changed

6 files changed

+126
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-dynamic-home-page': patch
3+
---
4+
5+
update quickaccess card's icon rendering logic

workspaces/homepage/packages/app/src/App.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
DynamicCustomizableHomePage,
6161
VisitListener,
6262
OnboardingSection,
63+
QuickAccessCard,
6364
EntitySection,
6465
TemplateSection,
6566
defaultLayouts,
@@ -115,6 +116,12 @@ const mountPoints: HomePageCardMountPoint[] = [
115116
layouts: defaultLayouts.onboarding,
116117
},
117118
},
119+
{
120+
Component: QuickAccessCard,
121+
config: {
122+
layouts: defaultLayouts.quickAccessCard,
123+
},
124+
},
118125
{
119126
Component: EntitySection,
120127
config: {

workspaces/homepage/plugins/dynamic-home-page/report.api.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,38 @@ export const defaultLayouts: {
114114
h: number;
115115
};
116116
};
117+
quickAccessCard: {
118+
xl: {
119+
w: number;
120+
h: number;
121+
x: number;
122+
};
123+
lg: {
124+
w: number;
125+
h: number;
126+
x: number;
127+
};
128+
md: {
129+
w: number;
130+
h: number;
131+
x: number;
132+
};
133+
sm: {
134+
w: number;
135+
h: number;
136+
x: number;
137+
};
138+
xs: {
139+
w: number;
140+
h: number;
141+
x: number;
142+
};
143+
xxs: {
144+
w: number;
145+
h: number;
146+
x: number;
147+
};
148+
};
117149
};
118150

119151
// @public

workspaces/homepage/plugins/dynamic-home-page/src/components/QuickAccessCard.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { makeStyles } from 'tss-react/mui';
2828

2929
import { useQuickAccessLinks } from '../hooks/useQuickAccessLinks';
3030
import { useTranslation } from '../hooks/useTranslation';
31+
import { QuickAccessIcon } from './QuickAccessIcon';
3132

3233
const useStyles = makeStyles()({
3334
center: {
@@ -36,10 +37,6 @@ const useStyles = makeStyles()({
3637
alignItems: 'center',
3738
justifyContent: 'center',
3839
},
39-
img: {
40-
height: '40px',
41-
width: 'auto',
42-
},
4340
title: {
4441
'& div > div > div > div > p': {
4542
textTransform: 'uppercase',
@@ -89,13 +86,7 @@ export const QuickAccessCard = (props: QuickAccessCardProps) => {
8986
title={item.title}
9087
tools={item.links.map(link => ({
9188
...link,
92-
icon: (
93-
<img
94-
className={classes.img}
95-
src={link.iconUrl}
96-
alt={link.label}
97-
/>
98-
),
89+
icon: <QuickAccessIcon icon={link.iconUrl} alt={link.label} />,
9990
}))}
10091
// Component creation is allowed inside component props only
10192
// if prop name starts with `render`.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright Red Hat, Inc.
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 type { ReactElement } from 'react';
18+
import { makeStyles } from 'tss-react/mui';
19+
20+
import { isValidElement } from 'react';
21+
import { useApp } from '@backstage/core-plugin-api';
22+
23+
import MuiIcon from '@mui/material/Icon';
24+
25+
const useStyles = makeStyles()({
26+
img: {
27+
height: '40px',
28+
width: 'auto',
29+
},
30+
});
31+
32+
export const QuickAccessIcon = ({
33+
icon,
34+
alt,
35+
}: {
36+
icon: string | ReactElement;
37+
alt: string;
38+
}) => {
39+
const { classes } = useStyles();
40+
const app = useApp();
41+
42+
if (!icon) {
43+
return null;
44+
}
45+
46+
if (isValidElement(icon)) {
47+
return icon;
48+
}
49+
50+
const strIcon = icon as string;
51+
52+
const SystemIcon = app.getSystemIcon(strIcon);
53+
54+
if (SystemIcon) {
55+
return <SystemIcon fontSize="large" />;
56+
}
57+
58+
if (strIcon.startsWith('<svg')) {
59+
const svgDataUri = `data:image/svg+xml;base64,${btoa(strIcon)}`;
60+
return <img src={svgDataUri} className={classes.img} alt={alt} />;
61+
}
62+
63+
if (
64+
strIcon.startsWith('https://') ||
65+
strIcon.startsWith('http://') ||
66+
strIcon.startsWith('/')
67+
) {
68+
return <img src={strIcon} className={classes.img} alt={alt} />;
69+
}
70+
71+
return <MuiIcon baseClassName="material-icons-outlined">{strIcon}</MuiIcon>;
72+
};

workspaces/homepage/plugins/dynamic-home-page/src/defaults.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,12 @@ export const defaultLayouts = {
6565
xs: { w: 12, h: 7.5 },
6666
xxs: { w: 12, h: 13.5 },
6767
},
68+
quickAccessCard: {
69+
xl: { w: 6, h: 8, x: 6 },
70+
lg: { w: 6, h: 8, x: 6 },
71+
md: { w: 6, h: 8, x: 6 },
72+
sm: { w: 12, h: 8, x: 6 },
73+
xs: { w: 12, h: 8, x: 6 },
74+
xxs: { w: 12, h: 8, x: 6 },
75+
},
6876
};

0 commit comments

Comments
 (0)