@shopify/app-bridge-host
Version:
App Bridge Host contains components and middleware to be consumed by the app's host, as well as the host itself. The middleware and `Frame` component are responsible for facilitating communication between the client and host, and used to act on actions se
55 lines (52 loc) • 2.85 kB
JavaScript
import { __assign } from 'tslib';
import * as TitleBar from '@shopify/app-bridge-core/actions/TitleBar';
import { isGroupedButton } from '@shopify/app-bridge-core/actions/ButtonGroup';
import { getMergedProps } from '@shopify/app-bridge-core/actions/helper';
import generateUuid from '@shopify/app-bridge-core/actions/uuid';
import { resetStateReducer } from '../utilities.js';
import { titleBarActionCreatorsMap } from './actionCreators.js';
import titleBarReducer, { defaultTitlebarStore } from './reducer.js';
import { unsub, updateButton } from './utils.js';
export { SET_APP_INFO, SET_PAGINATION, setAppInfo, setPagination } from './actions.js';
var getApi = function (_a) {
var actions = _a.actions, dispatch = _a.dispatch, subscribe = _a.subscribe, getState = _a.getState;
var api = {
update: function (payload) {
var newPayload = getMergedProps(getState().titleBar || {}, payload);
var buttons = newPayload.buttons;
// [TODO] revisit unsubscribe implementation
// Releases all subscriptions and retained methods because we're rebuilding all of them after.
unsub();
if (buttons === null || buttons === void 0 ? void 0 : buttons.primary) {
buttons.primary = __assign(__assign({}, buttons.primary), { id: updateButton(TitleBar.Action.BUTTON_CLICK, buttons.primary, subscribe, dispatch) });
}
if (buttons === null || buttons === void 0 ? void 0 : buttons.secondary) {
buttons.secondary = buttons.secondary.map(function (btn) {
if (isGroupedButton(btn)) {
// Groups can only be one level deep so it's not necessary to make a recursive call
return __assign(__assign({}, btn), { id: generateUuid(), buttons: btn.buttons.map(function (groupBtn) { return (__assign(__assign({}, groupBtn), { id: updateButton(TitleBar.Action.BUTTON_CLICK, groupBtn, subscribe, dispatch) })); }) });
}
else {
return __assign(__assign({}, btn), { id: updateButton(TitleBar.Action.BUTTON_CLICK, btn, subscribe, dispatch) });
}
});
}
actions.update(newPayload);
},
};
return api;
};
/**
* An object containing the key, actions, initial state and reducer of the TitleBar feature
* Can be used with the `withFeature` decorator to add the reducer
* and then make its actions and store available to the wrapped component
* @public
* */
var feature = {
actions: titleBarActionCreatorsMap,
key: 'titleBar',
initialState: defaultTitlebarStore,
reducer: resetStateReducer(titleBarReducer),
getApi: getApi,
};
export { titleBarReducer as default, defaultTitlebarStore, feature, titleBarActionCreatorsMap };