@applicaster/zapp-react-native-utils 15.1.6-alpha.2015591222 → 15.1.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.
Files changed (32) hide show
  1. package/actionsExecutor/ActionExecutorContext.tsx +0 -4
  2. package/actionsExecutor/actions/__tests__/showToast.test.ts +25 -3
  3. package/actionsExecutor/actions/index.ts +0 -4
  4. package/actionsExecutor/actions/refreshComponent.ts +29 -10
  5. package/actionsExecutor/actions/sendCloudEvent.ts +0 -8
  6. package/actionsExecutor/actions/showToast.ts +7 -5
  7. package/actionsExecutor/feedDecorator.ts +6 -6
  8. package/actionsExecutor/types.ts +3 -5
  9. package/modalState/index.ts +83 -30
  10. package/package.json +2 -2
  11. package/reactHooks/actions/index.ts +51 -1
  12. package/reactHooks/layout/index.ts +1 -1
  13. package/uiActionsRegistrator/index.ts +203 -0
  14. package/actionsExecutor/actions/__tests__/dismissBottomSheet.test.ts +0 -22
  15. package/actionsExecutor/actions/__tests__/loadItemsFromSource.test.ts +0 -88
  16. package/actionsExecutor/actions/__tests__/openBottomSheet.customContent.test.ts +0 -76
  17. package/actionsExecutor/actions/dismissBottomSheet.ts +0 -22
  18. package/actionsExecutor/actions/openBottomSheet.ts +0 -318
  19. package/modalState/ContentViewModel.ts +0 -111
  20. package/modalState/EditableCollection.ts +0 -17
  21. package/modalState/EditableCollectionRegistry.ts +0 -51
  22. package/modalState/ModalOrchestrator.ts +0 -198
  23. package/modalState/RemoteEditableCollection.ts +0 -189
  24. package/modalState/__tests__/ContentViewModel.editable.test.ts +0 -69
  25. package/modalState/__tests__/ContentViewModel.test.ts +0 -165
  26. package/modalState/__tests__/EditableCollectionRegistry.test.ts +0 -112
  27. package/modalState/__tests__/ModalOrchestrator.phase3.test.ts +0 -28
  28. package/modalState/__tests__/RemoteEditableCollection.test.ts +0 -259
  29. package/modalState/__tests__/useBottomSheetContent.test.ts +0 -372
  30. package/modalState/store.ts +0 -102
  31. package/modalState/types.ts +0 -110
  32. package/modalState/useBottomSheetContent.ts +0 -98
