-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathcloud-user-status.tsx
134 lines (126 loc) · 4.21 KB
/
cloud-user-status.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
import * as React from '@theia/core/shared/react';
import {
Disposable,
DisposableCollection,
} from '@theia/core/lib/common/disposable';
import { CloudSketchbookTreeModel } from './cloud-sketchbook-tree-model';
import { AuthenticationClientService } from '../../auth/authentication-client-service';
import { CloudUserCommands } from '../../auth/cloud-user-commands';
import { AuthenticationSessionAccountInformation } from '../../../common/protocol/authentication-service';
import { nls } from '@theia/core/lib/common';
export class UserStatus extends React.Component<
UserStatus.Props,
UserStatus.State
> {
protected readonly toDispose = new DisposableCollection();
constructor(props: UserStatus.Props) {
super(props);
this.state = {
status: this.status,
accountInfo: props.authenticationService.session?.account,
refreshing: false,
};
}
override componentDidMount(): void {
const statusListener = () => this.setState({ status: this.status });
window.addEventListener('online', statusListener);
window.addEventListener('offline', statusListener);
this.toDispose.pushAll([
this.props.authenticationService.onSessionDidChange((session) =>
this.setState({ accountInfo: session?.account })
),
Disposable.create(() =>
window.removeEventListener('online', statusListener)
),
Disposable.create(() =>
window.removeEventListener('offline', statusListener)
),
]);
}
override componentWillUnmount(): void {
this.toDispose.dispose();
}
override render(): React.ReactNode {
if (!this.props.authenticationService.session) {
return null;
}
return (
<div className="cloud-connection-status flex-line">
<div className="status item flex-line">
<div
className={`${
this.state.status === 'connected'
? 'connected-status-icon'
: 'offline-status-icon'
}`}
/>
{this.state.status === 'connected'
? nls.localize('arduino/cloud/connected', 'Connected')
: nls.localize('arduino/cloud/offline', 'Offline')}
</div>
<div className="actions item flex-line">
<div
title={nls.localize('arduino/cloud/sync', 'Sync')}
className={`fa fa-reload ${
(this.state.refreshing && 'rotating') || ''
}`}
style={{ cursor: 'pointer' }}
onClick={this.onDidClickRefresh}
/>
</div>
<div className="account item flex-line">
<div
title={nls.localize('arduino/cloud/account', 'Account')}
className="account-icon"
style={{ cursor: 'pointer' }}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
this.props.model.commandRegistry.executeCommand(
CloudUserCommands.OPEN_PROFILE_CONTEXT_MENU.id,
{
event: event.nativeEvent,
username: this.state.accountInfo?.label,
}
);
}}
>
{this.state.accountInfo?.picture && (
<img
src={this.state.accountInfo?.picture}
alt={nls.localize(
'arduino/cloud/profilePicture',
'Profile picture'
)}
/>
)}
</div>
</div>
</div>
);
}
private onDidClickRefresh = () => {
this.setState({ refreshing: true });
Promise.all([
this.props.model.updateRoot(),
new Promise((resolve) => setTimeout(() => resolve(true), 1000)),
]).then(() => {
this.props.model.sketchbookTree().refresh();
this.setState({ refreshing: false });
});
};
private get status(): 'connected' | 'offline' {
return window.navigator.onLine ? 'connected' : 'offline';
}
}
export namespace UserStatus {
export interface Props {
readonly model: CloudSketchbookTreeModel;
readonly authenticationService: AuthenticationClientService;
}
export interface State {
status: 'connected' | 'offline';
accountInfo?: AuthenticationSessionAccountInformation;
refreshing?: boolean;
}
}