-
Notifications
You must be signed in to change notification settings - Fork 295
/
LiveApp.tsx
96 lines (91 loc) · 3.11 KB
/
LiveApp.tsx
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React, { useCallback, useMemo } from "react";
import { useHistory, useLocation } from "react-router-dom";
import useTheme from "~/renderer/hooks/useTheme";
import { Card } from "~/renderer/components/Box";
import WebPlatformPlayer from "~/renderer/components/WebPlatformPlayer";
import { languageSelector } from "~/renderer/reducers/settings";
import { useSelector } from "react-redux";
import { useRemoteLiveAppManifest } from "@ledgerhq/live-common/platform/providers/RemoteLiveAppProvider/index";
import { useLocalLiveAppManifest } from "@ledgerhq/live-common/wallet-api/LocalLiveAppProvider/index";
type Props = {
match: {
params: {
appId: string;
};
isExact: boolean;
path: string;
url: string;
};
appId?: string;
location: {
hash: string;
params: {
[key: string]: string;
};
pathname: string;
search: string;
customDappUrl?: string;
};
};
export function LiveApp({ match, appId: propsAppId, location }: Props) {
const history = useHistory();
const { params: internalParams, search } = location;
const { state: urlParams, customDappUrl } = useLocation() as ReturnType<typeof useLocation> &
Props["location"] & {
state: {
returnTo: string;
accountId?: string;
customDappUrl?: string;
};
};
const appId = propsAppId || match.params?.appId;
const returnTo = useMemo(() => {
const params = new URLSearchParams(search);
return urlParams?.returnTo || params.get("returnTo") || internalParams?.returnTo;
}, [search, urlParams?.returnTo, internalParams?.returnTo]);
const _customDappUrl = useMemo(() => {
const params = new URLSearchParams(search);
return (
customDappUrl ||
urlParams?.customDappUrl ||
(params.has("customDappUrl") && params.get("customDappUrl")) ||
internalParams?.customDappUrl
);
}, [search, customDappUrl, urlParams?.customDappUrl, internalParams?.customDappUrl]);
const handleClose = useCallback(() => history.push(returnTo || `/platform`), [history, returnTo]);
const themeType = useTheme().colors.palette.type;
const lang = useSelector(languageSelector);
const params = {
theme: themeType,
lang,
...urlParams,
...internalParams,
};
const localManifest = useLocalLiveAppManifest(appId);
const remoteManifest = useRemoteLiveAppManifest(appId);
let manifest = localManifest || remoteManifest;
if (_customDappUrl && manifest && manifest.params && "dappUrl" in manifest.params) {
manifest = {
...manifest,
params: {
...manifest.params,
dappUrl: _customDappUrl,
},
};
}
// TODO for next urlscheme evolutions:
// - check if local settings allow to launch an app from this branch, else display an error
// - check if the app is available in store, else display a loader if apps are getting fetched from remote, else display an error stating that the app doesn't exist
return (
<Card
grow
style={{
overflow: "hidden",
}}
>
{manifest ? (
<WebPlatformPlayer manifest={manifest} onClose={handleClose} inputs={params} />
) : null}
</Card>
);
}