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

123 lines (122 loc) 5.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = require("react"); var react_compose_1 = __importDefault(require("@shopify/react-compose")); var actions_1 = require("@shopify/app-bridge/actions"); var safe_redirect_1 = require("@shopify/app-bridge/validate/safe-redirect"); var resolver_1 = __importDefault(require("./utilities/resolver")); var getResourceUrl_1 = __importDefault(require("./utilities/getResourceUrl")); var error_1 = require("../utilities/error"); var appUrl_1 = require("../utilities/appUrl"); var withFeature_1 = __importDefault(require("../../withFeature")); var HostProvider_1 = require("../../HostProvider"); var navigation_1 = require("../../store/reducers/embeddedApp/navigation"); var helpers_1 = require("../../store/middlewares/mobile/helpers"); /** * Handles all actions for the Navigation feature * @public * @requires RouterContext * */ function Navigation(props) { var routerContext = react_1.useContext(HostProvider_1.RouterContext); var updateAction = props.store.updateAction; react_1.useEffect(updateRoute, [updateAction]); return null; function updateRoute() { var actions = props.actions, updateAction = props.store.updateAction; if (!routerContext) { return error_1.throwMissingRouterError(); } var appRoot = routerContext.appRoot, hostname = routerContext.hostname, history = routerContext.history, _a = routerContext.location, pathname = _a.pathname, search = _a.search; var currentUrl = "" + pathname + search; if (!updateAction) { return; } var newUrl; var newContext; var type = updateAction.type, target = updateAction.target, payload = updateAction.payload; switch (target) { case actions_1.Redirect.Action.REMOTE: if (isRemotePayload(payload)) { newContext = payload.newContext; newUrl = payload.url; if (!newContext) { if (newUrl === window.location.href) { actions.completeRouteUpdate(); return; } var urlParts = new URL(newUrl); if (urlParts.hostname === hostname && !helpers_1.isMobile()) { history.push("" + urlParts.pathname + urlParts.search); } else if (safe_redirect_1.isSafe(newUrl)) { window.location.assign(newUrl); } actions.completeRouteUpdate(); return; } } break; case actions_1.Redirect.Action.APP: if (isAppPayload(payload)) { newUrl = resolver_1.default.app(appRoot, appUrl_1.normalizeRelativePath(payload.path)); } break; case actions_1.Redirect.Action.ADMIN_PATH: if (isAdminPathPayload(payload)) { newUrl = "" + resolver_1.default.home() + payload.path; newContext = payload.newContext; } break; case actions_1.Redirect.Action.ADMIN_SECTION: if (isAdminSectionPayload(payload)) { newUrl = getResourceUrl_1.default(resolver_1.default, payload.section); newContext = payload.newContext; } break; } if (newContext) { window.open(newUrl, '_blank'); actions.completeRouteUpdate(); return; } if (!newUrl || currentUrl === newUrl) { actions.completeRouteUpdate(); return; } switch (type) { case actions_1.History.Action.PUSH: { history.push(newUrl); break; } case actions_1.History.Action.REPLACE: { history.replace(newUrl); break; } default: history.push(newUrl); } actions.completeRouteUpdate(); } } exports.Navigation = Navigation; function isAdminPathPayload(payload) { return payload && payload.hasOwnProperty('path'); } function isAppPayload(payload) { return payload && payload.hasOwnProperty('path'); } function isRemotePayload(payload) { return payload && payload.hasOwnProperty('url'); } function isAdminSectionPayload(payload) { return payload && payload.hasOwnProperty('section'); } /** * The Navigation feature with its reducer, actions and a navigation actions handler * @public * */ exports.default = react_compose_1.default(withFeature_1.default(navigation_1.feature))(Navigation);