-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathDownloadFlashButton.tsx
More file actions
112 lines (109 loc) · 3.42 KB
/
DownloadFlashButton.tsx
File metadata and controls
112 lines (109 loc) · 3.42 KB
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
/**
* (c) 2021, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import {
ButtonGroup,
HStack,
IconButton,
Menu,
MenuButton,
MenuItem,
MenuList,
Portal,
ThemeTypings,
} from "@chakra-ui/react";
import { MdMoreVert } from "react-icons/md";
import { RiDownload2Line, RiFlashlightFill } from "react-icons/ri";
import { FormattedMessage } from "react-intl";
import { ConnectionStatus } from "../device/device";
import { useConnectionStatus } from "../device/device-hooks";
import DownloadButton from "./DownloadButton";
import FlashButton from "./FlashButton";
import { useProjectActions } from "./project-hooks";
interface DownloadFlashButtonProps {
size?: ThemeTypings["components"]["Button"]["sizes"];
}
/**
* The device connection area.
*
* It shows the current connection status and allows the user to
* flash (if WebUSB is supported) or otherwise just download a HEX.
*/
const DownloadFlashButton = ({ size }: DownloadFlashButtonProps) => {
const connectionStatus = useConnectionStatus();
const connected = connectionStatus === ConnectionStatus.CONNECTED;
const actions = useProjectActions();
const buttonWidth = "10rem"; // 8.1 with md buttons
return (
<HStack>
<Menu>
<ButtonGroup isAttached>
{connected ? (
<FlashButton width={buttonWidth} mode={"button"} size={size} />
) : (
<DownloadButton width={buttonWidth} mode={"button"} size={size} />
)}
<MenuButton
variant="solid"
borderLeft="1px"
borderRadius="button"
as={IconButton}
// Shift to compensate for border radius on the right
icon={
<MdMoreVert
style={{
marginLeft: "calc(-0.15 * var(--chakra-radii-button))",
}}
/>
}
size={size}
/>
<Portal>
{/* z-index above the xterm.js's layers (currently 10 but given some margin for increases as it can vary with config) */}
<MenuList zIndex={20}>
{!connected && (
<MenuItem
target="_blank"
rel="noopener"
icon={<RiFlashlightFill />}
onClick={actions.flash}
>
<FormattedMessage id="flash-button" />
</MenuItem>
)}
{connected && (
<MenuItem
target="_blank"
rel="noopener"
icon={<RiDownload2Line />}
onClick={actions.download}
>
<FormattedMessage id="download-hex" />
</MenuItem>
)}
<MenuItem
target="_blank"
rel="noopener"
icon={<RiDownload2Line />}
onClick={actions.downloadMainFile}
>
<FormattedMessage id="download-python" />
</MenuItem>
<MenuItem
target="_blank"
rel="noopener"
icon={<RiDownload2Line />}
onClick={actions.downloadMicrofsFiles}
>
<FormattedMessage id="download-microfs-files" />
</MenuItem>
</MenuList>
</Portal>
</ButtonGroup>
</Menu>
</HStack>
);
};
export default DownloadFlashButton;