forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud.tsx
More file actions
154 lines (127 loc) · 4.67 KB
/
cloud.tsx
File metadata and controls
154 lines (127 loc) · 4.67 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
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
/// <reference path="../../built/pxtlib.d.ts" />
import * as React from "react";
import * as data from "./data";
import * as sui from "./sui";
import * as cloudsync from "./cloudsync";
import * as codecard from "./codecard";
type ISettingsProps = pxt.editor.ISettingsProps;
export interface SignInDialogProps extends ISettingsProps {
onComplete?: () => void;
}
export interface SignInDialogState {
visible?: boolean;
onComplete?: () => void;
}
export class SignInDialog extends data.Component<SignInDialogProps, SignInDialogState> {
constructor(props: SignInDialogProps) {
super(props);
this.state = {
visible: false
}
this.close = this.close.bind(this);
this.onLoggedIn = this.onLoggedIn.bind(this);
}
hide() {
this.setState({ visible: false });
}
close() {
this.setState({ visible: false });
}
show() {
this.setState({ visible: true });
}
onLoggedIn() {
const { onComplete } = this.props;
this.hide();
if (onComplete) onComplete();
}
renderCore() {
const { visible } = this.state;
const providers = cloudsync.providers();
return (
<sui.Modal isOpen={visible} className="signindialog" size="small"
onClose={this.close} dimmer={true}
closeIcon={true} header={lf("Sign In")}
closeOnDimmerClick closeOnDocumentClick closeOnEscape
>
<div className="ui header">{lf("Please choose your cloud provider.")}</div>
<div className="ui grid padded">
<div className={"ui cards"}>
{providers.map(p => (
<SignInProviderButton key={p.name} provider={p} onLoggedIn={this.onLoggedIn} />
))}
</div>
</div>
</sui.Modal>
)
}
}
interface SignInProviderButtonProps {
provider?: cloudsync.IdentityProvider;
onLoggedIn?: () => void;
}
class SignInProviderButton extends sui.StatelessUIElement<SignInProviderButtonProps> {
constructor(props: SignInProviderButtonProps) {
super(props);
this.state = {
}
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
const { provider, onLoggedIn } = this.props;
provider.loginAsync()
.then(() => {
if (onLoggedIn) onLoggedIn();
});
}
renderCore() {
const { provider, ...rest } = this.props;
return <codecard.CodeCardView
ariaLabel={lf("Sign in with {0}.", provider.name)}
role="button"
key={'import'}
icon={`${provider.icon} large`}
name={provider.friendlyName}
onClick={this.handleClick}
{...rest}
/>
}
}
interface UserMenuProps extends ISettingsProps {
}
export class UserMenu extends data.Component<UserMenuProps, {}> {
constructor(props: UserMenuProps) {
super(props);
this.logout = this.logout.bind(this);
this.login = this.login.bind(this);
}
login() {
pxt.tickEvent("github.usermenu.signin", undefined, { interactiveConsent: true });
this.props.parent.cloudSignInDialog();
}
logout() {
pxt.tickEvent("github.usermenu.signout", undefined, { interactiveConsent: true });
this.props.parent.cloudSignOut();
}
renderCore() {
const user = this.getUser();
const title = user && user.name ? lf("{0}'s Account", user.name) : lf("Sign in");
const profile = user && user.profile;
const userPhoto = user && user.photo;
const userInitials = user && user.initials;
const providericon = this.getData("sync:providericon") || "";
return <sui.DropdownMenu role="menuitem" avatarImage={userPhoto} avatarInitials={userInitials}
icon={`${providericon} large`}
title={title}
className="item icon user-dropdown-menuitem"
tabIndex={0}>
{profile ? <a className="ui item" role="menuitem" href={profile} title={lf("Open user profile page")} target="_blank" rel="noopener noreferrer">
<i className={`ui icon ${providericon}`} />
{lf("Signed in as {0}", user.userName || user.name)}
</a> : undefined}
{user ? <div className="ui divider" /> : undefined}
{user ? <sui.Item role="menuitem" icon="sign out" text={lf("Sign out")} onClick={this.logout} /> : undefined}
{!user ? <sui.Item role="menuitem" icon="sign in" text={lf("Sign in")} onClick={this.login} /> : undefined}
</sui.DropdownMenu>
}
}