@applicaster/zapp-react-native-utils 15.1.13-alpha.1034774223 → 15.1.13

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 (68) hide show
  1. package/actionsExecutor/ActionExecutor.ts +2 -40
  2. package/actionsExecutor/ActionExecutorContext.tsx +16 -70
  3. package/actionsExecutor/actions/index.ts +0 -4
  4. package/actionsExecutor/actions/refreshComponent.ts +0 -6
  5. package/actionsExecutor/actions/sendCloudEvent.ts +1 -6
  6. package/actionsExecutor/consts.ts +0 -23
  7. package/actionsExecutor/types.ts +1 -1
  8. package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +2 -5
  9. package/appUtils/playerManager/OverlayObserver/__tests__/utils.test.ts +2 -55
  10. package/appUtils/playerManager/OverlayObserver/utils.ts +20 -51
  11. package/arrayUtils/index.ts +0 -79
  12. package/configurationUtils/__tests__/manifestKeyParser.test.ts +0 -21
  13. package/configurationUtils/manifestKeyParser.ts +10 -50
  14. package/manifestUtils/defaultManifestConfigurations/player.js +1 -22
  15. package/manifestUtils/index.js +0 -3
  16. package/modalState/__tests__/index.test.ts +1 -30
  17. package/modalState/index.ts +83 -35
  18. package/package.json +2 -2
  19. package/playerUtils/__tests__/getPlayerActionButtons.test.ts +54 -0
  20. package/playerUtils/getPlayerActionButtons.ts +17 -0
  21. package/playerUtils/index.ts +1 -17
  22. package/reactHooks/actions/index.ts +2 -67
  23. package/reactHooks/cell-click/__tests__/index.test.js +64 -0
  24. package/reactHooks/cell-click/index.ts +21 -14
  25. package/reactHooks/usePluginConfiguration.ts +3 -6
  26. package/uiActionsRegistrator/index.ts +203 -25
  27. package/actionsExecutor/actions/__tests__/dismissBottomSheet.test.ts +0 -25
  28. package/actionsExecutor/actions/__tests__/loadItemsFromSource.test.ts +0 -88
  29. package/actionsExecutor/actions/__tests__/openBottomSheet.customContent.test.ts +0 -76
  30. package/actionsExecutor/actions/__tests__/openBottomSheet.inlineItems.test.ts +0 -212
  31. package/actionsExecutor/actions/dismissBottomSheet.ts +0 -23
  32. package/actionsExecutor/actions/openBottomSheet.ts +0 -325
  33. package/appUtils/playerManager/OverlayObserver/getUpcomingQueue.android.tv.ts +0 -4
  34. package/appUtils/playerManager/OverlayObserver/getUpcomingQueue.ios.tv.ts +0 -4
  35. package/appUtils/playerManager/OverlayObserver/getUpcomingQueue.ts +0 -11
  36. package/appUtils/playerManager/OverlayObserver/getUpcomingQueue.web.ts +0 -4
  37. package/arrayUtils/__tests__/reorderByIds.test.ts +0 -50
  38. package/configurationUtils/__tests__/modalBlocksParser.test.ts +0 -260
  39. package/configurationUtils/modalBlocksParser.ts +0 -296
  40. package/entryActions/__tests__/buildEntryActions.test.ts +0 -156
  41. package/entryActions/aliasPresentation.ts +0 -124
  42. package/entryActions/buildEntryActions.ts +0 -166
  43. package/entryActions/index.ts +0 -17
  44. package/entryActions/types.ts +0 -39
  45. package/manifestUtils/modalBlocks.js +0 -304
  46. package/modalState/ContentViewModel.ts +0 -113
  47. package/modalState/EditableCollection.ts +0 -17
  48. package/modalState/EditableCollectionRegistry.ts +0 -51
  49. package/modalState/ModalOrchestrator.ts +0 -211
  50. package/modalState/RemoteEditableCollection.ts +0 -227
  51. package/modalState/__tests__/ContentViewModel.editable.test.ts +0 -69
  52. package/modalState/__tests__/ContentViewModel.test.ts +0 -165
  53. package/modalState/__tests__/EditableCollectionRegistry.test.ts +0 -112
  54. package/modalState/__tests__/ModalOrchestrator.phase3.test.ts +0 -47
  55. package/modalState/__tests__/RemoteEditableCollection.test.ts +0 -326
  56. package/modalState/__tests__/useBottomSheetContent.test.ts +0 -349
  57. package/modalState/store.ts +0 -102
  58. package/modalState/types.ts +0 -159
  59. package/modalState/useBottomSheetContent.ts +0 -104
  60. package/playerUtils/__tests__/resolvePlayerActions.test.ts +0 -138
  61. package/playerUtils/resolvePlayerActions.ts +0 -71
  62. package/uiActionsRegistrator/__tests__/resolveActionIdentifiers.test.ts +0 -93
  63. package/uiActionsRegistrator/__tests__/subscribe.test.ts +0 -107
  64. package/uiActionsRegistrator/__tests__/usableActionItems.test.ts +0 -52
  65. package/uiActionsRegistrator/registry.ts +0 -335
  66. package/uiActionsRegistrator/resolveActionIdentifiers.ts +0 -100
  67. package/uiActionsRegistrator/usableActionItems.ts +0 -66
  68. /package/reactHooks/actions/__tests__/{index.test.tsx → index.test.js} +0 -0
