@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
64 lines (61 loc) • 3.09 kB
JavaScript
import { __extends, __assign } from 'tslib';
import hoistStatics from 'hoist-non-react-statics';
import React from 'react';
import { throwError, AppActionType } from '@shopify/app-bridge-core/actions/Error';
import { appBridgeMiddlewareProptype } from './PropTypes.js';
function getComponentName(component) {
return (component && (component.displayName || component.name)) || 'Component';
}
function withApp(storeToProps, dispatchToProps) {
return function (WrappedComponent) {
var AppBridgeConnector = /** @class */ (function (_super) {
__extends(AppBridgeConnector, _super);
function AppBridgeConnector(props, context) {
var _this = _super.call(this, props, context) || this;
_this.state = {
mounted: false,
};
return _this;
}
AppBridgeConnector.prototype.augmentProps = function () {
var _a = this, props = _a.props, app = _a.app, mappedProps = _a.mappedProps;
var appState = app.getState();
var store = storeToProps ? storeToProps(appState) : appState;
var augmented = __assign(__assign({}, props), { app: app, store: store });
if (mappedProps) {
return __assign(__assign({}, mappedProps), augmented);
}
return augmented;
};
AppBridgeConnector.prototype.componentDidMount = function () {
var config = this.props.config;
var appBridgeMiddleware = this.context.appBridgeMiddleware;
if (!config) {
throwError(AppActionType.MISSING_CONFIG, 'Missing required prop `config`');
}
if (!appBridgeMiddleware || typeof appBridgeMiddleware.load !== 'function') {
throwError(AppActionType.MISSING_APP_BRIDGE_MIDDLEWARE, 'Missing required context `appBridgeMiddleware`. Maybe you forgot the App Bridge `<Provider>` component?');
}
this.app = this.context.appBridgeMiddleware.load({
config: config,
type: 'application',
});
if (dispatchToProps) {
this.mappedProps = dispatchToProps(this.app);
}
this.setState({ mounted: true });
};
AppBridgeConnector.prototype.render = function () {
return this.state.mounted ? React.createElement(WrappedComponent, __assign({}, this.augmentProps())) : null;
};
AppBridgeConnector.contextTypes = {
appBridgeMiddleware: appBridgeMiddlewareProptype,
};
AppBridgeConnector.displayName = "AppBridge(".concat(getComponentName(WrappedComponent), ")");
AppBridgeConnector.WrappedComponent = WrappedComponent;
return AppBridgeConnector;
}(React.Component));
return hoistStatics(AppBridgeConnector, WrappedComponent);
};
}
export { withApp };