Skip to content

Commit da94df6

Browse files
authored
Merge pull request #26933 from software-mansion-labs/ts-migration/growl-lib
2 parents 2435680 + 0451f2f commit da94df6

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

src/libs/Growl.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/libs/Growl.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react';
2+
import CONST from '../CONST';
3+
4+
type GrowlRef = {
5+
show?: (bodyText: string, type: string, duration: number) => void;
6+
};
7+
8+
const growlRef = React.createRef<GrowlRef>();
9+
let resolveIsReadyPromise: undefined | ((value?: unknown) => void);
10+
const isReadyPromise = new Promise((resolve) => {
11+
resolveIsReadyPromise = resolve;
12+
});
13+
14+
function setIsReady() {
15+
if (!resolveIsReadyPromise) return;
16+
resolveIsReadyPromise();
17+
}
18+
19+
/**
20+
* Show the growl notification
21+
*/
22+
function show(bodyText: string, type: string, duration: number = CONST.GROWL.DURATION) {
23+
isReadyPromise.then(() => {
24+
if (!growlRef?.current?.show) return;
25+
growlRef.current.show(bodyText, type, duration);
26+
});
27+
}
28+
29+
/**
30+
* Show error growl
31+
*/
32+
function error(bodyText: string, duration: number = CONST.GROWL.DURATION) {
33+
show(bodyText, CONST.GROWL.ERROR, duration);
34+
}
35+
36+
/**
37+
* Show success growl
38+
*/
39+
function success(bodyText: string, duration: number = CONST.GROWL.DURATION) {
40+
show(bodyText, CONST.GROWL.SUCCESS, duration);
41+
}
42+
43+
export default {
44+
show,
45+
error,
46+
success,
47+
};
48+
49+
export {growlRef, setIsReady};

0 commit comments

Comments
 (0)