From ef3f517dd09e920e64782b3208e1c0e9e4ba3dcd Mon Sep 17 00:00:00 2001 From: Bogdan Nechyporenko Date: Wed, 4 Oct 2023 12:52:17 +0200 Subject: [PATCH] Added a possibility to use a formatter for error titles. Applied it for scaffolder template Signed-off-by: Bogdan Nechyporenko --- .../components/ErrorPanel/ErrorPanel.test.tsx | 47 +++++++++++++++++++ .../src/components/ErrorPanel/ErrorPanel.tsx | 4 +- .../WarningPanel/WarningPanel.test.tsx | 19 +++++++- .../components/WarningPanel/WarningPanel.tsx | 17 ++++++- .../src/next/OngoingTask/OngoingTask.tsx | 1 + 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 packages/core-components/src/components/ErrorPanel/ErrorPanel.test.tsx diff --git a/packages/core-components/src/components/ErrorPanel/ErrorPanel.test.tsx b/packages/core-components/src/components/ErrorPanel/ErrorPanel.test.tsx new file mode 100644 index 0000000000..a3588b99c3 --- /dev/null +++ b/packages/core-components/src/components/ErrorPanel/ErrorPanel.test.tsx @@ -0,0 +1,47 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { WarningPanel } from '../WarningPanel'; +import { screen } from '@testing-library/react'; +import React from 'react'; +import { renderInTestApp } from '@backstage/test-utils'; +import { WarningProps } from '../WarningPanel/WarningPanel'; + +describe('', () => { + const propsErrorMessage: WarningProps = { + severity: 'error', + title: 'Mock title', + message: 'Some more info', + }; + + it('renders a title formatted by markdown', async () => { + await renderInTestApp( + , + ); + expect( + screen.getByText('Error: Step has been failed.'), + ).toBeInTheDocument(); + + expect(screen.getByText('Help')).toHaveAttribute( + 'href', + 'https://commonmark.org/help', + ); + }); +}); diff --git a/packages/core-components/src/components/ErrorPanel/ErrorPanel.tsx b/packages/core-components/src/components/ErrorPanel/ErrorPanel.tsx index 02520d2c5c..a25a864083 100644 --- a/packages/core-components/src/components/ErrorPanel/ErrorPanel.tsx +++ b/packages/core-components/src/components/ErrorPanel/ErrorPanel.tsx @@ -96,6 +96,7 @@ const ErrorList = ({ export type ErrorPanelProps = { error: Error; defaultExpanded?: boolean; + formatTitle?: string; title?: string; }; @@ -105,12 +106,13 @@ export type ErrorPanelProps = { * @public */ export function ErrorPanel(props: PropsWithChildren) { - const { title, error, defaultExpanded, children } = props; + const { title, error, defaultExpanded, formatTitle, children } = props; return ( ', () => { await renderInTestApp(); expect(screen.getByText('Error: Mock title')).toBeInTheDocument(); }); + it('renders a title formatted by markdown', async () => { + await renderInTestApp( + , + ); + expect( + screen.getByText('Error: Step has been failed.'), + ).toBeInTheDocument(); + + expect(screen.getByText('Help')).toHaveAttribute( + 'href', + 'https://commonmark.org/help', + ); + }); }); diff --git a/packages/core-components/src/components/WarningPanel/WarningPanel.tsx b/packages/core-components/src/components/WarningPanel/WarningPanel.tsx index 7e8248115f..19cb3dacf1 100644 --- a/packages/core-components/src/components/WarningPanel/WarningPanel.tsx +++ b/packages/core-components/src/components/WarningPanel/WarningPanel.tsx @@ -23,6 +23,7 @@ import Typography from '@material-ui/core/Typography'; import ErrorOutline from '@material-ui/icons/ErrorOutline'; import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; import React from 'react'; +import { MarkdownContent } from '../MarkdownContent'; const getWarningTextColor = ( severity: NonNullable, @@ -94,6 +95,11 @@ const useStyles = makeStyles( ), fontWeight: theme.typography.fontWeightBold, }, + markdownContent: { + '& p': { + display: 'inline', + }, + }, message: { width: '100%', display: 'block', @@ -124,6 +130,7 @@ const useStyles = makeStyles( export type WarningProps = { title?: string; severity?: 'warning' | 'error' | 'info'; + formatTitle?: string; message?: React.ReactNode; defaultExpanded?: boolean; children?: React.ReactNode; @@ -151,6 +158,7 @@ export function WarningPanel(props: WarningProps) { const { severity = 'warning', title, + formatTitle, message, children, defaultExpanded, @@ -172,7 +180,14 @@ export function WarningPanel(props: WarningProps) { > - {subTitle} + {formatTitle === 'markdown' ? ( + + ) : ( + subTitle + )} {(message || children) && ( diff --git a/plugins/scaffolder/src/next/OngoingTask/OngoingTask.tsx b/plugins/scaffolder/src/next/OngoingTask/OngoingTask.tsx index b483cd27cb..609f9bde8b 100644 --- a/plugins/scaffolder/src/next/OngoingTask/OngoingTask.tsx +++ b/plugins/scaffolder/src/next/OngoingTask/OngoingTask.tsx @@ -162,6 +162,7 @@ export const OngoingTask = (props: {