@applicaster/zapp-react-native-utils 16.0.0-alpha.4004661200 → 16.0.0-alpha.4065452341

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.
Files changed (35) hide show
  1. package/actionsExecutor/ActionExecutorContext.tsx +10 -1
  2. package/actionsExecutor/actions/__tests__/dismissBottomSheet.test.ts +3 -0
  3. package/actionsExecutor/actions/__tests__/openBottomSheet.inlineItems.test.ts +113 -1
  4. package/actionsExecutor/actions/__tests__/presentScreen.test.ts +127 -0
  5. package/actionsExecutor/actions/__tests__/showToast.test.ts +2 -2
  6. package/actionsExecutor/actions/dismissBottomSheet.ts +2 -1
  7. package/actionsExecutor/actions/index.ts +2 -0
  8. package/actionsExecutor/actions/openBottomSheet.ts +20 -11
  9. package/actionsExecutor/actions/presentScreen.ts +78 -0
  10. package/actionsExecutor/actions/showToast.ts +2 -2
  11. package/actionsExecutor/consts.ts +1 -0
  12. package/configurationUtils/__tests__/manifestKeyParser.test.ts +21 -0
  13. package/configurationUtils/__tests__/modalBlocksParser.test.ts +153 -0
  14. package/configurationUtils/manifestKeyParser.ts +12 -1
  15. package/configurationUtils/modalBlocksParser.ts +118 -0
  16. package/entryActions/__tests__/buildEntryActions.test.ts +39 -0
  17. package/entryActions/buildEntryActions.ts +1 -0
  18. package/manifestUtils/defaultManifestConfigurations/generalContent.js +6 -0
  19. package/modalState/ModalOrchestrator.ts +21 -9
  20. package/modalState/RemoteEditableCollection.ts +9 -1
  21. package/modalState/__tests__/RemoteEditableCollection.test.ts +90 -1
  22. package/modalState/__tests__/index.test.ts +30 -1
  23. package/modalState/__tests__/useModalStoreState.test.ts +142 -0
  24. package/modalState/index.ts +6 -1
  25. package/modalState/store.ts +9 -2
  26. package/modalState/types.ts +29 -2
  27. package/modalState/useBottomSheetContent.ts +24 -17
  28. package/package.json +2 -2
  29. package/reactHooks/cell-click/__tests__/index.test.js +64 -0
  30. package/reactHooks/cell-click/index.ts +21 -14
  31. package/reactHooks/navigation/__tests__/index.test.tsx +176 -1
  32. package/reactHooks/navigation/__tests__/usePresentScreen.test.ts +154 -0
  33. package/reactHooks/navigation/index.ts +2 -0
  34. package/reactHooks/navigation/usePresentScreen.ts +157 -0
  35. package/reactHooks/navigation/useRoute.ts +22 -5
