-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathExitIframe.jsx
43 lines (38 loc) · 1.25 KB
/
ExitIframe.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { useAppBridge } from "@shopify/app-bridge-react";
import { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
import { Banner, Layout, Page } from "@shopify/polaris";
export default function ExitIframe() {
const app = useAppBridge();
const { search } = useLocation();
const [showWarning, setShowWarning] = useState(false);
app.loading(true);
useEffect(() => {
if (!!app && !!search) {
const params = new URLSearchParams(search);
const redirectUri = params.get("redirectUri");
const url = new URL(decodeURIComponent(redirectUri));
if (
[location.hostname, "admin.shopify.com"].includes(url.hostname) ||
url.hostname.endsWith(".myshopify.com")
) {
window.open(url, "_top");
} else {
setShowWarning(true);
}
}
}, [app, search, setShowWarning]);
return showWarning ? (
<Page narrowWidth>
<Layout>
<Layout.Section>
<div style={{ marginTop: "100px" }}>
<Banner title="Redirecting outside of Shopify" status="warning">
Apps can only use /exitiframe to reach Shopify or the app itself.
</Banner>
</div>
</Layout.Section>
</Layout>
</Page>
) : null;
}