@applicaster/zapp-react-native-utils 15.1.5-rc.2 → 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/analyticsUtils/PlayerAnalyticsManager.ts +1 -1
- package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +12 -6
- package/appUtils/playerManager/__tests__/playerFactory.test.ts +1 -1
- package/appUtils/playerManager/{conts.ts → const.ts} +20 -0
- package/appUtils/playerManager/index.ts +1 -1
- package/appUtils/playerManager/player.ts +1 -1
- package/appUtils/playerManager/playerFactory.ts +1 -1
- package/appUtils/playerManager/playerNative.ts +1 -1
- package/appUtils/playerManager/usePlayerControllerSetup.tsx +1 -1
- package/appUtils/playerManager/userLanguagePreference.ts +1 -1
- package/manifestUtils/defaultManifestConfigurations/player.js +4 -4
- 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>;
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
} from "../appUtils/playerManager";
|
|
9
9
|
import { Player } from "../appUtils/playerManager/player";
|
|
10
10
|
import { AnalyticPlayerListener } from "./AnalyticPlayerListener";
|
|
11
|
-
import { PlayerRole } from "../appUtils/playerManager/
|
|
11
|
+
import { PlayerRole } from "../appUtils/playerManager/const";
|
|
12
12
|
|
|
13
13
|
enum PlayerAnalyticsTrackerType {
|
|
14
14
|
PlayerAnalyticsTracker = "PlayerAnalyticsTracker",
|
|
@@ -59,6 +59,7 @@ type PlayNextConfig = {
|
|
|
59
59
|
export type PlayNextState = PlayNextConfig & {
|
|
60
60
|
triggerTime: number;
|
|
61
61
|
handleUserCancelPlayNext: () => void;
|
|
62
|
+
hidePlayNext: () => void;
|
|
62
63
|
};
|
|
63
64
|
|
|
64
65
|
export class OverlaysObserver {
|
|
@@ -76,7 +77,7 @@ export class OverlaysObserver {
|
|
|
76
77
|
private chapterMarkerEvents: ChapterMarkerEvent[];
|
|
77
78
|
readonly player: Player;
|
|
78
79
|
private playNextConfig?: PlayNextConfig;
|
|
79
|
-
private
|
|
80
|
+
private isPlayNextSuppressed = false;
|
|
80
81
|
|
|
81
82
|
constructor({ player }: ChapterMarkersObserverProps) {
|
|
82
83
|
this.chapterSubject = new BehaviorSubject(null);
|
|
@@ -143,11 +144,15 @@ export class OverlaysObserver {
|
|
|
143
144
|
return this.titleSummarySubject.asObservable().pipe(distinctUntilChanged());
|
|
144
145
|
}
|
|
145
146
|
|
|
146
|
-
|
|
147
|
-
this.
|
|
147
|
+
hidePlayNext = () => {
|
|
148
|
+
this.isPlayNextSuppressed = true;
|
|
148
149
|
this.playNextSubject.next(null);
|
|
149
150
|
};
|
|
150
151
|
|
|
152
|
+
handleUserCancelPlayNext = () => {
|
|
153
|
+
this.hidePlayNext();
|
|
154
|
+
};
|
|
155
|
+
|
|
151
156
|
preparePlayNext = async () => {
|
|
152
157
|
try {
|
|
153
158
|
const plugins = appStore.get("plugins");
|
|
@@ -270,7 +275,7 @@ export class OverlaysObserver {
|
|
|
270
275
|
|
|
271
276
|
// TODO: Hack for video end, will be replaced with playlist prev/next in the future
|
|
272
277
|
getPlayNextEntry = () =>
|
|
273
|
-
!this.
|
|
278
|
+
!this.isPlayNextSuppressed ? this.playNextConfig?.entry : null;
|
|
274
279
|
|
|
275
280
|
prepareChapterMarkers = () => {
|
|
276
281
|
const chapterMarkers: ChapterMarkerOriginal[] =
|
|
@@ -353,7 +358,7 @@ export class OverlaysObserver {
|
|
|
353
358
|
const shouldPlayNextBeVisible = currentTime > triggerTime;
|
|
354
359
|
|
|
355
360
|
if (shouldPlayNextBeVisible) {
|
|
356
|
-
if (this.
|
|
361
|
+
if (this.isPlayNextSuppressed) {
|
|
357
362
|
return;
|
|
358
363
|
}
|
|
359
364
|
|
|
@@ -361,10 +366,11 @@ export class OverlaysObserver {
|
|
|
361
366
|
...this.playNextConfig,
|
|
362
367
|
triggerTime,
|
|
363
368
|
handleUserCancelPlayNext: this.handleUserCancelPlayNext,
|
|
369
|
+
hidePlayNext: this.hidePlayNext,
|
|
364
370
|
});
|
|
365
371
|
} else {
|
|
366
372
|
this.playNextSubject.next(null);
|
|
367
|
-
this.
|
|
373
|
+
this.isPlayNextSuppressed = false;
|
|
368
374
|
}
|
|
369
375
|
};
|
|
370
376
|
|
|
@@ -10,7 +10,7 @@ jest.mock("@applicaster/zapp-react-native-utils/pluginUtils", () => ({
|
|
|
10
10
|
mockFindPluginByIdentifier(id, plugins),
|
|
11
11
|
}));
|
|
12
12
|
|
|
13
|
-
import { PlayerRole } from "../
|
|
13
|
+
import { PlayerRole } from "../const";
|
|
14
14
|
import { playerFactory } from "../playerFactory";
|
|
15
15
|
|
|
16
16
|
const fakeComponent = () => null;
|
|
@@ -67,3 +67,23 @@ export enum PlayerRole {
|
|
|
67
67
|
Chromecast = "Chromecast",
|
|
68
68
|
Unspecified = "Unspecified",
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The playback engine backing a web player. Published on
|
|
73
|
+
* `window.applicaster.playerType` when a player is created, so that plugins
|
|
74
|
+
* observing the player can tell which engine they are looking at instead of
|
|
75
|
+
* inferring it from the API the instance happens to expose.
|
|
76
|
+
*
|
|
77
|
+
* Keep in sync with `ApplicasterGlobals["playerType"]` in applicaster-types.
|
|
78
|
+
*/
|
|
79
|
+
export enum WebPlayerEngine {
|
|
80
|
+
Videojs = "videojs",
|
|
81
|
+
Hlsjs = "hlsjs",
|
|
82
|
+
Dashjs = "dashjs",
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export enum PLAYER_EVENTS {
|
|
86
|
+
PlayNextTriggered = "play_next_triggered",
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type PlayNextTriggeredEvent = { id: string };
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
unsubscribeToDeviceNetworkChanges,
|
|
10
10
|
} from "@applicaster/quick-brick-core/App/NetworkStatusProvider/utils";
|
|
11
11
|
import { setUserLanguagePreference } from "./userLanguagePreference";
|
|
12
|
-
import { PlayerRole } from "./
|
|
12
|
+
import { PlayerRole } from "./const";
|
|
13
13
|
import { RNPlayerManager } from "./nativePlayerManager";
|
|
14
14
|
|
|
15
15
|
const { log_error, log_debug, log_warning } = createLogger({
|
|
@@ -3,7 +3,7 @@ import { BehaviorSubject } from "rxjs";
|
|
|
3
3
|
import { isEmptyOrNil } from "@applicaster/zapp-react-native-utils/cellUtils";
|
|
4
4
|
import { createLogger, utilsLogger } from "../../logger";
|
|
5
5
|
import { getBoolFromConfigValue } from "../../configurationUtils";
|
|
6
|
-
import { PlayerRole, SharedPlayerCallBacksKeys } from "./
|
|
6
|
+
import { PlayerRole, SharedPlayerCallBacksKeys } from "./const";
|
|
7
7
|
import { OverlaysObserver } from "./OverlayObserver/OverlaysObserver";
|
|
8
8
|
import { TVSeekController } from "../../reactHooks/player/TVSeekControlller/TVSeekController";
|
|
9
9
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { appStore } from "@applicaster/zapp-react-native-redux/AppStore";
|
|
2
2
|
import { findPluginByIdentifier } from "@applicaster/zapp-react-native-utils/pluginUtils";
|
|
3
3
|
import { Player } from "./player";
|
|
4
|
-
import { PlayerRole } from "./
|
|
4
|
+
import { PlayerRole } from "./const";
|
|
5
5
|
|
|
6
6
|
type PlayerFactoryItem = {
|
|
7
7
|
Component: any;
|
|
@@ -3,7 +3,7 @@ import { isEmptyOrNil } from "@applicaster/zapp-react-native-utils/cellUtils";
|
|
|
3
3
|
import { createLogger, utilsLogger } from "../../logger";
|
|
4
4
|
import { Player } from "./player";
|
|
5
5
|
import { findNodeHandle } from "react-native";
|
|
6
|
-
import { PlayerRole } from "./
|
|
6
|
+
import { PlayerRole } from "./const";
|
|
7
7
|
import { toBooleanWithDefaultFalse } from "../../booleanUtils";
|
|
8
8
|
import { RNPlayerManager } from "./nativePlayerManager";
|
|
9
9
|
|
|
@@ -3,7 +3,7 @@ import { useConfiguration } from "../../reactHooks/useConfiguration";
|
|
|
3
3
|
import { useEffect } from "react";
|
|
4
4
|
import { playerManager } from "./index";
|
|
5
5
|
import { useValidatePlayerConfig } from "../../playerUtils/useValidatePlayerConfig";
|
|
6
|
-
import { PlayerRole } from "./
|
|
6
|
+
import { PlayerRole } from "./const";
|
|
7
7
|
import { isAppleTV } from "@applicaster/zapp-react-native-ui-components/Helpers/Platform";
|
|
8
8
|
import { TVSeekController } from "../../reactHooks/player/TVSeekControlller/TVSeekController";
|
|
9
9
|
import { KeyInputHandler } from "../keyInputHandler/KeyInputHandler";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage";
|
|
2
2
|
import { sessionStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/SessionStorage";
|
|
3
3
|
import { createLogger, utilsLogger } from "../../logger";
|
|
4
|
-
import { userPreferencesNamespace } from "./
|
|
4
|
+
import { userPreferencesNamespace } from "./const";
|
|
5
5
|
|
|
6
6
|
export const preferredAudioLanguageStorageKey = "preferred_audio_language";
|
|
7
7
|
|
|
@@ -3464,13 +3464,13 @@ function getPlayerConfiguration({ platform, version }) {
|
|
|
3464
3464
|
label: "Advanced Player Styles",
|
|
3465
3465
|
type: "text_input",
|
|
3466
3466
|
multiline: true,
|
|
3467
|
-
initial_value: `.vjs-text-track-cue {
|
|
3468
|
-
|
|
3469
|
-
top: 94% !important;
|
|
3467
|
+
initial_value: `.vjs-text-track-cue {
|
|
3468
|
+
top: auto !important;
|
|
3470
3469
|
bottom: 0 !important;
|
|
3471
3470
|
left: 0 !important;
|
|
3472
|
-
right:
|
|
3471
|
+
right: auto !important;
|
|
3473
3472
|
width: 100% !important;
|
|
3473
|
+
height: auto !important;
|
|
3474
3474
|
}`,
|
|
3475
3475
|
label_tooltip:
|
|
3476
3476
|
"This field allows you to inject custom CSS styles to override default player styles. The styles will be injected into the document head and can be used to customize the appearance of the player. Leave empty to use only default styles.",
|
|
@@ -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
|
+
}
|