@@ -1,349 +0,0 @@
1
- import { renderHook, act, waitFor } from "@testing-library/react-native";
2
-
3
- import { modalOrchestrator } from "../ModalOrchestrator";
4
- import { MenuItem } from "../types";
5
- import { useBottomSheetContent } from "../useBottomSheetContent";
6
- import type { ContentState } from "../ContentViewModel";
7
-
8
- jest.mock("../ModalOrchestrator", () => ({
9
- isPresentSubMenuAction: jest.requireActual("../types").isPresentSubMenuAction,
10
- modalOrchestrator: {
11
- canGoBack: false,
12
- back: jest.fn(),
13
- close: jest.fn(),
14
- },
15
- }));
16
-
17
- type MockViewModel = {
18
- getState: jest.Mock<ContentState, []>;
19
- subscribe: jest.Mock<(listener: (state: ContentState) => void) => () => void>;
20
- refetch: jest.Mock<Promise<void>, []>;
21
- };
22
-
23
- const createViewModel = (
24
- initialState: ContentState
25
- ): MockViewModel & {
26
- emit: (nextState: ContentState) => void;
27
- } => {
28
- let listener: ((state: ContentState) => void) | undefined;
29
- const unsubscribe = jest.fn();
30
-
31
- const viewModel: MockViewModel & { emit: (nextState: ContentState) => void } =
32
- {
33
- getState: jest.fn(() => initialState),
34
- subscribe: jest.fn((nextListener) => {
35
- listener = nextListener;
36
-
37
- return unsubscribe;
38
- }),
39
- refetch: jest.fn().mockResolvedValue(undefined),
40
- emit: (nextState) => {
41
- listener?.(nextState);
42
- },
43
- };
44
-
45
- return viewModel;
46
- };
47
-
48
- describe("useBottomSheetContent", () => {
49
- const onPress = jest.fn().mockResolvedValue(undefined);
50
-
51
- beforeEach(() => {
52
- jest.clearAllMocks();
53
- modalOrchestrator.canGoBack = false;
54
- });
55
-
56
- it("returns initial state from the view model", () => {
57
- const viewModel = createViewModel({
58
- items: [{ title: "Item 1" }],
59
- isLoading: false,
60
- });
61
-
62
- const { result } = renderHook(() =>
63
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
64
- );
65
-
66
- expect(result.current.items).toEqual([{ title: "Item 1" }]);
67
- expect(result.current.isLoading).toBe(false);
68
- expect(result.current.error).toBeUndefined();
69
- expect(result.current.showCentralLoading).toBe(false);
70
- expect(result.current.canGoBack).toBe(false);
71
- expect(viewModel.subscribe).toHaveBeenCalledTimes(1);
72
- });
73
-
74
- it("computes showCentralLoading when loading with no items", () => {
75
- const viewModel = createViewModel({
76
- items: [],
77
- isLoading: true,
78
- });
79
-
80
- const { result } = renderHook(() =>
81
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
82
- );
83
-
84
- expect(result.current.showCentralLoading).toBe(true);
85
- });
86
-
87
- it("does not show central loading when items exist while loading", () => {
88
- const viewModel = createViewModel({
89
- items: [{ title: "Item 1" }],
90
- isLoading: true,
91
- });
92
-
93
- const { result } = renderHook(() =>
94
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
95
- );
96
-
97
- expect(result.current.showCentralLoading).toBe(false);
98
- });
99
-
100
- it("updates when the view model emits new state", () => {
101
- const viewModel = createViewModel({
102
- items: [],
103
- isLoading: true,
104
- });
105
-
106
- const { result } = renderHook(() =>
107
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
108
- );
109
-
110
- act(() => {
111
- viewModel.emit({
112
- items: [{ title: "Fetched Item" }],
113
- isLoading: false,
114
- });
115
- });
116
-
117
- expect(result.current.items).toEqual([{ title: "Fetched Item" }]);
118
- expect(result.current.isLoading).toBe(false);
119
- expect(result.current.showCentralLoading).toBe(false);
120
- });
121
-
122
- it("exposes error state from the view model", () => {
123
- const viewModel = createViewModel({
124
- items: [],
125
- isLoading: false,
126
- error: "Network Error",
127
- });
128
-
129
- const { result } = renderHook(() =>
130
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
131
- );
132
-
133
- expect(result.current.error).toBe("Network Error");
134
- });
135
-
136
- it("unsubscribes from the view model on unmount", () => {
137
- const viewModel = createViewModel({
138
- items: [],
139
- isLoading: false,
140
- });
141
-
142
- const { unmount } = renderHook(() =>
143
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
144
- );
145
-
146
- const unsubscribeFn = viewModel.subscribe.mock.results[0]
147
- .value as jest.Mock;
148
-
149
- unmount();
150
-
151
- expect(unsubscribeFn).toHaveBeenCalledTimes(1);
152
- });
153
-
154
- describe("handleItemPress", () => {
155
- it("delegates to onPress and refetches for leaf items", async () => {
156
- const viewModel = createViewModel({
157
- items: [{ title: "Leaf" }],
158
- isLoading: false,
159
- });
160
-
161
- const item: MenuItem = { title: "Leaf" };
162
-
163
- const { result } = renderHook(() =>
164
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
165
- );
166
-
167
- await act(async () => {
168
- await result.current.handleItemPress(item);
169
- });
170
-
171
- expect(onPress).toHaveBeenCalledWith(item);
172
- expect(viewModel.refetch).toHaveBeenCalledTimes(1);
173
- });
174
-
175
- it("does not refetch when the item opens a present submenu action", async () => {
176
- const viewModel = createViewModel({
177
- items: [{ title: "Parent" }],
178
- isLoading: false,
179
- });
180
-
181
- const item: MenuItem = {
182
- title: "Parent",
183
- action: {
184
- type: "presentSubMenu",
185
- menu: {
186
- content: {
187
- title: "Submenu",
188
- items: [{ title: "Child" }],
189
- },
190
- },
191
- },
192
- };
193
-
194
- const { result } = renderHook(() =>
195
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
196
- );
197
-
198
- await act(async () => {
199
- await result.current.handleItemPress(item);
200
- });
201
-
202
- expect(onPress).toHaveBeenCalledWith(item);
203
- expect(viewModel.refetch).not.toHaveBeenCalled();
204
- });
205
-
206
- it("awaits onPress before refetching", async () => {
207
- const viewModel = createViewModel({
208
- items: [{ title: "Leaf" }],
209
- isLoading: false,
210
- });
211
-
212
- const item: MenuItem = { title: "Leaf" };
213
- const callOrder: string[] = [];
214
-
215
- onPress.mockImplementation(async () => {
216
- callOrder.push("onPress");
217
- });
218
-
219
- viewModel.refetch.mockImplementation(async () => {
220
- callOrder.push("refetch");
221
- });
222
-
223
- const { result } = renderHook(() =>
224
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
225
- );
226
-
227
- await act(async () => {
228
- await result.current.handleItemPress(item);
229
- });
230
-
231
- expect(callOrder).toEqual(["onPress", "refetch"]);
232
- });
233
- });
234
-
235
- describe("goBackOrClose", () => {
236
- it("calls back when the orchestrator can go back", () => {
237
- modalOrchestrator.canGoBack = true;
238
-
239
- const viewModel = createViewModel({
240
- items: [],
241
- isLoading: false,
242
- });
243
-
244
- const { result } = renderHook(() =>
245
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
246
- );
247
-
248
- act(() => {
249
- result.current.goBackOrClose();
250
- });
251
-
252
- expect(modalOrchestrator.back).toHaveBeenCalledTimes(1);
253
- expect(modalOrchestrator.close).not.toHaveBeenCalled();
254
- });
255
-
256
- it("calls close when the orchestrator cannot go back", () => {
257
- modalOrchestrator.canGoBack = false;
258
-
259
- const viewModel = createViewModel({
260
- items: [],
261
- isLoading: false,
262
- });
263
-
264
- const { result } = renderHook(() =>
265
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
266
- );
267
-
268
- act(() => {
269
- result.current.goBackOrClose();
270
- });
271
-
272
- expect(modalOrchestrator.close).toHaveBeenCalledTimes(1);
273
- expect(modalOrchestrator.back).not.toHaveBeenCalled();
274
- });
275
- });
276
-
277
- it("exposes close from the modal orchestrator", () => {
278
- const viewModel = createViewModel({
279
- items: [],
280
- isLoading: false,
281
- });
282
-
283
- const { result } = renderHook(() =>
284
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
285
- );
286
-
287
- act(() => {
288
- result.current.close();
289
- });
290
-
291
- expect(modalOrchestrator.close).toHaveBeenCalledTimes(1);
292
- });
293
-
294
- it("reflects canGoBack from the modal orchestrator", () => {
295
- modalOrchestrator.canGoBack = true;
296
-
297
- const viewModel = createViewModel({
298
- items: [],
299
- isLoading: false,
300
- });
301
-
302
- const { result } = renderHook(() =>
303
- useBottomSheetContent({ viewModel: viewModel as any, onPress })
304
- );
305
-
306
- expect(result.current.canGoBack).toBe(true);
307
- });
308
-
309
- it("resubscribes when the view model instance changes", async () => {
310
- const firstViewModel = createViewModel({
311
- items: [{ title: "First" }],
312
- isLoading: false,
313
- });
314
-
315
- const secondViewModel = createViewModel({
316
- items: [{ title: "Second" }],
317
- isLoading: false,
318
- });
319
-
320
- const { result, rerender } = renderHook(
321
- ({ viewModel }) =>
322
- useBottomSheetContent({ viewModel: viewModel as any, onPress }),
323
- {
324
- initialProps: { viewModel: firstViewModel },
325
- }
326
- );
327
-
328
- expect(result.current.items).toEqual([{ title: "First" }]);
329
-
330
- rerender({ viewModel: secondViewModel });
331
-
332
- expect(
333
- firstViewModel.subscribe.mock.results[0].value
334
- ).toHaveBeenCalledTimes(1);
335
-
336
- expect(secondViewModel.subscribe).toHaveBeenCalledTimes(1);
337
-
338
- act(() => {
339
- secondViewModel.emit({
340
- items: [{ title: "Updated Second" }],
341
- isLoading: false,
342
- });
343
- });
344
-
345
- await waitFor(() => {
346
- expect(result.current.items).toEqual([{ title: "Updated Second" }]);
347
- });
348
- });
349
- });
@@ -1,102 +0,0 @@
1
- import { createStore, useStore } from "zustand";
2
-
3
- export interface ModalState {
4
- visible: boolean;
5
- screen: any;
6
- options: Record<string, unknown>;
7
- props: Record<string, unknown>;
8
- }
9
-
10
- export 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, get) => ({
29
- modalState: initialModalState,
30
- setModalState: (newState) =>
31
- set((state) => ({
32
- modalState: { ...state.modalState, ...newState },
33
- })),
34
- openModal: ({ item, options = {}, props }: OpenModalArg) => {
35
- const resetStore = () => set({ modalState: initialModalState });
36
- const { onDismiss, onRequestClose, ...restOptions } = options as any;
37
-
38
- set({
39
- modalState: {
40
- visible: true,
41
- screen: item,
42
- options: {
43
- animated: true,
44
- animationType: "slide",
45
- onShow: () => {},
46
- presentationStyle: "overFullScreen",
47
- transparent: true,
48
- ...restOptions,
49
- // Compose caller-provided callbacks with the store reset so both run
50
- onDismiss: () => {
51
- resetStore();
52
- onDismiss?.();
53
- },
54
- onRequestClose: () => {
55
- resetStore();
56
- onRequestClose?.();
57
- },
58
- },
59
- props,
60
- },
61
- });
62
- },
63
- dismissModal: () => {
64
- const onDismiss = (get().modalState?.options as any)?.onDismiss;
65
-
66
- if (typeof onDismiss === "function") {
67
- onDismiss();
68
- } else {
69
- set({ modalState: initialModalState });
70
- }
71
- },
72
- }));
73
-
74
- export const useModalStore = <T>(selector: (state: ModalStore) => T) =>
75
- useStore(modalStore, selector);
76
-
77
- export const openModal = modalStore.getInitialState().openModal;
78
-
79
- export const dismissModal = modalStore.getInitialState().dismissModal;
80
-
81
- export const openBottomSheetModal = ({
82
- ModalBottomSheetContent,
83
- modalBottomSheetContentProps,
84
- props = {},
85
- options = {},
86
- }: OpenModalBottomSheetArgs) => {
87
- openModal({
88
- item: {
89
- ModalBottomSheetContent,
90
- modalBottomSheetContentProps,
91
- },
92
- props,
93
- options: {
94
- animationType: "none",
95
- animated: false,
96
- ...options,
97
- },
98
- });
99
- };
100
-
101
- export const useModalStoreState = (): ModalState =>
102
- useModalStore<ModalState>((state) => state.modalState);
@@ -1,159 +0,0 @@
1
- export interface Header {
2
- title: string;
3
- subtitle?: string;
4
- icon?: string;
5
- }
6
-
7
- export interface Action {
8
- type: string;
9
- payload?: any;
10
- options?: any;
11
- }
12
-
13
- // Represented as 3 dots icon on top menu (opens new second level menu on desktop
14
- // or a new bottom sheet on mobile)
15
- export interface PresentSubMenuAction extends Action {
16
- menu: Menu;
17
- }
18
-
19
- export interface SecondaryAction {
20
- action: Action | PresentSubMenuAction;
21
- icon?: string;
22
- }
23
-
24
- /**
25
- * A group of actions attached to an entry, keyed by a button alias
26
- * (e.g. "remove_item", "reorder_item"). Injected by the feed decorator and
27
- * dispatched by the remote EditableCollection when an operation runs.
28
- */
29
- export interface EntryActionGroup {
30
- button?: { alias?: string };
31
- actions?: Action[];
32
- }
33
-
34
- export interface MenuItem {
35
- id: string;
36
- title: string;
37
- summary?: string;
38
- icon?: string;
39
- isSelected?: boolean;
40
- actions?: (Action | PresentSubMenuAction)[];
41
- /** @deprecated Use `actions` array instead. Maintained for backwards compatibility. */
42
- action?: Action | PresentSubMenuAction;
43
- secondaryAction?: SecondaryAction;
44
- trailingButton?: any;
45
- entryActions?: EntryActionGroup[];
46
- }
47
-
48
- export type SelectMode = "single" | "multi" | "none";
49
-
50
- export interface SelectionBehavior {
51
- selectMode?: SelectMode;
52
- currentSelection?: string[];
53
- selector?: string;
54
- }
55
-
56
- /**
57
- * Operations that act on a specific item in the collection.
58
- * Their action payloads are looked up on the item's entryActions (e.g. "remove_item", "reorder_item").
59
- */
60
- export type ItemScopedOperation = "remove" | "reorder";
61
-
62
- /**
63
- * Operations that act on the collection as a whole.
64
- * Their action payloads are looked up on dynamicCollection.events (e.g. events.add, events.clearAll, events.delete).
65
- */
66
- export type CollectionScopedOperation = "add" | "clearAll" | "delete";
67
-
68
- /**
69
- * UI display or behavior flags that do not require action payloads.
70
- */
71
- export type UIOperation = "nowPlaying";
72
-
73
- export const OPERATIONS = {
74
- REMOVE: "remove",
75
- REORDER: "reorder",
76
- ADD: "add",
77
- CLEAR_ALL: "clearAll",
78
- DELETE: "delete",
79
- NOW_PLAYING: "nowPlaying",
80
- } as const;
81
-
82
- export function hasOperation(
83
- operations: string[] | undefined,
84
- target: string
85
- ): boolean {
86
- if (!Array.isArray(operations)) return false;
87
-
88
- const targetLower = target.toLowerCase();
89
-
90
- return operations.some(
91
- (op) => typeof op === "string" && op.toLowerCase() === targetLower
92
- );
93
- }
94
-
95
- export type Operation =
96
- | ItemScopedOperation
97
- | CollectionScopedOperation
98
- | UIOperation;
99
-
100
- export interface DynamicCollectionOptions {
101
- operations: Operation[];
102
- postUrl?: string;
103
- provider?: string;
104
- /**
105
- * Action handlers for CollectionScopedOperations (e.g. "add", "clearAll", "delete").
106
- */
107
- events?: Record<CollectionScopedOperation | string, Action[]>;
108
- }
109
-
110
- export interface Content {
111
- title: string;
112
- stickyTitle?: boolean;
113
- items: MenuItem[];
114
- itemsUrl?: string;
115
- action?: Action;
116
- role?: string;
117
- behavior?: SelectionBehavior;
118
- dynamicCollection?: DynamicCollectionOptions;
119
- themePluginId?: string;
120
- styleOverrides?: Record<string, any>;
121
- }
122
-
123
- export interface Menu {
124
- header?: Header;
125
- content: Content;
126
- closeButton?: boolean;
127
- }
128
-
129
- export function isPresentSubMenuAction(
130
- action: any
131
- ): action is PresentSubMenuAction {
132
- return !!action && typeof action === "object" && "menu" in action;
133
- }
134
-
135
- /**
136
- * Normalizes an item's action/actions into an array of Action items.
137
- * Supports canonical `actions: Action[]` and backwards-compatible `action: Action | Action[]`.
138
- */
139
- export function getMenuItemActions(
140
- item?: Partial<MenuItem> | null
141
- ): (Action | PresentSubMenuAction)[] {
142
- if (!item) {
143
- return [];
144
- }
145
-
146
- if (Array.isArray(item.actions) && item.actions.length > 0) {
147
- return item.actions;
148
- }
149
-
150
- if (Array.isArray((item as any).action)) {
151
- return (item as any).action;
152
- }
153
-
154
- if (item.action) {
155
- return [item.action];
156
- }
157
-
158
- return [];
159
- }
@@ -1,104 +0,0 @@
1
- import React, { useEffect, useState } from "react";
2
- import { toBooleanWithDefaultFalse } from "@applicaster/zapp-react-native-utils/booleanUtils";
3
-
4
- import { MenuItem, modalOrchestrator } from "./ModalOrchestrator";
5
-
6
- import { ContentViewModel } from "./ContentViewModel";
7
- import {
8
- DynamicCollectionOptions,
9
- getMenuItemActions,
10
- isPresentSubMenuAction,
11
- SelectionBehavior,
12
- } from "./types";
13
- import type { EditableCollection } from "./EditableCollection";
14
-
15
- type Props = {
16
- viewModel?: ContentViewModel | null;
17
- onPress: (item: MenuItem) => void | Promise<void>;
18
- };
19
-
20
- type Return = {
21
- items: MenuItem[];
22
- isLoading: boolean;
23
- error?: Error | string;
24
- showCentralLoading: boolean;
25
- canGoBack: boolean;
26
- handleItemPress: (item: MenuItem) => Promise<void>;
27
- goBackOrClose: () => void;
28
- close: () => void;
29
- role?: string;
30
- behavior?: SelectionBehavior;
31
- dynamicCollection?: DynamicCollectionOptions;
32
- editableCollection: EditableCollection | null;
33
- themePluginId?: string;
34
- };
35
-
36
- export const useBottomSheetContent = (props: Props): Return => {
37
- const { onPress, viewModel } = props;
38
-
39
- const [state, setState] = useState(viewModel?.getState());
40
-
41
- const [editableItems, setEditableItems] = useState<MenuItem[]>(
42
- () => viewModel?.getState().items || []
43
- );
44
-
45
- // Subscribe to the viewModel state changes
46
- useEffect(() => {
47
- return viewModel?.subscribe((nextState) => {
48
- setState(nextState);
49
- });
50
- }, [viewModel]);
51
-
52
- // Subscribe to editable collection items
53
- useEffect(() => {
54
- if (viewModel?.editableCollection) {
55
- const subscription =
56
- viewModel.editableCollection.items$.subscribe(setEditableItems);
57
-
58
- return () => subscription.unsubscribe();
59
- }
60
- }, [viewModel?.editableCollection]);
61
-
62
- /**
63
- * Handles an item press:
64
- * - Delegates to the parent onPress (which runs the action and awaits it).
65
- * - If the item was a leaf action (not a submenu), triggers viewModel.refetch()
66
- * so that checkmarks (isSelected) reflect the updated server state.
67
- */
68
- const handleItemPress = async (item: MenuItem) => {
69
- const actions = getMenuItemActions(item);
70
- const isSubMenu = actions.some(isPresentSubMenuAction);
71
-
72
- await onPress(item);
73
-
74
- if (!isSubMenu) {
75
- await viewModel?.refetch();
76
- }
77
- };
78
-
79
- const goBackOrClose = React.useCallback(() => {
80
- if (modalOrchestrator.canGoBack) {
81
- modalOrchestrator.back();
82
- } else {
83
- modalOrchestrator.close();
84
- }
85
- }, [modalOrchestrator.canGoBack]);
86
-
87
- return {
88
- items: viewModel?.editableCollection ? editableItems : state?.items,
89
- isLoading: toBooleanWithDefaultFalse(state?.isLoading),
90
- error: state?.error,
91
- showCentralLoading: toBooleanWithDefaultFalse(
92
- state?.isLoading && state?.items.length === 0
93
- ),
94
- canGoBack: modalOrchestrator.canGoBack,
95
- handleItemPress,
96
- goBackOrClose,
97
- close: () => modalOrchestrator.close(),
98
- role: state?.role,
99
- behavior: state?.behavior,
100
- dynamicCollection: state?.dynamicCollection,
101
- editableCollection: viewModel?.editableCollection || null,
102
- themePluginId: state?.themePluginId,
103
- };
104
- };