-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
DeviceSelector.tsx
172 lines (161 loc) · 4.08 KB
/
DeviceSelector.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
import {
PlusSquareOutlined,
SyncOutlined,
UsbOutlined,
} from "@ant-design/icons";
import { gql, useMutation, useQuery } from "@apollo/client";
import { Button, Card, Empty, Typography } from "antd";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import useIsMobile from "renderer/hooks/useIsMobile";
import { Centered } from "renderer/shared/layouts";
import styled from "styled-components";
import DeviceList from "./DeviceList";
const Main = styled.div`
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
> * {
width: 100%;
max-width: 800px;
}
gap: 8px;
min-height: 0;
`;
const ButtonsContainer = styled.div`
height: 32px;
`;
type Props = {
variant?: "electron" | "web";
selectedDeviceId?: string;
onChange?: (selectedDeviceId?: string) => void;
disabled?: boolean;
};
const DevicesQuery = gql(/* GraphQL */ `
query Devices {
flashableDevices {
id
productName
serialNumber
vendorId
productId
}
}
`);
const DeviceSelector: React.FC<Props> = ({
selectedDeviceId,
variant = "web",
onChange,
disabled,
}) => {
const isMobile = useIsMobile();
const { data, loading, refetch } = useQuery(DevicesQuery);
const { t } = useTranslation("flashing");
const [requestDevice] = useMutation(
gql(/* GraphQL */ `
mutation RequestDevice {
requestFlashableDevice {
id
}
}
`),
{
refetchQueries: [DevicesQuery],
awaitRefetchQueries: true,
}
);
useEffect(() => {
if (
!loading &&
selectedDeviceId &&
!data?.flashableDevices.find((d) => d.id === selectedDeviceId)
) {
onChange?.(undefined);
}
}, [data, loading, selectedDeviceId, onChange]);
const devicesAvailable = (data?.flashableDevices.length ?? 0) > 0;
const actionButton =
variant === "electron" ? (
<Button
type={!devicesAvailable ? "primary" : undefined}
icon={<SyncOutlined />}
disabled={disabled}
onClick={() => {
void refetch();
}}
>
{t(`Refresh`)}
</Button>
) : (
<Button
icon={<PlusSquareOutlined />}
type={!devicesAvailable ? "primary" : undefined}
disabled={disabled}
onClick={() => {
// Select the device after it's been picked, as it's likely
// this is what the user wants
void requestDevice().then((result) => {
if (result.data?.requestFlashableDevice) {
onChange?.(result.data.requestFlashableDevice.id);
}
});
}}
>
{t(`Add new device`)}
</Button>
);
return (
<Main>
<Typography.Text>
{t(`Available devices`)} ({data?.flashableDevices.length ?? 0})
</Typography.Text>
<Card
style={{
height: isMobile ? "300px" : "100%",
overflowY: "auto",
}}
bodyStyle={{ height: "100%", padding: 8 }}
>
{loading || devicesAvailable ? (
<DeviceList
devices={data?.flashableDevices ?? []}
loading={loading}
onSelected={(deviceId) => {
onChange?.(deviceId);
}}
selectedDeviceId={selectedDeviceId}
disabled={disabled}
/>
) : (
<Centered style={{ height: "100%" }}>
<Empty
imageStyle={{
marginBottom: 0,
}}
image={
<UsbOutlined
style={{
fontSize: "64px",
color: "#d9d9d9",
}}
/>
}
description={
variant === "electron"
? t(`No devices found`)
: t(`Add a device to get started`)
}
>
{actionButton}
</Empty>
</Centered>
)}
</Card>
<ButtonsContainer>
{!loading && devicesAvailable && actionButton}
</ButtonsContainer>
</Main>
);
};
export default DeviceSelector;