Repository navigation
feat: add live preview gallery to the home page add widget dialog #844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
achinthajayaweera
wants to merge
2
commits into
openchoreo:main
Choose a base branch
from
achinthajayaweera:feature/widget-gallery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| '@openchoreo/backstage-portal-app': minor | ||
| --- | ||
|
|
||
| Replace the plain Add widget list on the home page with a live preview gallery. | ||
|
|
||
| - Shows each available widget as a card with a live preview, title and description in a three column grid. | ||
| - Adds a search box that filters widgets by title and description, with an empty state when nothing matches. | ||
| - Copies Backstage's `CustomHomepageGrid` and its supporting files into the portal app, because the add widget dialog is private to `@backstage/plugin-home`. | ||
| - Declares the `react-grid-layout`, `react-resizable`, `lodash` and `zod` dependencies that the copied files import. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/portal-app/src/components/Home/AddWidgetDialog.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { renderInTestApp } from '@backstage/test-utils'; | ||
| import { AddWidgetDialog } from './AddWidgetDialog'; | ||
| import type { Widget } from './types'; | ||
|
|
||
| const makeWidget = ( | ||
| name: string, | ||
| title: string, | ||
| description: string, | ||
| ): Widget => ({ | ||
| name, | ||
| title, | ||
| description, | ||
| component: <div data-testid={`preview-${name}`}>{title} preview</div>, | ||
| }); | ||
|
|
||
| const widgets: Widget[] = [ | ||
| makeWidget('Projects', 'My Projects', 'Project and component counts'), | ||
| makeWidget('Deployments', 'Recent Deployments', 'Latest releases'), | ||
| makeWidget('Visited', 'Recently Visited', 'Pages you visited recently'), | ||
| ]; | ||
|
|
||
| const renderDialog = (items: Widget[] = widgets, handleAdd = jest.fn()) => | ||
| renderInTestApp(<AddWidgetDialog widgets={items} handleAdd={handleAdd} />); | ||
|
|
||
| describe('AddWidgetDialog', () => { | ||
| it('shows every available widget with its title and description', async () => { | ||
| await renderDialog(); | ||
|
|
||
| expect(screen.getByText('My Projects')).toBeInTheDocument(); | ||
| expect(screen.getByText('Recent Deployments')).toBeInTheDocument(); | ||
| expect(screen.getByText('Latest releases')).toBeInTheDocument(); | ||
| expect(screen.getAllByRole('button')).toHaveLength(widgets.length); | ||
| }); | ||
|
|
||
| it('filters widgets by title as the user types', async () => { | ||
| await renderDialog(); | ||
|
|
||
| await userEvent.type(screen.getByPlaceholderText('Search widgets'), 'proj'); | ||
|
|
||
| expect(screen.getByText('My Projects')).toBeInTheDocument(); | ||
| expect(screen.queryByText('Recent Deployments')).not.toBeInTheDocument(); | ||
| expect(screen.queryByText('Recently Visited')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('also matches on description, ignoring case', async () => { | ||
| await renderDialog(); | ||
|
|
||
| await userEvent.type( | ||
| screen.getByPlaceholderText('Search widgets'), | ||
| 'LATEST', | ||
| ); | ||
|
|
||
| expect(screen.getByText('Recent Deployments')).toBeInTheDocument(); | ||
| expect(screen.queryByText('My Projects')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows a message when nothing matches the search', async () => { | ||
| await renderDialog(); | ||
|
|
||
| await userEvent.type( | ||
| screen.getByPlaceholderText('Search widgets'), | ||
| 'xyz123', | ||
| ); | ||
|
|
||
| expect( | ||
| screen.getByText('No widgets match your search.'), | ||
| ).toBeInTheDocument(); | ||
| expect(screen.queryByText('My Projects')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('calls handleAdd with the widget when its card is clicked', async () => { | ||
| const handleAdd = jest.fn(); | ||
| await renderDialog(widgets, handleAdd); | ||
|
|
||
| await userEvent.click(screen.getByText('Recent Deployments')); | ||
|
|
||
| expect(handleAdd).toHaveBeenCalledTimes(1); | ||
| expect(handleAdd).toHaveBeenCalledWith(widgets[1]); | ||
| }); | ||
|
|
||
| it('shows the empty message and no search box when no widgets are left', async () => { | ||
| await renderDialog([]); | ||
|
|
||
| expect( | ||
| screen.getByText( | ||
| 'All available widgets have been added to the dashboard.', | ||
| ), | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.queryByPlaceholderText('Search widgets'), | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('hides live previews from assistive tech and the tab order', async () => { | ||
| await renderDialog(); | ||
|
|
||
| const preview = screen.getByTestId('preview-Projects').parentElement; | ||
| expect(preview).toHaveAttribute('aria-hidden', 'true'); | ||
| expect(preview).toHaveAttribute('inert'); | ||
| }); | ||
| }); |
209 changes: 209 additions & 0 deletions
209
packages/portal-app/src/components/Home/AddWidgetDialog.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| /* | ||
| * Copyright 2023 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 { useMemo, useState } from 'react'; | ||
| import { Widget } from './types'; | ||
| import Box from '@material-ui/core/Box'; | ||
| import DialogContent from '@material-ui/core/DialogContent'; | ||
| import DialogTitle from '@material-ui/core/DialogTitle'; | ||
| import Typography from '@material-ui/core/Typography'; | ||
| import ButtonBase from '@material-ui/core/ButtonBase'; | ||
| import TextField from '@material-ui/core/TextField'; | ||
| import InputAdornment from '@material-ui/core/InputAdornment'; | ||
| import SearchIcon from '@material-ui/icons/Search'; | ||
| import { makeStyles, createStyles, Theme } from '@material-ui/core/styles'; | ||
| import { useTranslationRef } from '@backstage/frontend-plugin-api'; | ||
| import { homeTranslationRef } from './translation'; | ||
|
|
||
| interface AddWidgetDialogProps { | ||
| widgets: Widget[]; | ||
| handleAdd: (widget: Widget) => void; | ||
| } | ||
|
|
||
| const getTitle = (widget: Widget) => { | ||
| return widget.title || widget.name; | ||
| }; | ||
|
|
||
| const useStyles = makeStyles((theme: Theme) => | ||
| createStyles({ | ||
| grid: { | ||
| display: 'flex', | ||
| flexWrap: 'wrap', | ||
| justifyContent: 'center', | ||
| gap: theme.spacing(2), | ||
| maxHeight: '60vh', | ||
| overflowY: 'auto', | ||
| padding: theme.spacing(0.5), | ||
| }, | ||
| card: { | ||
| flex: `0 0 calc((100% - ${theme.spacing(2) * 2}px) / 3)`, | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| alignItems: 'stretch', | ||
| justifyContent: 'flex-start', | ||
| borderRadius: theme.spacing(1), | ||
| border: `1px solid ${theme.palette.divider}`, | ||
| overflow: 'hidden', | ||
| textAlign: 'left', | ||
| transition: 'box-shadow 0.15s ease, transform 0.15s ease', | ||
| '&:hover': { | ||
| boxShadow: theme.shadows[4], | ||
| transform: 'translateY(-2px)', | ||
| }, | ||
| '&:hover $addOverlay': { | ||
| opacity: 1, | ||
| }, | ||
| }, | ||
| previewFrame: { | ||
| position: 'relative', | ||
| height: 160, | ||
| overflow: 'hidden', | ||
| backgroundColor: theme.palette.background.default, | ||
| pointerEvents: 'none', | ||
| display: 'flex', | ||
| alignItems: 'flex-start', | ||
| justifyContent: 'center', | ||
| }, | ||
| previewScale: { | ||
| position: 'absolute', | ||
| top: 0, | ||
| left: '50%', | ||
| width: '166%', | ||
| height: '166%', | ||
| transform: 'translate(-50%, 0) scale(0.6)', | ||
| transformOrigin: 'top center', | ||
| }, | ||
| clickBlocker: { | ||
| position: 'absolute', | ||
| inset: 0, | ||
| }, | ||
| addOverlay: { | ||
| position: 'absolute', | ||
| inset: 0, | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| backgroundColor: 'rgba(0, 0, 0, 0.45)', | ||
| color: theme.palette.common.white, | ||
| opacity: 0, | ||
| transition: 'opacity 0.15s ease', | ||
| fontWeight: 600, | ||
| }, | ||
| meta: { | ||
| padding: theme.spacing(1.5), | ||
| }, | ||
| searchField: { | ||
| marginBottom: theme.spacing(2), | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| export const AddWidgetDialog = (props: AddWidgetDialogProps) => { | ||
| const { widgets, handleAdd } = props; | ||
| const { t } = useTranslationRef(homeTranslationRef); | ||
| const classes = useStyles(); | ||
| const [searchQuery, setSearchQuery] = useState(''); | ||
|
|
||
| const filteredWidgets = useMemo(() => { | ||
| const query = searchQuery.trim().toLowerCase(); | ||
| if (!query) { | ||
| return widgets; | ||
| } | ||
| return widgets.filter(widget => { | ||
| const title = getTitle(widget).toLowerCase(); | ||
| const description = (widget.description ?? '').toLowerCase(); | ||
| return title.includes(query) || description.includes(query); | ||
| }); | ||
| }, [widgets, searchQuery]); | ||
|
|
||
| return ( | ||
| <> | ||
| <DialogTitle>{t('addWidgetDialog.title')}</DialogTitle> | ||
| <DialogContent> | ||
| {widgets.length === 0 ? ( | ||
| <Box pb={3} textAlign="center"> | ||
| <Typography variant="body1" color="textSecondary"> | ||
| {t('addWidgetDialog.noAvailableWidgets')} | ||
| </Typography> | ||
| </Box> | ||
| ) : ( | ||
| <> | ||
| <TextField | ||
| className={classes.searchField} | ||
| fullWidth | ||
| variant="outlined" | ||
| size="small" | ||
| placeholder={t('addWidgetDialog.searchPlaceholder')} | ||
| value={searchQuery} | ||
| onChange={e => setSearchQuery(e.target.value)} | ||
| InputProps={{ | ||
| startAdornment: ( | ||
| <InputAdornment position="start"> | ||
| <SearchIcon fontSize="small" /> | ||
| </InputAdornment> | ||
| ), | ||
| }} | ||
| /> | ||
| {filteredWidgets.length === 0 ? ( | ||
| <Box pb={3} textAlign="center"> | ||
| <Typography variant="body1" color="textSecondary"> | ||
| {t('addWidgetDialog.noMatchingWidgets')} | ||
| </Typography> | ||
| </Box> | ||
| ) : ( | ||
| <div className={classes.grid}> | ||
| {filteredWidgets.map(widget => ( | ||
| <ButtonBase | ||
| key={widget.name} | ||
| className={classes.card} | ||
| onClick={() => handleAdd(widget)} | ||
| focusRipple | ||
| > | ||
| <div className={classes.previewFrame}> | ||
| <div | ||
| className={classes.previewScale} | ||
| aria-hidden="true" | ||
| {...{ inert: '' }} | ||
| > | ||
| {widget.component} | ||
| </div> | ||
| <div className={classes.clickBlocker} /> | ||
| <div className={classes.addOverlay}> | ||
| <Typography variant="button" style={{ color: '#fff' }}> | ||
| + {t('addWidgetDialog.title')} | ||
| </Typography> | ||
| </div> | ||
| </div> | ||
| <div className={classes.meta}> | ||
| <Typography variant="subtitle2" color="textPrimary"> | ||
| {getTitle(widget)} | ||
| </Typography> | ||
| {widget.description && ( | ||
| <Typography variant="caption" color="textSecondary"> | ||
| {widget.description} | ||
| </Typography> | ||
| )} | ||
| </div> | ||
| </ButtonBase> | ||
| ))} | ||
| </div> | ||
| )} | ||
| </> | ||
| )} | ||
| </DialogContent> | ||
| </> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.