@momo-kits/foundation 0.169.16-beta.5 → 0.169.16-beta.7
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/Application/BottomTab/Legacy/TabScreen.tsx +16 -22
- package/Application/BottomTab/Legacy/hooks.ts +1 -1
- package/Application/BottomTab/Legacy/index.tsx +5 -1
- package/Application/BottomTab/TabScreen.tsx +12 -22
- package/Application/BottomTab/index.tsx +1 -1
- package/Application/Components/HeaderLeft.tsx +2 -2
- package/Application/Components/SearchHeader.tsx +2 -2
- package/Application/Components/SwipeBackHaptic.tsx +1 -1
- package/Application/Components/useToolkitActions.tsx +34 -64
- package/Application/KitsBindingProvider.tsx +22 -0
- package/Application/KitsHost.ts +183 -0
- package/Application/ModalScreen.tsx +2 -2
- package/Application/NavigationContainer.tsx +11 -10
- package/Application/Navigator.ts +7 -2
- package/Application/StackScreen.tracking.ts +22 -12
- package/Application/StackScreen.tsx +20 -32
- package/Application/WidgetContainer.tsx +9 -6
- package/Application/__tests__/KitsHost.test.ts +155 -0
- package/Application/hooks.ts +14 -0
- package/Application/index.ts +4 -0
- package/Application/types.binding.ts +134 -0
- package/Application/types.ts +16 -3
- package/package.json +1 -1
|
@@ -20,6 +20,10 @@ import { styles } from './index.styles';
|
|
|
20
20
|
|
|
21
21
|
const Stack = createStackNavigator();
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Content of one tab in the docked bar: its own stack, faded in on focus, plus
|
|
25
|
+
* screen_navigated tracking.
|
|
26
|
+
*/
|
|
23
27
|
const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
24
28
|
const { navigator } = useContext(ApplicationContext);
|
|
25
29
|
const context = useContext<any>(MiniAppContext);
|
|
@@ -38,7 +42,7 @@ const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
|
38
42
|
transform: [{ scale: scale.value }],
|
|
39
43
|
}));
|
|
40
44
|
|
|
41
|
-
const
|
|
45
|
+
const handleScreenNavigated = (pre: string | undefined, current: string) => {
|
|
42
46
|
if (!isBackgroundToForeground) {
|
|
43
47
|
const data: any = {
|
|
44
48
|
preScreenName: pre,
|
|
@@ -56,7 +60,7 @@ const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
|
56
60
|
/**
|
|
57
61
|
* debug toast
|
|
58
62
|
*/
|
|
59
|
-
navigator?.
|
|
63
|
+
navigator?.binding.showToastDebug({
|
|
60
64
|
appId: context.appId,
|
|
61
65
|
message: `${screenName} screen_navigated`,
|
|
62
66
|
type: 'ERROR',
|
|
@@ -71,15 +75,10 @@ const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
|
71
75
|
scale.value = withTiming(1, { duration: 200 });
|
|
72
76
|
|
|
73
77
|
setTimeout(() => {
|
|
74
|
-
navigator?.
|
|
75
|
-
|
|
76
|
-
(
|
|
77
|
-
|
|
78
|
-
navigator?.maxApi?.setObserver?.('current_screen', {
|
|
79
|
-
screenName,
|
|
80
|
-
});
|
|
81
|
-
},
|
|
82
|
-
);
|
|
78
|
+
navigator?.binding.getCurrentScreen(data => {
|
|
79
|
+
handleScreenNavigated(data?.screenName, screenName);
|
|
80
|
+
navigator?.binding.setCurrentScreen({ screenName });
|
|
81
|
+
});
|
|
83
82
|
}, 100);
|
|
84
83
|
|
|
85
84
|
return () => {
|
|
@@ -96,22 +95,17 @@ const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
|
96
95
|
|
|
97
96
|
useEffect(
|
|
98
97
|
() => {
|
|
99
|
-
const
|
|
98
|
+
const focusAppListener = navigator?.binding.onFocusApp(() => {
|
|
100
99
|
if (navigation.isFocused()) {
|
|
101
|
-
navigator?.
|
|
102
|
-
|
|
103
|
-
(
|
|
104
|
-
|
|
105
|
-
navigator?.maxApi?.setObserver?.('current_screen', {
|
|
106
|
-
screenName,
|
|
107
|
-
});
|
|
108
|
-
},
|
|
109
|
-
);
|
|
100
|
+
navigator?.binding.getCurrentScreen(data => {
|
|
101
|
+
handleScreenNavigated(data?.screenName, screenName);
|
|
102
|
+
navigator?.binding.setCurrentScreen({ screenName });
|
|
103
|
+
});
|
|
110
104
|
}
|
|
111
105
|
});
|
|
112
106
|
|
|
113
107
|
return () => {
|
|
114
|
-
|
|
108
|
+
focusAppListener?.remove();
|
|
115
109
|
};
|
|
116
110
|
},
|
|
117
111
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -185,7 +185,7 @@ const useLiquidSelection = ({
|
|
|
185
185
|
};
|
|
186
186
|
|
|
187
187
|
const tick = useCallback(() => {
|
|
188
|
-
callbacks.current.navigator?.
|
|
188
|
+
callbacks.current.navigator?.binding.vibrateLight();
|
|
189
189
|
}, []);
|
|
190
190
|
|
|
191
191
|
const select = useCallback(
|
|
@@ -28,6 +28,10 @@ import { styles } from './index.styles';
|
|
|
28
28
|
|
|
29
29
|
const Tab = createBottomTabNavigator();
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Pre-redesign app tab navigator with the docked phone bar; a tab press ticks a
|
|
33
|
+
* light haptic through the host binding.
|
|
34
|
+
*/
|
|
31
35
|
const BottomTab: React.FC<BottomTabProps> = ({
|
|
32
36
|
nested,
|
|
33
37
|
tabs,
|
|
@@ -100,7 +104,7 @@ const BottomTab: React.FC<BottomTabProps> = ({
|
|
|
100
104
|
state?: (e: any) => void;
|
|
101
105
|
} = {
|
|
102
106
|
tabPress: e => {
|
|
103
|
-
navigator?.
|
|
107
|
+
navigator?.binding.vibrateLight();
|
|
104
108
|
listeners?.tabPress?.(e);
|
|
105
109
|
},
|
|
106
110
|
focus: e => {
|
|
@@ -39,7 +39,7 @@ const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
|
39
39
|
);
|
|
40
40
|
const tabInsetStyle = { paddingBottom: isInsetClaimed ? 0 : tabBarInset };
|
|
41
41
|
|
|
42
|
-
const handleScreenNavigated = (pre: string, current: string) => {
|
|
42
|
+
const handleScreenNavigated = (pre: string | undefined, current: string) => {
|
|
43
43
|
if (!isBackgroundToForeground) {
|
|
44
44
|
const data: any = {
|
|
45
45
|
preScreenName: pre,
|
|
@@ -54,7 +54,7 @@ const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
|
54
54
|
...data,
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
-
navigator?.
|
|
57
|
+
navigator?.binding.showToastDebug({
|
|
58
58
|
appId: context.appId,
|
|
59
59
|
message: `${screenName} screen_navigated`,
|
|
60
60
|
type: 'ERROR',
|
|
@@ -66,15 +66,10 @@ const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
|
66
66
|
React.useCallback(
|
|
67
67
|
() => {
|
|
68
68
|
setTimeout(() => {
|
|
69
|
-
navigator?.
|
|
70
|
-
|
|
71
|
-
(
|
|
72
|
-
|
|
73
|
-
navigator?.maxApi?.setObserver?.('current_screen', {
|
|
74
|
-
screenName,
|
|
75
|
-
});
|
|
76
|
-
},
|
|
77
|
-
);
|
|
69
|
+
navigator?.binding.getCurrentScreen(data => {
|
|
70
|
+
handleScreenNavigated(data?.screenName, screenName);
|
|
71
|
+
navigator?.binding.setCurrentScreen({ screenName });
|
|
72
|
+
});
|
|
78
73
|
}, NAVIGATED_REPORT_DELAY);
|
|
79
74
|
},
|
|
80
75
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -84,22 +79,17 @@ const TabScreen: React.FC<any> = ({ route, navigation }) => {
|
|
|
84
79
|
|
|
85
80
|
useEffect(
|
|
86
81
|
() => {
|
|
87
|
-
const focusAppListener = navigator?.
|
|
82
|
+
const focusAppListener = navigator?.binding.onFocusApp(() => {
|
|
88
83
|
if (navigation.isFocused()) {
|
|
89
|
-
navigator?.
|
|
90
|
-
|
|
91
|
-
(
|
|
92
|
-
|
|
93
|
-
navigator?.maxApi?.setObserver?.('current_screen', {
|
|
94
|
-
screenName,
|
|
95
|
-
});
|
|
96
|
-
},
|
|
97
|
-
);
|
|
84
|
+
navigator?.binding.getCurrentScreen(data => {
|
|
85
|
+
handleScreenNavigated(data?.screenName, screenName);
|
|
86
|
+
navigator?.binding.setCurrentScreen({ screenName });
|
|
87
|
+
});
|
|
98
88
|
}
|
|
99
89
|
});
|
|
100
90
|
|
|
101
91
|
return () => {
|
|
102
|
-
focusAppListener?.remove
|
|
92
|
+
focusAppListener?.remove();
|
|
103
93
|
};
|
|
104
94
|
},
|
|
105
95
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -120,7 +120,7 @@ const FloatingBottomTab: React.FC<BottomTabProps> = ({
|
|
|
120
120
|
state?: (e: any) => void;
|
|
121
121
|
} = {
|
|
122
122
|
tabPress: e => {
|
|
123
|
-
navigator?.
|
|
123
|
+
navigator?.binding.vibrateLight();
|
|
124
124
|
listeners?.tabPress?.(e);
|
|
125
125
|
},
|
|
126
126
|
focus: e => {
|
|
@@ -38,8 +38,8 @@ const HeaderLeft: React.FC<HeaderBackProps> = ({
|
|
|
38
38
|
|
|
39
39
|
if (canGoBack) {
|
|
40
40
|
navigator?.ref?.current?.goBack?.();
|
|
41
|
-
} else if (navigator
|
|
42
|
-
navigator
|
|
41
|
+
} else if (navigator) {
|
|
42
|
+
navigator.binding.dismiss('current', navigator.dismissData);
|
|
43
43
|
} else {
|
|
44
44
|
(globalThis as any)?.miniAppApi?.dispatch?.('dismiss', context);
|
|
45
45
|
}
|
|
@@ -78,8 +78,8 @@ const SearchHeader = React.forwardRef<InputRef, SearchHeaderProps>(
|
|
|
78
78
|
|
|
79
79
|
if (canGoBack) {
|
|
80
80
|
navigator?.ref?.current?.goBack?.();
|
|
81
|
-
} else if (navigator
|
|
82
|
-
navigator
|
|
81
|
+
} else if (navigator) {
|
|
82
|
+
navigator.binding.dismiss('current', navigator.dismissData);
|
|
83
83
|
} else {
|
|
84
84
|
(globalThis as any)?.miniAppApi?.dispatch?.('dismiss', context);
|
|
85
85
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useContext, useEffect, useState } from 'react';
|
|
2
2
|
import { ApplicationContext, MiniAppContext } from '../../Context';
|
|
3
3
|
import { PopupNotify } from '../../Popup';
|
|
4
4
|
import { Tool, ToolGroup } from '../types';
|
|
5
5
|
|
|
6
|
-
const DID_SYNC_NEW_HOME = 'did_sync_new_home';
|
|
7
6
|
const LOADING_RESET_DELAY = 1000;
|
|
8
7
|
|
|
9
8
|
type ToolkitActionsParams = {
|
|
@@ -33,42 +32,28 @@ const useToolkitActions = ({
|
|
|
33
32
|
const [isFavorite, setIsFavorite] = useState<boolean>(false);
|
|
34
33
|
const [isLoading, setIsLoading] = useState(false);
|
|
35
34
|
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
'isFavoriteApp',
|
|
39
|
-
{ code: context?.code },
|
|
40
|
-
(result: boolean) => {
|
|
41
|
-
setIsFavorite(result);
|
|
42
|
-
},
|
|
43
|
-
);
|
|
44
|
-
}, [context?.code, navigator?.maxApi]);
|
|
35
|
+
const binding = navigator?.binding;
|
|
36
|
+
const appCode = context?.code;
|
|
45
37
|
|
|
46
38
|
useEffect(() => {
|
|
47
|
-
if (useShortcut) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const favoriteObserver = navigator?.maxApi?.observer?.(
|
|
51
|
-
DID_SYNC_NEW_HOME,
|
|
52
|
-
(updatedData: any) => {
|
|
53
|
-
const isInFavorites = updatedData?.items?.includes(context?.code);
|
|
54
|
-
setIsFavorite(isInFavorites);
|
|
55
|
-
},
|
|
56
|
-
);
|
|
57
|
-
return () => {
|
|
58
|
-
favoriteObserver?.remove?.();
|
|
59
|
-
};
|
|
39
|
+
if (!useShortcut || !binding) {
|
|
40
|
+
return;
|
|
60
41
|
}
|
|
61
|
-
|
|
42
|
+
const request = binding.checkFavorite(appCode, setIsFavorite);
|
|
43
|
+
const favoriteObserver = binding.observeFavorites(appCodes => {
|
|
44
|
+
setIsFavorite(appCodes.includes(appCode));
|
|
45
|
+
});
|
|
46
|
+
return () => {
|
|
47
|
+
request.cancel();
|
|
48
|
+
favoriteObserver.remove();
|
|
49
|
+
};
|
|
50
|
+
}, [useShortcut, appCode, binding]);
|
|
62
51
|
|
|
63
52
|
const handleDismiss = () => {
|
|
64
53
|
if (useCloseIcon) {
|
|
65
|
-
navigator?.
|
|
66
|
-
'dismiss',
|
|
67
|
-
navigator?.dismissData,
|
|
68
|
-
undefined,
|
|
69
|
-
);
|
|
54
|
+
binding?.dismiss('current', navigator?.dismissData);
|
|
70
55
|
} else {
|
|
71
|
-
|
|
56
|
+
binding?.dismiss('all');
|
|
72
57
|
}
|
|
73
58
|
};
|
|
74
59
|
|
|
@@ -111,20 +96,7 @@ const useToolkitActions = ({
|
|
|
111
96
|
screenName: currentRoute?.params?.screen?.name ?? currentRoute?.name,
|
|
112
97
|
});
|
|
113
98
|
setIsLoading(true);
|
|
114
|
-
|
|
115
|
-
'onToolAction',
|
|
116
|
-
{
|
|
117
|
-
item: {
|
|
118
|
-
key: 'onFavorite',
|
|
119
|
-
},
|
|
120
|
-
context,
|
|
121
|
-
},
|
|
122
|
-
({ success }: { success: boolean }) => {
|
|
123
|
-
if (success) {
|
|
124
|
-
setIsFavorite(shouldBeFavorite);
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
);
|
|
99
|
+
binding?.toggleFavorite(appCode, setIsFavorite);
|
|
128
100
|
setTimeout(() => {
|
|
129
101
|
setIsLoading(false);
|
|
130
102
|
}, LOADING_RESET_DELAY);
|
|
@@ -139,7 +111,13 @@ const useToolkitActions = ({
|
|
|
139
111
|
screenName: currentRoute?.params?.screen?.name ?? currentRoute?.name,
|
|
140
112
|
});
|
|
141
113
|
|
|
142
|
-
|
|
114
|
+
binding?.showHelpCenter({
|
|
115
|
+
appId: context?.appId,
|
|
116
|
+
code: appCode,
|
|
117
|
+
name: context?.name,
|
|
118
|
+
icon: context?.icon,
|
|
119
|
+
description: context?.description,
|
|
120
|
+
});
|
|
143
121
|
};
|
|
144
122
|
|
|
145
123
|
const handlePressIconMore = () => {
|
|
@@ -151,25 +129,17 @@ const useToolkitActions = ({
|
|
|
151
129
|
screenName: currentRoute?.params?.screen?.name ?? currentRoute?.name,
|
|
152
130
|
});
|
|
153
131
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
for (const group of tools) {
|
|
163
|
-
const pressedTool = group?.items?.find?.(
|
|
164
|
-
(tool: Tool) => tool?.key === key,
|
|
165
|
-
);
|
|
166
|
-
if (pressedTool) {
|
|
167
|
-
pressedTool?.onPress();
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
132
|
+
binding?.showToolkit({ useSystemTools, tools, context }, key => {
|
|
133
|
+
for (const group of tools) {
|
|
134
|
+
const pressedTool = group?.items?.find?.(
|
|
135
|
+
(tool: Tool) => tool?.key === key,
|
|
136
|
+
);
|
|
137
|
+
if (pressedTool) {
|
|
138
|
+
pressedTool?.onPress();
|
|
139
|
+
break;
|
|
170
140
|
}
|
|
171
|
-
}
|
|
172
|
-
);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
173
143
|
};
|
|
174
144
|
|
|
175
145
|
let iconShortcut = isFavorite ? 'pin_star_checked' : 'pin_star';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React, { createContext, FC } from 'react';
|
|
2
|
+
import type { KitsBinding, KitsBindingProviderProps } from './types.binding';
|
|
3
|
+
|
|
4
|
+
/** A host's `KitsBindingProvider` hand-off; kit containers never set it. */
|
|
5
|
+
const KitsBindingContext = createContext<KitsBinding | undefined>(undefined);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Host hand-off: a mini app container (MMApplication) that doesn't render the
|
|
9
|
+
* mini app's kit containers gives them `binding`, unless one passes its own.
|
|
10
|
+
*/
|
|
11
|
+
const KitsBindingProvider: FC<KitsBindingProviderProps> = ({
|
|
12
|
+
binding,
|
|
13
|
+
children,
|
|
14
|
+
}) => {
|
|
15
|
+
return (
|
|
16
|
+
<KitsBindingContext.Provider value={binding}>
|
|
17
|
+
{children}
|
|
18
|
+
</KitsBindingContext.Provider>
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export { KitsBindingContext, KitsBindingProvider };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
KitsBinding,
|
|
3
|
+
KitsRequest,
|
|
4
|
+
KitsSubscription,
|
|
5
|
+
ResolvedKitsBinding,
|
|
6
|
+
} from './types.binding';
|
|
7
|
+
|
|
8
|
+
type BindingMethod = keyof KitsBinding;
|
|
9
|
+
|
|
10
|
+
const NO_REQUEST: KitsRequest = { cancel: () => {} };
|
|
11
|
+
const NO_SUBSCRIPTION: KitsSubscription = { remove: () => {} };
|
|
12
|
+
|
|
13
|
+
/** What the kit does without a host: every service is inert. */
|
|
14
|
+
const DefaultKitsBinding: ResolvedKitsBinding = {
|
|
15
|
+
dismiss: () => {},
|
|
16
|
+
trackEvent: () => {},
|
|
17
|
+
getLanguage: () => NO_REQUEST,
|
|
18
|
+
vibrateLight: () => {},
|
|
19
|
+
checkFavorite: () => NO_REQUEST,
|
|
20
|
+
toggleFavorite: () => NO_REQUEST,
|
|
21
|
+
showToolkit: () => NO_REQUEST,
|
|
22
|
+
showHelpCenter: () => NO_REQUEST,
|
|
23
|
+
logError: () => {},
|
|
24
|
+
observeConfig: () => NO_SUBSCRIPTION,
|
|
25
|
+
observeFavorites: () => NO_SUBSCRIPTION,
|
|
26
|
+
getCurrentScreen: () => NO_REQUEST,
|
|
27
|
+
setCurrentScreen: () => {},
|
|
28
|
+
onFocusApp: () => NO_SUBSCRIPTION,
|
|
29
|
+
startScreenTrace: () => NO_REQUEST,
|
|
30
|
+
stopTrace: () => {},
|
|
31
|
+
showToastDebug: () => {},
|
|
32
|
+
showNoticePopup: () => {},
|
|
33
|
+
openWeb: () => {},
|
|
34
|
+
startFeatureCode: () => {},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Resolved bindings delegate to the host, so one installed as the host would loop.
|
|
38
|
+
const resolvedBindings = new WeakSet<KitsBinding>();
|
|
39
|
+
let installed: KitsBinding | undefined;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The host app installs its binding here once; a container uses it unless one
|
|
43
|
+
* is passed in or handed off. Until then `binding` is `DefaultKitsBinding`.
|
|
44
|
+
*/
|
|
45
|
+
const KitsHost = {
|
|
46
|
+
get binding(): KitsBinding {
|
|
47
|
+
return installed ?? DefaultKitsBinding;
|
|
48
|
+
},
|
|
49
|
+
/** The last install wins. The kit borrows the binding and never closes it. */
|
|
50
|
+
install(binding: KitsBinding) {
|
|
51
|
+
installed = binding;
|
|
52
|
+
},
|
|
53
|
+
uninstall() {
|
|
54
|
+
installed = undefined;
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Object that implements `method`, looked up at call time: `binding`, else the
|
|
60
|
+
* installed host binding unless that is `binding` itself, else the default.
|
|
61
|
+
*/
|
|
62
|
+
const getMethodOwner = (
|
|
63
|
+
binding: KitsBinding | undefined,
|
|
64
|
+
method: BindingMethod,
|
|
65
|
+
): KitsBinding => {
|
|
66
|
+
if (typeof binding?.[method] === 'function') {
|
|
67
|
+
return binding;
|
|
68
|
+
}
|
|
69
|
+
const host = KitsHost.binding;
|
|
70
|
+
const isOtherHost = host !== binding && !resolvedBindings.has(host);
|
|
71
|
+
if (isOtherHost && typeof host[method] === 'function') {
|
|
72
|
+
return host;
|
|
73
|
+
}
|
|
74
|
+
return DefaultKitsBinding;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Starts a host request whose callback is dropped once the kit cancels it, so a
|
|
79
|
+
* late host answer never reaches an unmounted screen. `cancel` is idempotent.
|
|
80
|
+
*/
|
|
81
|
+
const startRequest = <T>(
|
|
82
|
+
start: (callback: (value: T) => void) => KitsRequest | undefined,
|
|
83
|
+
callback: (value: T) => void,
|
|
84
|
+
): KitsRequest => {
|
|
85
|
+
let isActive = true;
|
|
86
|
+
const request = start(value => {
|
|
87
|
+
if (isActive) {
|
|
88
|
+
callback(value);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
return {
|
|
92
|
+
cancel: () => {
|
|
93
|
+
if (!isActive) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
isActive = false;
|
|
97
|
+
request?.cancel();
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** Every method of `binding`, each falling back to `KitsHost` when missing. */
|
|
103
|
+
const resolveKitsBinding = (binding?: KitsBinding): ResolvedKitsBinding => {
|
|
104
|
+
/** Owner of `method` for this binding. */
|
|
105
|
+
const owner = (method: BindingMethod) => getMethodOwner(binding, method);
|
|
106
|
+
const resolved: ResolvedKitsBinding = {
|
|
107
|
+
dismiss: (target, data) => owner('dismiss').dismiss?.(target, data),
|
|
108
|
+
trackEvent: (eventName, attributes) => {
|
|
109
|
+
owner('trackEvent').trackEvent?.(eventName, attributes);
|
|
110
|
+
},
|
|
111
|
+
getLanguage: callback => {
|
|
112
|
+
return startRequest(
|
|
113
|
+
cb => owner('getLanguage').getLanguage?.(cb),
|
|
114
|
+
callback,
|
|
115
|
+
);
|
|
116
|
+
},
|
|
117
|
+
vibrateLight: () => owner('vibrateLight').vibrateLight?.(),
|
|
118
|
+
checkFavorite: (appCode, callback) => {
|
|
119
|
+
return startRequest(
|
|
120
|
+
cb => owner('checkFavorite').checkFavorite?.(appCode, cb),
|
|
121
|
+
callback,
|
|
122
|
+
);
|
|
123
|
+
},
|
|
124
|
+
toggleFavorite: (appCode, callback) => {
|
|
125
|
+
return startRequest(
|
|
126
|
+
cb => owner('toggleFavorite').toggleFavorite?.(appCode, cb),
|
|
127
|
+
callback,
|
|
128
|
+
);
|
|
129
|
+
},
|
|
130
|
+
showToolkit: (request, onAction) => {
|
|
131
|
+
return startRequest(
|
|
132
|
+
cb => owner('showToolkit').showToolkit?.(request, cb),
|
|
133
|
+
onAction,
|
|
134
|
+
);
|
|
135
|
+
},
|
|
136
|
+
showHelpCenter: request => {
|
|
137
|
+
return owner('showHelpCenter').showHelpCenter?.(request) ?? NO_REQUEST;
|
|
138
|
+
},
|
|
139
|
+
logError: message => owner('logError').logError?.(message),
|
|
140
|
+
observeConfig: (key, callback) => {
|
|
141
|
+
const host = owner('observeConfig');
|
|
142
|
+
return host.observeConfig?.(key, callback) ?? NO_SUBSCRIPTION;
|
|
143
|
+
},
|
|
144
|
+
observeFavorites: callback => {
|
|
145
|
+
const host = owner('observeFavorites');
|
|
146
|
+
return host.observeFavorites?.(callback) ?? NO_SUBSCRIPTION;
|
|
147
|
+
},
|
|
148
|
+
getCurrentScreen: callback => {
|
|
149
|
+
return startRequest(
|
|
150
|
+
cb => owner('getCurrentScreen').getCurrentScreen?.(cb),
|
|
151
|
+
callback,
|
|
152
|
+
);
|
|
153
|
+
},
|
|
154
|
+
setCurrentScreen: screen => {
|
|
155
|
+
owner('setCurrentScreen').setCurrentScreen?.(screen);
|
|
156
|
+
},
|
|
157
|
+
onFocusApp: listener => {
|
|
158
|
+
return owner('onFocusApp').onFocusApp?.(listener) ?? NO_SUBSCRIPTION;
|
|
159
|
+
},
|
|
160
|
+
startScreenTrace: (kind, screenName, context, callback) => {
|
|
161
|
+
const host = owner('startScreenTrace');
|
|
162
|
+
return startRequest(
|
|
163
|
+
cb => host.startScreenTrace?.(kind, screenName, context, cb),
|
|
164
|
+
callback,
|
|
165
|
+
);
|
|
166
|
+
},
|
|
167
|
+
stopTrace: (traceId, value) => {
|
|
168
|
+
owner('stopTrace').stopTrace?.(traceId, value);
|
|
169
|
+
},
|
|
170
|
+
showToastDebug: toast => owner('showToastDebug').showToastDebug?.(toast),
|
|
171
|
+
showNoticePopup: popup => {
|
|
172
|
+
owner('showNoticePopup').showNoticePopup?.(popup);
|
|
173
|
+
},
|
|
174
|
+
openWeb: request => owner('openWeb').openWeb?.(request),
|
|
175
|
+
startFeatureCode: (featureCode, params) => {
|
|
176
|
+
owner('startFeatureCode').startFeatureCode?.(featureCode, params);
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
resolvedBindings.add(resolved);
|
|
180
|
+
return resolved;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export { KitsHost, resolveKitsBinding };
|
|
@@ -66,7 +66,7 @@ const Modal: React.FC<ModalParams> = props => {
|
|
|
66
66
|
|
|
67
67
|
// Auto-tracks the popup as displayed on the current screen.
|
|
68
68
|
useEffect(() => {
|
|
69
|
-
navigator?.
|
|
69
|
+
navigator?.binding.getCurrentScreen(curScreen => {
|
|
70
70
|
context.autoTracking({
|
|
71
71
|
...context,
|
|
72
72
|
screenName: curScreen?.screenName,
|
|
@@ -75,7 +75,7 @@ const Modal: React.FC<ModalParams> = props => {
|
|
|
75
75
|
title,
|
|
76
76
|
});
|
|
77
77
|
});
|
|
78
|
-
}, [context, description, navigator?.
|
|
78
|
+
}, [context, description, navigator?.binding, title]);
|
|
79
79
|
|
|
80
80
|
const opacity = useRef(new Animated.Value(0)).current;
|
|
81
81
|
const scale = useRef(new Animated.Value(INITIAL_SCALE)).current;
|
|
@@ -18,7 +18,7 @@ import ModalScreen from './ModalScreen';
|
|
|
18
18
|
import Navigator from './Navigator';
|
|
19
19
|
import { getModalOptions, getStackOptions } from './navigationOptions';
|
|
20
20
|
import { isWhiteListDesignSystem } from './utils';
|
|
21
|
-
import { useNativeCanGoBack } from './hooks';
|
|
21
|
+
import { useKitsBinding, useNativeCanGoBack } from './hooks';
|
|
22
22
|
import { NavigationContainerProps } from './types';
|
|
23
23
|
import { ApplicationContext, MiniAppContext } from '../Context';
|
|
24
24
|
import Localize from './Localize';
|
|
@@ -34,14 +34,14 @@ import { ReservedRegionsProvider } from '../Layout/ReservedRegions';
|
|
|
34
34
|
const Stack = createStackNavigator();
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
* Root of a mini app: safe area, reserved regions
|
|
38
|
-
*
|
|
37
|
+
* Root of a mini app: safe area, reserved regions, the merged mini app context
|
|
38
|
+
* and its host binding around the kit's navigator.
|
|
39
39
|
*/
|
|
40
40
|
const NavigationContainer: React.FC<NavigationContainerProps> = ({
|
|
41
41
|
screen,
|
|
42
42
|
theme = defaultTheme,
|
|
43
43
|
options,
|
|
44
|
-
|
|
44
|
+
binding,
|
|
45
45
|
initialParams,
|
|
46
46
|
localize = new Localize({ vi: {}, en: {} }),
|
|
47
47
|
features = {
|
|
@@ -53,13 +53,14 @@ const NavigationContainer: React.FC<NavigationContainerProps> = ({
|
|
|
53
53
|
}) => {
|
|
54
54
|
const context = useContext<any>(MiniAppContext);
|
|
55
55
|
const [currentContext, setCurrentContext] = useState<any>({});
|
|
56
|
+
const resolved = useKitsBinding(binding);
|
|
56
57
|
|
|
57
58
|
useEffect(() => {
|
|
58
|
-
const sub =
|
|
59
|
+
const sub = resolved.observeConfig('font_scale_config', data => {
|
|
59
60
|
setCurrentContext((prev: any) => ({ ...prev, fontScale: data }));
|
|
60
61
|
});
|
|
61
|
-
return () => sub
|
|
62
|
-
}, [
|
|
62
|
+
return () => sub.remove();
|
|
63
|
+
}, [resolved]);
|
|
63
64
|
|
|
64
65
|
const mergedContext = {
|
|
65
66
|
...context,
|
|
@@ -85,7 +86,7 @@ const NavigationContainer: React.FC<NavigationContainerProps> = ({
|
|
|
85
86
|
screen={screen}
|
|
86
87
|
theme={theme}
|
|
87
88
|
options={options}
|
|
88
|
-
|
|
89
|
+
binding={resolved}
|
|
89
90
|
setCurrentContext={setCurrentContext}
|
|
90
91
|
initialParams={initialParams}
|
|
91
92
|
localize={localize}
|
|
@@ -104,7 +105,7 @@ const Navigation: React.FC<any> = ({
|
|
|
104
105
|
screen,
|
|
105
106
|
theme = defaultTheme,
|
|
106
107
|
options,
|
|
107
|
-
|
|
108
|
+
binding,
|
|
108
109
|
initialParams,
|
|
109
110
|
setCurrentContext,
|
|
110
111
|
localize = new Localize({ vi: {}, en: {} }),
|
|
@@ -131,7 +132,7 @@ const Navigation: React.FC<any> = ({
|
|
|
131
132
|
headerGradient = context?.designSystemConfig?.config?.headerGradient;
|
|
132
133
|
}
|
|
133
134
|
|
|
134
|
-
navigator.current.
|
|
135
|
+
navigator.current.binding = binding;
|
|
135
136
|
|
|
136
137
|
/** Text for a localization key, or the current-language side of `{ vi, en }`. */
|
|
137
138
|
const translate = (data?: string | { vi: string; en: string }) => {
|
package/Application/Navigator.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import {CommonActions, StackActions} from '@react-navigation/native';
|
|
2
2
|
import {BottomSheetParams, ModalParams, ScreenParams} from './types';
|
|
3
|
+
import { resolveKitsBinding } from './KitsHost';
|
|
4
|
+
import type { ResolvedKitsBinding } from './types.binding';
|
|
5
|
+
|
|
6
|
+
const HOST_BINDING = resolveKitsBinding();
|
|
3
7
|
|
|
4
8
|
/**
|
|
5
9
|
* Imperative navigation API exposed to features through `ApplicationContext`.
|
|
@@ -9,7 +13,8 @@ class Navigator {
|
|
|
9
13
|
ref?: any;
|
|
10
14
|
isReady?: any;
|
|
11
15
|
isWidget?: any;
|
|
12
|
-
|
|
16
|
+
/** Host services; the container replaces this `KitsHost` default with its own. */
|
|
17
|
+
binding: ResolvedKitsBinding = HOST_BINDING;
|
|
13
18
|
dismissData?: any;
|
|
14
19
|
|
|
15
20
|
constructor(navigation: any, isReady: any, isWidget = false) {
|
|
@@ -121,7 +126,7 @@ class Navigator {
|
|
|
121
126
|
}
|
|
122
127
|
};
|
|
123
128
|
|
|
124
|
-
/** Sets the payload the header passes to `
|
|
129
|
+
/** Sets the payload the header passes to `binding.dismiss` on close. */
|
|
125
130
|
setDismissData = (data: any) => {
|
|
126
131
|
this.dismissData = data;
|
|
127
132
|
};
|
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
import { Alert } from 'react-native';
|
|
2
2
|
import { version } from '../package.json';
|
|
3
|
+
import type { NavigatorProps } from './types';
|
|
4
|
+
|
|
5
|
+
const MS_PER_SECOND = 1000;
|
|
3
6
|
|
|
4
7
|
type ScreenTrackingDeps = {
|
|
5
8
|
tracking: { current: any };
|
|
6
9
|
context: any;
|
|
7
|
-
navigator
|
|
10
|
+
navigator?: NavigatorProps;
|
|
8
11
|
screenName: string;
|
|
9
12
|
};
|
|
10
13
|
|
|
14
|
+
/** Ends a host performance trace; skipped when the host never returned an id. */
|
|
15
|
+
const stopTrace = (
|
|
16
|
+
navigator: NavigatorProps | undefined,
|
|
17
|
+
traceId: string | undefined,
|
|
18
|
+
durationMs: number,
|
|
19
|
+
) => {
|
|
20
|
+
if (traceId) {
|
|
21
|
+
navigator?.binding.stopTrace(traceId, durationMs / MS_PER_SECOND);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
11
25
|
/**
|
|
12
26
|
* Auto-tracking reporters of a stack screen. Each event fires at most once per
|
|
13
27
|
* screen, guarded by the `release*` flags on `tracking.current`.
|
|
@@ -37,14 +51,10 @@ export const createScreenTracking = ({
|
|
|
37
51
|
version: version,
|
|
38
52
|
lastElement: tracking.current.lastElement,
|
|
39
53
|
});
|
|
40
|
-
|
|
41
|
-
tracking.current.traceIdLoad,
|
|
42
|
-
{ value: timeLoad / 1000 },
|
|
43
|
-
null,
|
|
44
|
-
);
|
|
54
|
+
stopTrace(navigator, tracking.current.traceIdLoad, timeLoad);
|
|
45
55
|
tracking.current.releaseLoad = true;
|
|
46
56
|
|
|
47
|
-
navigator?.
|
|
57
|
+
navigator?.binding.showToastDebug({
|
|
48
58
|
appId: context.appId,
|
|
49
59
|
message: `${screenName} screen_load_time ${timeLoad}`,
|
|
50
60
|
type: 'ERROR',
|
|
@@ -80,14 +90,14 @@ export const createScreenTracking = ({
|
|
|
80
90
|
params: tracking.current.params,
|
|
81
91
|
version: version,
|
|
82
92
|
});
|
|
83
|
-
|
|
93
|
+
stopTrace(
|
|
94
|
+
navigator,
|
|
84
95
|
tracking.current.traceIdInteraction,
|
|
85
|
-
|
|
86
|
-
null,
|
|
96
|
+
tracking.current.timeInteraction,
|
|
87
97
|
);
|
|
88
98
|
tracking.current.releaseInteraction = true;
|
|
89
99
|
|
|
90
|
-
navigator?.
|
|
100
|
+
navigator?.binding.showToastDebug({
|
|
91
101
|
appId: context.appId,
|
|
92
102
|
message: `${screenName} screen_interaction_time ${tracking.current.timeInteraction}`,
|
|
93
103
|
type: 'ERROR',
|
|
@@ -109,7 +119,7 @@ export const createScreenTracking = ({
|
|
|
109
119
|
});
|
|
110
120
|
tracking.current.releaseUserInteraction = true;
|
|
111
121
|
|
|
112
|
-
navigator?.
|
|
122
|
+
navigator?.binding.showToastDebug({
|
|
113
123
|
appId: context.appId,
|
|
114
124
|
message: `${screenName} user_interaction ${timeLoad}`,
|
|
115
125
|
type: 'ERROR',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React, { useContext, useEffect, useLayoutEffect, useRef } from 'react';
|
|
2
2
|
import { useHeaderHeight } from '@react-navigation/elements';
|
|
3
|
-
import { ScreenTrackingParams } from './types';
|
|
3
|
+
import { type KitsSubscription, ScreenTrackingParams } from './types';
|
|
4
4
|
import Navigation from './Navigation';
|
|
5
5
|
import { ApplicationContext, MiniAppContext, ScreenContext } from '../Context';
|
|
6
6
|
import { SwipeBackHaptic } from './Components/SwipeBackHaptic';
|
|
@@ -88,9 +88,9 @@ const StackScreen: React.FC<any> = props => {
|
|
|
88
88
|
|
|
89
89
|
useEffect(() => {
|
|
90
90
|
let focusScreen: any;
|
|
91
|
-
let focusAppListener:
|
|
91
|
+
let focusAppListener: KitsSubscription | undefined;
|
|
92
92
|
if (['Invalid', 'screen'].includes(screenName)) {
|
|
93
|
-
navigator?.
|
|
93
|
+
navigator?.binding.showNoticePopup({
|
|
94
94
|
title: 'Invalid screen name',
|
|
95
95
|
message:
|
|
96
96
|
'Your screen has not been rendered because Platform has not detected the screen name. Please migrate to support this feature.',
|
|
@@ -101,42 +101,30 @@ const StackScreen: React.FC<any> = props => {
|
|
|
101
101
|
|
|
102
102
|
if (!bottomTab) {
|
|
103
103
|
focusScreen = props.navigation.addListener('focus', () => {
|
|
104
|
-
navigator?.
|
|
104
|
+
navigator?.binding.getCurrentScreen(item => {
|
|
105
105
|
handleScreenNavigated(item?.screenName, screenName);
|
|
106
|
-
navigator?.
|
|
107
|
-
screenName,
|
|
108
|
-
startTime,
|
|
109
|
-
});
|
|
106
|
+
navigator?.binding.setCurrentScreen({ screenName, startTime });
|
|
110
107
|
});
|
|
111
108
|
});
|
|
112
|
-
focusAppListener = navigator?.
|
|
109
|
+
focusAppListener = navigator?.binding.onFocusApp(() => {
|
|
113
110
|
if (props.navigation.isFocused()) {
|
|
114
|
-
navigator?.
|
|
115
|
-
|
|
116
|
-
(
|
|
117
|
-
|
|
118
|
-
navigator?.maxApi?.setDataObserver?.('current_screen', {
|
|
119
|
-
screenName,
|
|
120
|
-
startTime,
|
|
121
|
-
});
|
|
122
|
-
},
|
|
123
|
-
);
|
|
111
|
+
navigator?.binding.getCurrentScreen(item => {
|
|
112
|
+
handleScreenNavigated(item?.screenName, screenName);
|
|
113
|
+
navigator?.binding.setCurrentScreen({ screenName, startTime });
|
|
114
|
+
});
|
|
124
115
|
}
|
|
125
116
|
});
|
|
126
117
|
}
|
|
127
118
|
|
|
128
|
-
navigator?.
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
},
|
|
134
|
-
);
|
|
135
|
-
navigator?.maxApi?.startTraceScreenInteraction?.(
|
|
119
|
+
navigator?.binding.startScreenTrace('load', screenName, context, id => {
|
|
120
|
+
tracking.current.traceIdLoad = id;
|
|
121
|
+
});
|
|
122
|
+
navigator?.binding.startScreenTrace(
|
|
123
|
+
'interaction',
|
|
136
124
|
screenName,
|
|
137
125
|
context,
|
|
138
|
-
|
|
139
|
-
tracking.current.traceIdInteraction =
|
|
126
|
+
id => {
|
|
127
|
+
tracking.current.traceIdInteraction = id;
|
|
140
128
|
},
|
|
141
129
|
);
|
|
142
130
|
|
|
@@ -154,13 +142,13 @@ const StackScreen: React.FC<any> = props => {
|
|
|
154
142
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
155
143
|
clearTimeout(tracking.current.timeoutLoading);
|
|
156
144
|
focusScreen?.();
|
|
157
|
-
focusAppListener?.remove
|
|
145
|
+
focusAppListener?.remove();
|
|
158
146
|
};
|
|
159
147
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
160
148
|
}, []);
|
|
161
149
|
|
|
162
150
|
/** Reports a navigation to this screen: `back` if it was shown before. */
|
|
163
|
-
const handleScreenNavigated = (pre: string, current: string) => {
|
|
151
|
+
const handleScreenNavigated = (pre: string | undefined, current: string) => {
|
|
164
152
|
if (!isBackgroundToForeground) {
|
|
165
153
|
const item: any = {
|
|
166
154
|
preScreenName: pre,
|
|
@@ -177,7 +165,7 @@ const StackScreen: React.FC<any> = props => {
|
|
|
177
165
|
|
|
178
166
|
tracking.current.mounted = true;
|
|
179
167
|
|
|
180
|
-
navigator?.
|
|
168
|
+
navigator?.binding.showToastDebug({
|
|
181
169
|
appId: context.appId,
|
|
182
170
|
message: `${screenName} screen_navigated`,
|
|
183
171
|
type: 'ERROR',
|
|
@@ -15,6 +15,7 @@ import { isWhiteListDesignSystem } from './utils';
|
|
|
15
15
|
import { createStackNavigator } from '@react-navigation/stack';
|
|
16
16
|
import ModalScreen from './ModalScreen';
|
|
17
17
|
import { StyleSheet, View } from 'react-native';
|
|
18
|
+
import { useKitsBinding } from './hooks';
|
|
18
19
|
|
|
19
20
|
const Stack = createStackNavigator();
|
|
20
21
|
|
|
@@ -34,12 +35,13 @@ const WidgetContent: React.FC = ({ route }: any) => {
|
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
37
|
* Root of an embedded widget: its own navigator with the widget screen and the
|
|
37
|
-
* `Modal` route, sized to the widget's content height.
|
|
38
|
+
* `Modal` route, sized to the widget's content height. Its binding is never
|
|
39
|
+
* inherited from an enclosing container (see `useKitsBinding`).
|
|
38
40
|
*/
|
|
39
41
|
const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
40
42
|
widget,
|
|
41
43
|
theme = defaultTheme,
|
|
42
|
-
|
|
44
|
+
binding,
|
|
43
45
|
localize = new Localize({ vi: {}, en: {} }),
|
|
44
46
|
}) => {
|
|
45
47
|
const [height, setHeight] = useState<any>(undefined);
|
|
@@ -48,13 +50,14 @@ const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
|
48
50
|
const isReady = useRef(false);
|
|
49
51
|
const navigator = useRef(new Navigator(navigationRef, isReady, true));
|
|
50
52
|
const [currentContext, setCurrentContext] = useState({});
|
|
53
|
+
const resolved = useKitsBinding(binding);
|
|
51
54
|
|
|
52
55
|
useEffect(() => {
|
|
53
|
-
const sub =
|
|
56
|
+
const sub = resolved.observeConfig('font_scale_config', data => {
|
|
54
57
|
setCurrentContext((prev: any) => ({ ...prev, fontScale: data }));
|
|
55
58
|
});
|
|
56
|
-
return () => sub
|
|
57
|
-
}, [
|
|
59
|
+
return () => sub.remove();
|
|
60
|
+
}, [resolved]);
|
|
58
61
|
|
|
59
62
|
let headerBackground = context?.designConfig?.headerBar;
|
|
60
63
|
let headerGradient = theme.colors?.gradient;
|
|
@@ -66,7 +69,7 @@ const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
|
66
69
|
headerGradient = context?.designConfig?.headerGradient;
|
|
67
70
|
}
|
|
68
71
|
|
|
69
|
-
navigator.current.
|
|
72
|
+
navigator.current.binding = resolved;
|
|
70
73
|
|
|
71
74
|
/** Text for a localization key, or the current-language side of `{ vi, en }`. */
|
|
72
75
|
const translate = (data?: string | { vi: string; en: string }) => {
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { createElement, type ReactElement } from 'react';
|
|
2
|
+
import { act, create } from 'react-test-renderer';
|
|
3
|
+
import { useKitsBinding } from '../hooks';
|
|
4
|
+
import { KitsBindingProvider } from '../KitsBindingProvider';
|
|
5
|
+
import { KitsHost, resolveKitsBinding } from '../KitsHost';
|
|
6
|
+
import type { KitsBinding } from '../types.binding';
|
|
7
|
+
|
|
8
|
+
type ContainerProps = {
|
|
9
|
+
name: string;
|
|
10
|
+
binding?: KitsBinding;
|
|
11
|
+
children?: ReactElement;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** Host binding that records each call as `[method, ...args]`. */
|
|
15
|
+
const createRecordingBinding = (calls: unknown[][]): KitsBinding => {
|
|
16
|
+
return {
|
|
17
|
+
trackEvent: (eventName, attributes) => {
|
|
18
|
+
calls.push(['host.trackEvent', eventName, attributes]);
|
|
19
|
+
},
|
|
20
|
+
vibrateLight: () => {
|
|
21
|
+
calls.push(['host.vibrateLight']);
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Binding whose `trackEvent` records `name`. */
|
|
27
|
+
const createNamedBinding = (name: string, calls: unknown[][]): KitsBinding => {
|
|
28
|
+
return {
|
|
29
|
+
trackEvent: eventName => {
|
|
30
|
+
calls.push([name, eventName]);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Resolves its binding as a kit container does and tracks `name` on render. */
|
|
36
|
+
const Container = ({ name, binding, children }: ContainerProps) => {
|
|
37
|
+
useKitsBinding(binding).trackEvent(name, {});
|
|
38
|
+
return children ?? null;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Renders `element` once, synchronously. */
|
|
42
|
+
const render = (element: ReactElement) => {
|
|
43
|
+
act(() => {
|
|
44
|
+
create(element);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
describe('KitsHost', () => {
|
|
49
|
+
afterEach(() => {
|
|
50
|
+
KitsHost.uninstall();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('does nothing and never throws before a host installs a binding', () => {
|
|
54
|
+
const binding = resolveKitsBinding();
|
|
55
|
+
expect(() => binding.dismiss('current')).not.toThrow();
|
|
56
|
+
expect(() => binding.observeConfig('key', () => {}).remove()).not.toThrow();
|
|
57
|
+
expect(() => binding.getLanguage(() => {}).cancel()).not.toThrow();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('falls back per method to the installed binding at call time', () => {
|
|
61
|
+
const calls: unknown[][] = [];
|
|
62
|
+
const binding = resolveKitsBinding({
|
|
63
|
+
trackEvent: eventName => {
|
|
64
|
+
calls.push(['own.trackEvent', eventName]);
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
KitsHost.install(createRecordingBinding(calls));
|
|
68
|
+
|
|
69
|
+
binding.trackEvent('event', {});
|
|
70
|
+
binding.vibrateLight();
|
|
71
|
+
|
|
72
|
+
expect(calls).toEqual([['own.trackEvent', 'event'], ['host.vibrateLight']]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('uses DefaultKitsBinding when the binding is the installed one', () => {
|
|
76
|
+
const calls: unknown[][] = [];
|
|
77
|
+
const host = createRecordingBinding(calls);
|
|
78
|
+
KitsHost.install(host);
|
|
79
|
+
|
|
80
|
+
expect(() => resolveKitsBinding(host).dismiss('all')).not.toThrow();
|
|
81
|
+
expect(calls).toEqual([]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('never loops when a resolved binding is installed as the host', () => {
|
|
85
|
+
KitsHost.install(resolveKitsBinding());
|
|
86
|
+
|
|
87
|
+
expect(() => resolveKitsBinding().vibrateLight()).not.toThrow();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('drops a callback that arrives after cancel and cancels once', () => {
|
|
91
|
+
let answer: ((language: string) => void) | undefined;
|
|
92
|
+
const onLanguage = jest.fn();
|
|
93
|
+
const hostCancel = jest.fn();
|
|
94
|
+
const request = resolveKitsBinding({
|
|
95
|
+
getLanguage: callback => {
|
|
96
|
+
answer = callback;
|
|
97
|
+
return { cancel: hostCancel };
|
|
98
|
+
},
|
|
99
|
+
}).getLanguage(onLanguage);
|
|
100
|
+
|
|
101
|
+
request.cancel();
|
|
102
|
+
request.cancel();
|
|
103
|
+
answer?.('en');
|
|
104
|
+
|
|
105
|
+
expect(onLanguage).not.toHaveBeenCalled();
|
|
106
|
+
expect(hostCancel).toHaveBeenCalledTimes(1);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('useKitsBinding', () => {
|
|
111
|
+
afterEach(() => {
|
|
112
|
+
KitsHost.uninstall();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('never inherits the binding of an enclosing container', () => {
|
|
116
|
+
const calls: unknown[][] = [];
|
|
117
|
+
KitsHost.install(createNamedBinding('host', calls));
|
|
118
|
+
const inner = createElement(Container, { name: 'inner' });
|
|
119
|
+
const outer = createElement(Container, {
|
|
120
|
+
name: 'outer',
|
|
121
|
+
binding: createNamedBinding('A', calls),
|
|
122
|
+
children: inner,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
render(outer);
|
|
126
|
+
|
|
127
|
+
expect(calls).toEqual([
|
|
128
|
+
['A', 'outer'],
|
|
129
|
+
['host', 'inner'],
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('uses the host hand-off of KitsBindingProvider unless a prop is passed', () => {
|
|
134
|
+
const calls: unknown[][] = [];
|
|
135
|
+
KitsHost.install(createNamedBinding('host', calls));
|
|
136
|
+
const inner = createElement(Container, { name: 'inner' });
|
|
137
|
+
const outer = createElement(Container, {
|
|
138
|
+
name: 'outer',
|
|
139
|
+
binding: createNamedBinding('A', calls),
|
|
140
|
+
children: inner,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
render(
|
|
144
|
+
createElement(KitsBindingProvider, {
|
|
145
|
+
binding: createNamedBinding('handOff', calls),
|
|
146
|
+
children: outer,
|
|
147
|
+
}),
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
expect(calls).toEqual([
|
|
151
|
+
['A', 'outer'],
|
|
152
|
+
['handOff', 'inner'],
|
|
153
|
+
]);
|
|
154
|
+
});
|
|
155
|
+
});
|
package/Application/hooks.ts
CHANGED
|
@@ -13,6 +13,9 @@ import {
|
|
|
13
13
|
isRNAnimatedValue,
|
|
14
14
|
isSharedValue,
|
|
15
15
|
} from './utils';
|
|
16
|
+
import { KitsBindingContext } from './KitsBindingProvider';
|
|
17
|
+
import { resolveKitsBinding } from './KitsHost';
|
|
18
|
+
import type { KitsBinding, ResolvedKitsBinding } from './types.binding';
|
|
16
19
|
|
|
17
20
|
const FOREGROUND_FLAG_DURATION = 100;
|
|
18
21
|
|
|
@@ -129,8 +132,19 @@ const useNormalizedSharedValue = (
|
|
|
129
132
|
return fallback;
|
|
130
133
|
};
|
|
131
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Binding of a container: its `binding` prop, else the host's hand-off through
|
|
137
|
+
* `KitsBindingProvider`, else `KitsHost`. Never an enclosing container's.
|
|
138
|
+
*/
|
|
139
|
+
const useKitsBinding = (binding?: KitsBinding): ResolvedKitsBinding => {
|
|
140
|
+
const handedOff = useContext(KitsBindingContext);
|
|
141
|
+
const source = binding ?? handedOff;
|
|
142
|
+
return useMemo(() => resolveKitsBinding(source), [source]);
|
|
143
|
+
};
|
|
144
|
+
|
|
132
145
|
export {
|
|
133
146
|
useComponentId,
|
|
147
|
+
useKitsBinding,
|
|
134
148
|
useAppState,
|
|
135
149
|
useNativeCanGoBack,
|
|
136
150
|
useNormalizedSharedValue,
|
package/Application/index.ts
CHANGED
|
@@ -20,6 +20,8 @@ import Navigation from './Navigation';
|
|
|
20
20
|
import Navigator from './Navigator';
|
|
21
21
|
import ScaleSizeProvider from './ScaleSizeProvider';
|
|
22
22
|
import { useTooltipPortal } from './TooltipPortal';
|
|
23
|
+
import { KitsHost } from './KitsHost';
|
|
24
|
+
import { KitsBindingProvider } from './KitsBindingProvider';
|
|
23
25
|
import {
|
|
24
26
|
getModalLayout,
|
|
25
27
|
useModalLayout,
|
|
@@ -51,6 +53,8 @@ export {
|
|
|
51
53
|
HeaderBarMetrics,
|
|
52
54
|
getHeaderLayout,
|
|
53
55
|
useHeaderLayout,
|
|
56
|
+
KitsHost,
|
|
57
|
+
KitsBindingProvider,
|
|
54
58
|
};
|
|
55
59
|
|
|
56
60
|
export type { ModalLayout, ModalRect, HeaderContentLayout, HeaderLayout };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { ToolGroup } from './types.header';
|
|
3
|
+
|
|
4
|
+
/** `current` closes only this mini app; `all` goes back to the host home. */
|
|
5
|
+
export type KitsDismissTarget = 'current' | 'all';
|
|
6
|
+
|
|
7
|
+
/** Pending host call. Idempotent; the kit drops late callbacks after cancel. */
|
|
8
|
+
export type KitsRequest = {
|
|
9
|
+
cancel: () => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** Host listener the kit removes on unmount. */
|
|
13
|
+
export type KitsSubscription = {
|
|
14
|
+
remove: () => void;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Mini app context as the kit holds it (`MiniAppContext`). */
|
|
18
|
+
export type KitsMiniAppContext = Record<string, unknown>;
|
|
19
|
+
|
|
20
|
+
/** Localized `{ vi, en }` text or a plain string, as in the mini app context. */
|
|
21
|
+
export type KitsLocalizedText = string | { vi?: string; en?: string };
|
|
22
|
+
|
|
23
|
+
/** Mini app shown in the help center sheet, mirroring native. */
|
|
24
|
+
export type KitsHelpCenterRequest = {
|
|
25
|
+
appId?: string;
|
|
26
|
+
code?: string;
|
|
27
|
+
name?: KitsLocalizedText;
|
|
28
|
+
icon?: string;
|
|
29
|
+
description?: KitsLocalizedText;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** Tools of the header "more" sheet; `context` is the calling mini app. */
|
|
33
|
+
export type KitsToolkitRequest = {
|
|
34
|
+
tools: ToolGroup[];
|
|
35
|
+
useSystemTools: boolean;
|
|
36
|
+
context: KitsMiniAppContext;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Screen the host last recorded as shown, shared across mini apps. */
|
|
40
|
+
export type KitsCurrentScreen = {
|
|
41
|
+
screenName?: string;
|
|
42
|
+
startTime?: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Performance trace of a screen: until loaded, or until interactive. */
|
|
46
|
+
export type KitsScreenTraceKind = 'load' | 'interaction';
|
|
47
|
+
|
|
48
|
+
/** Developer toast with a tracking event, shown only in debug hosts. */
|
|
49
|
+
export type KitsDebugToast = {
|
|
50
|
+
appId?: string;
|
|
51
|
+
message: string;
|
|
52
|
+
type: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** Host notice popup with a single dismiss action. */
|
|
56
|
+
export type KitsNoticePopup = {
|
|
57
|
+
title: string;
|
|
58
|
+
message: string;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Web page the host opens in its in-app browser. */
|
|
62
|
+
export type KitsOpenWebRequest = {
|
|
63
|
+
url: string;
|
|
64
|
+
title?: string;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Host services the kit calls, mirroring native `KitsBinding`. A method left out
|
|
69
|
+
* falls back to the one installed in `KitsHost`, then to `DefaultKitsBinding`.
|
|
70
|
+
* Callbacks fire only on success; containers borrow the binding and never close it.
|
|
71
|
+
*/
|
|
72
|
+
export interface KitsBinding {
|
|
73
|
+
dismiss?(target: KitsDismissTarget, data?: unknown): void;
|
|
74
|
+
trackEvent?(eventName: string, attributes: Record<string, unknown>): void;
|
|
75
|
+
getLanguage?(callback: (language: string) => void): KitsRequest;
|
|
76
|
+
vibrateLight?(): void;
|
|
77
|
+
checkFavorite?(
|
|
78
|
+
appCode: string | undefined,
|
|
79
|
+
callback: (isFavorite: boolean) => void,
|
|
80
|
+
): KitsRequest;
|
|
81
|
+
/** `callback` gets whether the app is a favorite afterwards. */
|
|
82
|
+
toggleFavorite?(
|
|
83
|
+
appCode: string | undefined,
|
|
84
|
+
callback: (isFavorite: boolean) => void,
|
|
85
|
+
): KitsRequest;
|
|
86
|
+
showToolkit?(
|
|
87
|
+
request: KitsToolkitRequest,
|
|
88
|
+
onAction: (actionKey: string) => void,
|
|
89
|
+
): KitsRequest;
|
|
90
|
+
/** The request stays open while the sheet may still emit events. */
|
|
91
|
+
showHelpCenter?(request: KitsHelpCenterRequest): KitsRequest;
|
|
92
|
+
logError?(message: string): void;
|
|
93
|
+
/** RN only: host config by key, e.g. `font_scale_config`. */
|
|
94
|
+
observeConfig?(
|
|
95
|
+
key: string,
|
|
96
|
+
callback: (value: unknown) => void,
|
|
97
|
+
): KitsSubscription;
|
|
98
|
+
/** RN only: app codes on the home favorites after each home sync. */
|
|
99
|
+
observeFavorites?(callback: (appCodes: string[]) => void): KitsSubscription;
|
|
100
|
+
/** RN only. */
|
|
101
|
+
getCurrentScreen?(
|
|
102
|
+
callback: (screen: KitsCurrentScreen | undefined) => void,
|
|
103
|
+
): KitsRequest;
|
|
104
|
+
/** RN only. */
|
|
105
|
+
setCurrentScreen?(screen: KitsCurrentScreen): void;
|
|
106
|
+
/** RN only: the mini app comes back to the foreground. */
|
|
107
|
+
onFocusApp?(listener: () => void): KitsSubscription;
|
|
108
|
+
/** RN only: `callback` gets the trace id to pass to `stopTrace`. */
|
|
109
|
+
startScreenTrace?(
|
|
110
|
+
kind: KitsScreenTraceKind,
|
|
111
|
+
screenName: string,
|
|
112
|
+
context: KitsMiniAppContext,
|
|
113
|
+
callback: (traceId: string) => void,
|
|
114
|
+
): KitsRequest;
|
|
115
|
+
/** RN only: `value` is the measured duration in seconds. */
|
|
116
|
+
stopTrace?(traceId: string, value: number): void;
|
|
117
|
+
/** RN only. */
|
|
118
|
+
showToastDebug?(toast: KitsDebugToast): void;
|
|
119
|
+
/** RN only. */
|
|
120
|
+
showNoticePopup?(popup: KitsNoticePopup): void;
|
|
121
|
+
/** RN only. */
|
|
122
|
+
openWeb?(request: KitsOpenWebRequest): void;
|
|
123
|
+
/** RN only. */
|
|
124
|
+
startFeatureCode?(featureCode: string, params: Record<string, unknown>): void;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A binding with every method present, as containers resolve it. */
|
|
128
|
+
export type ResolvedKitsBinding = Required<KitsBinding>;
|
|
129
|
+
|
|
130
|
+
/** Props of `KitsBindingProvider`. */
|
|
131
|
+
export type KitsBindingProviderProps = {
|
|
132
|
+
binding: KitsBinding;
|
|
133
|
+
children: ReactNode;
|
|
134
|
+
};
|
package/Application/types.ts
CHANGED
|
@@ -10,10 +10,12 @@ import type {
|
|
|
10
10
|
} from './types.header';
|
|
11
11
|
import type { LocalizeProps, Theme } from './types.theme';
|
|
12
12
|
import type { HeaderContentLayout } from './headerLayout';
|
|
13
|
+
import type { KitsBinding, ResolvedKitsBinding } from './types.binding';
|
|
13
14
|
|
|
14
15
|
export * from './types.theme';
|
|
15
16
|
export * from './types.header';
|
|
16
17
|
export * from './types.tabs';
|
|
18
|
+
export * from './types.binding';
|
|
17
19
|
|
|
18
20
|
export type NavigationProps = {
|
|
19
21
|
context: any;
|
|
@@ -24,7 +26,8 @@ export type NavigatorProps = {
|
|
|
24
26
|
ref?: any;
|
|
25
27
|
isReady?: any;
|
|
26
28
|
isWidget?: any;
|
|
27
|
-
|
|
29
|
+
/** Host services of this container, resolved through `KitsHost`. */
|
|
30
|
+
binding: ResolvedKitsBinding;
|
|
28
31
|
dismissData?: any;
|
|
29
32
|
push: (params: ScreenParams) => void;
|
|
30
33
|
replace: (params: ScreenParams) => void;
|
|
@@ -59,7 +62,12 @@ export type NavigationContainerProps = {
|
|
|
59
62
|
screen: React.ComponentType<NavigationScreenProps>;
|
|
60
63
|
options?: NavigationOptions;
|
|
61
64
|
theme?: Theme;
|
|
62
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Optional. Host services for this mini app; defaults to a host's
|
|
67
|
+
* `KitsBindingProvider`, then `KitsHost`, never an enclosing container's.
|
|
68
|
+
* Keep it stable.
|
|
69
|
+
*/
|
|
70
|
+
binding?: KitsBinding;
|
|
63
71
|
initialParams?: any;
|
|
64
72
|
localize: LocalizeProps;
|
|
65
73
|
features?: FeatureFlags;
|
|
@@ -68,7 +76,12 @@ export type NavigationContainerProps = {
|
|
|
68
76
|
export type WidgetContainerProps = {
|
|
69
77
|
widget: React.ComponentType<any>;
|
|
70
78
|
theme?: Theme;
|
|
71
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Optional. Host services for this widget's app; defaults to a host's
|
|
81
|
+
* `KitsBindingProvider`, then `KitsHost`, never an enclosing container's.
|
|
82
|
+
* Keep it stable.
|
|
83
|
+
*/
|
|
84
|
+
binding?: KitsBinding;
|
|
72
85
|
localize: LocalizeProps;
|
|
73
86
|
};
|
|
74
87
|
|