UNPKG

5.78 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __rest = (this && this.__rest) || function (s, e) {
14 var t = {};
15 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16 t[p] = s[p];
17 if (s != null && typeof Object.getOwnPropertySymbols === "function")
18 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
19 t[p[i]] = s[p[i]];
20 return t;
21};
22var __importStar = (this && this.__importStar) || function (mod) {
23 if (mod && mod.__esModule) return mod;
24 var result = {};
25 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
26 result["default"] = mod;
27 return result;
28};
29var __importDefault = (this && this.__importDefault) || function (mod) {
30 return (mod && mod.__esModule) ? mod : { "default": mod };
31};
32Object.defineProperty(exports, "__esModule", { value: true });
33var react_1 = __importStar(require("react"));
34var app_bridge_1 = require("@shopify/app-bridge");
35var actions_1 = require("@shopify/app-bridge/actions");
36var react_compose_1 = __importDefault(require("@shopify/react-compose"));
37var appUrl_1 = require("./utilities/appUrl");
38var error_1 = require("./utilities/error");
39var Frame_1 = __importDefault(require("../Frame"));
40var HostProvider_1 = require("../HostProvider");
41var withFeature_1 = __importDefault(require("../withFeature"));
42var navigation_1 = require("../store/reducers/embeddedApp/navigation");
43var appBridge_1 = require("../store/reducers/embeddedApp/appBridge");
44var style = {
45 position: 'relative',
46 border: 'none',
47 width: '100%',
48 flex: '1',
49 display: 'flex',
50};
51/**
52 * Renders a Frame component with the Context set to `Main`
53 * Handles updating the iframe url for all app-related Navigation actions
54 * @public
55 * @requires RouterContext
56 * @requires HostContext
57 * */
58function MainFrame(props) {
59 var hostContext = react_1.useContext(HostProvider_1.HostContext);
60 var routerContext = react_1.useContext(HostProvider_1.RouterContext);
61 var iframe = react_1.useRef();
62 if (!hostContext) {
63 error_1.throwMissingHostProvider();
64 return null;
65 }
66 if (!routerContext) {
67 error_1.throwMissingRouterError();
68 return null;
69 }
70 var app = hostContext.app, config = hostContext.config;
71 var location = routerContext.location;
72 var pathname = location.pathname;
73 var apiKey = config.apiKey, handle = config.handle, url = config.url;
74 var appURL = react_1.useRef(appUrl_1.buildAppUrl({ handle: handle, apiKey: apiKey, url: url, pathname: pathname })).current;
75 var _a = react_1.useState(false), shouldUpdate = _a[0], setShouldUpdate = _a[1];
76 var actions = props.actions, updateAction = props.store.updateAction, onInit = props.onInit, onLocationUpdate = props.onLocationUpdate, extraProps = __rest(props, ["actions", "store", "onInit", "onLocationUpdate"]);
77 var appControlledRedirect = app.isTransportSubscribed(app_bridge_1.Context.Main, actions_1.Redirect.ActionType.APP);
78 var initHandler = react_1.useCallback(function (frame) {
79 iframe.current = frame.iframe;
80 if (onInit) {
81 onInit(frame);
82 }
83 }, [onInit]);
84 react_1.useEffect(function () {
85 if (!updateAction) {
86 setShouldUpdate(true);
87 return;
88 }
89 var type = updateAction.type;
90 if (type === actions_1.History.Action.PUSH || type === actions_1.History.Action.REPLACE) {
91 setShouldUpdate(false);
92 return;
93 }
94 setShouldUpdate(true);
95 }, [updateAction]);
96 /**
97 * Reload the iframe for apps that don't handle their own redirects
98 * We only update the iframe url after the config change
99 * because we need to refetch the app url from the server
100 * */
101 react_1.useEffect(function () {
102 if (!shouldUpdate || appControlledRedirect) {
103 return;
104 }
105 var pathname = location.pathname;
106 var iframeUrl = appUrl_1.buildAppUrl({ handle: handle, apiKey: apiKey, url: url, pathname: pathname });
107 app.dispatch(appBridge_1.reset());
108 updateIframeUrl(iframe.current, iframeUrl.href);
109 }, [apiKey, handle, url]);
110 /**
111 * Notify the app of location changes if app is handling their own redirects
112 * */
113 react_1.useEffect(function () {
114 if (!shouldUpdate) {
115 return;
116 }
117 var pathname = location.pathname, search = location.search;
118 var iframeUrl = appUrl_1.buildAppUrl({ handle: handle, apiKey: apiKey, url: url, pathname: pathname });
119 if (appControlledRedirect) {
120 actions.handleRedirectApp({ path: "" + iframeUrl.pathname + search });
121 }
122 else {
123 onLocationUpdate(location);
124 }
125 }, [location]);
126 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: appURL.href, onInit: initHandler })));
127}
128exports.MainFrame = MainFrame;
129function updateIframeUrl(iframe, newUrl) {
130 if (!iframe || !iframe.contentWindow) {
131 return;
132 }
133 iframe.contentWindow.location.replace(newUrl);
134}
135/**
136 * The MainFrame component with the Navigation feature
137 * @public
138 * */
139exports.default = react_compose_1.default(withFeature_1.default(navigation_1.feature))(MainFrame);