Skip to content

Commit

Permalink
WIP - cleanup, dialog invocation and continuation actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer committed Sep 11, 2024
1 parent 1f6f77f commit 6e91d23
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 85 deletions.
163 changes: 79 additions & 84 deletions pkg/shell/hosts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,15 @@ export class CockpitHosts extends React.Component {
editing: false,
current_user: "",
current_key: props.machine.key,
show_modal: false,
edit_machine: null,
switch_machine: null,
error_options: null,
problem: null,
just_connect: false,
modal_properties: null,
modal_callback: null,
};

this.toggleMenu = this.toggleMenu.bind(this);
this.filterHosts = this.filterHosts.bind(this);
this.onAddNewHost = this.onAddNewHost.bind(this);
this.onEditHosts = this.onEditHosts.bind(this);
this.onHostEdit = this.onHostEdit.bind(this);
this.onHostSwitch = this.onHostSwitch.bind(this);
this.onRemove = this.onRemove.bind(this);
}

Expand All @@ -96,8 +91,8 @@ export class CockpitHosts extends React.Component {
}).catch(exc => console.log(exc));

window.trigger_connection_flow = machine => {
if (!this.state.show_modal)
this.onHostSwitch(machine, true);
if (!this.state.modal_properties)
this.connectHost(machine);
};
this.props.index.navigate(null, true);
}
Expand Down Expand Up @@ -131,53 +126,83 @@ export class CockpitHosts extends React.Component {
});
}

onAddNewHost() {
this.setState({ show_modal: true });
showModal(properties) {
return new Promise((resolve, reject) => {
this.setState({ modal_properties: properties,
modal_callback: result => { resolve(result); return Promise.resolve() },
});
});
}

onHostEdit(event, machine) {
this.setState({ show_modal: true, edit_machine: machine });
async onAddNewHost() {
await this.showModal({ });
}

onHostSwitch(machine, just_connect) {
if (machine.state == "connected" || machine.address == "localhost") {
if (!just_connect) {
const addr = this.props.hostAddr({ host: machine.address }, true);
async onHostEdit(event, machine) {
const connection_string = await this.showModal({ address: machine.address });
console.log("EDIT done", connection_string, machine.connection_string);
if (connection_string) {
const parts = this.props.machines.split_connection_string(connection_string);
const addr = this.props.hostAddr({ host: parts.address }, true);
// XXX - doesn't the edit dialog change machine.address?
if (machine == this.props.machine && parts.address != machine.address) {
this.props.loader.connect(parts.address);
this.props.jump(addr);
}
} else if (machine.state != "connecting") {
if (machine.problem && codes[machine.problem]) {
this.setState({
show_modal: true,
switch_machine: machine,
problem: machine.problem,
just_connect,
}
}

async connectHost(machine) {
if (machine.address == "localhost" || machine.state == "connected" || machine.state == "connecting")
return machine.connection_string;

let connection_string = null;

if (machine.problem && codes[machine.problem]) {
// trouble shooting
connection_string = await this.showModal({
address: machine.address,
template: codes[machine.problem],
})

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (91% of all statements in
the enclosing function
have an explicit semicolon).
} else if (!window.sessionStorage.getItem("connection-warning-shown")) {
// connect by launching into the "Connection warning" dialog.
connection_string = await this.showModal({
address: machine.address,
template: "connect"
});
} else {
// Try to connect without any dialog
try {
await try2Connect(this.props.machines, machine.connection_string);
connection_string = machine.connection_string;
} catch (err) {
// continue with troubleshooting in the dialog
connection_string = await this.showModal({
address: machine.address,
template: codes[err.problem] || "change-port",
error_options: err,
});
} else if (!window.sessionStorage.getItem("connection-warning-shown"))
this.setState({ show_modal: true, switch_machine: machine, just_connect });
else {
try2Connect(this.props.machines, machine.connection_string)
.then(() => {
const parts = this.props.machines.split_connection_string(machine.connection_string);
const addr = this.props.hostAddr({ host: parts.address }, true);
this.props.loader.connect(parts.address);
if (just_connect) {
this.props.index.navigate();
} else {
this.props.jump(addr);
}
})
.catch(err => {
this.setState({
show_modal: true,
switch_machine: machine,
error_options: err,
problem: err.problem,
just_connect
});
});
}
}

if (connection_string) {
// make the rest of the shell aware that the machine is now connected
const parts = this.props.machines.split_connection_string(connection_string);
this.props.loader.connect(parts.address);
this.props.index.frames.remove(machine);
this.props.index.navigate();
}

return connection_string;
}

async onHostSwitch(machine) {
const connection_string = await this.connectHost(machine);
if (connection_string) {
const parts = this.props.machines.split_connection_string(connection_string);
const addr = this.props.hostAddr({ host: parts.address }, true);
this.props.jump(addr);
}
}

onEditHosts() {
Expand Down Expand Up @@ -296,43 +321,13 @@ export class CockpitHosts extends React.Component {
</HostsSelector>
}
</div>
{this.state.show_modal &&
<HostModal machines_ins={this.props.machines}
onClose={() => this.setState({
show_modal: false,
edit_machine: null,
switch_machine: null,
error_options: null,
problem: null,
})}
address={this.state.edit_machine?.address || this.state.switch_machine?.address}
template={this.state.switch_machine
? (this.state.problem
? codes[this.state.problem] || "change-port"
: "connect")
: null}
error_options={this.state.error_options}
caller_callback={(new_connection_string) => {
const parts = this.props.machines.split_connection_string(new_connection_string);
const addr = this.props.hostAddr({ host: parts.address }, true);
if (this.state.edit_machine) {
if (this.state.edit_machine == this.props.machine &&
parts.address != this.state.edit_machine.address) {
this.props.loader.connect(parts.address);
this.props.jump(addr);
}
} else if (this.state.switch_machine) {
this.props.loader.connect(parts.address);
this.props.index.frames.remove(this.state.switch_machine);
if (this.state.just_connect) {
this.props.index.navigate();
} else {
this.props.jump(addr);
}
}
return Promise.resolve();
}}
/>
{this.state.modal_properties &&
<HostModal machines_ins={this.props.machines}
onClose={() => this.setState({ modal_properties: null })}
{...this.state.modal_properties}
caller_callback={this.state.modal_callback}
caller_cancelled={() => this.state.modal_callback(null)}
/>
}
</>
);
Expand Down
6 changes: 5 additions & 1 deletion pkg/shell/hosts_dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,11 @@ export class HostModal extends React.Component {
error_options: this.state.error_options,
dialogError: this.state.dialogError,
machines_ins: this.props.machines_ins,
onClose: this.props.onClose,
onClose: () => {
if (this.props.caller_cancelled)
this.props.caller_cancelled();
this.props.onClose();
},
run: this.run,
setGoal: this.setGoal,
setError: this.setError,
Expand Down

0 comments on commit 6e91d23

Please sign in to comment.