Repository navigation
CONSOLE-5586: Add console.app/drawer extension and useAppDrawer hook to plugin SDK #17567
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import type { FC } from 'react'; | ||
| import { | ||
| DrawerActions, | ||
| DrawerCloseButton, | ||
| DrawerHead, | ||
| DrawerPanelBody, | ||
| Title, | ||
| } from '@patternfly/react-core'; | ||
| import type { AppDrawerPanelProps } from '@openshift-console/dynamic-plugin-sdk'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| export const DEMO_APP_DRAWER_ID = 'console-demo-plugin-drawer'; | ||
|
|
||
| export const DemoAppDrawerPanel: FC<AppDrawerPanelProps> = ({ onClose }) => { | ||
| const { t } = useTranslation('plugin__console-demo-plugin'); | ||
| return ( | ||
| <div data-test="demo-app-drawer-panel"> | ||
| <DrawerHead> | ||
| <Title headingLevel="h2">{t('Demo app drawer')}</Title> | ||
| <DrawerActions> | ||
| <DrawerCloseButton aria-label={t('Close demo app drawer')} onClick={onClose} /> | ||
| </DrawerActions> | ||
| </DrawerHead> | ||
| <DrawerPanelBody>{t('This side panel is contributed by the demo plugin.')}</DrawerPanelBody> | ||
| </div> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import type { FC, ReactNode } from 'react'; | ||
| import { Suspense, useCallback, useEffect, useMemo, useState } from 'react'; | ||
| import { | ||
| Drawer, | ||
| DrawerActions, | ||
| DrawerCloseButton, | ||
| DrawerContent, | ||
| DrawerContentBody, | ||
| DrawerHead, | ||
| DrawerPanelContent, | ||
| } from '@patternfly/react-core'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import type { AppDrawer } from '@console/dynamic-plugin-sdk'; | ||
| import { isAppDrawer, useResolvedExtensions } from '@console/dynamic-plugin-sdk'; | ||
| import { ErrorBoundaryInline } from '@console/shared/src/components/error/fallbacks/ErrorBoundaryInline'; | ||
| import { AppDrawerContext } from '@console/shared/src/hooks/useAppDrawer'; | ||
|
|
||
| interface AppDrawerHostProps { | ||
| children: ReactNode; | ||
| } | ||
|
|
||
| interface AppDrawerErrorFallbackProps { | ||
| children: ReactNode; | ||
| onClose: () => void; | ||
| } | ||
|
|
||
| // Keeps the drawer closable when the plugin panel, which normally renders the close button, fails | ||
| const AppDrawerErrorFallback: FC<AppDrawerErrorFallbackProps> = ({ children, onClose }) => { | ||
| const { t } = useTranslation('console-app'); | ||
| return ( | ||
| <DrawerHead> | ||
| {children} | ||
| <DrawerActions> | ||
| <DrawerCloseButton aria-label={t('Close')} onClick={onClose} /> | ||
| </DrawerActions> | ||
| </DrawerHead> | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Renders app-level side panels contributed via the `console.app/drawer` extension as a | ||
| * PatternFly Drawer wrapping the Console page. Owns the open state shared with plugins through | ||
| * `useAppDrawer`. At most one app drawer is open at a time. | ||
| */ | ||
| export const AppDrawerHost: FC<AppDrawerHostProps> = ({ children }) => { | ||
| const { t } = useTranslation('console-app'); | ||
| const [drawers] = useResolvedExtensions<AppDrawer>(isAppDrawer); | ||
| const [openDrawerIds, setOpenDrawerIds] = useState<string[]>([]); | ||
|
|
||
| const setDrawerOpen = useCallback((id: string, open: boolean) => { | ||
| setOpenDrawerIds((prev) => { | ||
| if (open) { | ||
| return prev.length === 1 && prev[0] === id ? prev : [id]; | ||
| } | ||
| return prev.includes(id) ? prev.filter((openId) => openId !== id) : prev; | ||
| }); | ||
| }, []); | ||
|
|
||
| const contextValue = useMemo( | ||
| () => ({ openDrawerIds, setDrawerOpen }), | ||
| [openDrawerIds, setDrawerOpen], | ||
| ); | ||
|
|
||
| const activeDrawer = drawers.find((drawer) => openDrawerIds.includes(drawer.properties.id)); | ||
| const activeDrawerId = activeDrawer?.properties.id; | ||
|
|
||
| useEffect(() => { | ||
| const timeout = setTimeout(() => window.dispatchEvent(new Event('sidebar_toggle')), 250); | ||
| return () => clearTimeout(timeout); | ||
| }, [activeDrawerId]); | ||
|
|
||
| const closeActiveDrawer = useCallback( | ||
| () => setDrawerOpen(activeDrawerId, false), | ||
| [activeDrawerId, setDrawerOpen], | ||
| ); | ||
|
|
||
| const errorFallbackWrapper = useCallback( | ||
| ({ children: fallback }: { children: ReactNode }) => ( | ||
| <AppDrawerErrorFallback onClose={closeActiveDrawer}>{fallback}</AppDrawerErrorFallback> | ||
| ), | ||
| [closeActiveDrawer], | ||
| ); | ||
|
|
||
| const panelContent = activeDrawer ? ( | ||
| <DrawerPanelContent | ||
| defaultSize={activeDrawer.properties.defaultSize} | ||
| isResizable={activeDrawer.properties.isResizable ?? true} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Translate the resize control's accessible label. Resizing defaults to enabled, but this panel does not supply Based on learnings, user-facing React labels must resolve translation resources.
|
||
| key={activeDrawerId} | ||
| minSize={activeDrawer.properties.minSize} | ||
| resizeAriaLabel={t('Resize')} | ||
| > | ||
| <ErrorBoundaryInline wrapper={errorFallbackWrapper}> | ||
| <Suspense fallback={null}> | ||
| <activeDrawer.properties.panel onClose={closeActiveDrawer} /> | ||
| </Suspense> | ||
| </ErrorBoundaryInline> | ||
| </DrawerPanelContent> | ||
| ) : null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for future improvement - a user could have more than one drawer open, for example the quick start drawer and the OLS drawer. make sure the API is built so that in the future, this extension could exercise PF tabs so the user can choose which drawer they want open, a la vscode
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we are already OK here since the hook works per drawer. |
||
|
|
||
| return ( | ||
| <AppDrawerContext.Provider value={contextValue}> | ||
| <Drawer isInline isExpanded={!!activeDrawer} id="co-app-drawer"> | ||
| <DrawerContent panelContent={panelContent}> | ||
| <DrawerContentBody>{children}</DrawerContentBody> | ||
| </DrawerContent> | ||
| </Drawer> | ||
| </AppDrawerContext.Provider> | ||
|
Comment on lines
+100
to
+107
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for limiting to one drawer open at a time and keeping children stable |
||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset panel sizing when the active drawer changes.
If A and B both specify
defaultSize="35%", resizing A and then opening B directly leaves B at A's resized width. PatternFly writes the resize directly to the panel element. React does not restore an unchanged style value. The key onErrorBoundaryInlinedoes not replace that element. (raw.githubusercontent.com)Add
key={activeDrawerId}toDrawerPanelContent. This resets sizing between drawers and preserves resizing during rerenders of the same drawer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