@@ -0,0 +1,154 @@
1
+ import { modalStore } from "../../../modalState";
2
+ import { isWeb } from "../../../reactUtils";
3
+ import { presentScreen } from "../usePresentScreen";
4
+
5
+ jest.mock("../../../reactUtils", () => ({
6
+ ...jest.requireActual("../../../reactUtils"),
7
+ isWeb: jest.fn(() => false),
8
+ }));
9
+
10
+ const river = { id: "river-parent-lock", type: "parent_lock" } as any;
11
+
12
+ const rivers = { "river-parent-lock": river };
13
+
14
+ const contentTypes = { parent_lock: { screen_id: "river-parent-lock" } };
15
+
16
+ const present = (args = {}) =>
17
+ presentScreen({ typeMapping: "parent_lock", rivers, contentTypes, ...args });
18
+
19
+ const modalState = () => modalStore.getState().modalState;
20
+
21
+ const screenCallback = () => modalState().props?.resultCallback as hookCallback;
22
+
23
+ describe("presentScreen", () => {
24
+ beforeEach(() => {
25
+ jest.clearAllMocks();
26
+ (isWeb as jest.Mock).mockReturnValue(false);
27
+ modalStore.getState().dismissModal();
28
+ });
29
+
30
+ it("opens a modal with the river resolved from typeMapping", () => {
31
+ void present();
32
+
33
+ expect(modalState().visible).toBe(true);
34
+ expect(modalState().screen).toBe(river);
35
+ });
36
+
37
+ it("resolves with success when the presented screen reports success", async () => {
38
+ const result = present();
39
+
40
+ screenCallback()({ success: true, payload: { verified: true } });
41
+
42
+ await expect(result).resolves.toMatchObject({
43
+ success: true,
44
+ cancelled: false,
45
+ payload: { verified: true },
46
+ });
47
+ });
48
+
49
+ it("dismisses the modal once the presented screen reports a result", async () => {
50
+ const result = present();
51
+
52
+ screenCallback()({ success: true, payload: {} });
53
+ await result;
54
+
55
+ expect(modalState().visible).toBe(false);
56
+ });
57
+
58
+ it("resolves as cancelled when the modal is dismissed without a result", async () => {
59
+ const result = present();
60
+
61
+ // What the hardware back button does: RN Modal's onRequestClose resets modal state.
62
+ modalStore.getState().dismissModal();
63
+
64
+ await expect(result).resolves.toMatchObject({
65
+ success: false,
66
+ cancelled: true,
67
+ });
68
+ });
69
+
70
+ it("resolves as cancelled when the presented screen reports cancellation", async () => {
71
+ const result = present();
72
+
73
+ screenCallback()({ success: false, payload: {}, cancelled: true });
74
+
75
+ await expect(result).resolves.toMatchObject({
76
+ success: false,
77
+ cancelled: true,
78
+ });
79
+ });
80
+
81
+ it("resolves as cancelled when another modal replaces the presented screen", async () => {
82
+ const result = present();
83
+
84
+ modalStore.getState().openModal({ item: { id: "other-modal" } as any });
85
+
86
+ await expect(result).resolves.toMatchObject({ cancelled: true });
87
+ });
88
+
89
+ it("leaves the modal that replaced the presented screen open", async () => {
90
+ const result = present();
91
+ const other = { id: "other-modal" } as any;
92
+
93
+ modalStore.getState().openModal({ item: other });
94
+ await result;
95
+
96
+ expect(modalState().screen).toBe(other);
97
+ expect(modalState().visible).toBe(true);
98
+ });
99
+
100
+ it("ignores a result reported after the modal was already cancelled", async () => {
101
+ const result = present();
102
+ const callback = screenCallback();
103
+
104
+ modalStore.getState().dismissModal();
105
+ callback({ success: true, payload: {} });
106
+
107
+ await expect(result).resolves.toMatchObject({
108
+ success: false,
109
+ cancelled: true,
110
+ });
111
+ });
112
+
113
+ it("hands the entry to the ModalProvider so it reaches ScreenDataContext", () => {
114
+ const entry = { id: "entry-1", type: { value: "video" } } as any;
115
+
116
+ void present({ entry });
117
+
118
+ expect(modalState().props?.entry).toBe(entry);
119
+ });
120
+
121
+ it("omits the entry when the screen is presented without one", () => {
122
+ void present();
123
+
124
+ expect(modalState().props).not.toHaveProperty("entry");
125
+ });
126
+
127
+ it("fails without opening a modal when typeMapping has no content type", async () => {
128
+ const result = await present({ typeMapping: "unmapped" });
129
+
130
+ expect(result).toMatchObject({ success: false, cancelled: false });
131
+ expect(result.error).toEqual(expect.any(String));
132
+ expect(modalState().visible).toBe(false);
133
+ });
134
+
135
+ it("fails without opening a modal when the content type resolves to a missing river", async () => {
136
+ const result = await present({
137
+ contentTypes: { parent_lock: { screen_id: "gone" } },
138
+ });
139
+
140
+ expect(result).toMatchObject({ success: false, cancelled: false });
141
+ expect(result.error).toEqual(expect.any(String));
142
+ expect(modalState().visible).toBe(false);
143
+ });
144
+
145
+ it("fails without opening a modal on web, where no modal host exists", async () => {
146
+ (isWeb as jest.Mock).mockReturnValue(true);
147
+
148
+ const result = await present();
149
+
150
+ expect(result).toMatchObject({ success: false, cancelled: false });
151
+ expect(result.error).toEqual(expect.any(String));
152
+ expect(modalState().visible).toBe(false);
153
+ });
154
+ });
@@ -22,6 +22,8 @@ import { useLayoutVersion } from "../layout";
22
22
 
