-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
create_notifications.tsx
55 lines (48 loc) · 1.72 KB
/
create_notifications.tsx
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import * as React from 'react';
import { KibanaServices } from '../context/types';
import { KibanaReactNotifications } from './types';
import { toMountPoint } from '../util';
export const createNotifications = (services: KibanaServices): KibanaReactNotifications => {
const show: KibanaReactNotifications['toasts']['show'] = ({
title,
body,
color,
iconType,
toastLifeTimeMs,
onClose,
}) => {
if (!services.notifications) {
throw new TypeError('Could not show notification as notifications service is not available.');
}
services.notifications!.toasts.add({
title: toMountPoint(title, { theme$: services.theme?.theme$ }),
text: toMountPoint(<>{body || null}</>, { theme$: services.theme?.theme$ }),
color,
iconType,
toastLifeTimeMs,
onClose,
});
};
const success: KibanaReactNotifications['toasts']['success'] = (input) =>
show({ color: 'success', iconType: 'check', ...input });
const warning: KibanaReactNotifications['toasts']['warning'] = (input) =>
show({ color: 'warning', iconType: 'help', ...input });
const danger: KibanaReactNotifications['toasts']['danger'] = (input) =>
show({ color: 'danger', iconType: 'alert', ...input });
const notifications: KibanaReactNotifications = {
toasts: {
show,
success,
warning,
danger,
},
};
return notifications;
};