-
Notifications
You must be signed in to change notification settings - Fork 315
/
LiveAppDrawer.tsx
202 lines (188 loc) · 6.62 KB
/
LiveAppDrawer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { useState, useCallback, useMemo } from "react";
import styled from "styled-components";
import { useDispatch, useSelector } from "react-redux";
import { useTranslation } from "react-i18next";
import { SideDrawer } from "~/renderer/components/SideDrawer";
import CheckBox from "~/renderer/components/CheckBox";
import Button from "~/renderer/components/Button";
import { openURL } from "~/renderer/linking";
import Box from "~/renderer/components/Box";
import { dismissBanner } from "~/renderer/actions/settings";
import { closePlatformAppDrawer } from "~/renderer/actions/UI";
import { platformAppDrawerStateSelector } from "~/renderer/reducers/UI";
import Text from "./Text";
import { AppDetails } from "./Platform/AppDetails";
import ExternalLink from "./ExternalLink/index";
import LiveAppDisclaimer from "./WebPlatformPlayer/LiveAppDisclaimer";
import { AppManifest } from "@ledgerhq/live-common/wallet-api/types";
import DeviceAction from "~/renderer/components/DeviceAction";
import {
createAction,
StartExchangeErrorResult,
StartExchangeSuccessResult,
} from "@ledgerhq/live-common/hw/actions/startExchange";
import startExchange from "@ledgerhq/live-common/exchange/platform/startExchange";
import connectApp from "@ledgerhq/live-common/hw/connectApp";
import CompleteExchange, {
Data as CompleteExchangeData,
isCompleteExchangeData,
} from "~/renderer/modals/Platform/Exchange/CompleteExchange/Body";
import { ExchangeType } from "@ledgerhq/live-common/wallet-api/Exchange/server";
import { Exchange } from "@ledgerhq/live-common/exchange/types";
import { renderLoading } from "./DeviceAction/rendering";
const Divider = styled(Box)`
border: 1px solid ${p => p.theme.colors.palette.divider};
`;
const ContentWrapper = styled.main`
display: flex;
flex-direction: column;
max-height: calc(100% - 62px);
overflow: auto;
flex: 1;
justify-content: space-between;
`;
export type StartExchangeData = {
onCancel?: (startExchangeError: StartExchangeErrorResult) => void;
exchangeType: ExchangeType;
provider?: string;
exchange?: Exchange;
onResult: (startExchangeResult: StartExchangeSuccessResult) => void;
};
export function isStartExchangeData(data: unknown): data is StartExchangeData {
if (data === null || typeof data !== "object") {
return false;
}
return "exchangeType" in data;
}
export const LiveAppDrawer = () => {
const [dismissDisclaimerChecked, setDismissDisclaimerChecked] = useState<boolean>(false);
const { t } = useTranslation();
const dispatch = useDispatch();
// @ts-expect-error how to type payload?
const {
isOpen,
payload,
}: {
isOpen: boolean;
payload: {
title: string;
type: string;
manifest: AppManifest;
disclaimerId: string;
data?: StartExchangeData | CompleteExchangeData;
next: (manifest: AppManifest, isChecked: boolean) => void;
};
} = useSelector(platformAppDrawerStateSelector);
const onContinue = useCallback(() => {
if (payload && payload.type === "DAPP_DISCLAIMER") {
const { manifest, disclaimerId, next } = payload;
if (dismissDisclaimerChecked && disclaimerId) {
dispatch(dismissBanner(disclaimerId));
}
dispatch(closePlatformAppDrawer());
next(manifest, dismissDisclaimerChecked);
}
}, [dismissDisclaimerChecked, dispatch, payload]);
const drawerContent = useMemo(() => {
if (!payload) {
return null;
}
const { type, manifest, data } = payload;
const action = createAction(connectApp, startExchange);
switch (type) {
case "DAPP_INFO":
return manifest ? (
<Box pt={7} px={6}>
<AppDetails manifest={manifest} />
<Divider my={6} />
<Text ff="Inter|SemiBold">{t(`platform.app.informations.website`)}</Text>
<Text ff="Inter" color="#8a80db">
<ExternalLink
label={manifest?.homepageUrl}
isInternal={false}
onClick={() => openURL(manifest?.homepageUrl)}
/>
</Text>
</Box>
) : null;
case "DAPP_DISCLAIMER":
return (
<>
<Box px={6} flex={1} justifyContent="center">
<Box>{manifest ? <LiveAppDisclaimer manifest={manifest} /> : null}</Box>
</Box>
<Box pb={24}>
<Divider my={24} />
<Box px={6} horizontal alignItems="center" justifyContent="space-between">
<Box
horizontal
alignItems="flex-start"
onClick={() => setDismissDisclaimerChecked(!dismissDisclaimerChecked)}
style={{
flex: 1,
cursor: "pointer",
}}
>
<CheckBox isChecked={dismissDisclaimerChecked} data-testid="dismiss-disclaimer" />
<Text
ff="Inter|SemiBold"
fontSize={4}
style={{
marginLeft: 8,
overflowWrap: "break-word",
flex: 1,
}}
>
{t("platform.disclaimer.checkbox")}
</Text>
</Box>
<Button primary onClick={onContinue} data-testid="drawer-continue-button">
{t("platform.disclaimer.CTA")}
</Button>
</Box>
</Box>
</>
);
case "EXCHANGE_START":
return data && isStartExchangeData(data) ? (
<DeviceAction
action={action}
request={data}
Result={() => renderLoading()}
onResult={result => {
if ("startExchangeResult" in result) {
data.onResult(result.startExchangeResult);
}
if ("startExchangeError" in result) {
data.onCancel?.(result.startExchangeError);
dispatch(closePlatformAppDrawer());
}
}}
/>
) : null;
case "EXCHANGE_COMPLETE":
return data && isCompleteExchangeData(data) ? (
<CompleteExchange
data={data}
onClose={() => {
dispatch(closePlatformAppDrawer());
}}
/>
) : null;
default:
return null;
}
}, [payload, t, dismissDisclaimerChecked, onContinue, dispatch]);
return (
<SideDrawer
title={payload ? t(payload.title) : ""}
isOpen={isOpen}
onRequestClose={() => {
dispatch(closePlatformAppDrawer());
}}
direction="left"
>
<ContentWrapper>{drawerContent}</ContentWrapper>
</SideDrawer>
);
};