@applicaster/quick-brick-core 16.0.0-rc.8 → 16.0.0-rc.81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/App/ActionsProvider/ActionsProvider.tsx +31 -9
- package/App/ActionsProvider/LegacyActionsRegistryAdapter.tsx +107 -0
- package/App/DeepLinking/URLSchemeHandler/SchemeHandlerHooks/__tests__/useOpenSchemeHandler.test.tsx +25 -15
- package/App/ModalProvider/ModalBottomSheet/DraggableBottomSheet/__tests__/index.test.tsx +88 -0
- package/App/ModalProvider/ModalBottomSheet/DraggableBottomSheet/index.tsx +30 -56
- package/App/ModalProvider/ModalBottomSheet/ModalBottomSheetFrame.tsx +9 -2
- package/App/ModalProvider/ModalBottomSheet/hooks/__tests__/useKeyboardHeight.android.test.ts +105 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/__tests__/useKeyboardHeight.test.ts +102 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/index.ts +1 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/useKeyboardHeight.ts +30 -0
- package/App/ModalProvider/ModalBottomSheet/index.tsx +5 -2
- package/App/NavigationProvider/NavigationProvider.tsx +51 -1
- package/App/NavigationProvider/__tests__/utils.test.ts +31 -1
- package/App/NavigationProvider/utils.ts +14 -0
- package/App/NotificationToastRenderer/NotificationToastManager.ts +214 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tsx +134 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tv.tsx +10 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.web.tsx +61 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastManager.test.ts +237 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.test.tsx +233 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.web.test.tsx +90 -0
- package/App/NotificationToastRenderer/__tests__/resolveConfirmationToastStyle.test.ts +118 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationHeight.test.tsx +58 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationToastState.test.ts +112 -0
- package/App/NotificationToastRenderer/index.tsx +9 -0
- package/App/NotificationToastRenderer/resolveConfirmationToastStyle.ts +33 -0
- package/App/NotificationToastRenderer/useNotificationHeight.tsx +14 -0
- package/App/NotificationToastRenderer/useNotificationToastState.tsx +12 -0
- package/App/index.tsx +8 -5
- package/package.json +8 -8
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react-native";
|
|
2
|
+
import { Keyboard } from "react-native";
|
|
3
|
+
|
|
4
|
+
jest.mock("@applicaster/zapp-react-native-utils/reactUtils", () => ({
|
|
5
|
+
isApplePlatform: () => false,
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
// Import after mocks so module-level isApple resolves to false.
|
|
9
|
+
// eslint-disable-next-line import/first
|
|
10
|
+
import { useKeyboardHeight } from "../useKeyboardHeight";
|
|
11
|
+
|
|
12
|
+
type KeyboardListener = (event?: {
|
|
13
|
+
endCoordinates: { height: number };
|
|
14
|
+
}) => void;
|
|
15
|
+
|
|
16
|
+
function setupKeyboardListenerMock() {
|
|
17
|
+
const listeners: Record<string, KeyboardListener> = {};
|
|
18
|
+
const removeMocks: Record<string, jest.Mock> = {};
|
|
19
|
+
|
|
20
|
+
const addListenerSpy = jest
|
|
21
|
+
.spyOn(Keyboard, "addListener")
|
|
22
|
+
.mockImplementation((event: string, callback: KeyboardListener) => {
|
|
23
|
+
listeners[event] = callback;
|
|
24
|
+
|
|
25
|
+
const remove = jest.fn();
|
|
26
|
+
removeMocks[event] = remove;
|
|
27
|
+
|
|
28
|
+
return { remove };
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return { listeners, removeMocks, addListenerSpy };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe("useKeyboardHeight (non-Apple platforms)", () => {
|
|
35
|
+
let listeners: Record<string, KeyboardListener>;
|
|
36
|
+
let removeMocks: Record<string, jest.Mock>;
|
|
37
|
+
let addListenerSpy: jest.SpyInstance;
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
const mock = setupKeyboardListenerMock();
|
|
41
|
+
listeners = mock.listeners;
|
|
42
|
+
removeMocks = mock.removeMocks;
|
|
43
|
+
addListenerSpy = mock.addListenerSpy;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
addListenerSpy.mockRestore();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("returns 0 initially and subscribes to didShow / didHide events", () => {
|
|
51
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
52
|
+
|
|
53
|
+
expect(result.current).toBe(0);
|
|
54
|
+
expect(addListenerSpy).toHaveBeenCalledTimes(2);
|
|
55
|
+
|
|
56
|
+
expect(addListenerSpy).toHaveBeenCalledWith(
|
|
57
|
+
"keyboardDidShow",
|
|
58
|
+
expect.any(Function)
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
expect(addListenerSpy).toHaveBeenCalledWith(
|
|
62
|
+
"keyboardDidHide",
|
|
63
|
+
expect.any(Function)
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("updates height when keyboardDidShow fires", () => {
|
|
68
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
69
|
+
|
|
70
|
+
act(() => {
|
|
71
|
+
listeners.keyboardDidShow?.({
|
|
72
|
+
endCoordinates: { height: 412 },
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(result.current).toBe(412);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("resets height to 0 when keyboardDidHide fires", () => {
|
|
80
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
81
|
+
|
|
82
|
+
act(() => {
|
|
83
|
+
listeners.keyboardDidShow?.({
|
|
84
|
+
endCoordinates: { height: 412 },
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(result.current).toBe(412);
|
|
89
|
+
|
|
90
|
+
act(() => {
|
|
91
|
+
listeners.keyboardDidHide?.();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expect(result.current).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("removes listeners on unmount", () => {
|
|
98
|
+
const { unmount } = renderHook(() => useKeyboardHeight());
|
|
99
|
+
|
|
100
|
+
unmount();
|
|
101
|
+
|
|
102
|
+
expect(removeMocks.keyboardDidShow).toHaveBeenCalledTimes(1);
|
|
103
|
+
expect(removeMocks.keyboardDidHide).toHaveBeenCalledTimes(1);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react-native";
|
|
2
|
+
import { Keyboard } from "react-native";
|
|
3
|
+
import { useKeyboardHeight } from "../useKeyboardHeight";
|
|
4
|
+
|
|
5
|
+
jest.mock("@applicaster/zapp-react-native-utils/reactUtils", () => ({
|
|
6
|
+
isApplePlatform: () => true,
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
type KeyboardListener = (event?: {
|
|
10
|
+
endCoordinates: { height: number };
|
|
11
|
+
}) => void;
|
|
12
|
+
|
|
13
|
+
function setupKeyboardListenerMock() {
|
|
14
|
+
const listeners: Record<string, KeyboardListener> = {};
|
|
15
|
+
const removeMocks: Record<string, jest.Mock> = {};
|
|
16
|
+
|
|
17
|
+
const addListenerSpy = jest
|
|
18
|
+
.spyOn(Keyboard, "addListener")
|
|
19
|
+
.mockImplementation((event: string, callback: KeyboardListener) => {
|
|
20
|
+
listeners[event] = callback;
|
|
21
|
+
|
|
22
|
+
const remove = jest.fn();
|
|
23
|
+
removeMocks[event] = remove;
|
|
24
|
+
|
|
25
|
+
return { remove };
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return { listeners, removeMocks, addListenerSpy };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe("useKeyboardHeight (Apple platforms)", () => {
|
|
32
|
+
let listeners: Record<string, KeyboardListener>;
|
|
33
|
+
let removeMocks: Record<string, jest.Mock>;
|
|
34
|
+
let addListenerSpy: jest.SpyInstance;
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
const mock = setupKeyboardListenerMock();
|
|
38
|
+
listeners = mock.listeners;
|
|
39
|
+
removeMocks = mock.removeMocks;
|
|
40
|
+
addListenerSpy = mock.addListenerSpy;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
addListenerSpy.mockRestore();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("returns 0 initially and subscribes to willShow / willHide events", () => {
|
|
48
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
49
|
+
|
|
50
|
+
expect(result.current).toBe(0);
|
|
51
|
+
expect(addListenerSpy).toHaveBeenCalledTimes(2);
|
|
52
|
+
|
|
53
|
+
expect(addListenerSpy).toHaveBeenCalledWith(
|
|
54
|
+
"keyboardWillShow",
|
|
55
|
+
expect.any(Function)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
expect(addListenerSpy).toHaveBeenCalledWith(
|
|
59
|
+
"keyboardWillHide",
|
|
60
|
+
expect.any(Function)
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("updates height when keyboardWillShow fires", () => {
|
|
65
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
66
|
+
|
|
67
|
+
act(() => {
|
|
68
|
+
listeners.keyboardWillShow?.({
|
|
69
|
+
endCoordinates: { height: 336 },
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
expect(result.current).toBe(336);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("resets height to 0 when keyboardWillHide fires", () => {
|
|
77
|
+
const { result } = renderHook(() => useKeyboardHeight());
|
|
78
|
+
|
|
79
|
+
act(() => {
|
|
80
|
+
listeners.keyboardWillShow?.({
|
|
81
|
+
endCoordinates: { height: 280 },
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
expect(result.current).toBe(280);
|
|
86
|
+
|
|
87
|
+
act(() => {
|
|
88
|
+
listeners.keyboardWillHide?.();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
expect(result.current).toBe(0);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("removes listeners on unmount", () => {
|
|
95
|
+
const { unmount } = renderHook(() => useKeyboardHeight());
|
|
96
|
+
|
|
97
|
+
unmount();
|
|
98
|
+
|
|
99
|
+
expect(removeMocks.keyboardWillShow).toHaveBeenCalledTimes(1);
|
|
100
|
+
expect(removeMocks.keyboardWillHide).toHaveBeenCalledTimes(1);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useKeyboardHeight } from "./useKeyboardHeight";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Keyboard, KeyboardEvent } from "react-native";
|
|
3
|
+
import { isApplePlatform } from "@applicaster/zapp-react-native-utils/reactUtils";
|
|
4
|
+
|
|
5
|
+
const isApple = isApplePlatform();
|
|
6
|
+
|
|
7
|
+
export function useKeyboardHeight() {
|
|
8
|
+
const [keyboardHeight, setKeyboardHeight] = React.useState(0);
|
|
9
|
+
|
|
10
|
+
React.useEffect(() => {
|
|
11
|
+
// iOS supports smooth 'willShow' / 'willHide' events
|
|
12
|
+
const showEvent = isApple ? "keyboardWillShow" : "keyboardDidShow";
|
|
13
|
+
const hideEvent = isApple ? "keyboardWillHide" : "keyboardDidHide";
|
|
14
|
+
|
|
15
|
+
const showListener = Keyboard.addListener(showEvent, (e: KeyboardEvent) => {
|
|
16
|
+
setKeyboardHeight(e.endCoordinates.height);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const hideListener = Keyboard.addListener(hideEvent, () => {
|
|
20
|
+
setKeyboardHeight(0);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return () => {
|
|
24
|
+
showListener.remove();
|
|
25
|
+
hideListener.remove();
|
|
26
|
+
};
|
|
27
|
+
}, []);
|
|
28
|
+
|
|
29
|
+
return keyboardHeight;
|
|
30
|
+
}
|
|
@@ -47,9 +47,11 @@ export function ModalBottomSheet(props: Props) {
|
|
|
47
47
|
|
|
48
48
|
const {
|
|
49
49
|
ModalBottomSheetContent = ModalComponent,
|
|
50
|
-
modalBottomSheetContentProps,
|
|
50
|
+
modalBottomSheetContentProps = {},
|
|
51
51
|
} = props;
|
|
52
52
|
|
|
53
|
+
const { key, ...restContentProps } = modalBottomSheetContentProps;
|
|
54
|
+
|
|
53
55
|
useEffect(() => {
|
|
54
56
|
Keyboard.dismiss();
|
|
55
57
|
}, []);
|
|
@@ -82,11 +84,12 @@ export function ModalBottomSheet(props: Props) {
|
|
|
82
84
|
}}
|
|
83
85
|
>
|
|
84
86
|
<ModalBottomSheetContent
|
|
87
|
+
key={key as any}
|
|
85
88
|
width={
|
|
86
89
|
Math.min(sheetTabletMaxWidth, dimensions.width) || dimensions.width
|
|
87
90
|
}
|
|
88
91
|
maxHeight={sheetMaxHeight}
|
|
89
|
-
{...
|
|
92
|
+
{...restContentProps}
|
|
90
93
|
dismiss={dismiss}
|
|
91
94
|
currentRoute={navigator.currentRoute}
|
|
92
95
|
/>
|
|
@@ -55,6 +55,7 @@ import {
|
|
|
55
55
|
getTargetScreen,
|
|
56
56
|
legacyScreenData,
|
|
57
57
|
openExternalUrl,
|
|
58
|
+
stripHookPrefix,
|
|
58
59
|
} from "./utils";
|
|
59
60
|
import {
|
|
60
61
|
debounce,
|
|
@@ -414,6 +415,19 @@ export function NavigationProvider({ children }: Props) {
|
|
|
414
415
|
},
|
|
415
416
|
dispose
|
|
416
417
|
) => {
|
|
418
|
+
logger.debug({
|
|
419
|
+
message: `Hook screen presented: ${
|
|
420
|
+
hookPlugin.isModalHook() ? `(modal) ${route}` : route
|
|
421
|
+
}`,
|
|
422
|
+
data: {
|
|
423
|
+
route,
|
|
424
|
+
hookIdentifier: hookPlugin?.identifier,
|
|
425
|
+
hookScreenId: hookPlugin?.screen_id,
|
|
426
|
+
isModal: hookPlugin.isModalHook(),
|
|
427
|
+
lastHook: hookPlugin?.lastHook,
|
|
428
|
+
},
|
|
429
|
+
});
|
|
430
|
+
|
|
417
431
|
if (hookPlugin?.lastHook) {
|
|
418
432
|
dispose();
|
|
419
433
|
}
|
|
@@ -536,6 +550,17 @@ export function NavigationProvider({ children }: Props) {
|
|
|
536
550
|
currentStartUpHooks.current && setStartUpHooks(false);
|
|
537
551
|
}
|
|
538
552
|
|
|
553
|
+
logger.debug({
|
|
554
|
+
message: `Hooks complete, navigating to: ${targetRoute}`,
|
|
555
|
+
data: {
|
|
556
|
+
targetRoute,
|
|
557
|
+
hookIdentifier: hookPlugin?.identifier,
|
|
558
|
+
screenId: screen?.id,
|
|
559
|
+
screenName: screen?.name,
|
|
560
|
+
options,
|
|
561
|
+
},
|
|
562
|
+
});
|
|
563
|
+
|
|
539
564
|
dispatch(
|
|
540
565
|
navigationAction(targetRoute, { screen, entry: payload, options })
|
|
541
566
|
);
|
|
@@ -574,12 +599,28 @@ export function NavigationProvider({ children }: Props) {
|
|
|
574
599
|
) => {
|
|
575
600
|
const { entry, screen, targetRoute, externalUrl } = getNavigationTarget(
|
|
576
601
|
item,
|
|
577
|
-
action === ACTIONS.REPLACE ? "" : pathnameRef.current,
|
|
602
|
+
action === ACTIONS.REPLACE ? "" : stripHookPrefix(pathnameRef.current),
|
|
578
603
|
contentTypes,
|
|
579
604
|
layoutVersion,
|
|
580
605
|
rivers
|
|
581
606
|
);
|
|
582
607
|
|
|
608
|
+
logger.debug({
|
|
609
|
+
message: `Navigating: ${pathnameRef.current || "(none)"} -> ${
|
|
610
|
+
targetRoute || externalUrl || "(unresolved)"
|
|
611
|
+
}`,
|
|
612
|
+
data: {
|
|
613
|
+
action,
|
|
614
|
+
from: pathnameRef.current,
|
|
615
|
+
to: targetRoute,
|
|
616
|
+
externalUrl,
|
|
617
|
+
screenId: screen?.id,
|
|
618
|
+
screenName: screen?.name,
|
|
619
|
+
entryType: entry?.type?.value,
|
|
620
|
+
options,
|
|
621
|
+
},
|
|
622
|
+
});
|
|
623
|
+
|
|
583
624
|
if (externalUrl) {
|
|
584
625
|
const openURL = async (url) => {
|
|
585
626
|
const inflatedURL = await inflateUrl(url);
|
|
@@ -668,6 +709,15 @@ export function NavigationProvider({ children }: Props) {
|
|
|
668
709
|
|
|
669
710
|
hooksManager.handleHooks(legacyScreenData({ entry, screen }));
|
|
670
711
|
} else {
|
|
712
|
+
logger.debug({
|
|
713
|
+
message: `Navigating offline (hooks skipped) to: ${targetRoute}`,
|
|
714
|
+
data: {
|
|
715
|
+
targetRoute,
|
|
716
|
+
screenId: screen?.id,
|
|
717
|
+
screenName: screen?.name,
|
|
718
|
+
},
|
|
719
|
+
});
|
|
720
|
+
|
|
671
721
|
dispatch(navigationAction(targetRoute, { screen, entry, options }));
|
|
672
722
|
}
|
|
673
723
|
},
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { targetShouldOpenExternally } = require("../utils");
|
|
1
|
+
const { targetShouldOpenExternally, stripHookPrefix } = require("../utils");
|
|
2
2
|
|
|
3
3
|
const mockedOpenUrl = jest.fn();
|
|
4
4
|
|
|
@@ -116,3 +116,33 @@ describe("targetShouldOpenExternally", () => {
|
|
|
116
116
|
});
|
|
117
117
|
});
|
|
118
118
|
});
|
|
119
|
+
|
|
120
|
+
describe("stripHookPrefix", () => {
|
|
121
|
+
const HOOK = "203d96a9-f4af-49d1-9890-435e683b9274";
|
|
122
|
+
const MANAGE = "9f8e1e06-abd6-48b9-b73a-4caf4601f870";
|
|
123
|
+
|
|
124
|
+
it("drops the hook segment so a hook screen navigates from the root", () => {
|
|
125
|
+
expect(stripHookPrefix(`/hooks/${HOOK}`)).toBe("");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("keeps the screens opened from a hook, without their hook prefix", () => {
|
|
129
|
+
expect(stripHookPrefix(`/hooks/${HOOK}/river/${MANAGE}`)).toBe(
|
|
130
|
+
`/river/${MANAGE}`
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("leaves a regular route untouched", () => {
|
|
135
|
+
expect(stripHookPrefix(`/river/${MANAGE}`)).toBe(`/river/${MANAGE}`);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("only strips a hook segment at the start of the path", () => {
|
|
139
|
+
expect(stripHookPrefix(`/river/${MANAGE}/hooks/${HOOK}`)).toBe(
|
|
140
|
+
`/river/${MANAGE}/hooks/${HOOK}`
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("returns an empty prefix for an empty or missing path", () => {
|
|
145
|
+
expect(stripHookPrefix("")).toBe("");
|
|
146
|
+
expect(stripHookPrefix(undefined)).toBe("");
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -18,6 +18,20 @@ const WEBVIEW_SCREEN_IDENTIFIER = "webview_screen_qb";
|
|
|
18
18
|
|
|
19
19
|
const logger = coreAppLogger.addSubsystem("Navigator");
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Drops a leading `/hooks/<screen_id>` segment from a path.
|
|
23
|
+
*
|
|
24
|
+
* Routes are built by appending to the current path, and a hook screen is
|
|
25
|
+
* presented at an absolute `/hooks/<screen_id>`. Without this, every screen
|
|
26
|
+
* opened from a hook would inherit that prefix for good: the hook's stack entry
|
|
27
|
+
* would stay buried under its descendants, and `canGoBack` — which treats any
|
|
28
|
+
* route containing `/hooks` as a hook — would count the whole branch as one
|
|
29
|
+
* entry and send Back home instead of popping.
|
|
30
|
+
*/
|
|
31
|
+
export function stripHookPrefix(pathname?: string): string {
|
|
32
|
+
return pathname?.replace(/^\/hooks\/[^/]+/, "") ?? "";
|
|
33
|
+
}
|
|
34
|
+
|
|
21
35
|
export function targetShouldOpenExternally(
|
|
22
36
|
target: ZappEntry,
|
|
23
37
|
targetScreen: ZappRiver | null,
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { subscriber } from "@applicaster/zapp-react-native-utils/functionUtils";
|
|
2
|
+
import { subscriberFor } from "@applicaster/zapp-react-native-utils/reactHooks/useSubscriberFor";
|
|
3
|
+
|
|
4
|
+
// events accepted on the shared cross-package event bus (see useSubscriberFor),
|
|
5
|
+
// so packages that must not depend on quick-brick-core (e.g. OfflineHandler in
|
|
6
|
+
// zapp-react-native-ui-components) can trigger a toast by event name alone,
|
|
7
|
+
// without importing notificationToastManager directly.
|
|
8
|
+
const unwrapEventPayload = (payload: unknown) =>
|
|
9
|
+
Array.isArray(payload) ? payload[0] : payload;
|
|
10
|
+
|
|
11
|
+
export const NOTIFICATION_TOAST_EVENTS = {
|
|
12
|
+
SHOW_TOAST: "showToast", // in: request to enqueue a toast
|
|
13
|
+
DISMISS: "dismiss", // in: renderer/user dismissed the visible toast early
|
|
14
|
+
SHOW: "show", // out: renderer should display this toast
|
|
15
|
+
HIDE: "hide", // out: renderer should hide the current toast
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
export interface ToastStyle {
|
|
19
|
+
backgroundColor?: string;
|
|
20
|
+
color?: string;
|
|
21
|
+
fontFamily?: string;
|
|
22
|
+
fontSize?: number;
|
|
23
|
+
lineHeight?: number;
|
|
24
|
+
letterSpacing?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const DEFAULT_TOAST_TIMEOUT = 4000;
|
|
28
|
+
|
|
29
|
+
export const CONFIRMATION_TOAST_SOURCE = "confirmation";
|
|
30
|
+
|
|
31
|
+
export interface ToastPayload {
|
|
32
|
+
id?: string;
|
|
33
|
+
message: string;
|
|
34
|
+
extraMessage?: string; // secondary line; TV renderers show it as a subtitle, mobile ignores it
|
|
35
|
+
timeout?: number; // ms; defaults to DEFAULT_TOAST_TIMEOUT (4000) if omitted
|
|
36
|
+
style?: ToastStyle;
|
|
37
|
+
// stamped by the showToast action; Theme confirmation styles apply when
|
|
38
|
+
// source === CONFIRMATION_TOAST_SOURCE ("confirmation")
|
|
39
|
+
source?: string;
|
|
40
|
+
[key: string]: unknown;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class NotificationToastManager {
|
|
44
|
+
private static instance: NotificationToastManager;
|
|
45
|
+
|
|
46
|
+
private eventHandler = subscriber();
|
|
47
|
+
private queue: ToastPayload[] = [];
|
|
48
|
+
private hideTimer: ReturnType<typeof setTimeout> | null = null;
|
|
49
|
+
private isShowing = false;
|
|
50
|
+
private nextId = 0;
|
|
51
|
+
|
|
52
|
+
private constructor() {
|
|
53
|
+
// subscribe to the shared event bus so other packages can trigger toasts
|
|
54
|
+
subscriberFor(
|
|
55
|
+
NOTIFICATION_TOAST_EVENTS.SHOW_TOAST,
|
|
56
|
+
this.handleShowToastEvent
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
subscriberFor(NOTIFICATION_TOAST_EVENTS.DISMISS, this.handleDismiss);
|
|
60
|
+
|
|
61
|
+
this.getCurrentState = this.getCurrentState.bind(this);
|
|
62
|
+
this.subscribe = this.subscribe.bind(this);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static getInstance(): NotificationToastManager {
|
|
66
|
+
if (!NotificationToastManager.instance) {
|
|
67
|
+
NotificationToastManager.instance = new NotificationToastManager();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return NotificationToastManager.instance;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
on(event: string, callback: (...args: any[]) => void) {
|
|
74
|
+
this.eventHandler.on(event, callback);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
removeHandler(event: string, callback: (...args: any[]) => void) {
|
|
78
|
+
this.eventHandler.removeHandler(event, callback);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
showToast(payload: ToastPayload) {
|
|
82
|
+
this.handleShowToast(payload);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
dismiss() {
|
|
86
|
+
this.handleDismiss();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getCurrentState() {
|
|
90
|
+
return this.queue[0] ?? null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
subscribe(callback) {
|
|
94
|
+
this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
|
|
95
|
+
|
|
96
|
+
this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
|
|
97
|
+
|
|
98
|
+
return () => {
|
|
99
|
+
this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
|
|
100
|
+
|
|
101
|
+
this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// subscriberFor's shared bus can deliver the raw payload (component emitters,
|
|
106
|
+
// see useSubscriberFor) or an args array (postEvent-style callers) - accept both.
|
|
107
|
+
private handleShowToastEvent = (payload: unknown) => {
|
|
108
|
+
if (!payload) {
|
|
109
|
+
console.warn(
|
|
110
|
+
"NotificationToastManager: Invalid showToast payload:",
|
|
111
|
+
payload
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const unwrappedPayload = unwrapEventPayload(payload) as ToastPayload;
|
|
118
|
+
|
|
119
|
+
if (!unwrappedPayload || typeof unwrappedPayload.message !== "string") {
|
|
120
|
+
console.warn(
|
|
121
|
+
"NotificationToastManager: Invalid showToast payload:",
|
|
122
|
+
payload
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
this.handleShowToast(unwrappedPayload);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
private handleShowToast = (payload: ToastPayload) => {
|
|
132
|
+
const item = {
|
|
133
|
+
...payload,
|
|
134
|
+
id: payload.id ?? String(this.nextId++),
|
|
135
|
+
timeout: payload.timeout ?? DEFAULT_TOAST_TIMEOUT,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const queuedIndex = this.queue.findIndex((queued) => queued.id === item.id);
|
|
139
|
+
|
|
140
|
+
if (queuedIndex === -1) {
|
|
141
|
+
this.queue.push(item);
|
|
142
|
+
} else if (queuedIndex === 0 && this.isShowing) {
|
|
143
|
+
// this id is the toast currently on screen - swap its content in place
|
|
144
|
+
// (new one appears in front of it) and dismiss the old timer/content
|
|
145
|
+
this.clearTimer();
|
|
146
|
+
this.queue[0] = item;
|
|
147
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, item);
|
|
148
|
+
|
|
149
|
+
if (item.timeout) {
|
|
150
|
+
this.hideTimer = setTimeout(() => this.finishCurrent(), item.timeout);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return;
|
|
154
|
+
} else {
|
|
155
|
+
// still queued, not yet shown - update it in place instead of duplicating
|
|
156
|
+
this.queue[queuedIndex] = item;
|
|
157
|
+
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!this.isShowing) {
|
|
162
|
+
this.processNext();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
private handleDismiss = () => {
|
|
167
|
+
if (!this.isShowing) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
this.finishCurrent();
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
private processNext() {
|
|
175
|
+
const next = this.queue[0];
|
|
176
|
+
|
|
177
|
+
if (!next) {
|
|
178
|
+
this.isShowing = false;
|
|
179
|
+
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
this.isShowing = true;
|
|
184
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, next);
|
|
185
|
+
|
|
186
|
+
if (next.timeout) {
|
|
187
|
+
this.hideTimer = setTimeout(() => this.finishCurrent(), next.timeout);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private finishCurrent() {
|
|
192
|
+
this.clearTimer();
|
|
193
|
+
this.queue.shift();
|
|
194
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.HIDE);
|
|
195
|
+
this.isShowing = false;
|
|
196
|
+
this.processNext();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private clearTimer() {
|
|
200
|
+
if (this.hideTimer) {
|
|
201
|
+
clearTimeout(this.hideTimer);
|
|
202
|
+
this.hideTimer = null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public reset(): void {
|
|
207
|
+
this.clearTimer();
|
|
208
|
+
this.queue = [];
|
|
209
|
+
this.isShowing = false;
|
|
210
|
+
this.nextId = 0;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export const notificationToastManager = NotificationToastManager.getInstance();
|