-
Notifications
You must be signed in to change notification settings - Fork 295
/
RunLocalAppButton.tsx
150 lines (140 loc) · 4.58 KB
/
RunLocalAppButton.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
import { ipcRenderer } from "electron";
import React, { useCallback } from "react";
import Button from "~/renderer/components/Button";
import { useTranslation } from "react-i18next";
import { readFile, writeFile } from "fs";
import { SettingsSectionRow as Row } from "../../SettingsSection";
import { useHistory } from "react-router-dom";
import styled from "styled-components";
import { Flex } from "@ledgerhq/react-ui";
import { useDispatch } from "react-redux";
import { openModal } from "~/renderer/actions/modals";
import { useLocalLiveAppContext } from "@ledgerhq/live-common/wallet-api/LocalLiveAppProvider/index";
import { LiveAppManifest } from "@ledgerhq/live-common/platform/types";
const ButtonContainer = styled.div`
display: flex;
flex-direction: row;
gap: 15px;
`;
const RunLocalAppButton = () => {
const dispatch = useDispatch();
const { t } = useTranslation();
const {
addLocalManifest,
state: localLiveApps,
removeLocalManifestById,
} = useLocalLiveAppContext();
const history = useHistory();
const onExportLocalManifest = useCallback(
(manifest: LiveAppManifest) => {
const { id, name } = manifest;
ipcRenderer
.invoke("show-save-dialog", {
title: "Export Manifest",
defaultPath: `${name}-manifest.json`,
buttonLabel: "Export",
filters: [{ name: "JSON", extensions: ["json"] }],
})
.then(function (response) {
if (!response.canceled && response.filePath) {
const exportedManifest = localLiveApps.find(
(manifest: LiveAppManifest) => manifest.id === id,
);
const manifestData = JSON.stringify(exportedManifest, null, 2);
try {
writeFile(response.filePath, manifestData, "utf-8", () =>
console.log("File exported successfully!"),
);
} catch (parseError) {
console.warn(parseError);
}
}
});
},
[localLiveApps],
);
const onBrowseLocalManifest = useCallback(() => {
ipcRenderer
.invoke("show-open-dialog", {
properties: ["openFile"],
})
.then(function (response) {
if (!response.canceled) {
const fileName = response.filePaths[0];
readFile(fileName, (readError, data) => {
if (!readError) {
try {
const manifest = JSON.parse(data.toString());
if (Array.isArray(manifest)) {
manifest.forEach(m => {
addLocalManifest(m);
});
} else {
addLocalManifest(manifest);
}
} catch (parseError) {
console.log(parseError);
}
}
});
} else {
console.log("no file selected");
}
});
}, [addLocalManifest]);
const onOpenModal = useCallback(
(manifest?: LiveAppManifest) => {
dispatch(
openModal("MODAL_CREATE_LOCAL_APP", {
manifest,
}),
);
},
[dispatch],
);
return (
<>
<Row
title={t("settings.developer.addLocalApp")}
desc={t("settings.developer.addLocalAppDesc")}
>
<Flex flexDirection={"row"} columnGap={3}>
<Button small primary onClick={onBrowseLocalManifest}>
{t("settings.developer.addLocalAppButton")}
</Button>
<Button
small
primary
onClick={onOpenModal}
data-test-id="settings-open-local-manifest-form"
>
{t("settings.developer.createLocalAppModal.create")}
</Button>
</Flex>
</Row>
{localLiveApps.map((manifest: LiveAppManifest) => (
<Row key={manifest.id} title={manifest.name} desc={manifest.url as string}>
<ButtonContainer>
<Button small primary onClick={() => history.push(`/platform/${manifest.id}`)}>
{t("settings.developer.runLocalAppOpenButton")}
</Button>
<Button
small
outline
onClick={() => {
onExportLocalManifest(manifest);
}}
data-test-id="settings-export-local-manifest"
>
{t("settings.developer.createLocalAppModal.export")}
</Button>
<Button small danger onClick={() => removeLocalManifestById(manifest.id)}>
{t("settings.developer.runLocalAppDeleteButton")}
</Button>
</ButtonContainer>
</Row>
))}
</>
);
};
export default RunLocalAppButton;