Skip to content

Commit d993157

Browse files
committed
Add form to configure custom web instance
1 parent 06237b1 commit d993157

File tree

3 files changed

+60
-27
lines changed

3 files changed

+60
-27
lines changed

src/Preferences.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Preferences extends EventEmitter {
2525
// used to differentiate web from native if a client supports both
2626
this.platform = null;
2727
this.homeservers = null;
28-
this.preferredWebInstances = {};
28+
this.customWebInstances = {};
2929

3030
const prefsStr = localStorage.getItem("preferred_client");
3131
if (prefsStr) {
@@ -37,9 +37,9 @@ export class Preferences extends EventEmitter {
3737
if (serversStr) {
3838
this.homeservers = JSON.parse(serversStr);
3939
}
40-
const preferredWebInstancesStr = localStorage.getItem("preferred_web_instances");
41-
if (preferredWebInstancesStr) {
42-
this.preferredWebInstances = JSON.parse(preferredWebInstancesStr);
40+
const customWebInstancesStr = localStorage.getItem("custom_web_instances");
41+
if (customWebInstancesStr) {
42+
this.customWebInstances = JSON.parse(customWebInstancesStr);
4343
}
4444
}
4545

@@ -59,27 +59,27 @@ export class Preferences extends EventEmitter {
5959
}
6060
}
6161

62-
setPreferredWebInstance(client_id, instance_url) {
63-
this.preferredWebInstances[client_id] = instance_url;
64-
this._localStorage.setItem("preferred_web_instances", JSON.stringify(this.preferredWebInstances));
62+
setCustomWebInstance(client_id, instance_url) {
63+
this.customWebInstances[client_id] = instance_url;
64+
this._localStorage.setItem("custom_web_instances", JSON.stringify(this.customWebInstances));
6565
this.emit("canClear");
6666
}
6767

68-
getPreferredWebInstance(client_id) {
69-
return this.preferredWebInstances[client_id];
68+
getCustomWebInstance(client_id) {
69+
return this.customWebInstances[client_id];
7070
}
7171

7272
clear() {
7373
this._localStorage.removeItem("preferred_client");
7474
this._localStorage.removeItem("consented_servers");
75-
this._localStorage.removeItem("preferred_web_instances");
75+
this._localStorage.removeItem("custom_web_instances");
7676
this.clientId = null;
7777
this.platform = null;
7878
this.homeservers = null;
79-
this.preferredWebInstances = {};
79+
this.customWebInstances = {};
8080
}
8181

8282
get canClear() {
83-
return !!this.clientId || !!this.platform || !!this.homeservers;
83+
return !!this.clientId || !!this.platform || !!this.homeservers || !!this.customWebInstances;
8484
}
8585
}

src/open/ClientView.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ function renderInstructions(parts) {
3939
export class ClientView extends TemplateView {
4040

4141
render(t, vm) {
42+
return t.mapView(vm => vm.customWebInstanceFormOpen, open => {
43+
switch (open) {
44+
case true: return new SetCustomWebInstanceView(vm);
45+
case false: return new TemplateView(vm, t => this.renderContent(t, vm));
46+
}
47+
});
48+
}
49+
renderContent(t, vm) {
4250
return t.div({className: {"ClientView": true, "isPreferred": vm => vm.hasPreferredWebInstance}}, [
4351
... vm.hasPreferredWebInstance ? [t.div({className: "hostedBanner"}, vm.hostedByBannerLabel)] : [],
4452
t.div({className: "header"}, [
@@ -119,32 +127,40 @@ export class SetCustomWebInstanceView extends TemplateView {
119127
"Use a custom web instance for the ", t.strong(vm.name), " client:",
120128
]),
121129
t.form({action: "#", id: "setCustomWebInstanceForm", onSubmit: evt => this._onSubmit(evt)}, [
122-
t.label([
123-
"Host name:",
124-
t.input({
125-
type: "text",
126-
className: "line",
127-
placeholder: "chat.example.org",
128-
name: "instanceHostname",
129-
})
130-
])
130+
t.input({
131+
type: "text",
132+
className: "fullwidth large",
133+
placeholder: "chat.example.org",
134+
name: "instanceHostname",
135+
value: vm.preferredWebInstance || "",
136+
}),
137+
t.input({type: "submit", value: "Save", className: "primary fullwidth"}),
138+
t.input({type: "button", value: "Use Default Instance", className: "secondary fullwidth", onClick: evt => this._onReset(evt)}),
131139
])
132140
]);
133141
}
134142

135143
_onSubmit(evt) {
136144
evt.preventDefault();
137-
this.value.continueWithSelection(this._askEveryTimeChecked);
145+
const form = evt.target;
146+
const {instanceHostname} = form.elements;
147+
this.value.setCustomWebInstance(instanceHostname.value || undefined);
148+
this.value.closeCustomWebInstanceForm();
149+
}
150+
151+
_onReset(evt) {
152+
this.value.setCustomWebInstance(undefined);
153+
this.value.closeCustomWebInstanceForm();
138154
}
139155
}
140156

141157
function showBack(t, vm) {
142158
return t.p({className: {caption: true, "back": true, hidden: vm => !vm.showBack}}, [
143159
`Continue with ${vm.name} · `,
144160
t.button({className: "text", onClick: () => vm.back()}, "Change"),
145-
t.span({hidden: vm => !vm.showSetWebInstance}, [
161+
t.span({hidden: vm => !vm.supportsCustomWebInstances}, [
146162
' · ',
147-
t.button({className: "text", onClick: () => vm.setCustomWebInstance()}, "Use Custom Web Instance"),
163+
t.button({className: "text", onClick: () => vm.configureCustomWebInstance()}, "Use Custom Web Instance"),
148164
])
149165

150166
]);

src/open/ClientViewModel.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class ClientViewModel extends ViewModel {
3535
this._pickClient = pickClient;
3636
// to provide "choose other client" button after calling pick()
3737
this._clientListViewModel = null;
38+
this.customWebInstanceFormOpen = false;
3839
this._update();
3940
}
4041

@@ -132,7 +133,7 @@ export class ClientViewModel extends ViewModel {
132133
// also check there is a web platform that matches the platforms the user is on (mobile or desktop web)
133134
if (!this._webPlatform) return undefined;
134135
return (
135-
this.preferences.getPreferredWebInstance(this._client.id)
136+
this.preferences.getCustomWebInstance(this._client.id)
136137
|| this._client.getPreferredWebInstance(this._link)
137138
);
138139
}
@@ -231,7 +232,7 @@ export class ClientViewModel extends ViewModel {
231232
return !!this._clientListViewModel;
232233
}
233234

234-
get showSetWebInstance() {
235+
get supportsCustomWebInstances() {
235236
return !!this._client.supportsCustomInstances;
236237
}
237238

@@ -243,10 +244,26 @@ export class ClientViewModel extends ViewModel {
243244
// in the list with all clients, and also if we refresh, we get the list with
244245
// all clients rather than having our "change client" click reverted.
245246
this.preferences.setClient(undefined, undefined);
246-
this.preferences.setPreferredWebInstance(this._client.id, undefined);
247+
this.preferences.setCustomWebInstance(this._client.id, undefined);
247248
this._update();
248249
this.emitChange();
249250
vm.showAll();
250251
}
251252
}
253+
254+
configureCustomWebInstance() {
255+
this.customWebInstanceFormOpen = true;
256+
this.emitChange();
257+
}
258+
259+
closeCustomWebInstanceForm() {
260+
this.customWebInstanceFormOpen = false;
261+
this.emitChange();
262+
}
263+
264+
setCustomWebInstance(hostname) {
265+
this.preferences.setClient(this._client.id, hostname ? this._webPlatform : (this._nativePlatform || this._webPlatform));
266+
this.preferences.setCustomWebInstance(this._client.id, hostname);
267+
this._update();
268+
}
252269
}

0 commit comments

Comments
 (0)