-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathindex.ts
More file actions
78 lines (70 loc) · 2.67 KB
/
index.ts
File metadata and controls
78 lines (70 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { getExternalProps } from '../../utils/external-props';
import { getAppLayoutInitialMessages, getAppLayoutMessageHandler, pushInitialMessage, setInitialMessage } from './core';
import {
AppLayoutUpdateMessage,
DrawerPayload,
FeatureNotificationsPayload,
FeatureNotificationsPayloadPublic,
RegisterDrawerMessage,
RegisterFeatureNotificationsMessage,
WidgetMessage,
} from './interfaces';
/**
* Registers a new left runtime drawer to app layout
* @param drawer
*/
export function registerLeftDrawer(drawer: DrawerPayload) {
const message: RegisterDrawerMessage = { type: 'registerLeftDrawer', payload: drawer };
pushInitialMessage(message);
getAppLayoutMessageHandler()?.(message as WidgetMessage<unknown>);
}
/**
* Registers a new bottom runtime drawer to app layout
* @param drawer
*/
export function registerBottomDrawer(drawer: DrawerPayload) {
const message: RegisterDrawerMessage = { type: 'registerBottomDrawer', payload: { ...drawer, position: 'bottom' } };
pushInitialMessage(message);
getAppLayoutMessageHandler()?.(message as WidgetMessage<unknown>);
}
/**
* Registers a new feature notifications runtime drawer to app layout
* @param payload
*/
export function registerFeatureNotifications<T>(payload: FeatureNotificationsPayload<T>) {
const message: RegisterFeatureNotificationsMessage<T> = {
type: 'registerFeatureNotifications',
payload,
};
pushInitialMessage(message);
getAppLayoutMessageHandler()?.(message as WidgetMessage<unknown>);
}
export function registerFeatureNotificationsPublic<T>(payload: FeatureNotificationsPayloadPublic<T>) {
registerFeatureNotifications(getExternalProps(payload));
}
export function showFeaturePromptIfPossible() {
updateDrawer({ type: 'showFeaturePromptIfPossible' });
}
export function clearFeatureNotifications() {
updateDrawer({ type: 'clearFeatureNotifications' });
}
/**
* Interact with already registered app layout drawers
* @param message
*/
export function updateDrawer<T = unknown>(message: AppLayoutUpdateMessage<T>) {
const initialMessages = getAppLayoutInitialMessages();
if (message.type === 'updateDrawerConfig') {
initialMessages.forEach(initialMessage => {
if (initialMessage.payload.id === message.payload.id) {
initialMessage.payload = { ...initialMessage.payload, ...message.payload };
}
});
}
if (message.type === 'clearFeatureNotifications') {
setInitialMessage(initialMessages.filter(initialMessage => initialMessage.type !== 'registerFeatureNotifications'));
}
getAppLayoutMessageHandler()?.(message as WidgetMessage<unknown>);
}