Open dialog to create new network on IRC URL click

If we're running under a bouncer and the user clicks a link with
a server we aren't connected to yet, open the dialog to add a new
network.

References: https://todo.sr.ht/~emersion/gamja/71
This commit is contained in:
Simon Ser 2021-10-13 16:40:34 +02:00
parent 405bc51c26
commit 3562478946
2 changed files with 16 additions and 8 deletions

View file

@ -883,7 +883,13 @@ export default class App extends Component {
} }
} }
if (!bouncerNetID) { if (!bouncerNetID) {
// TODO: open dialog to create network if bouncer // Open dialog to create network if bouncer
let client = this.clients.values().next().value;
if (client && client.enabledCaps["soju.im/bouncer-networks"]) {
event.preventDefault();
let params = { host: url.host };
this.openDialog("network", { params });
}
return; return;
} }
@ -1364,13 +1370,15 @@ export default class App extends Component {
let dialog = null; let dialog = null;
switch (this.state.dialog) { switch (this.state.dialog) {
case "network": case "network":
let title = this.state.dialogData ? "Edit network" : "Add network"; let isNew = !!(!this.state.dialogData || !this.state.dialogData.id);
let title = isNew ? "Add network" : "Edit network";
dialog = html` dialog = html`
<${Dialog} title=${title} onDismiss=${this.dismissDialog}> <${Dialog} title=${title} onDismiss=${this.dismissDialog}>
<${NetworkForm} <${NetworkForm}
onSubmit=${this.handleNetworkSubmit} onSubmit=${this.handleNetworkSubmit}
onRemove=${this.handleNetworkRemove} onRemove=${this.handleNetworkRemove}
params=${this.state.dialogData ? this.state.dialogData.params : null} params=${this.state.dialogData ? this.state.dialogData.params : null}
isNew=${isNew}
/> />
</> </>
`; `;

View file

@ -14,7 +14,6 @@ export default class NetworkForm extends Component {
prevParams = null; prevParams = null;
state = { state = {
...defaultParams, ...defaultParams,
isNew: true,
}; };
constructor(props) { constructor(props) {
@ -25,8 +24,6 @@ export default class NetworkForm extends Component {
this.handleChange = this.handleChange.bind(this); this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
this.state.isNew = !props.params;
if (props.params) { if (props.params) {
Object.keys(defaultParams).forEach((k) => { Object.keys(defaultParams).forEach((k) => {
if (props.params[k] !== undefined) { if (props.params[k] !== undefined) {
@ -48,7 +45,10 @@ export default class NetworkForm extends Component {
let params = {}; let params = {};
Object.keys(defaultParams).forEach((k) => { Object.keys(defaultParams).forEach((k) => {
if (this.prevParams[k] == this.state[k]) { if (!this.props.isNew && this.prevParams[k] == this.state[k]) {
return;
}
if (this.props.isNew && defaultParams[k] == this.state[k]) {
return; return;
} }
params[k] = this.state[k]; params[k] = this.state[k];
@ -59,7 +59,7 @@ export default class NetworkForm extends Component {
render() { render() {
let removeNetwork = null; let removeNetwork = null;
if (!this.state.isNew) { if (!this.props.isNew) {
removeNetwork = html` removeNetwork = html`
<button type="button" class="danger" onClick=${() => this.props.onRemove()}> <button type="button" class="danger" onClick=${() => this.props.onRemove()}>
Remove network Remove network
@ -121,7 +121,7 @@ export default class NetworkForm extends Component {
${removeNetwork} ${removeNetwork}
${" "} ${" "}
<button> <button>
${this.state.isNew ? "Add network" : "Save network"} ${this.props.isNew ? "Add network" : "Save network"}
</button> </button>
</form> </form>
`; `;