@applicaster/zapp-react-native-utils 16.0.0-alpha.1151561937 → 16.0.0-alpha.1180700101
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/actionsExecutor/ActionExecutorContext.tsx +26 -1
- package/actionsExecutor/actions/__tests__/dismissBottomSheet.test.ts +3 -0
- package/actionsExecutor/actions/__tests__/goBack.test.ts +62 -0
- package/actionsExecutor/actions/__tests__/goHome.test.ts +19 -0
- package/actionsExecutor/actions/__tests__/openBottomSheet.inlineItems.test.ts +113 -1
- package/actionsExecutor/actions/__tests__/presentScreen.test.ts +127 -0
- package/actionsExecutor/actions/dismissBottomSheet.ts +2 -1
- package/actionsExecutor/actions/goBack.ts +60 -0
- package/actionsExecutor/actions/goHome.ts +31 -0
- package/actionsExecutor/actions/index.ts +6 -0
- package/actionsExecutor/actions/openBottomSheet.ts +15 -10
- package/actionsExecutor/actions/presentScreen.ts +78 -0
- package/actionsExecutor/consts.ts +3 -0
- package/appUtils/playerManager/__tests__/playerNative.test.ts +38 -0
- package/appUtils/playerManager/playerNative.ts +5 -3
- package/configurationUtils/__tests__/manifestKeyParser.test.ts +21 -0
- package/configurationUtils/__tests__/modalBlocksParser.test.ts +153 -0
- package/configurationUtils/manifestKeyParser.ts +12 -1
- package/configurationUtils/modalBlocksParser.ts +118 -0
- package/entryActions/__tests__/buildEntryActions.test.ts +156 -0
- package/entryActions/aliasPresentation.ts +124 -0
- package/entryActions/buildEntryActions.ts +166 -0
- package/entryActions/index.ts +17 -0
- package/entryActions/types.ts +39 -0
- package/manifestUtils/_internals/index.js +8 -3
- package/manifestUtils/defaultManifestConfigurations/player.js +22 -1
- package/modalState/ModalOrchestrator.ts +21 -9
- package/modalState/__tests__/index.test.ts +30 -1
- package/modalState/index.ts +6 -1
- package/modalState/types.ts +28 -2
- package/modalState/useBottomSheetContent.ts +9 -7
- package/package.json +2 -2
- package/playerUtils/__tests__/resolvePlayerActions.test.ts +138 -0
- package/playerUtils/index.ts +17 -1
- package/playerUtils/resolvePlayerActions.ts +71 -0
- package/reactHooks/actions/index.ts +67 -2
- package/reactHooks/navigation/__tests__/index.test.tsx +48 -0
- package/reactHooks/navigation/__tests__/usePresentScreen.test.ts +154 -0
- package/reactHooks/navigation/index.ts +2 -0
- package/reactHooks/navigation/usePresentScreen.ts +157 -0
- package/reactHooks/navigation/useRoute.ts +14 -1
- package/uiActionsRegistrator/__tests__/resolveActionIdentifiers.test.ts +93 -0
- package/uiActionsRegistrator/__tests__/subscribe.test.ts +107 -0
- package/uiActionsRegistrator/__tests__/usableActionItems.test.ts +52 -0
- package/uiActionsRegistrator/index.ts +25 -203
- package/uiActionsRegistrator/registry.ts +335 -0
- package/uiActionsRegistrator/resolveActionIdentifiers.ts +100 -0
- package/uiActionsRegistrator/usableActionItems.ts +66 -0
- package/playerUtils/__tests__/getPlayerActionButtons.test.ts +0 -54
- package/playerUtils/getPlayerActionButtons.ts +0 -17
|
@@ -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
|
+
};
|
|
@@ -80,7 +80,20 @@ export const useRoute = (
|
|
|
80
80
|
// if path is hook grab screenData from screenData
|
|
81
81
|
|
|
82
82
|
if (isModalPathname(pathname)) {
|
|
83
|
-
|
|
83
|
+
if (!modalScreenData) {
|
|
84
|
+
return { screenData: {} as ZappEntry, pathname };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Mirror the pushed-screen shape ({ ...entry, targetScreen }) so a screen
|
|
88
|
+
// presented as a modal reads its entry the same way it would when
|
|
89
|
+
// navigated to, instead of only seeing the bare river.
|
|
90
|
+
const screenData = legacyScreenData(
|
|
91
|
+
{
|
|
92
|
+
screen: modalScreenData,
|
|
93
|
+
entry: modalState.props?.entry as NavigatorEntry,
|
|
94
|
+
},
|
|
95
|
+
plugins
|
|
96
|
+
);
|
|
84
97
|
|
|
85
98
|
return { screenData, pathname };
|
|
86
99
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseActionIdentifiers,
|
|
3
|
+
resolveActionIdentifiers,
|
|
4
|
+
} from "../resolveActionIdentifiers";
|
|
5
|
+
import { uiActionsRegistry } from "../registry";
|
|
6
|
+
|
|
7
|
+
const action = (identifier: string) =>
|
|
8
|
+
({ identifier, action: { invokeAction: jest.fn() } }) as any;
|
|
9
|
+
|
|
10
|
+
describe("parseActionIdentifiers", () => {
|
|
11
|
+
it("parses comma-separated identifiers", () => {
|
|
12
|
+
expect(parseActionIdentifiers(" a, b , ,c ")).toEqual(["a", "b", "c"]);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("parses identifier arrays", () => {
|
|
16
|
+
expect(parseActionIdentifiers([" a ", "", "b", " ", "c"])).toEqual([
|
|
17
|
+
"a",
|
|
18
|
+
"b",
|
|
19
|
+
"c",
|
|
20
|
+
]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("returns empty array for nil config", () => {
|
|
24
|
+
expect(parseActionIdentifiers(null)).toEqual([]);
|
|
25
|
+
expect(parseActionIdentifiers(undefined)).toEqual([]);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("resolveActionIdentifiers", () => {
|
|
30
|
+
const entry = { id: "entry-1" } as any;
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
jest
|
|
34
|
+
.spyOn(uiActionsRegistry, "getActions")
|
|
35
|
+
.mockImplementation((identifier) => [action(identifier)] as any);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
jest.restoreAllMocks();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("resolves through registry by default", () => {
|
|
43
|
+
const result = resolveActionIdentifiers({
|
|
44
|
+
identifiers: "favorites,share",
|
|
45
|
+
entry,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(result.map((item) => item.identifier)).toEqual([
|
|
49
|
+
"favorites",
|
|
50
|
+
"share",
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
expect(uiActionsRegistry.getActions).toHaveBeenCalledWith(
|
|
54
|
+
"favorites",
|
|
55
|
+
entry
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
expect(uiActionsRegistry.getActions).toHaveBeenCalledWith("share", entry);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("expands entry_actions through the caller, not the registry", () => {
|
|
62
|
+
const expanded = [action("entry_action_0"), action("entry_action_1")];
|
|
63
|
+
const expandEntryActions = jest.fn(() => expanded);
|
|
64
|
+
|
|
65
|
+
const unregister = uiActionsRegistry.registerAction("share", {
|
|
66
|
+
invokeAction: jest.fn(),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const result = resolveActionIdentifiers({
|
|
71
|
+
identifiers: "entry_actions,share",
|
|
72
|
+
entry,
|
|
73
|
+
expandEntryActions,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(expandEntryActions).toHaveBeenCalledWith(entry);
|
|
77
|
+
|
|
78
|
+
expect(result.map((item) => item.identifier)).toEqual([
|
|
79
|
+
"entry_action_0",
|
|
80
|
+
"entry_action_1",
|
|
81
|
+
"share",
|
|
82
|
+
]);
|
|
83
|
+
} finally {
|
|
84
|
+
unregister();
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("resolves nothing for entry_actions when the caller cannot expand it", () => {
|
|
89
|
+
expect(
|
|
90
|
+
resolveActionIdentifiers({ identifiers: "entry_actions", entry })
|
|
91
|
+
).toEqual([]);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { isActionAvailableFor, uiActionsRegistry } from "../registry";
|
|
2
|
+
|
|
3
|
+
const value = { invokeAction: jest.fn() };
|
|
4
|
+
|
|
5
|
+
describe("uiActionsRegistry.subscribe", () => {
|
|
6
|
+
it("fires on registration and on unregistration", () => {
|
|
7
|
+
const listener = jest.fn();
|
|
8
|
+
const unsubscribe = uiActionsRegistry.subscribe(listener);
|
|
9
|
+
|
|
10
|
+
const unregister = uiActionsRegistry.registerAction("late-action", value);
|
|
11
|
+
|
|
12
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
13
|
+
|
|
14
|
+
unregister();
|
|
15
|
+
|
|
16
|
+
expect(listener).toHaveBeenCalledTimes(2);
|
|
17
|
+
|
|
18
|
+
unsubscribe();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("lets a listener see the action it was told about", () => {
|
|
22
|
+
let seen: string[] = [];
|
|
23
|
+
|
|
24
|
+
const unsubscribe = uiActionsRegistry.subscribe(() => {
|
|
25
|
+
seen = uiActionsRegistry
|
|
26
|
+
.getActions("late-action", { id: "e1" } as any)
|
|
27
|
+
.map((item) => item.identifier);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const unregister = uiActionsRegistry.registerAction("late-action", value);
|
|
31
|
+
|
|
32
|
+
expect(seen).toEqual(["late-action"]);
|
|
33
|
+
|
|
34
|
+
unregister();
|
|
35
|
+
unsubscribe();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("stops calling a listener once unsubscribed", () => {
|
|
39
|
+
const listener = jest.fn();
|
|
40
|
+
|
|
41
|
+
uiActionsRegistry.subscribe(listener)();
|
|
42
|
+
|
|
43
|
+
uiActionsRegistry.registerAction("another-action", value)();
|
|
44
|
+
|
|
45
|
+
expect(listener).not.toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("survives a listener that unsubscribes itself while being notified", () => {
|
|
49
|
+
const other = jest.fn();
|
|
50
|
+
|
|
51
|
+
const unsubscribeOther = uiActionsRegistry.subscribe(other);
|
|
52
|
+
|
|
53
|
+
const unsubscribeSelf = uiActionsRegistry.subscribe(() => {
|
|
54
|
+
unsubscribeSelf();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
expect(() =>
|
|
58
|
+
uiActionsRegistry.registerAction("self-removing", value)()
|
|
59
|
+
).not.toThrow();
|
|
60
|
+
|
|
61
|
+
expect(other).toHaveBeenCalled();
|
|
62
|
+
|
|
63
|
+
unsubscribeOther();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("isActionAvailableFor", () => {
|
|
68
|
+
const entry = { id: "e1" } as any;
|
|
69
|
+
|
|
70
|
+
it("shows an action that states no opinion", () => {
|
|
71
|
+
expect(isActionAvailableFor({ invokeAction: jest.fn() }, entry)).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it.each([
|
|
75
|
+
["isSupportDownloads"],
|
|
76
|
+
["isActionSupported"],
|
|
77
|
+
["isActionAvailable"],
|
|
78
|
+
])("honours %s", (predicate) => {
|
|
79
|
+
expect(
|
|
80
|
+
isActionAvailableFor(
|
|
81
|
+
{ invokeAction: jest.fn(), [predicate]: () => false } as any,
|
|
82
|
+
entry
|
|
83
|
+
)
|
|
84
|
+
).toBe(false);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("asks only the first predicate the action defines", () => {
|
|
88
|
+
const isActionAvailable = jest.fn(() => true);
|
|
89
|
+
|
|
90
|
+
expect(
|
|
91
|
+
isActionAvailableFor(
|
|
92
|
+
{
|
|
93
|
+
invokeAction: jest.fn(),
|
|
94
|
+
isSupportDownloads: () => false,
|
|
95
|
+
isActionAvailable,
|
|
96
|
+
} as any,
|
|
97
|
+
entry
|
|
98
|
+
)
|
|
99
|
+
).toBe(false);
|
|
100
|
+
|
|
101
|
+
expect(isActionAvailable).not.toHaveBeenCalled();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("treats a missing action as unavailable-free", () => {
|
|
105
|
+
expect(isActionAvailableFor(undefined, entry)).toBe(true);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { usableActionItems } from "../usableActionItems";
|
|
2
|
+
|
|
3
|
+
const action = (identifier: string) =>
|
|
4
|
+
({ identifier, action: { invokeAction: jest.fn() } }) as any;
|
|
5
|
+
|
|
6
|
+
describe("usableActionItems", () => {
|
|
7
|
+
it("keeps a usable list untouched, in order", () => {
|
|
8
|
+
const base = [action("share"), action("favorites")];
|
|
9
|
+
|
|
10
|
+
expect(usableActionItems(base).map((item) => item.identifier)).toEqual([
|
|
11
|
+
"share",
|
|
12
|
+
"favorites",
|
|
13
|
+
]);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("keeps the first of a repeated identifier", () => {
|
|
17
|
+
const firstShare = action("share");
|
|
18
|
+
const secondShare = action("share");
|
|
19
|
+
|
|
20
|
+
const result = usableActionItems([
|
|
21
|
+
firstShare,
|
|
22
|
+
action("favorites"),
|
|
23
|
+
secondShare,
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
expect(result.map((item) => item.identifier)).toEqual([
|
|
27
|
+
"share",
|
|
28
|
+
"favorites",
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
expect(result[0]).toBe(firstShare);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("drops items that cannot be invoked or have no identifier", () => {
|
|
35
|
+
const result = usableActionItems([
|
|
36
|
+
action("share"),
|
|
37
|
+
{ identifier: "blank" } as any,
|
|
38
|
+
{ identifier: "no-invoke", action: {} } as any,
|
|
39
|
+
{ action: { invokeAction: jest.fn() } } as any,
|
|
40
|
+
action("entry_action_0"),
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
expect(result.map((item) => item.identifier)).toEqual([
|
|
44
|
+
"share",
|
|
45
|
+
"entry_action_0",
|
|
46
|
+
]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("tolerates a missing list", () => {
|
|
50
|
+
expect(usableActionItems(undefined as any)).toEqual([]);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -1,203 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
) => void;
|
|
27
|
-
isActionAvailable?: (item: ZappEntry | ZappFeed) => boolean;
|
|
28
|
-
addListener?: (
|
|
29
|
-
entryId: string,
|
|
30
|
-
listener: CellActionStateListenerFunction
|
|
31
|
-
) => CellActionStateRemoveListenerFunction;
|
|
32
|
-
/**
|
|
33
|
-
* Optional RX stream of the action state for a given entry. When not
|
|
34
|
-
* provided, `observeEntryState` derives one from `initialEntryState` +
|
|
35
|
-
* `addListener` (see below).
|
|
36
|
-
*/
|
|
37
|
-
observeEntryState?: (
|
|
38
|
-
entry: ZappEntry | ZappFeed
|
|
39
|
-
) => Observable<CellActionEntryState>;
|
|
40
|
-
[K: string]: any;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export type RegisteredAction = {
|
|
44
|
-
identifier: string; // Plugin identifier or synthetic name
|
|
45
|
-
action: RegisteredActionValue;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* A function that, given an optional context (currently just the `entry`),
|
|
50
|
-
* returns the list of actions it wants to contribute.
|
|
51
|
-
*
|
|
52
|
-
* Returning an array is what allows a single provider (plugin or otherwise) to
|
|
53
|
-
* expose more than one action, and to compute those actions based on the entry.
|
|
54
|
-
*/
|
|
55
|
-
export type UIActionProvider = (params: {
|
|
56
|
-
entry?: ZappEntry | ZappFeed;
|
|
57
|
-
}) => RegisteredAction[];
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Returns an RX `Observable` that emits the action state for `entry`.
|
|
61
|
-
*
|
|
62
|
-
* If the action already exposes its own `observeEntryState`, it is used as-is.
|
|
63
|
-
* Otherwise we build the stream from the legacy imperative API:
|
|
64
|
-
* - it immediately emits `initialEntryState(entry)` (if available), and
|
|
65
|
-
* - it subscribes to further changes via `addListener(entryId, listener)`,
|
|
66
|
-
* unsubscribing automatically when the observable is torn down.
|
|
67
|
-
*
|
|
68
|
-
* This lets every action - old or new - be consumed reactively through a single
|
|
69
|
-
* mechanism, without each consumer having to wire up `addListener` manually.
|
|
70
|
-
*/
|
|
71
|
-
export function observeEntryState(
|
|
72
|
-
action: RegisteredActionValue | undefined,
|
|
73
|
-
entry: ZappEntry | ZappFeed
|
|
74
|
-
): Observable<CellActionEntryState> {
|
|
75
|
-
if (typeof action?.observeEntryState === "function") {
|
|
76
|
-
return action.observeEntryState(entry);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return new Observable<CellActionEntryState>((subscriber) => {
|
|
80
|
-
const initialState = action?.initialEntryState?.(entry);
|
|
81
|
-
|
|
82
|
-
if (initialState !== undefined) {
|
|
83
|
-
subscriber.next(initialState);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const removeListener = action?.addListener?.(
|
|
87
|
-
String((entry as ZappEntry)?.id),
|
|
88
|
-
(state: CellActionEntryState) => subscriber.next(state)
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
return () => removeListener?.();
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
class UIActionsRegistry {
|
|
96
|
-
private registeredActions: Record<string, UIActionProvider> = {};
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Register a dynamic action provider under a unique `name`.
|
|
100
|
-
* The provider can return one or more actions and may use the passed `entry`.
|
|
101
|
-
* Returns an unregister function.
|
|
102
|
-
*/
|
|
103
|
-
registerActionProvider(
|
|
104
|
-
name: string,
|
|
105
|
-
actionProvider: UIActionProvider
|
|
106
|
-
): () => void {
|
|
107
|
-
const isOverride = Boolean(this.registeredActions[name]);
|
|
108
|
-
|
|
109
|
-
if (isOverride) {
|
|
110
|
-
log_warning(
|
|
111
|
-
`registerActionProvider: overriding existing provider for "${name}"`,
|
|
112
|
-
{ name }
|
|
113
|
-
);
|
|
114
|
-
} else {
|
|
115
|
-
log_debug(`registerActionProvider: registered "${name}"`, { name });
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
this.registeredActions[name] = actionProvider;
|
|
119
|
-
|
|
120
|
-
return () => {
|
|
121
|
-
// Only remove if it's still the same provider to avoid races where a
|
|
122
|
-
// newer registration replaced this one.
|
|
123
|
-
if (this.registeredActions[name] === actionProvider) {
|
|
124
|
-
delete this.registeredActions[name];
|
|
125
|
-
log_debug(`registerActionProvider: unregistered "${name}"`, { name });
|
|
126
|
-
} else {
|
|
127
|
-
log_debug(
|
|
128
|
-
`registerActionProvider: skipped unregister for "${name}" – newer provider is active`,
|
|
129
|
-
{ name }
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Convenience helper to register a single, static action from anywhere in the
|
|
137
|
-
* app (it does not need to be a plugin). Returns an unregister function.
|
|
138
|
-
*/
|
|
139
|
-
registerAction(
|
|
140
|
-
identifier: string,
|
|
141
|
-
action: RegisteredActionValue
|
|
142
|
-
): () => void {
|
|
143
|
-
log_debug(`registerAction: registering single action "${identifier}"`, {
|
|
144
|
-
identifier,
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
return this.registerActionProvider(identifier, () => [
|
|
148
|
-
{ identifier, action },
|
|
149
|
-
]);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
getEntryActions(entry: ZappEntry): RegisteredAction[] {
|
|
153
|
-
const results = Object.keys(this.registeredActions)
|
|
154
|
-
.map((key) => this.registeredActions[key]({ entry }))
|
|
155
|
-
.flatMap((a) => a);
|
|
156
|
-
|
|
157
|
-
log_verbose(
|
|
158
|
-
`getEntryActions: resolved ${results.length} action(s) for entry "${entry?.id}"`,
|
|
159
|
-
{ entryId: entry?.id, identifiers: results.map((r) => r.identifier) }
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
return results;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
getActions(name: string, entry?: ZappEntry | ZappFeed): RegisteredAction[] {
|
|
166
|
-
if (!this.registeredActions[name]) {
|
|
167
|
-
log_warning(`getActions: no provider found for "${name}"`, { name });
|
|
168
|
-
|
|
169
|
-
return [];
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const results = this.registeredActions[name]({ entry }) ?? [];
|
|
173
|
-
|
|
174
|
-
log_verbose(
|
|
175
|
-
`getActions: resolved ${results.length} action(s) for "${name}"`,
|
|
176
|
-
{
|
|
177
|
-
name,
|
|
178
|
-
entryId: (entry as ZappEntry)?.id,
|
|
179
|
-
identifiers: results.map((r) => r.identifier),
|
|
180
|
-
}
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
return results;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Returns the runtime value of the first action registered under `name`, or
|
|
188
|
-
* `undefined`. This mirrors the shape returned by the legacy `useActions`
|
|
189
|
-
* hook, easing migration of single-action consumers.
|
|
190
|
-
*/
|
|
191
|
-
getAction(
|
|
192
|
-
name: string,
|
|
193
|
-
entry?: ZappEntry | ZappFeed
|
|
194
|
-
): RegisteredActionValue | undefined {
|
|
195
|
-
return this.getActions(name, entry)[0]?.action;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
hasAction(name: string): boolean {
|
|
199
|
-
return Boolean(this.registeredActions[name]);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export const uiActionsRegistry = new UIActionsRegistry();
|
|
1
|
+
export {
|
|
2
|
+
canRenderActionIcon,
|
|
3
|
+
resolveActionAsset,
|
|
4
|
+
isActionAvailableFor,
|
|
5
|
+
observeEntryState,
|
|
6
|
+
uiActionsRegistry,
|
|
7
|
+
} from "./registry";
|
|
8
|
+
|
|
9
|
+
export type {
|
|
10
|
+
RegisteredAction,
|
|
11
|
+
RegisteredActionSource,
|
|
12
|
+
RegisteredActionValue,
|
|
13
|
+
RegistryChangeListener,
|
|
14
|
+
UIActionProvider,
|
|
15
|
+
} from "./registry";
|
|
16
|
+
|
|
17
|
+
export { usableActionItems } from "./usableActionItems";
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
ENTRY_ACTIONS_IDENTIFIER,
|
|
21
|
+
parseActionIdentifiers,
|
|
22
|
+
resolveActionIdentifiers,
|
|
23
|
+
} from "./resolveActionIdentifiers";
|
|
24
|
+
|
|
25
|
+
export type { ResolveActionIdentifiersParams } from "./resolveActionIdentifiers";
|