Skip to content

Commit f2254f4

Browse files
georgylobkoamanabiy
authored andcommitted
chore: Clear feature notifications (#4343)
1 parent 8ba671e commit f2254f4

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

pages/feature-notifications/feature-prompt.page.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import React, { useContext, useEffect, useRef } from 'react';
3+
import React, { useContext, useEffect, useRef, useState } from 'react';
44

55
import {
66
AppLayoutProps,
@@ -191,6 +191,7 @@ type DemoContext = React.Context<
191191
>;
192192

193193
export default function () {
194+
const [mountApp, setMountApp] = useState(true);
194195
const featurePromptRef = useRef<FeaturePromptProps.Ref>(null);
195196
const triggerRef = useRef<HTMLButtonElement>(null);
196197
const { urlParams, setUrlParams } = useContext(AppContext as DemoContext);
@@ -214,6 +215,10 @@ export default function () {
214215
};
215216
}, []);
216217

218+
if (!mountApp) {
219+
return null;
220+
}
221+
217222
return (
218223
<ScreenshotArea gutters={false}>
219224
<I18nProvider messages={[messages]} locale="en">
@@ -360,6 +365,16 @@ export default function () {
360365
>
361366
Override
362367
</Button>
368+
<Button
369+
onClick={() => {
370+
setMountApp(false);
371+
setTimeout(() => {
372+
setMountApp(true);
373+
}, 100);
374+
}}
375+
>
376+
Remount application
377+
</Button>
363378
</SpaceBetween>
364379
</Container>
365380
</Box>

src/app-layout/__tests__/runtime-feature-notifications.test.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,17 @@ describeEachAppLayout({ themes: ['refresh-toolbar'] }, ({ size }) => {
225225

226226
test('clears feature notifications correctly', () => {
227227
featureNotifications.registerFeatureNotifications({ ...featureNotificationsDefaults, mountItem: undefined });
228-
const { wrapper } = renderComponent(<AppLayout />);
228+
const { wrapper, unmount } = renderComponent(<AppLayout />);
229229
expect(wrapper.findDrawerTriggerById(featureNotificationsDefaults.id)).toBeTruthy();
230230

231231
featureNotifications.clearFeatureNotifications();
232232
expect(wrapper.findDrawerTriggerById(featureNotificationsDefaults.id)).toBeFalsy();
233+
234+
unmount();
235+
236+
const { wrapper: wrapperAfterRemount } = renderComponent(<AppLayout />);
237+
238+
expect(wrapperAfterRemount.findDrawerTriggerById(featureNotificationsDefaults.id)).toBeFalsy();
233239
});
234240

235241
test('supports custom filterFeatures function', () => {

src/internal/plugins/widget/core.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export function pushInitialMessage<T>(message: InitialMessage<T>) {
4141
win[storageKeyInitialMessages].push(message as InitialMessage<unknown>);
4242
}
4343

44+
export function setInitialMessage(message: Array<InitialMessage<unknown>>) {
45+
const win = getWindow();
46+
win[storageKeyInitialMessages] = message;
47+
}
48+
4449
export function registerAppLayoutHandler(handler: MessageHandler) {
4550
const win = getWindow();
4651
if (win[storageKeyMessageHandler]) {

src/internal/plugins/widget/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { getExternalProps } from '../../utils/external-props';
5-
import { getAppLayoutInitialMessages, getAppLayoutMessageHandler, pushInitialMessage } from './core';
5+
import { getAppLayoutInitialMessages, getAppLayoutMessageHandler, pushInitialMessage, setInitialMessage } from './core';
66
import {
77
AppLayoutUpdateMessage,
88
DrawerPayload,
@@ -62,12 +62,17 @@ export function clearFeatureNotifications() {
6262
* @param message
6363
*/
6464
export function updateDrawer<T = unknown>(message: AppLayoutUpdateMessage<T>) {
65+
const initialMessages = getAppLayoutInitialMessages();
6566
if (message.type === 'updateDrawerConfig') {
66-
getAppLayoutInitialMessages().forEach(initialMessage => {
67+
initialMessages.forEach(initialMessage => {
6768
if (initialMessage.payload.id === message.payload.id) {
6869
initialMessage.payload = { ...initialMessage.payload, ...message.payload };
6970
}
7071
});
7172
}
73+
74+
if (message.type === 'clearFeatureNotifications') {
75+
setInitialMessage(initialMessages.filter(initialMessage => initialMessage.type !== 'registerFeatureNotifications'));
76+
}
7277
getAppLayoutMessageHandler()?.(message as WidgetMessage<unknown>);
7378
}

0 commit comments

Comments
 (0)