Skip to content
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

fix: Runtime drawers default active #2870

Merged
merged 10 commits into from
Oct 16, 2024
31 changes: 31 additions & 0 deletions src/app-layout/__tests__/runtime-drawers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1042,5 +1042,36 @@ describe('toolbar mode only features', () => {

expect(wrapper.findActiveDrawer()!.getElement()).toHaveTextContent('runtime drawer content');
});

test('dynamically registered drawers with defaultActive: true should open even when there are already open drawer(s) on the page', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have negative case scenarios?

  1. After max drawers already open, defaultActive does not open a new one
  2. After user opened a global drawer manually, late registered defaultActive don't open

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No for both. Do we have any specific requirements for this? If so, could you point me to where I can check them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my understanding of our discussion for this bug yesterday. I do not think there is any written down text

const { wrapper, globalDrawersWrapper } = await renderComponent(<AppLayout drawers={[testDrawer]} />);

wrapper.findDrawerTriggerById('security')!.click();
expect(wrapper.findActiveDrawer()!.getElement()).toHaveTextContent('Security');

awsuiPlugins.appLayout.registerDrawer({
...drawerDefaults,
id: 'global1',
type: 'global',
defaultActive: true,
});

await delay();

expect(globalDrawersWrapper.findDrawerById('global1')!.isActive()).toBe(true);

awsuiPlugins.appLayout.registerDrawer({
...drawerDefaults,
id: 'global2',
type: 'global',
defaultActive: true,
});

await delay();

expect(wrapper.findActiveDrawer()!.getElement()).toHaveTextContent('Security');
expect(globalDrawersWrapper.findDrawerById('global1')!.isActive()).toBe(true);
expect(globalDrawersWrapper.findDrawerById('global2')!.isActive()).toBe(true);
});
});
});
20 changes: 12 additions & 8 deletions src/app-layout/utils/use-drawers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ function useRuntimeDrawers(
const onLocalDrawerChangeStable = useStableCallback(onActiveDrawerChange);
const onGlobalDrawersChangeStable = useStableCallback(onActiveGlobalDrawersChange);

const drawersWereOpenRef = useRef(false);
drawersWereOpenRef.current = drawersWereOpenRef.current || !!activeDrawerId || !!activeGlobalDrawersIds.length;
const localDrawerWasOpenRef = useRef(false);
localDrawerWasOpenRef.current = localDrawerWasOpenRef.current || !!activeDrawerId;
const activeGlobalDrawersIdsRef = useRef<Array<string>>([]);
activeGlobalDrawersIdsRef.current = activeGlobalDrawersIds;

useEffect(() => {
if (disableRuntimeDrawers) {
Expand All @@ -74,17 +76,19 @@ function useRuntimeDrawers(
const localDrawers = drawers.filter(drawer => drawer.type !== 'global');
const globalDrawers = drawers.filter(drawer => drawer.type === 'global');
setRuntimeDrawers(convertRuntimeDrawers(localDrawers, globalDrawers));
if (!drawersWereOpenRef.current) {
if (!localDrawerWasOpenRef.current) {
const defaultActiveLocalDrawer = sortByPriority(localDrawers).find(drawer => drawer.defaultActive);
if (defaultActiveLocalDrawer) {
onLocalDrawerChangeStable(defaultActiveLocalDrawer.id);
}

const defaultActiveGlobalDrawers = sortByPriority(globalDrawers).filter(drawer => drawer.defaultActive);
defaultActiveGlobalDrawers.forEach(drawer => {
onGlobalDrawersChangeStable(drawer.id);
});
}

const defaultActiveGlobalDrawers = sortByPriority(globalDrawers).filter(
drawer => !activeGlobalDrawersIdsRef.current.includes(drawer.id) && drawer.defaultActive
);
defaultActiveGlobalDrawers.forEach(drawer => {
onGlobalDrawersChangeStable(drawer.id);
});
});
return () => {
unsubscribe();
Expand Down
Loading