Skip to content

Commit

Permalink
chore: Move onDrawerOpened listener to use-drawers handler and introd…
Browse files Browse the repository at this point in the history
…uce an appropriate test suite
  • Loading branch information
georgylobko committed Sep 10, 2024
1 parent 520af02 commit fa84a6a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/app-layout/__tests__/runtime-drawers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,20 @@ describeEachAppLayout(({ theme, size }) => {
wrapper.findToolsClose().click();
expect(onToolsChange).toHaveBeenCalledWith({ open: false });
});

test('opens a drawer when openDrawer is called', async () => {
awsuiPlugins.appLayout.registerDrawer(drawerDefaults);

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

expect(wrapper.findActiveDrawer()).toBeFalsy();

awsuiPlugins.appLayout.openDrawer('test');

await delay();

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

describe('toolbar mode only features', () => {
Expand Down
37 changes: 35 additions & 2 deletions src/app-layout/utils/use-drawers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function useRuntimeDrawers(
activeDrawerId: string | null,
onActiveDrawerChange: (newDrawerId: string | null) => void,
activeGlobalDrawersIds: Array<string>,
setActiveGlobalDrawersIds: (newDrawerId: string) => void
setActiveGlobalDrawersIds: (newDrawerId: string) => void,
drawers: AppLayoutProps.Drawer[]
) {
const [runtimeLocalDrawers, setRuntimeLocalDrawers] = useState<DrawersLayout>({ before: [], after: [] });
const [runtimeGlobalDrawers, setRuntimeGlobalDrawers] = useState<DrawersLayout>({ before: [], after: [] });
Expand Down Expand Up @@ -96,6 +97,37 @@ function useRuntimeDrawers(
};
}, [activeGlobalDrawersIds, disableRuntimeDrawers, onGlobalDrawersChangeStable, onLocalDrawerChangeStable]);

useEffect(() => {
const unsubscribe = awsuiPluginsInternal.appLayout.onDrawerOpened(drawerId => {
const localDrawer = [...runtimeLocalDrawers.before, ...drawers, ...runtimeLocalDrawers.after]?.find(
drawer => drawer.id === drawerId
);
const globalDrawer = [...runtimeGlobalDrawers.before, ...runtimeGlobalDrawers.after]?.find(
drawer => drawer.id === drawerId
);
if (localDrawer && activeDrawerId !== drawerId) {
onActiveDrawerChange(drawerId);
}
if (globalDrawer && !activeGlobalDrawersIds.includes(drawerId)) {
setActiveGlobalDrawersIds(drawerId);
}
});

return () => {
unsubscribe();
};
}, [
activeDrawerId,
activeGlobalDrawersIds,
drawers,
onActiveDrawerChange,
runtimeGlobalDrawers.after,
runtimeGlobalDrawers.before,
runtimeLocalDrawers.after,
runtimeLocalDrawers.before,
setActiveGlobalDrawersIds,
]);

return {
local: runtimeLocalDrawers,
global: runtimeGlobalDrawers,
Expand Down Expand Up @@ -187,7 +219,8 @@ export function useDrawers(
activeDrawerId,
onActiveDrawerChange,
activeGlobalDrawersIds,
onActiveGlobalDrawersChange
onActiveGlobalDrawersChange,
drawers ?? []
);
const combinedLocalDrawers = drawers
? [...runtimeLocalDrawers.before, ...drawers, ...runtimeLocalDrawers.after]
Expand Down

0 comments on commit fa84a6a

Please sign in to comment.