-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
cloud-tile.tsx
202 lines (182 loc) · 5.02 KB
/
cloud-tile.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, {useCallback, useEffect, useState} from 'react';
import styled from 'styled-components';
import {Logout, Login} from '../common/icons';
import {CenterVerticalFlexbox, Button, CheckMark} from '../common/styled-components';
import {Provider, CloudUser} from '@kepler.gl/cloud-providers';
import {useCloudListProvider} from '../hooks/use-cloud-list-provider';
interface StyledTileWrapperProps {
selected?: boolean;
}
const StyledTileWrapper = styled.div.attrs({
className: 'provider-tile__wrapper'
})<StyledTileWrapperProps>`
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
border-radius: 2px;
border: 1px solid
${props => (props.selected ? props.theme.primaryBtnBgd : props.theme.selectBorderColorLT)};
color: ${props => (props.selected ? props.theme.primaryBtnBgd : props.theme.selectBorderColorLT)};
cursor: pointer;
font-weight: 500;
width: 120px;
height: 168px;
background-color: #ffffff;
transition: ${props => props.theme.transition};
position: relative;
:hover {
border: 1px solid ${props => props.theme.primaryBtnBgd};
color: ${props => props.theme.primaryBtnBgd};
}
.button {
margin-top: 20px;
}
`;
const StyledBox = styled(CenterVerticalFlexbox)`
margin-right: 12px;
position: relative;
`;
const StyledCloudName = styled.div`
font-size: 12px;
margin-top: 12px;
margin-bottom: 4px;
`;
const StyledUserName = styled.div`
font-size: 11px;
margin-top: 8px;
text-align: center;
color: ${props => props.theme.primaryBtnActBgd};
overflow: hidden;
width: 100px;
text-overflow: ellipsis;
`;
interface OnClickProps {
onClick?: React.MouseEventHandler<HTMLButtonElement>;
}
const LoginButton = ({onClick}: OnClickProps) => (
<Button className="login-button" link small onClick={onClick}>
<Login />
Login
</Button>
);
const LogoutButton = ({onClick}: OnClickProps) => (
<Button className="logout-button" link small onClick={onClick}>
<Logout />
Logout
</Button>
);
const NewTag = styled.div`
width: 37px;
height: 19px;
display: flex;
align-content: center;
justify-content: center;
border-radius: 8px;
padding: 4px 8px;
background-color: #EDE9F9;
color: #8863F8;
position: absolute;
left: 35%;
top: -8px
z-index: 500;
font-size: 11px;
line-height: 10px;
`;
export const StyledWarning = styled.span`
color: ${props => props.theme.errorColor};
font-weight: ${props => props.theme.selectFontWeightBold};
`;
interface CloudTileProps {
actionName?: string | null;
// cloud provider class
provider: Provider;
}
/**
* this component display a provider and allows users to select and set the current provider
* @param provider
* @param actionName
* @constructor
*/
const CloudTile: React.FC<CloudTileProps> = ({provider, actionName}) => {
const [user, setUser] = useState<CloudUser | null>(null);
const [error, setError] = useState<Error | null>(null);
const [isLoading, setIsLoading] = useState(false);
const {provider: currentProvider, setProvider} = useCloudListProvider();
const isSelected = provider === currentProvider;
useEffect(() => {
if (!provider) {
return;
}
setError(null);
setIsLoading(true);
setError(null);
provider
.getUser()
.then(setUser)
.catch(setError)
.finally(() => {
setError(null);
setIsLoading(false);
});
}, [provider]);
const onLogin = useCallback(async () => {
setError(null);
setIsLoading(true);
try {
const user = await provider.login();
setUser(user);
setProvider(provider);
} catch (error) {
setError(error as Error);
}
setIsLoading(false);
}, [provider]);
const onSelect = useCallback(async () => {
if (isLoading) {
return;
}
if (user) {
setProvider(provider);
return;
}
try {
await onLogin();
setProvider(provider);
} catch (err) {
setError(err as Error);
setProvider(null);
}
}, [setProvider, provider, user, isLoading]);
const onLogout = useCallback(async () => {
setIsLoading(true);
try {
await provider.logout();
} catch (error) {
setError(error as Error);
}
setIsLoading(false);
setUser(null);
setProvider(null);
}, [provider]);
const {displayName, name} = provider;
return (
<StyledBox>
{provider.isNew ? <NewTag>New</NewTag> : null}
<div></div>
<StyledTileWrapper onClick={onSelect} selected={isSelected}>
<StyledCloudName>{displayName || name}</StyledCloudName>
{provider.icon ? <provider.icon height="64px" /> : null}
{isLoading ? (
<div>Loading ...</div>
) : (
<>{user ? <StyledUserName>{actionName || user.name}</StyledUserName> : null}</>
)}
{isSelected ? <CheckMark /> : null}
</StyledTileWrapper>
{user || error ? <LogoutButton onClick={onLogout} /> : <LoginButton onClick={onLogin} />}
{error ? <StyledWarning>{error.message}</StyledWarning> : null}
</StyledBox>
);
};
export default CloudTile;