@momo-kits/foundation 0.169.16-beta.5 → 0.169.16-beta.6
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 +182 -0
- package/Application/ModalScreen.tsx +2 -2
- package/Application/NavigationContainer.tsx +22 -18
- package/Application/Navigator.ts +7 -2
- package/Application/StackScreen.tracking.ts +22 -12
- package/Application/StackScreen.tsx +20 -32
- package/Application/WidgetContainer.tsx +83 -77
- package/Application/__tests__/KitsHost.test.ts +76 -0
- package/Application/hooks.ts +15 -0
- package/Application/index.ts +4 -0
- package/Application/types.binding.ts +134 -0
- package/Application/types.ts +14 -3
- package/package.json +1 -1
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,8 @@ 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';
|
|
19
|
+
import { KitsBindingContext } from './KitsBindingProvider';
|
|
18
20
|
|
|
19
21
|
const Stack = createStackNavigator();
|
|
20
22
|
|
|
@@ -34,12 +36,13 @@ const WidgetContent: React.FC = ({ route }: any) => {
|
|
|
34
36
|
|
|
35
37
|
/**
|
|
36
38
|
* Root of an embedded widget: its own navigator with the widget screen and the
|
|
37
|
-
* `Modal` route, sized to the widget's content height.
|
|
39
|
+
* `Modal` route, sized to the widget's content height. Without a `binding` it
|
|
40
|
+
* uses the enclosing container's.
|
|
38
41
|
*/
|
|
39
42
|
const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
40
43
|
widget,
|
|
41
44
|
theme = defaultTheme,
|
|
42
|
-
|
|
45
|
+
binding,
|
|
43
46
|
localize = new Localize({ vi: {}, en: {} }),
|
|
44
47
|
}) => {
|
|
45
48
|
const [height, setHeight] = useState<any>(undefined);
|
|
@@ -48,13 +51,14 @@ const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
|
48
51
|
const isReady = useRef(false);
|
|
49
52
|
const navigator = useRef(new Navigator(navigationRef, isReady, true));
|
|
50
53
|
const [currentContext, setCurrentContext] = useState({});
|
|
54
|
+
const { source, resolved } = useKitsBinding(binding);
|
|
51
55
|
|
|
52
56
|
useEffect(() => {
|
|
53
|
-
const sub =
|
|
57
|
+
const sub = resolved.observeConfig('font_scale_config', data => {
|
|
54
58
|
setCurrentContext((prev: any) => ({ ...prev, fontScale: data }));
|
|
55
59
|
});
|
|
56
|
-
return () => sub
|
|
57
|
-
}, [
|
|
60
|
+
return () => sub.remove();
|
|
61
|
+
}, [resolved]);
|
|
58
62
|
|
|
59
63
|
let headerBackground = context?.designConfig?.headerBar;
|
|
60
64
|
let headerGradient = theme.colors?.gradient;
|
|
@@ -66,7 +70,7 @@ const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
|
66
70
|
headerGradient = context?.designConfig?.headerGradient;
|
|
67
71
|
}
|
|
68
72
|
|
|
69
|
-
navigator.current.
|
|
73
|
+
navigator.current.binding = resolved;
|
|
70
74
|
|
|
71
75
|
/** Text for a localization key, or the current-language side of `{ vi, en }`. */
|
|
72
76
|
const translate = (data?: string | { vi: string; en: string }) => {
|
|
@@ -93,84 +97,86 @@ const WidgetContainer: React.FC<WidgetContainerProps> = ({
|
|
|
93
97
|
<View style={[styles.container, { height: height ?? '100%' }]}>
|
|
94
98
|
<SafeAreaProvider>
|
|
95
99
|
<MiniAppContext.Provider value={mergedContext}>
|
|
96
|
-
<
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
...theme.colors,
|
|
104
|
-
gradient: headerGradient,
|
|
105
|
-
},
|
|
106
|
-
assets: {
|
|
107
|
-
...theme.assets,
|
|
108
|
-
headerBackground,
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
translate,
|
|
112
|
-
}}
|
|
113
|
-
>
|
|
114
|
-
<NavigationIndependentTree>
|
|
115
|
-
<ReactNavigationContainer
|
|
116
|
-
theme={{
|
|
117
|
-
dark: false,
|
|
100
|
+
<KitsBindingContext.Provider value={source}>
|
|
101
|
+
<ApplicationContext.Provider
|
|
102
|
+
value={{
|
|
103
|
+
navigator: navigator.current,
|
|
104
|
+
theme: {
|
|
105
|
+
...theme,
|
|
106
|
+
font: fontFamily,
|
|
118
107
|
colors: {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
card: theme.colors.background.surface,
|
|
122
|
-
text: theme.colors.text.default,
|
|
123
|
-
border: theme.colors.border.default,
|
|
124
|
-
notification: theme.colors.error.primary,
|
|
108
|
+
...theme.colors,
|
|
109
|
+
gradient: headerGradient,
|
|
125
110
|
},
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
111
|
+
assets: {
|
|
112
|
+
...theme.assets,
|
|
113
|
+
headerBackground,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
translate,
|
|
117
|
+
}}
|
|
118
|
+
>
|
|
119
|
+
<NavigationIndependentTree>
|
|
120
|
+
<ReactNavigationContainer
|
|
121
|
+
theme={{
|
|
122
|
+
dark: false,
|
|
123
|
+
colors: {
|
|
124
|
+
primary: theme.colors.primary,
|
|
125
|
+
background: theme.colors.background.default,
|
|
126
|
+
card: theme.colors.background.surface,
|
|
127
|
+
text: theme.colors.text.default,
|
|
128
|
+
border: theme.colors.border.default,
|
|
129
|
+
notification: theme.colors.error.primary,
|
|
138
130
|
},
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
131
|
+
fonts: {
|
|
132
|
+
regular: {
|
|
133
|
+
fontFamily: navigationFontFamily,
|
|
134
|
+
fontWeight: '400',
|
|
135
|
+
},
|
|
136
|
+
medium: {
|
|
137
|
+
fontFamily: navigationFontFamily,
|
|
138
|
+
fontWeight: '400',
|
|
139
|
+
},
|
|
140
|
+
bold: {
|
|
141
|
+
fontFamily: navigationFontFamily,
|
|
142
|
+
fontWeight: '400',
|
|
143
|
+
},
|
|
144
|
+
heavy: {
|
|
145
|
+
fontFamily: navigationFontFamily,
|
|
146
|
+
fontWeight: '400',
|
|
147
|
+
},
|
|
142
148
|
},
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
isReady.current = true;
|
|
148
|
-
}}
|
|
149
|
-
>
|
|
150
|
-
<Stack.Navigator
|
|
151
|
-
initialRouteName="Stack"
|
|
152
|
-
screenOptions={{
|
|
153
|
-
headerMode: 'screen',
|
|
149
|
+
}}
|
|
150
|
+
ref={navigationRef}
|
|
151
|
+
onReady={() => {
|
|
152
|
+
isReady.current = true;
|
|
154
153
|
}}
|
|
155
154
|
>
|
|
156
|
-
<Stack.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
options={{
|
|
161
|
-
headerShown: false,
|
|
155
|
+
<Stack.Navigator
|
|
156
|
+
initialRouteName="Stack"
|
|
157
|
+
screenOptions={{
|
|
158
|
+
headerMode: 'screen',
|
|
162
159
|
}}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
160
|
+
>
|
|
161
|
+
<Stack.Screen
|
|
162
|
+
name="Stack"
|
|
163
|
+
component={WidgetContent}
|
|
164
|
+
initialParams={{ screen: widget, setHeight }}
|
|
165
|
+
options={{
|
|
166
|
+
headerShown: false,
|
|
167
|
+
}}
|
|
168
|
+
/>
|
|
169
|
+
<Stack.Screen
|
|
170
|
+
name="Modal"
|
|
171
|
+
component={ModalScreen}
|
|
172
|
+
options={getModalOptions()}
|
|
173
|
+
initialParams={{ screen: widget }}
|
|
174
|
+
/>
|
|
175
|
+
</Stack.Navigator>
|
|
176
|
+
</ReactNavigationContainer>
|
|
177
|
+
</NavigationIndependentTree>
|
|
178
|
+
</ApplicationContext.Provider>
|
|
179
|
+
</KitsBindingContext.Provider>
|
|
174
180
|
</MiniAppContext.Provider>
|
|
175
181
|
</SafeAreaProvider>
|
|
176
182
|
</View>
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { KitsHost, resolveKitsBinding } from '../KitsHost';
|
|
2
|
+
import type { KitsBinding } from '../types.binding';
|
|
3
|
+
|
|
4
|
+
/** Host binding that records each call as `[method, ...args]`. */
|
|
5
|
+
const createRecordingBinding = (calls: unknown[][]): KitsBinding => {
|
|
6
|
+
return {
|
|
7
|
+
trackEvent: (eventName, attributes) => {
|
|
8
|
+
calls.push(['host.trackEvent', eventName, attributes]);
|
|
9
|
+
},
|
|
10
|
+
vibrateLight: () => {
|
|
11
|
+
calls.push(['host.vibrateLight']);
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe('KitsHost', () => {
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
KitsHost.uninstall();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('does nothing and never throws before a host installs a binding', () => {
|
|
22
|
+
const binding = resolveKitsBinding();
|
|
23
|
+
expect(() => binding.dismiss('current')).not.toThrow();
|
|
24
|
+
expect(() => binding.observeConfig('key', () => {}).remove()).not.toThrow();
|
|
25
|
+
expect(() => binding.getLanguage(() => {}).cancel()).not.toThrow();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('falls back per method to the installed binding at call time', () => {
|
|
29
|
+
const calls: unknown[][] = [];
|
|
30
|
+
const binding = resolveKitsBinding({
|
|
31
|
+
trackEvent: eventName => {
|
|
32
|
+
calls.push(['own.trackEvent', eventName]);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
KitsHost.install(createRecordingBinding(calls));
|
|
36
|
+
|
|
37
|
+
binding.trackEvent('event', {});
|
|
38
|
+
binding.vibrateLight();
|
|
39
|
+
|
|
40
|
+
expect(calls).toEqual([['own.trackEvent', 'event'], ['host.vibrateLight']]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('uses the no-op when the binding is the installed one', () => {
|
|
44
|
+
const calls: unknown[][] = [];
|
|
45
|
+
const host = createRecordingBinding(calls);
|
|
46
|
+
KitsHost.install(host);
|
|
47
|
+
|
|
48
|
+
expect(() => resolveKitsBinding(host).dismiss('all')).not.toThrow();
|
|
49
|
+
expect(calls).toEqual([]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('never loops when a resolved binding is installed as the host', () => {
|
|
53
|
+
KitsHost.install(resolveKitsBinding());
|
|
54
|
+
|
|
55
|
+
expect(() => resolveKitsBinding().vibrateLight()).not.toThrow();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('drops a callback that arrives after cancel and cancels once', () => {
|
|
59
|
+
let answer: ((language: string) => void) | undefined;
|
|
60
|
+
const onLanguage = jest.fn();
|
|
61
|
+
const hostCancel = jest.fn();
|
|
62
|
+
const request = resolveKitsBinding({
|
|
63
|
+
getLanguage: callback => {
|
|
64
|
+
answer = callback;
|
|
65
|
+
return { cancel: hostCancel };
|
|
66
|
+
},
|
|
67
|
+
}).getLanguage(onLanguage);
|
|
68
|
+
|
|
69
|
+
request.cancel();
|
|
70
|
+
request.cancel();
|
|
71
|
+
answer?.('en');
|
|
72
|
+
|
|
73
|
+
expect(onLanguage).not.toHaveBeenCalled();
|
|
74
|
+
expect(hostCancel).toHaveBeenCalledTimes(1);
|
|
75
|
+
});
|
|
76
|
+
});
|
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 } from './types.binding';
|
|
16
19
|
|
|
17
20
|
const FOREGROUND_FLAG_DURATION = 100;
|
|
18
21
|
|
|
@@ -129,8 +132,20 @@ const useNormalizedSharedValue = (
|
|
|
129
132
|
return fallback;
|
|
130
133
|
};
|
|
131
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Binding of a container: its `binding` prop, else the nearest enclosing one,
|
|
137
|
+
* else `KitsHost`. `source` is what the container passes on to nested ones.
|
|
138
|
+
*/
|
|
139
|
+
const useKitsBinding = (binding?: KitsBinding) => {
|
|
140
|
+
const inherited = useContext(KitsBindingContext);
|
|
141
|
+
const source = binding ?? inherited;
|
|
142
|
+
const resolved = useMemo(() => resolveKitsBinding(source), [source]);
|
|
143
|
+
return { source, resolved };
|
|
144
|
+
};
|
|
145
|
+
|
|
132
146
|
export {
|
|
133
147
|
useComponentId,
|
|
148
|
+
useKitsBinding,
|
|
134
149
|
useAppState,
|
|
135
150
|
useNativeCanGoBack,
|
|
136
151
|
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 a no-op.
|
|
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
|
+
};
|