@@ -19,11 +19,9 @@ import {
19
19
  appRestartAction,
20
20
  confirmDialogAction,
21
21
  createNavigateToScreenAction,
22
- dismissBottomSheetAction,
23
22
  localStorageRemoveAction,
24
23
  localStorageSetAction,
25
24
  localStorageToggleFlagAction,
26
- openBottomSheetAction,
27
25
  refreshComponentAction,
28
26
  screenSetVariableAction,
29
27
  screenToggleFlagAction,
@@ -92,8 +90,6 @@ const prepareDefaultActions = (actionExecutor) => {
92
90
 
93
91
  actionExecutor.registerAction("screenSetVariable", screenSetVariableAction);
94
92
  actionExecutor.registerAction("screenToggleFlag", screenToggleFlagAction);
95
- actionExecutor.registerAction("openBottomSheet", openBottomSheetAction);
96
- actionExecutor.registerAction("dismissBottomSheet", dismissBottomSheetAction);
97
93
  actionExecutor.registerAction("showToast", showToastAction);
98
94
  };
99
95
 
@@ -11,14 +11,13 @@ describe("showToastAction", () => {
11
11
  jest.clearAllMocks();
12
12
  });
13
13
 
14
- it("calls postEvent with showToast payload and returns ActionResult.Success", async () => {
14
+ it("calls postEvent with the provided options and returns Success", async () => {
15
15
  const action = {
16
16
  type: "showToast",
17
17
  options: {
18
18
  id: "toast-1",
19
19
  message: "Added to queue",
20
20
  extraMessage: "Extra info",
21
- timeout: 3000,
22
21
  },
23
22
  };
24
23
 
@@ -32,13 +31,36 @@ describe("showToastAction", () => {
32
31
  message: "Added to queue",
33
32
  extraMessage: "Extra info",
34
33
  style: undefined,
35
- timeout: 3000,
34
+ timeout: undefined,
36
35
  },
37
36
  ]);
38
37
 
39
38
  expect(result).toBe(ActionResult.Success);
40
39
  });
41
40
 
41
+ it("preserves an explicit timeout and style override", async () => {
42
+ const action = {
43
+ type: "showToast",
44
+ options: {
45
+ message: "Playlist created",
46
+ timeout: 3000,
47
+ style: { backgroundColor: "#00FF00" },
48
+ },
49
+ };
50
+
51
+ await showToastAction(action);
52
+
53
+ expect(postEvent).toHaveBeenCalledWith("showToast", [
54
+ {
55
+ id: undefined,
56
+ message: "Playlist created",
57
+ extraMessage: undefined,
58
+ style: { backgroundColor: "#00FF00" },
59
+ timeout: 3000,
60
+ },
61
+ ]);
62
+ });
63
+
42
64
  it("handles missing or empty options without throwing", async () => {
43
65
  const action = {
44
66
  type: "showToast",
@@ -28,7 +28,3 @@ export { screenToggleFlagAction } from "./screenToggleFlag";
28
28
  export { createNavigateToScreenAction } from "./navigateToScreen";
29
29
 
30
30
  export { showToastAction } from "./showToast";
31
-
32
- export { openBottomSheetAction } from "./openBottomSheet";
33
-
34
- export { dismissBottomSheetAction } from "./dismissBottomSheet";
@@ -14,8 +14,10 @@ const { log_info } = createLogger({
14
14
  category: "General",
15
15
  });
16
16
 
17
- /** Prefer component data.source, otherwise direct UIComponentContext parent. */
18
- function getComponentFeedData(component) {
17
+ /**
18
+ * Helper function to prefer component data.source, otherwise direct UIComponentContext parent.
19
+ */
20
+ function getComponentFeedData(component: any) {
19
21
  if (component?.data?.source) {
20
22
  return component.data;
21
23
  }
@@ -27,6 +29,25 @@ function getComponentFeedData(component) {
27
29
  return null;
28
30
  }
29
31
 
32
+ /**
33
+ * Helper function to find the parent component in a component tree.
34
+ */
35
+ function findParentComponent(
36
+ childId: string,
37
+ parent: ZappRiver | ZappUIComponent
38
+ ): ZappRiver | ZappUIComponent | null {
39
+ for (const child of parent?.ui_components || []) {
40
+ if (child.id === childId) return parent;
41
+
42
+ if (child.ui_components) {
43
+ const found = findParentComponent(childId, child);
44
+ if (found) return found;
45
+ }
46
+ }
47
+
48
+ return null;
49
+ }
50
+
30
51
  /**
31
52
  * Refreshes a component's data by reloading its data source.
32
53
  * Supports URL inflation with context variables.
@@ -37,7 +58,11 @@ export const refreshComponentAction: ActionHandler = async (
37
58
  ): Promise<ActionResult> => {
38
59
  const dispatch = appStore.getDispatch();
39
60
 
40
- const componentData = getComponentFeedData(context?.component);
61
+ const componentData =
62
+ getComponentFeedData(context?.component) ||
63
+ (context?.component?.id && context?.screenData
64
+ ? findParentComponent(context.component.id, context.screenData)?.data
65
+ : null);
41
66
 
42
67
  const source = componentData?.source;
43
68
  const mapping = componentData?.mapping;
@@ -49,7 +74,7 @@ export const refreshComponentAction: ActionHandler = async (
49
74
  getInflatedDataSourceUrl({
50
75
  source,
51
76
  contexts: {
52
- entry: context?.screenEntry,
77
+ entry: context?.entryContext || context?.screenEntry,
53
78
  screen: context?.screenData,
54
79
  search: getSearchContext(null, mapping),
55
80
  },
@@ -57,12 +82,6 @@ export const refreshComponentAction: ActionHandler = async (
57
82
  }) || source;
58
83
  }
59
84
 
60
- if (!dataSource) {
61
- log_info("handleAction: refreshComponent skipped: no dataSource found");
62
-
63
- return ActionResult.Success;
64
- }
65
-
66
85
  log_info(`handleAction: refreshComponent for dataSource:${dataSource}`, {
67
86
  source,
68
87
  inflatedUrl: dataSource,
@@ -62,18 +62,10 @@ export const sendCloudEventAction: ActionHandler<
62
62
  subject: options.subject || String((entry as ZappEntry)?.id || ""),
63
63
  });
64
64
 
65
- console.log(
66
- "[sendCloudEvent] sending event to URL:",
67
- options.url,
68
- cloudEvent
69
- );
70
-
71
65
  log_info("handleAction: sendCloudEvent event", { cloudEvent });
72
66
 
73
67
  const { error, code } = await sendCloudEvent(cloudEvent, options.url);
74
68
 
75
- console.log("[sendCloudEvent] result code:", code, "error:", error);
76
-
77
69
  if (error) {
78
70
  log_error("sendCloudEvent: error sending cloud event", { error });
79
71
 
@@ -17,13 +17,15 @@ export interface ShowToastActionOptions {
17
17
  export const showToastAction: ActionHandler<ShowToastActionOptions> = async (
18
18
  action
19
19
  ): Promise<ActionResult> => {
20
+ const options = action?.options;
21
+
20
22
  postEvent("showToast", [
21
23
  {
22
- id: action?.options?.id,
23
- message: action?.options?.message,
24
- extraMessage: action?.options?.extraMessage,
25
- style: action?.options?.style,
26
- timeout: action?.options?.timeout,
24
+ id: options?.id,
25
+ message: options?.message,
26
+ extraMessage: options?.extraMessage,
27
+ style: options?.style,
28
+ timeout: options?.timeout,
27
29
  },
28
30
  ]);
29
31
 
@@ -27,7 +27,7 @@ function makeMultiSelect(feed: ZappFeed, key, decoratedFeed) {
27
27
  );
28
28
 
29
29
  const behavior = {
30
- ...feed.extensions?.behavior,
30
+ ...feed.extensions?.["behavior"],
31
31
  select_mode: "multi",
32
32
  current_selection: `@{${scope}/${key}}`,
33
33
  };
@@ -75,7 +75,7 @@ function makeSingleSelect(feed: ZappFeed, key, decoratedFeed) {
75
75
  );
76
76
 
77
77
  const behavior = {
78
- ...feed.extensions?.behavior,
78
+ ...feed.extensions?.["behavior"],
79
79
  select_mode: "single",
80
80
  current_selection: `@{${scope}/${key}}`,
81
81
  };
@@ -141,11 +141,11 @@ function makeSingleSelect(feed: ZappFeed, key, decoratedFeed) {
141
141
  }
142
142
 
143
143
  export const decorateFeed = (feed: ZappFeed) => {
144
- if (!(feed.extensions?.role === "preference_editor")) {
144
+ if (!(feed.extensions?.["role"] === "preference_editor")) {
145
145
  return feed;
146
146
  }
147
147
 
148
- const key = feed.extensions?.preference_editor_options?.key;
148
+ const key = feed.extensions?.["preference_editor_options"]?.["key"];
149
149
 
150
150
  if (!key) {
151
151
  log_error(
@@ -160,8 +160,8 @@ export const decorateFeed = (feed: ZappFeed) => {
160
160
  const decoratedFeed = R.clone(feed);
161
161
 
162
162
  const isSingleSelect =
163
- (feed.extensions?.preference_editor_options?.select_mode ||
164
- feed.extensions?.behavior?.select_mode) === "single";
163
+ (feed.extensions?.["preference_editor_options"]?.select_mode ||
164
+ feed.extensions?.["behavior"]?.select_mode) === "single";
165
165
 
166
166
  if (isSingleSelect) {
167
167
  return makeSingleSelect(feed, key, decoratedFeed);
@@ -16,10 +16,8 @@ export interface ActionExecutionContext {
16
16
  /** The entry that defines the screen (if screen was opened from an entry) */
17
17
  screenEntry?: ZappEntry;
18
18
 
19
- /** The UI component that triggered the action.
20
- * From UIComponentContext as `{ ...component, parent }`.
21
- */
22
- component?: ZappUIComponent & { parent?: ZappUIComponent };
19
+ /** The UI component that triggered the action */
20
+ component?: ZappUIComponent;
23
21
 
24
22
  /** The route identifier for the current screen */
25
23
  screenRoute?: string;
@@ -33,7 +31,7 @@ export interface ActionExecutionContext {
33
31
  /** Callback function for action completion */
34
32
  callback?: (result: { success: boolean; error: any; abort: boolean }) => void;
35
33
 
36
- screenState?: Record<any, any>;
34
+ screenState: Record<any, any>;
37
35
  }
38
36
 
39
37
  /**
@@ -1,30 +1,83 @@
1
- export {
2
- modalStore,
3
- useModalStore,
4
- openModal,
5
- dismissModal,
6
- openBottomSheetModal,
7
- useModalStoreState,
8
- } from "./store";
9
-
10
- export type { ModalState, ModalStore } from "./store";
11
-
12
- export { modalOrchestrator } from "./ModalOrchestrator";
13
-
14
- export { ContentViewModel } from "./ContentViewModel";
15
-
16
- export type {
17
- Menu,
18
- MenuItem,
19
- Header,
20
- Content,
21
- Action,
22
- PresentSubMenuAction,
23
- SecondaryAction,
24
- SelectMode,
25
- SelectionBehavior,
26
- } from "./types";
27
-
28
- export { isPresentSubMenuAction } from "./types";
29
-
30
- export { useBottomSheetContent } from "./useBottomSheetContent";
1
+ import { useStore, createStore } from "zustand";
2
+
3
+ interface ModalState {
4
+ visible: boolean;
5
+ screen: any;
6
+ options: Record<string, unknown>;
7
+ props: Record<string, unknown>;
8
+ }
9
+
10
+ interface ModalStore {
11
+ modalState: ModalState;
12
+ setModalState: (newState: Partial<ModalState>) => void;
13
+ openModal: (
14
+ item: any,
15
+ options?: Record<string, unknown>,
16
+ props?: Record<string, unknown>
17
+ ) => void;
18
+ dismissModal: () => void;
19
+ }
20
+
21
+ const initialModalState: ModalState = {
22
+ visible: false,
23
+ screen: null,
24
+ options: {},
25
+ props: {},
26
+ };
27
+
28
+ export const modalStore = createStore<ModalStore>((set) => ({
29
+ modalState: initialModalState,
30
+ setModalState: (newState) =>
31
+ set((state) => ({
32
+ modalState: { ...state.modalState, ...newState },
33
+ })),
34
+ openModal: ({ item, options = {}, props }: OpenModalArg) =>
35
+ set({
36
+ modalState: {
37
+ visible: true,
38
+ screen: item,
39
+ options: {
40
+ animated: true,
41
+ animationType: "slide",
42
+ onShow: () => {},
43
+ presentationStyle: "overFullScreen",
44
+ transparent: true,
45
+ ...options,
46
+ onDismiss: () => set({ modalState: initialModalState }),
47
+ onRequestClose: () => set({ modalState: initialModalState }),
48
+ },
49
+ props,
50
+ },
51
+ }),
52
+ dismissModal: () => set(() => ({ modalState: initialModalState })),
53
+ }));
54
+
55
+ export const useModalStore = <T>(selector: (state: ModalStore) => T) =>
56
+ useStore(modalStore, selector);
57
+
58
+ export const openModal = modalStore.getInitialState().openModal;
59
+
60
+ export const dismissModal = modalStore.getInitialState().dismissModal;
61
+
62
+ export const openBottomSheetModal = ({
63
+ ModalBottomSheetContent,
64
+ modalBottomSheetContentProps,
65
+ props = {},
66
+ options = {},
67
+ }: OpenModalBottomSheetArgs) => {
68
+ openModal({
69
+ item: {
70
+ ModalBottomSheetContent,
71
+ modalBottomSheetContentProps,
72
+ },
73
+ props,
74
+ options: {
75
+ animationType: "none",
76
+ animated: false,
77
+ ...options,
78
+ },
79
+ });
80
+ };
81
+
82
+ export const useModalStoreState = (): ModalState =>
83
+ useModalStore<ModalState>((state) => state.modalState);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "15.1.6-alpha.2015591222",
3
+ "version": "15.1.6",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/applicaster/quickbrick#readme",
29
29
  "dependencies": {
30
- "@applicaster/applicaster-types": "15.1.6-alpha.2015591222",
30
+ "@applicaster/applicaster-types": "15.1.6",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",
@@ -1,8 +1,12 @@
1
1
  /// <reference types="@applicaster/applicaster-types" />
2
2
  /* eslint-disable @typescript-eslint/no-unused-vars */
3
- import { Context, useContext, useState } from "react";
3
+ import { Context, useCallback, useContext, useEffect, useState } from "react";
4
4
  import { ActionsContext } from "@applicaster/zapp-react-native-ui-components/Contexts/ActionsContext";
5
5
 
6
+ import {
7
+ observeEntryState,
8
+ RegisteredActionValue,
9
+ } from "../../uiActionsRegistrator";
6
10
  import { reactHooksLogger } from "../logger";
7
11
 
8
12
  const logger = reactHooksLogger.addSubsystem("actions");
@@ -46,3 +50,49 @@ export function useActions<T = unknown>(plugId: string): any {
46
50
 
47
51
  return context;
48
52
  }
53
+
54
+ export type UseEntryActionState = {
55
+ state: CellActionEntryState | undefined;
56
+ invokeAction: (options?: InvokeArgsOptions) => void;
57
+ };
58
+
59
+ /**
60
+ * Subscribes a component to a single action's state for a given `entry`.
61
+ *
62
+ * It observes state changes reactively via `observeEntryState` (which is backed
63
+ * by the action's `addListener`), seeds the initial value synchronously from
64
+ * `initialEntryState`, and returns a memoized `invokeAction` that forwards
65
+ * optimistic `updateState` updates back into local state.
66
+ *
67
+ * This is the single, unified way to drive an action button - it works the same
68
+ * for registry actions, legacy context-provider actions and entry actions.
69
+ */
70
+ export function useEntryActionState(
71
+ action: RegisteredActionValue | undefined,
72
+ entry: ZappEntry | ZappFeed
73
+ ): UseEntryActionState {
74
+ const [state, setState] = useState<CellActionEntryState | undefined>(() =>
75
+ action?.initialEntryState?.(entry)
76
+ );
77
+
78
+ const entryId = (entry as ZappEntry)?.id;
79
+
80
+ useEffect(() => {
81
+ if (!action) return undefined;
82
+
83
+ const subscription = observeEntryState(action, entry).subscribe(setState);
84
+
85
+ return () => subscription.unsubscribe();
86
+ // eslint-disable-next-line @wogns3623/better-exhaustive-deps/exhaustive-deps
87
+ }, [action, entryId]);
88
+
89
+ const invokeAction = useCallback(
90
+ (options: InvokeArgsOptions = {}) => {
91
+ action?.invokeAction?.(entry, { updateState: setState, ...options });
92
+ },
93
+ // eslint-disable-next-line @wogns3623/better-exhaustive-deps/exhaustive-deps
94
+ [action, entryId]
95
+ );
96
+
97
+ return { state, invokeAction };
98
+ }
@@ -49,7 +49,7 @@ export function useStatusBarHeight() {
49
49
  });
50
50
  }
51
51
 
52
- export const getNavbarHeight = () => {
52
+ const getNavbarHeight = () => {
53
53
  return isTV() ? 60 : platformSelect({ ios: 44, android: 56 });
54
54
  };
55
55
 
@@ -0,0 +1,203 @@
1
+ /* eslint-disable no-dupe-class-members */
2
+ import { Observable } from "rxjs";
3
+ import { createLogger } from "../logger";
4
+
5
+ const { log_debug, log_warning, log_verbose } = createLogger({
6
+ subsystem: "UIActionsRegistry",
7
+ category: "ActionRegistration",
8
+ });
9
+
10
+ /**
11
+ * The runtime value of a UI action.
12
+ *
13
+ * This is the object that consumers interact with (render buttons for,
14
+ * invoke on press, read initial state from, etc.). It intentionally keeps an
15
+ * open shape (`[K: string]: any`) so that both the modern registry-based
16
+ * actions and the legacy context-provider based actions can be represented by
17
+ * the same structure.
18
+ */
19
+ export type RegisteredActionValue = {
20
+ state?: any;
21
+ getInitialState?: () => Promise<void>;
22
+ initialEntryState?: (item: ZappEntry | ZappFeed) => CellActionEntryState;
23
+ invokeAction: (
24
+ item: ZappEntry | ZappFeed,
25
+ options?: InvokeArgsOptions
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();