-
Notifications
You must be signed in to change notification settings - Fork 7
/
PlatformView.tsx
215 lines (179 loc) · 6.44 KB
/
PlatformView.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
203
204
205
206
207
208
209
210
211
212
213
214
215
import React from 'react';
import { Stack, CompoundButton, Button, PrimaryButton, registerIconAlias } from 'office-ui-fabric-react';
import { Image, ImageFit } from 'office-ui-fabric-react/lib/Image';
import { Dropdown, DropdownMenuItemType, IDropdownStyles, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown';
import { ResponsiveMode } from 'office-ui-fabric-react/lib/utilities/decorators/withResponsiveMode';
import libs from '../../../core/libs.js';
import { settings, getSettings } from '../../../core/helpers/settings.js';
import { IPlatform, IClient, IClientConfig, ILib, ISettings, IPlatforms } from '../../../core/typings';
import { Toggle } from 'office-ui-fabric-react/lib/Toggle';
getSettings();
const dropdownStyles: Partial<IDropdownStyles> = {
dropdown: { width: 280 }
};
const ClientsDropdown: IDropdownOption[] = [
// dropdown entries go here
];
interface IPlatformState {
Client: IClient;
}
interface IPlatformView {
Platform: IPlatform;
DefaultClient: string;
OnClientChanged?: Function;
OnLoaded?: Function;
isSettingsView?: boolean;
}
function GetClientFromPlatform(clientName: string, platform: IPlatform): IClient | undefined {
let returnClient: IClient | undefined;
for (let client of Object.values(platform.clients)) {
if (client.name == clientName) {
returnClient = client;
}
}
return returnClient;
}
function GetCurrentAppConfig(platform: string): IClientConfig {
let prefferedApp: string = (settings as any)[platform].prefferedApp;
return (libs as any).platforms[platform].clients[prefferedApp].config;
}
function GetCurrentPlatform(platformName: string): IPlatform {
return (libs as any).platforms[platformName];
}
function GetDefaultClientForPlatform(platformName: string): IClient | undefined {
let prefferedClient: string = (settings as any)[platformName].prefferedApp;
return GetClientFromPlatform(prefferedClient, (libs as any).platforms[platformName]);
}
function PopulateClientsDropdown(platform: string, clear?: boolean): void {
if (clear) {
while (ClientsDropdown.length > 0) ClientsDropdown.shift();
}
for (let i of Object.values((libs as unknown as ILib).platforms[platform].clients)) {
ClientsDropdown.push({
text: i.name,
key: i.name
});
}
}
function RelaySettingsState() {
chrome.runtime.sendMessage({
updateSettings: settings
});
}
export class PlatformView extends React.Component<IPlatformView, IPlatformState> {
constructor(props: IPlatformView, state: IPlatformState) {
super(props);
let client: IClient | undefined = GetClientFromPlatform(this.props.DefaultClient, this.props.Platform);
if (!client) return;
this.state = {
Client: client
};
this.OnClientSelected = this.OnClientSelected.bind(this);
this.LaunchClient = this.LaunchClient.bind(this);
PopulateClientsDropdown(this.props.Platform.name, true);
}
componentDidMount() {
if (this.props.OnLoaded != undefined) this.props.OnLoaded(this.props.DefaultClient);
}
OnClientSelected = (event: React.FormEvent<HTMLDivElement>, option?: IDropdownOption | undefined, index?: number | undefined) => {
if (option == undefined) return;
let newClient: IClient | undefined = GetClientFromPlatform(option.text, this.props.Platform);
if (!newClient) return;
this.setState({
Client: newClient
});
if (this.props.OnClientChanged != undefined) {
this.props.OnClientChanged(newClient);
}
(settings as ISettings).platforms[this.props.Platform.name].prefferedApp = newClient.name;
RelaySettingsState();
if (!this.props.isSettingsView) {
chrome.runtime.sendMessage({
updateIcon: true
});
}
};
OnEnabledChanged = (event: React.MouseEvent<HTMLElement, MouseEvent>, checked?: boolean | undefined) => {
if (checked != undefined) (settings as ISettings).platforms[this.props.Platform.name].isEnabled = checked;
RelaySettingsState();
};
OnCloseOnSwitchChanged = (event: React.MouseEvent<HTMLElement, MouseEvent>, checked?: boolean | undefined) => {
if (checked != undefined) (settings as ISettings).platforms[this.props.Platform.name].closeOnSwitch = checked;
RelaySettingsState();
};
LaunchClient = () => {
chrome.runtime.sendMessage({
launch: true
});
};
render() {
return (
<Stack
horizontalAlign="start"
verticalAlign="end"
verticalFill
gap={5}
styles={{
root: {
margin: '0px 10px',
textAlign: 'center'
}
}}>
<Image
styles={{
root: {
margin: "auto"
}
}}
width={175}
height={(this.props.isSettingsView == true ? 225 : 175)}
imageFit={ImageFit.centerContain}
src={this.state.Client.config.logo} />
<PrimaryButton
styles={{
root: {
margin: "auto"
}
}}
onClick={this.LaunchClient} iconProps={{ iconName: "OpenInNewWindow", styles: { root: { fontSize: 18 } } }} style={{
height: "40px",
width: "250px",
margin: "10px",
display: (this.props.isSettingsView == true ? "none" : "block")
}}>
Open in {this.state.Client.name}
</PrimaryButton>
<Toggle
style={{ margin: 7 }}
defaultChecked={(settings as ISettings).platforms[this.props.Platform.name].isEnabled}
label={"Auto launch with:"}
inlineLabel={true}
styles={{
container: {
marginBottom: -5
},
label: {
marginBottom: -5
}
}}
onChange={this.OnEnabledChanged}
/>
{/* <Toggle
defaultChecked={(settings as ISettings).platforms[this.props.Platform.name].closeOnSwitch}
label="Close window on switch"
styles={{
container: {
display: navigator.appVersion.includes('Edge') ? "none" : "unset"
},
label: {
display: navigator.appVersion.includes('Edge') ? "none" : "unset"
}
}}
inlineLabel={true}
onChange={this.OnCloseOnSwitchChanged}
/> */}
<Dropdown placeholder="Choose another app..." defaultSelectedKey={this.state.Client.name} responsiveMode={ResponsiveMode.large} options={ClientsDropdown} styles={dropdownStyles} onChange={this.OnClientSelected} />
</Stack >
);
}
};