-
Notifications
You must be signed in to change notification settings - Fork 197
fix: Fix AppLayoutToolbar breadcrumbs SSR glitch #3856
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
44fb6a1
729244b
d0ec01c
baf472e
fe11b4d
f7cef11
4098792
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,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
|
||
import { | ||
NotificationsSkeleton, | ||
ToolbarSkeleton, | ||
} from '../../../../../lib/components/app-layout/visual-refresh-toolbar/skeleton/skeleton-parts'; | ||
|
||
describe('ToolbarSkeleton', () => { | ||
test('renders with discoveredBreadcrumbs', () => { | ||
const { container } = render( | ||
<ToolbarSkeleton | ||
appLayoutInternals={ | ||
{ | ||
breadcrumbs: null, | ||
discoveredBreadcrumbs: { items: [] }, | ||
} as any | ||
} | ||
toolbarProps={{} as any} | ||
/> | ||
); | ||
expect(container).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('NotificationsSkeleton', () => { | ||
test('renders', () => { | ||
const { container } = render(<NotificationsSkeleton appLayoutInternals={{} as any}>{null}</NotificationsSkeleton>); | ||
expect(container).toBeTruthy(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ import React from 'react'; | |
import { AppLayoutNotificationsImplementationProps } from '../notifications'; | ||
import { AppLayoutToolbarImplementationProps } from '../toolbar'; | ||
import { SkeletonPartProps } from './interfaces'; | ||
import { BreadcrumbsSlot, NotificationsSlot, ToolbarSlot } from './slots'; | ||
import { NotificationsSlot } from './slots'; | ||
import { ToolbarSkeletonStructure } from './toolbar-container'; | ||
|
||
import styles from './styles.css.js'; | ||
|
||
|
@@ -17,11 +18,7 @@ export const BeforeMainSlotSkeleton = React.forwardRef<HTMLElement, SkeletonPart | |
({ toolbarProps, appLayoutProps }, ref) => { | ||
return ( | ||
<> | ||
{!!toolbarProps && ( | ||
<ToolbarSlot ref={ref}> | ||
<BreadcrumbsSlot ownBreadcrumbs={appLayoutProps.breadcrumbs} /> | ||
</ToolbarSlot> | ||
)} | ||
{!!toolbarProps && <ToolbarSkeletonStructure ref={ref} ownBreadcrumbs={appLayoutProps.breadcrumbs} />} | ||
{toolbarProps?.navigationOpen && <div className={styles.navigation} />} | ||
</> | ||
); | ||
|
@@ -34,15 +31,14 @@ export const BeforeMainSlotSkeleton = React.forwardRef<HTMLElement, SkeletonPart | |
|
||
export const ToolbarSkeleton = React.forwardRef<HTMLElement, AppLayoutToolbarImplementationProps>( | ||
({ appLayoutInternals }: AppLayoutToolbarImplementationProps, ref) => ( | ||
<ToolbarSlot ref={ref}> | ||
<BreadcrumbsSlot | ||
ownBreadcrumbs={appLayoutInternals.breadcrumbs} | ||
discoveredBreadcrumbs={appLayoutInternals.discoveredBreadcrumbs} | ||
/> | ||
</ToolbarSlot> | ||
<ToolbarSkeletonStructure | ||
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. Codecov points at this line (34) and line 43 below as needing coverage. To be fair, it looks like they were already missing it. Is the change in line 43 necessary though? |
||
ref={ref} | ||
ownBreadcrumbs={appLayoutInternals.breadcrumbs} | ||
discoveredBreadcrumbs={appLayoutInternals.discoveredBreadcrumbs} | ||
/> | ||
) | ||
); | ||
|
||
export const NotificationsSkeleton = React.forwardRef<HTMLElement, AppLayoutNotificationsImplementationProps>( | ||
(props: AppLayoutNotificationsImplementationProps, ref) => <NotificationsSlot ref={ref} /> | ||
(_props: AppLayoutNotificationsImplementationProps, ref) => <NotificationsSlot ref={ref} /> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,14 @@ interface ToolbarSlotProps { | |
} | ||
|
||
export const ToolbarSlot = React.forwardRef<HTMLElement, ToolbarSlotProps>(({ className, style, children }, ref) => ( | ||
<section ref={ref as React.Ref<any>} className={clsx(styles['toolbar-container'], className)} style={style}> | ||
<section | ||
ref={ref as React.Ref<any>} | ||
className={clsx(styles['toolbar-container'], className)} | ||
style={{ | ||
insetBlockStart: style?.insetBlockStart ?? 0, | ||
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. Is this line the actual fix? Undefined was not behaving the same way as 0? |
||
...style, | ||
}} | ||
> | ||
{children} | ||
</section> | ||
)); | ||
|
@@ -41,10 +48,13 @@ interface BreadcrumbsSlotProps { | |
} | ||
|
||
export function BreadcrumbsSlot({ ownBreadcrumbs, discoveredBreadcrumbs }: BreadcrumbsSlotProps) { | ||
const isSSR = typeof window === 'undefined'; | ||
const contextValue = React.useMemo(() => ({ isInToolbar: true }), []); | ||
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. Do we really need to use |
||
|
||
return ( | ||
<BreadcrumbsSlotContext.Provider value={{ isInToolbar: true }}> | ||
<BreadcrumbsSlotContext.Provider value={contextValue}> | ||
<div className={styles['breadcrumbs-own']}>{ownBreadcrumbs}</div> | ||
{discoveredBreadcrumbs && ( | ||
{discoveredBreadcrumbs && !isSSR && ( | ||
<div className={styles['breadcrumbs-discovered']}> | ||
<BreadcrumbGroupImplementation | ||
{...discoveredBreadcrumbs} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
|
||
import { BreadcrumbGroupProps } from '../../../breadcrumb-group/interfaces'; | ||
import { BreadcrumbsSlot, ToolbarSlot } from './slots'; | ||
|
||
import testutilStyles from '../../test-classes/styles.css.js'; | ||
import toolbarStyles from '../toolbar/styles.css.js'; | ||
|
||
interface ToolbarContainerProps { | ||
children: React.ReactNode; | ||
hasAiDrawer?: boolean; | ||
} | ||
|
||
export function ToolbarContainer({ children, hasAiDrawer }: ToolbarContainerProps) { | ||
return ( | ||
<div className={clsx(toolbarStyles['toolbar-container'], hasAiDrawer && toolbarStyles['with-ai-drawer'])}> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
interface ToolbarBreadcrumbsWrapperProps { | ||
children: React.ReactNode; | ||
includeTestUtils?: boolean; | ||
} | ||
|
||
export function ToolbarBreadcrumbsWrapper({ children, includeTestUtils = false }: ToolbarBreadcrumbsWrapperProps) { | ||
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. This component is only used once further down, so it does not need to be exported, or it could even be removed? |
||
return ( | ||
<div | ||
className={clsx(toolbarStyles['universal-toolbar-breadcrumbs'], includeTestUtils && testutilStyles.breadcrumbs)} | ||
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. Why do we only add the |
||
> | ||
{children} | ||
</div> | ||
); | ||
} | ||
|
||
export function ToolbarDrawersWrapper() { | ||
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. Also this is used only once below in the same file |
||
return <div className={toolbarStyles['universal-toolbar-drawers']} />; | ||
} | ||
|
||
interface ToolbarBreadcrumbsSectionProps { | ||
ownBreadcrumbs: React.ReactNode; | ||
discoveredBreadcrumbs?: BreadcrumbGroupProps | null; | ||
includeTestUtils?: boolean; | ||
} | ||
|
||
export function ToolbarBreadcrumbsSection({ | ||
ownBreadcrumbs, | ||
discoveredBreadcrumbs, | ||
includeTestUtils = false, | ||
}: ToolbarBreadcrumbsSectionProps) { | ||
return ( | ||
<ToolbarBreadcrumbsWrapper includeTestUtils={includeTestUtils}> | ||
<BreadcrumbsSlot ownBreadcrumbs={ownBreadcrumbs} discoveredBreadcrumbs={discoveredBreadcrumbs} /> | ||
</ToolbarBreadcrumbsWrapper> | ||
); | ||
} | ||
|
||
interface ToolbarSkeletonStructureProps { | ||
ownBreadcrumbs: React.ReactNode; | ||
discoveredBreadcrumbs?: BreadcrumbGroupProps | null; | ||
} | ||
|
||
export const ToolbarSkeletonStructure = React.forwardRef<HTMLElement, ToolbarSkeletonStructureProps>( | ||
({ ownBreadcrumbs, discoveredBreadcrumbs }, ref) => ( | ||
<ToolbarSlot ref={ref}> | ||
<ToolbarContainer> | ||
<ToolbarBreadcrumbsSection ownBreadcrumbs={ownBreadcrumbs} discoveredBreadcrumbs={discoveredBreadcrumbs} /> | ||
<ToolbarDrawersWrapper /> | ||
</ToolbarContainer> | ||
</ToolbarSlot> | ||
) | ||
); |
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.
I think here we could reuse this
isWidgetReady
function:components/src/app-layout/visual-refresh-toolbar/state/invariants.ts
Lines 5 to 7 in d11c844