23
23
  export { useNavigation } from "./useNavigation";
24
24
 
25
+ export { usePresentScreen, presentScreen } from "./usePresentScreen";
26
+
25
27
  export { useRoute } from "./useRoute";
26
28
 
27
29
  export { usePathname } from "./usePathname";
@@ -0,0 +1,157 @@
1
+ /// <reference types="@applicaster/applicaster-types" />
2
+ import { useCallback } from "react";
3
+
4
+ import { useContentTypes } from "@applicaster/zapp-react-native-redux/hooks";
5
+
6
+ import { modalStore } from "../../modalState";
7
+ import { isWeb } from "../../reactUtils";
8
+ import { createLogger } from "../../logger";
9
+ import { useRivers } from "../state";
10
+
11
+ const { log_error, log_info } = createLogger({
12
+ subsystem: "usePresentScreen",
13
+ category: "General",
14
+ });
15
+
16
+ export interface PresentScreenArgs {
17
+ /** The screen type to present (from content types mapping) */
18
+ typeMapping: string;
19
+
20
+ /** Entry to present the screen with. Omit to present the screen on its own. */
21
+ entry?: ZappEntry;
22
+ }
23
+
24
+ export interface PresentScreenResult {
25
+ /** The presented screen completed successfully */
26
+ success: boolean;
27
+
28
+ /** The presented screen was dismissed without completing (e.g. the back button) */
29
+ cancelled: boolean;
30
+
31
+ /** Set when the screen could not be presented at all */
32
+ error?: string;
33
+
34
+ /** Whatever the presented screen reported back */
35
+ payload?: unknown;
36
+ }
37
+
38
+ export type PresentScreenFn = (
39
+ args: PresentScreenArgs
40
+ ) => Promise<PresentScreenResult>;
41
+
42
+ interface Dependencies {
43
+ rivers: Record<string, ZappRiver>;
44
+ contentTypes: ZappContentTypes;
45
+ }
46
+
47
+ const failure = (error: string): Promise<PresentScreenResult> => {
48
+ log_error(`presentScreen: ${error}`);
49
+
50
+ return Promise.resolve({ success: false, cancelled: false, error });
51
+ };
52
+
53
+ /**
54
+ * Presents a screen as a modal and resolves once that screen reports a result
55
+ * or is dismissed.
56
+ *
57
+ * The screen is rendered by the ModalProvider through the regular ScreenResolver,
58
+ * so it receives the `resultCallback` we pass here as its `callback` prop — the
59
+ * same channel hook screens already use to report success and cancellation.
60
+ */
61
+ export const presentScreen = ({
62
+ typeMapping,
63
+ entry,
64
+ rivers,
65
+ contentTypes,
66
+ }: PresentScreenArgs & Dependencies): Promise<PresentScreenResult> => {
67
+ if (!typeMapping) {
68
+ return failure("typeMapping option is missing");
69
+ }
70
+
71
+ // The web ModalProvider renders nothing, so no result could ever arrive and
72
+ // awaiting one would stall the caller indefinitely.
73
+ if (isWeb()) {
74
+ return failure("screens can not be presented as modals on web");
75
+ }
76
+
77
+ const screenId = contentTypes?.[typeMapping]?.screen_id;
78
+
79
+ if (!screenId) {
80
+ return failure(`can not resolve screen type mapping: ${typeMapping}`);
81
+ }
82
+
83
+ const river = rivers?.[screenId];
84
+
85
+ if (!river) {
86
+ return failure(`can not resolve river for screen id: ${screenId}`);
87
+ }
88
+
89
+ return new Promise<PresentScreenResult>((resolve) => {
90
+ let settled = false;
91
+ let unsubscribe: () => void = () => {};
92
+
93
+ const settle = (result: PresentScreenResult) => {
94
+ if (settled) {
95
+ return;
96
+ }
97
+
98
+ settled = true;
99
+ unsubscribe();
100
+ resolve(result);
101
+ };
102
+
103
+ const resultCallback = ({
104
+ success,
105
+ cancelled,
106
+ payload,
107
+ }: hookCallbackArgs) =>
108
+ settle({ success: !!success, cancelled: !!cancelled, payload });
109
+
110
+ const props: Record<string, unknown> = { resultCallback };
111
+
112
+ if (entry) {
113
+ // The ModalProvider turns this into the screen's ScreenDataContext and
114
+ // screenData, so the presented screen reads the entry exactly as it
115
+ // would when navigated to.
116
+ props.entry = entry;
117
+ }
118
+
119
+ log_info(
120
+ `presentScreen: presenting screen type: ${typeMapping}${
121
+ entry ? ` with entry id: ${entry.id}` : ""
122
+ }`
123
+ );
124
+
125
+ modalStore.getState().openModal({ item: river, props });
126
+
127
+ // Covers every dismissal we do not initiate ourselves: the hardware back
128
+ // button (via the Modal's onRequestClose), a swipe dismiss, another modal
129
+ // taking our slot, or dismissModal() called from anywhere else.
130
+ unsubscribe = modalStore.subscribe(({ modalState }) => {
131
+ if (!modalState.visible || modalState.screen !== river) {
132
+ settle({ success: false, cancelled: true });
133
+ }
134
+ });
135
+ }).then((result) => {
136
+ // Only take down our own modal — by now something else may already own the slot.
137
+ if (modalStore.getState().modalState.screen === river) {
138
+ modalStore.getState().dismissModal();
139
+ }
140
+
141
+ return result;
142
+ });
143
+ };
144
+
145
+ /**
146
+ * @returns a function presenting a screen as a modal, resolving with its result.
147
+ */
148
+ export const usePresentScreen = (): PresentScreenFn => {
149
+ const rivers = useRivers();
150
+ const contentTypes = useContentTypes();
151
+
152
+ return useCallback(
153
+ (args: PresentScreenArgs) =>
154
+ presentScreen({ ...args, rivers, contentTypes }),
155
+ [rivers, contentTypes]
156
+ );
157
+ };
@@ -50,14 +50,18 @@ export const useRoute = (
50
50
  const rivers = useRivers();
51
51
  const contentTypes = useContentTypes();
52
52
 
53
- const modalState = useModalStoreState();
54
-
55
- const modalScreenData = modalState.screen;
56
-
53
+ // useRoute runs in every cell, so both modal stores are subscribed to only on
54
+ // the routes that actually read them. Off those routes the selectors return a
55
+ // stable reference, which keeps an unrelated modal opening or closing from
56
+ // re-rendering the whole tree.
57
57
  const hookModalScreenData = useHookModalScreenData(
58
58
  !isHookModalPathname(pathname)
59
59
  );
60
60
 
61
+ const modalState = useModalStoreState(!isModalPathname(pathname));
62
+
63
+ const modalScreenData = modalState.screen;
64
+
61
65
  const videoModalScreenData =
62
66
  navigator?.videoModalState?.item &&
63
67
  legacyScreenData(
@@ -80,7 +84,20 @@ export const useRoute = (
80
84
  // if path is hook grab screenData from screenData
81
85
 
82
86
  if (isModalPathname(pathname)) {
83
- const screenData = modalScreenData ?? ({} as ZappEntry);
87
+ if (!modalScreenData) {
88
+ return { screenData: {} as ZappEntry, pathname };
89
+ }
90
+
91
+ // Mirror the pushed-screen shape ({ ...entry, targetScreen }) so a screen
92
+ // presented as a modal reads its entry the same way it would when
93
+ // navigated to, instead of only seeing the bare river.
94
+ const screenData = legacyScreenData(
95
+ {
96
+ screen: modalScreenData,
97
+ entry: modalState.props?.entry as NavigatorEntry,
98
+ },
99
+ plugins
100
+ );
84
101
 
85
102
  return { screenData, pathname };
86
103
  }