UNPKG

@shopify/app-bridge-host

Version:

App Bridge Host contains middleware and components that are meant to be consumed by the app's host. The middleware and `Frame` component are responsible for facilitating messages posted between the client and host, and used to act on actions sent from the

127 lines (126 loc) 5.23 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; return t; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = __importStar(require("react")); var app_bridge_1 = require("@shopify/app-bridge"); var actions_1 = require("@shopify/app-bridge/actions"); var react_compose_1 = __importDefault(require("@shopify/react-compose")); var appUrl_1 = require("./utilities/appUrl"); var error_1 = require("./utilities/error"); var Frame_1 = __importDefault(require("../Frame")); var HostProvider_1 = require("../HostProvider"); var withFeature_1 = __importDefault(require("../withFeature")); var navigation_1 = require("../store/reducers/embeddedApp/navigation"); var appBridge_1 = require("../store/reducers/embeddedApp/appBridge"); var style = { position: 'relative', border: 'none', width: '100%', flex: '1', display: 'flex', }; /** * Renders a Frame component with the Context set to `Main` * Handles updating the iframe url for all app-related Navigation actions * @public * @requires RouterContext * @requires HostContext * */ function MainFrame(props) { var hostContext = react_1.useContext(HostProvider_1.HostContext); var routerContext = react_1.useContext(HostProvider_1.RouterContext); if (!hostContext) { error_1.throwMissingHostProvider(); return null; } if (!routerContext) { error_1.throwMissingRouterError(); return null; } var app = hostContext.app, config = hostContext.config; var location = routerContext.location; var _a = react_1.useState(), iframeUrl = _a[0], setIframeUrl = _a[1]; var _b = react_1.useState(false), shouldUpdate = _b[0], setShouldUpdate = _b[1]; var actions = props.actions, updateAction = props.store.updateAction, extraProps = __rest(props, ["actions", "store"]); function updateIframeUrl(iframe, newUrl) { if (app.isTransportSubscribed(app_bridge_1.Context.Main, actions_1.Redirect.ActionType.APP)) { return; } if (!iframe || !iframe.contentWindow) { return; } app.dispatch(appBridge_1.reset()); iframe.contentWindow.location.replace(newUrl); } react_1.useEffect(function () { var pathname = location.pathname; var apiKey = config.apiKey, handle = config.handle, url = config.url; setIframeUrl(appUrl_1.buildAppUrl({ handle: handle, apiKey: apiKey, url: url, pathname: pathname })); }, []); react_1.useEffect(function () { if (!updateAction) { setShouldUpdate(true); return; } var target = updateAction.target, type = updateAction.type; if (type === actions_1.History.Action.PUSH || type === actions_1.History.Action.REPLACE || target !== actions_1.Redirect.Action.APP) { setShouldUpdate(false); return; } setShouldUpdate(true); }, [updateAction]); react_1.useEffect(function () { if (!shouldUpdate) { return; } var pathname = location.pathname, search = location.search; var apiKey = config.apiKey, handle = config.handle, url = config.url; var iframeUrl = appUrl_1.buildAppUrl({ handle: handle, apiKey: apiKey, url: url, pathname: pathname }); if (app.isTransportSubscribed(app_bridge_1.Context.Main, actions_1.Redirect.ActionType.APP)) { actions.handleRedirectApp({ path: "" + iframeUrl.pathname + search }); return; } setIframeUrl(iframeUrl); }, [location]); if (!iframeUrl) { return null; } return (react_1.default.createElement(Frame_1.default, __assign({}, extraProps, { config: config, style: style, context: app_bridge_1.Context.Main, app: app, title: config.name, url: iframeUrl.href, onUrlChange: updateIframeUrl }))); } exports.MainFrame = MainFrame; /** * The MainFrame component with the Navigation feature * @public * */ exports.default = react_compose_1.default(withFeature_1.default(navigation_1.feature))(MainFrame);