UNPKG

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