-
Notifications
You must be signed in to change notification settings - Fork 92
/
notifier.tsx
97 lines (85 loc) · 2.44 KB
/
notifier.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { TCromwellNotify } from '@cromwell/core';
import React from 'react';
import { toast as toastify, ToastOptions } from 'react-toastify';
export type NotifierActionOptions = ToastOptions & {
Wrapper?: React.ComponentType<{
className?: string;
severity?: any;
options?: NotifierActionOptions;
children?: React.ReactNode;
}>;
};
/** @internal */
const DefaultWrapper = (props) => {
return <div className={props.className}>{props.children}</div>;
};
/** @internal */
export class Notifier implements TCromwellNotify {
constructor(private defaultOptions?: NotifierActionOptions) {}
public success(text: string, options?: NotifierActionOptions) {
options = Object.assign({}, this.defaultOptions, options);
const Wrapper = options?.Wrapper ?? DefaultWrapper;
toastify.success(
<Wrapper severity="success" options={options}>
{text}
</Wrapper>,
{
...(options ?? {}),
},
);
}
public warning(text: string, options?: NotifierActionOptions) {
options = Object.assign({}, this.defaultOptions, options);
const Wrapper = options?.Wrapper ?? DefaultWrapper;
toastify.warn(
<Wrapper severity="warning" options={options}>
{text}
</Wrapper>,
{
...(options ?? {}),
},
);
}
public error(text: string, options?: NotifierActionOptions) {
options = Object.assign({}, this.defaultOptions, options);
const Wrapper = options?.Wrapper ?? DefaultWrapper;
toastify.error(
<Wrapper severity="error" options={options}>
{text}
</Wrapper>,
{
...(options ?? {}),
},
);
}
public info(text: string, options?: NotifierActionOptions) {
options = Object.assign({}, this.defaultOptions, options);
const Wrapper = options?.Wrapper ?? DefaultWrapper;
toastify.info(
<Wrapper severity="info" options={options}>
{text}
</Wrapper>,
{
...(options ?? {}),
},
);
}
public POSITION = toastify.POSITION;
public warn = this.warning;
}
/** @internal */
export const defaultNotifier = new Notifier();
/** @internal */
const notifierState: {
defaultNotifier: TCromwellNotify;
customNotifier?: TCromwellNotify;
} = {
defaultNotifier,
};
/** @internal */
export function getNotifier() {
return notifierState.customNotifier ?? notifierState.defaultNotifier;
}
export function setNotifier(notifier: TCromwellNotify) {
notifierState.customNotifier = notifier;
}