@applicaster/zapp-react-native-utils 15.1.5 → 15.1.6-alpha.2015591222
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/ActionExecutor.ts +8 -10
- package/actionsExecutor/ActionExecutorContext.tsx +49 -340
- package/actionsExecutor/actions/__tests__/dismissBottomSheet.test.ts +22 -0
- package/actionsExecutor/actions/__tests__/loadItemsFromSource.test.ts +88 -0
- package/actionsExecutor/actions/__tests__/openBottomSheet.customContent.test.ts +76 -0
- package/actionsExecutor/actions/__tests__/showToast.test.ts +63 -0
- package/actionsExecutor/actions/appRestart.ts +24 -0
- package/actionsExecutor/actions/confirmDialog.ts +53 -0
- package/actionsExecutor/actions/dismissBottomSheet.ts +22 -0
- package/actionsExecutor/actions/index.ts +34 -0
- package/actionsExecutor/actions/localStorageRemove.ts +27 -0
- package/actionsExecutor/actions/localStorageSet.ts +28 -0
- package/actionsExecutor/actions/localStorageToggleFlag.ts +28 -0
- package/actionsExecutor/actions/navigateToScreen.ts +123 -0
- package/actionsExecutor/actions/openBottomSheet.ts +318 -0
- package/actionsExecutor/actions/refreshComponent.ts +85 -0
- package/actionsExecutor/actions/screenSetVariable.ts +35 -0
- package/actionsExecutor/actions/screenToggleFlag.ts +38 -0
- package/actionsExecutor/actions/sendCloudEvent.ts +96 -0
- package/actionsExecutor/actions/sessionStorageRemove.ts +19 -0
- package/actionsExecutor/actions/sessionStorageSet.ts +20 -0
- package/actionsExecutor/actions/sessionStorageToggleFlag.ts +15 -0
- package/actionsExecutor/actions/showToast.ts +31 -0
- package/actionsExecutor/actions/switchLayout.ts +40 -0
- package/actionsExecutor/feedDecorator.ts +6 -6
- package/actionsExecutor/types.ts +59 -0
- package/modalState/ContentViewModel.ts +111 -0
- package/modalState/EditableCollection.ts +17 -0
- package/modalState/EditableCollectionRegistry.ts +51 -0
- package/modalState/ModalOrchestrator.ts +198 -0
- package/modalState/RemoteEditableCollection.ts +189 -0
- package/modalState/__tests__/ContentViewModel.editable.test.ts +69 -0
- package/modalState/__tests__/ContentViewModel.test.ts +165 -0
- package/modalState/__tests__/EditableCollectionRegistry.test.ts +112 -0
- package/modalState/__tests__/ModalOrchestrator.phase3.test.ts +28 -0
- package/modalState/__tests__/RemoteEditableCollection.test.ts +259 -0
- package/modalState/__tests__/useBottomSheetContent.test.ts +372 -0
- package/modalState/index.ts +30 -83
- package/modalState/store.ts +102 -0
- package/modalState/types.ts +110 -0
- package/modalState/useBottomSheetContent.ts +98 -0
- package/package.json +2 -2
- package/reactHooks/layout/index.ts +1 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/// <reference types="@applicaster/applicaster-types" />
|
|
2
|
+
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
3
|
+
import * as QuickBrickManager from "@applicaster/zapp-react-native-bridge/QuickBrick";
|
|
4
|
+
import { QUICK_BRICK_EVENTS } from "@applicaster/zapp-react-native-bridge/QuickBrick";
|
|
5
|
+
import { ACTIVE_LAYOUT_ID_STORAGE_KEY } from "@applicaster/quick-brick-core/App/remoteContextReloader/consts";
|
|
6
|
+
import { ActionResult } from "../ActionExecutor";
|
|
7
|
+
import { ActionHandler } from "../types";
|
|
8
|
+
import { createLogger } from "../../logger";
|
|
9
|
+
|
|
10
|
+
const { log_info } = createLogger({
|
|
11
|
+
subsystem: "ActionExecutorContext",
|
|
12
|
+
category: "General",
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options for the switchLayout action.
|
|
17
|
+
*/
|
|
18
|
+
export interface SwitchLayoutOptions {
|
|
19
|
+
/** The ID of the layout to switch to */
|
|
20
|
+
layoutId: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Switches the active layout and triggers an app reload.
|
|
25
|
+
* Saves the new layout ID to local storage before reloading.
|
|
26
|
+
*/
|
|
27
|
+
export const switchLayoutAction: ActionHandler<SwitchLayoutOptions> = async (
|
|
28
|
+
action
|
|
29
|
+
): Promise<ActionResult> => {
|
|
30
|
+
log_info("handleAction: switchLayout event");
|
|
31
|
+
|
|
32
|
+
await localStorage.setItem(
|
|
33
|
+
ACTIVE_LAYOUT_ID_STORAGE_KEY,
|
|
34
|
+
action.options.layoutId
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
QuickBrickManager.sendQuickBrickEvent(QUICK_BRICK_EVENTS.FORCE_APP_RELOAD);
|
|
38
|
+
|
|
39
|
+
return ActionResult.Success;
|
|
40
|
+
};
|
|
@@ -27,7 +27,7 @@ function makeMultiSelect(feed: ZappFeed, key, decoratedFeed) {
|
|
|
27
27
|
);
|
|
28
28
|
|
|
29
29
|
const behavior = {
|
|
30
|
-
...feed.extensions?.
|
|
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?.
|
|
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?.
|
|
144
|
+
if (!(feed.extensions?.role === "preference_editor")) {
|
|
145
145
|
return feed;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
const key = feed.extensions?.
|
|
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?.
|
|
164
|
-
feed.extensions?.
|
|
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);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/// <reference types="@applicaster/applicaster-types" />
|
|
2
|
+
import { ActionResult } from "./ActionExecutor";
|
|
3
|
+
import { useScreenStateStore } from "../reactHooks/navigation/useScreenStateStore";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base context object passed to action handlers during execution.
|
|
7
|
+
* Contains references to the current entry, screen, and other execution state.
|
|
8
|
+
*/
|
|
9
|
+
export interface ActionExecutionContext {
|
|
10
|
+
/** The entry associated with this action (e.g., the item that was tapped) */
|
|
11
|
+
entry?: ZappEntry;
|
|
12
|
+
|
|
13
|
+
/** The current screen/river data */
|
|
14
|
+
screenData?: ZappRiver;
|
|
15
|
+
|
|
16
|
+
/** The entry that defines the screen (if screen was opened from an entry) */
|
|
17
|
+
screenEntry?: ZappEntry;
|
|
18
|
+
|
|
19
|
+
/** The UI component that triggered the action.
|
|
20
|
+
* From UIComponentContext as `{ ...component, parent }`.
|
|
21
|
+
*/
|
|
22
|
+
component?: ZappUIComponent & { parent?: ZappUIComponent };
|
|
23
|
+
|
|
24
|
+
/** The route identifier for the current screen */
|
|
25
|
+
screenRoute?: string;
|
|
26
|
+
|
|
27
|
+
/** The screen state store for managing screen-level state (Zustand store) */
|
|
28
|
+
screenStateStore?: ReturnType<typeof useScreenStateStore>;
|
|
29
|
+
|
|
30
|
+
/** Context entry for additional data */
|
|
31
|
+
entryContext?: ZappEntry;
|
|
32
|
+
|
|
33
|
+
/** Callback function for action completion */
|
|
34
|
+
callback?: (result: { success: boolean; error: any; abort: boolean }) => void;
|
|
35
|
+
|
|
36
|
+
screenState?: Record<any, any>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Type for action handler functions.
|
|
41
|
+
* Handlers receive the action (which may have typed options) and execution context.
|
|
42
|
+
*
|
|
43
|
+
* @template _TOptions - Optional type hint for the action's options object (for documentation/intellisense)
|
|
44
|
+
* @param action - The action to execute
|
|
45
|
+
* @param context - The execution context
|
|
46
|
+
* @returns A promise that resolves to the action result
|
|
47
|
+
*/
|
|
48
|
+
export type ActionHandler<_TOptions = any> = (
|
|
49
|
+
action: ActionType,
|
|
50
|
+
context?: ActionExecutionContext
|
|
51
|
+
) => Promise<ActionResult>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Factory function that creates an action handler.
|
|
55
|
+
* Used for actions that need to capture dependencies at registration time.
|
|
56
|
+
*/
|
|
57
|
+
export type ActionHandlerFactory<_TOptions = any> = (
|
|
58
|
+
...dependencies: any[]
|
|
59
|
+
) => ActionHandler<_TOptions>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { BehaviorSubject, Observable } from "rxjs";
|
|
2
|
+
import {
|
|
3
|
+
Content,
|
|
4
|
+
MenuItem,
|
|
5
|
+
SelectionBehavior,
|
|
6
|
+
DynamicCollectionOptions,
|
|
7
|
+
} from "./types";
|
|
8
|
+
import { loadItemsFromSource } from "../actionsExecutor/actions/openBottomSheet";
|
|
9
|
+
import { resolveEditableCollection } from "./EditableCollectionRegistry";
|
|
10
|
+
import { EditableCollection } from "./EditableCollection";
|
|
11
|
+
|
|
12
|
+
export interface ContentState {
|
|
13
|
+
items: MenuItem[];
|
|
14
|
+
isLoading: boolean;
|
|
15
|
+
error?: Error | string;
|
|
16
|
+
role?: string;
|
|
17
|
+
behavior?: SelectionBehavior;
|
|
18
|
+
dynamicCollection?: DynamicCollectionOptions;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class ContentViewModel {
|
|
22
|
+
private state$: BehaviorSubject<ContentState>;
|
|
23
|
+
readonly itemsUrl?: string;
|
|
24
|
+
public editableCollection: EditableCollection | null = null;
|
|
25
|
+
private content: Content;
|
|
26
|
+
|
|
27
|
+
constructor(content: Content) {
|
|
28
|
+
this.content = content;
|
|
29
|
+
this.itemsUrl = content.itemsUrl;
|
|
30
|
+
|
|
31
|
+
this.state$ = new BehaviorSubject<ContentState>({
|
|
32
|
+
items: content.items || [],
|
|
33
|
+
isLoading: false,
|
|
34
|
+
role: content.role,
|
|
35
|
+
behavior: content.behavior,
|
|
36
|
+
dynamicCollection: content.dynamicCollection,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
this.initEditableCollection(content);
|
|
40
|
+
|
|
41
|
+
if (this.itemsUrl && this.state$.value.items.length === 0) {
|
|
42
|
+
this.refetch();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private initEditableCollection(content: Content) {
|
|
47
|
+
this.editableCollection = resolveEditableCollection(content, this.refetch);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getState = (): ContentState => this.state$.value;
|
|
51
|
+
|
|
52
|
+
get stateObservable(): Observable<ContentState> {
|
|
53
|
+
return this.state$.asObservable();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
subscribe = (listener: (state: ContentState) => void) => {
|
|
57
|
+
const subscription = this.state$.subscribe(listener);
|
|
58
|
+
|
|
59
|
+
return () => subscription.unsubscribe();
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
private updateState(partialState: Partial<ContentState>) {
|
|
63
|
+
this.state$.next({ ...this.state$.value, ...partialState });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
refetch = async () => {
|
|
67
|
+
if (!this.itemsUrl) return;
|
|
68
|
+
|
|
69
|
+
this.updateState({ isLoading: true, error: undefined });
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const separator = this.itemsUrl.includes("?") ? "&" : "?";
|
|
73
|
+
const cacheBustedUrl = `${this.itemsUrl}${separator}_t=${Date.now()}`;
|
|
74
|
+
|
|
75
|
+
const { items, role, behavior, dynamicCollection } =
|
|
76
|
+
await loadItemsFromSource(cacheBustedUrl);
|
|
77
|
+
|
|
78
|
+
this.content = {
|
|
79
|
+
...this.content,
|
|
80
|
+
items,
|
|
81
|
+
role,
|
|
82
|
+
behavior,
|
|
83
|
+
dynamicCollection,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
this.updateState({
|
|
87
|
+
items,
|
|
88
|
+
role,
|
|
89
|
+
behavior,
|
|
90
|
+
dynamicCollection,
|
|
91
|
+
isLoading: false,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
if (this.editableCollection) {
|
|
95
|
+
// Reuse the existing collection instance. Pushing the freshly loaded
|
|
96
|
+
// server items replaces any optimistic local state with the
|
|
97
|
+
// authoritative result (and rolls forward a successful mutation).
|
|
98
|
+
this.editableCollection.sync?.(items);
|
|
99
|
+
} else {
|
|
100
|
+
// The collection may only become resolvable once options arrive from
|
|
101
|
+
// the feed (e.g. inline content had none). Create it lazily here.
|
|
102
|
+
this.initEditableCollection(this.content);
|
|
103
|
+
}
|
|
104
|
+
} catch (err) {
|
|
105
|
+
this.updateState({
|
|
106
|
+
error: err?.message || "Failed to load",
|
|
107
|
+
isLoading: false,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
import { MenuItem, Operation } from "./types";
|
|
3
|
+
|
|
4
|
+
export interface EditableCollection {
|
|
5
|
+
items$: Observable<MenuItem[]>;
|
|
6
|
+
operations: Operation[];
|
|
7
|
+
add?(item?: MenuItem): void | Promise<void>;
|
|
8
|
+
remove?(index: number): void | Promise<void>;
|
|
9
|
+
move?(itemIds: string[]): void | Promise<void>;
|
|
10
|
+
deleteCollection?(): void | Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Push authoritative items (e.g. a fresh server feed after a mutation) into
|
|
13
|
+
* the collection, replacing any optimistic local state. Called by the
|
|
14
|
+
* ContentViewModel on refetch so the instance is reused instead of recreated.
|
|
15
|
+
*/
|
|
16
|
+
sync?(items: MenuItem[]): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Content } from "./types";
|
|
2
|
+
import { EditableCollection } from "./EditableCollection";
|
|
3
|
+
import { RemoteEditableCollection } from "./RemoteEditableCollection";
|
|
4
|
+
import { createLogger } from "../logger";
|
|
5
|
+
|
|
6
|
+
const { log_error } = createLogger({
|
|
7
|
+
subsystem: "modalState",
|
|
8
|
+
category: "EditableCollectionRegistry",
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
type ProviderFactory = () => EditableCollection;
|
|
12
|
+
|
|
13
|
+
export class EditableCollectionRegistry {
|
|
14
|
+
private static providers: Record<string, ProviderFactory> = {};
|
|
15
|
+
|
|
16
|
+
static register(token: string, factory: ProviderFactory) {
|
|
17
|
+
this.providers[token] = factory;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static getProvider(token: string): EditableCollection | null {
|
|
21
|
+
const factory = this.providers[token];
|
|
22
|
+
|
|
23
|
+
return factory ? factory() : null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function resolveEditableCollection(
|
|
28
|
+
content: Content,
|
|
29
|
+
refetch: () => Promise<void>
|
|
30
|
+
): EditableCollection | null {
|
|
31
|
+
const options = content.dynamicCollection;
|
|
32
|
+
if (!options) return null;
|
|
33
|
+
|
|
34
|
+
if (options.provider) {
|
|
35
|
+
const provider = EditableCollectionRegistry.getProvider(options.provider);
|
|
36
|
+
|
|
37
|
+
if (!provider) {
|
|
38
|
+
log_error(
|
|
39
|
+
`No EditableCollection provider registered for token "${options.provider}"`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return provider;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (options.postUrl) {
|
|
47
|
+
return new RemoteEditableCollection(content, refetch);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { dismissModal, modalStore, openBottomSheetModal } from "./store";
|
|
2
|
+
import { isPresentSubMenuAction, Menu, MenuItem } from "./types";
|
|
3
|
+
import { ContentViewModel } from "./ContentViewModel";
|
|
4
|
+
|
|
5
|
+
export type { Menu, MenuItem };
|
|
6
|
+
|
|
7
|
+
export { isPresentSubMenuAction };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Converts a Menu model into the props expected by BottomSheetModalContent.
|
|
11
|
+
* - Maps MenuItem.title → label (required by BottomSheetModalContent)
|
|
12
|
+
* - Maps MenuItem.icon → image src
|
|
13
|
+
* - Flattens header.title / header.subtitle into top-level title / summary
|
|
14
|
+
*/
|
|
15
|
+
interface StackEntry {
|
|
16
|
+
menu: Menu;
|
|
17
|
+
onItemPress?: (item: MenuItem) => void | Promise<void>;
|
|
18
|
+
viewModel: ContentViewModel;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function menuToModalArgs(
|
|
22
|
+
menu: Menu,
|
|
23
|
+
viewModel: ContentViewModel,
|
|
24
|
+
onItemPress: (item: MenuItem) => void | Promise<void>,
|
|
25
|
+
key: string
|
|
26
|
+
): OpenModalBottomSheetArgs {
|
|
27
|
+
const items = menu.content.items.map((item) => ({
|
|
28
|
+
...item,
|
|
29
|
+
// BottomSheetModalContent renders `theme[item.label] ?? item.label`
|
|
30
|
+
label: item.title,
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
modalBottomSheetContentProps: {
|
|
35
|
+
key,
|
|
36
|
+
items,
|
|
37
|
+
itemsUrl: menu.content.itemsUrl,
|
|
38
|
+
title: menu.header?.title ?? menu.content.title,
|
|
39
|
+
summary: menu.header?.subtitle,
|
|
40
|
+
onPress: onItemPress,
|
|
41
|
+
viewModel,
|
|
42
|
+
},
|
|
43
|
+
options: {},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* ModalOrchestrator manages a stack of Menu modals, enabling push/pop
|
|
49
|
+
* navigation between bottom sheet screens.
|
|
50
|
+
*
|
|
51
|
+
* Usage:
|
|
52
|
+
* modalOrchestrator.open(menu);
|
|
53
|
+
* modalOrchestrator.back(); // go to previous modal in the stack
|
|
54
|
+
* modalOrchestrator.close(); // dismiss all and clear the stack
|
|
55
|
+
*/
|
|
56
|
+
class ModalOrchestrator {
|
|
57
|
+
private _stack: StackEntry[] = [];
|
|
58
|
+
|
|
59
|
+
/** Number of entries currently in the stack. */
|
|
60
|
+
get stackSize(): number {
|
|
61
|
+
return this._stack.length;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Whether there is a previous modal to go back to. */
|
|
65
|
+
get canGoBack(): boolean {
|
|
66
|
+
return this._stack.length > 1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The Menu currently shown, or null if the stack is empty. */
|
|
70
|
+
get current(): Menu | null {
|
|
71
|
+
return this._stack[this._stack.length - 1]?.menu ?? null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Push a new Menu onto the stack and display it.
|
|
76
|
+
* If a menu item carries a PresentSubMenuAction it will automatically
|
|
77
|
+
* push the sub-menu when pressed; all other items delegate to
|
|
78
|
+
* the optional `onItemPress` callback.
|
|
79
|
+
*/
|
|
80
|
+
open(
|
|
81
|
+
menu: Menu,
|
|
82
|
+
onItemPress?: (item: MenuItem) => void | Promise<void>
|
|
83
|
+
): void {
|
|
84
|
+
const viewModel = new ContentViewModel(menu.content);
|
|
85
|
+
this._stack.push({ menu, onItemPress, viewModel });
|
|
86
|
+
this._present(menu, viewModel, onItemPress);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Pop the current modal and return to the previous one.
|
|
91
|
+
* If the stack has only one entry, closes entirely.
|
|
92
|
+
*/
|
|
93
|
+
back(): void {
|
|
94
|
+
if (this._stack.length === 0) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this._stack.pop();
|
|
99
|
+
|
|
100
|
+
const previous = this._stack[this._stack.length - 1];
|
|
101
|
+
|
|
102
|
+
if (previous) {
|
|
103
|
+
this._present(previous.menu, previous.viewModel, previous.onItemPress);
|
|
104
|
+
} else {
|
|
105
|
+
dismissModal();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Close all modals and clear the entire stack.
|
|
111
|
+
*/
|
|
112
|
+
close(): void {
|
|
113
|
+
this._stack = [];
|
|
114
|
+
dismissModal();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Replace the current top of the stack without adding a new history entry.
|
|
119
|
+
*/
|
|
120
|
+
replace(
|
|
121
|
+
menu: Menu,
|
|
122
|
+
onItemPress?: (item: MenuItem) => void | Promise<void>
|
|
123
|
+
): void {
|
|
124
|
+
if (this._stack.length === 0) {
|
|
125
|
+
this.open(menu, onItemPress);
|
|
126
|
+
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const viewModel = new ContentViewModel(menu.content);
|
|
131
|
+
this._stack[this._stack.length - 1] = { menu, onItemPress, viewModel };
|
|
132
|
+
this._present(menu, viewModel, onItemPress);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private _buildOnPress(
|
|
136
|
+
onItemPress?: (item: MenuItem) => void | Promise<void>
|
|
137
|
+
): (item: MenuItem) => void | Promise<void> {
|
|
138
|
+
return (item: MenuItem) => {
|
|
139
|
+
if (isPresentSubMenuAction(item.action)) {
|
|
140
|
+
// Navigate deeper into the sub-menu
|
|
141
|
+
this.open(item.action.menu, onItemPress);
|
|
142
|
+
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return onItemPress?.(item);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Called when the UI dismisses the modal (swipe / overlay tap).
|
|
152
|
+
* Clears the stack without calling dismissModal() again to avoid
|
|
153
|
+
* a feedback loop with the store reset.
|
|
154
|
+
*/
|
|
155
|
+
private _onUIDismiss(): void {
|
|
156
|
+
this._stack = [];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* True when the Zustand modal store already has a visible bottom sheet.
|
|
161
|
+
* Used to skip the slide-in animation when navigating to a deeper level.
|
|
162
|
+
*/
|
|
163
|
+
private get _isOpen(): boolean {
|
|
164
|
+
return modalStore.getState().modalState.visible;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private _present(
|
|
168
|
+
menu: Menu,
|
|
169
|
+
viewModel: ContentViewModel,
|
|
170
|
+
onItemPress?: (item: MenuItem) => void | Promise<void>
|
|
171
|
+
): void {
|
|
172
|
+
const key = `menu-level-${this._stack.length}`;
|
|
173
|
+
|
|
174
|
+
const args = menuToModalArgs(
|
|
175
|
+
menu,
|
|
176
|
+
viewModel,
|
|
177
|
+
this._buildOnPress(onItemPress),
|
|
178
|
+
key
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
const alreadyOpen = this._isOpen;
|
|
182
|
+
|
|
183
|
+
openBottomSheetModal({
|
|
184
|
+
...args,
|
|
185
|
+
options: {
|
|
186
|
+
...args.options,
|
|
187
|
+
// Skip the slide-in when updating an already-visible sheet (sub-level nav)
|
|
188
|
+
animated: !alreadyOpen,
|
|
189
|
+
animationType: alreadyOpen ? "none" : "slide",
|
|
190
|
+
// Keep the stack in sync when the user dismisses via swipe or overlay
|
|
191
|
+
onDismiss: () => this._onUIDismiss(),
|
|
192
|
+
onRequestClose: () => this._onUIDismiss(),
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export const modalOrchestrator = new ModalOrchestrator();
|