Auto-dismiss client error on reconnect

References: https://todo.sr.ht/~emersion/gamja/74
This commit is contained in:
Simon Ser 2021-11-17 10:12:36 +01:00
parent 82e5a2795d
commit e37c2a2cec

View file

@ -132,6 +132,8 @@ function showNotification(title, options) {
} }
} }
let lastErrorID = 0;
export default class App extends Component { export default class App extends Component {
state = { state = {
...State.create(), ...State.create(),
@ -188,7 +190,7 @@ export default class App extends Component {
this.handleAddNetworkClick = this.handleAddNetworkClick.bind(this); this.handleAddNetworkClick = this.handleAddNetworkClick.bind(this);
this.handleNetworkSubmit = this.handleNetworkSubmit.bind(this); this.handleNetworkSubmit = this.handleNetworkSubmit.bind(this);
this.handleNetworkRemove = this.handleNetworkRemove.bind(this); this.handleNetworkRemove = this.handleNetworkRemove.bind(this);
this.dismissError = this.dismissError.bind(this); this.handleDismissError = this.handleDismissError.bind(this);
this.saveReceipts = debounce(this.saveReceipts.bind(this), 500); this.saveReceipts = debounce(this.saveReceipts.bind(this), 500);
@ -295,11 +297,24 @@ export default class App extends Component {
} }
} }
dismissError(event) { showError(msg) {
event.preventDefault(); this.setState({ error: msg });
lastErrorID++;
return lastErrorID;
}
dismissError(id) {
if (id && id !== lastErrorID) {
return;
}
this.setState({ error: null }); this.setState({ error: null });
} }
handleDismissError(event) {
event.preventDefault();
this.dismissError();
}
setServerState(id, updater, callback) { setServerState(id, updater, callback) {
this.setState((state) => { this.setState((state) => {
return State.updateServer(state, id, updater); return State.updateServer(state, id, updater);
@ -560,10 +575,15 @@ export default class App extends Component {
this.clients.set(serverID, client); this.clients.set(serverID, client);
this.setServerState(serverID, { status: client.status }); this.setServerState(serverID, { status: client.status });
let errorID = null;
client.addEventListener("status", () => { client.addEventListener("status", () => {
this.setServerState(serverID, { status: client.status }); this.setServerState(serverID, { status: client.status });
if (client.status === Client.Status.REGISTERED) { if (client.status === Client.Status.REGISTERED) {
this.setState({ connectForm: false }); this.setState({ connectForm: false });
if (errorID) {
this.dismissError(errorID);
}
} }
}); });
@ -572,7 +592,7 @@ export default class App extends Component {
}); });
client.addEventListener("error", (event) => { client.addEventListener("error", (event) => {
this.setState({ error: event.detail }); errorID = this.showError(event.detail);
}); });
this.createBuffer(serverID, SERVER_BUFFER); this.createBuffer(serverID, SERVER_BUFFER);
@ -936,7 +956,7 @@ export default class App extends Component {
default: default:
if (irc.isError(msg.command) && msg.command != irc.ERR_NOMOTD) { if (irc.isError(msg.command) && msg.command != irc.ERR_NOMOTD) {
let description = msg.params[msg.params.length - 1]; let description = msg.params[msg.params.length - 1];
this.setState({ error: description }); this.showError(description);
} }
} }
@ -946,7 +966,7 @@ export default class App extends Component {
} }
handleConnectSubmit(connectParams) { handleConnectSubmit(connectParams) {
this.setState({ error: null }); this.dismissError();
if (connectParams.autoconnect) { if (connectParams.autoconnect) {
store.autoconnect.put(connectParams); store.autoconnect.put(connectParams);
@ -1036,7 +1056,7 @@ export default class App extends Component {
fetchBacklog(client, target, after, before) { fetchBacklog(client, target, after, before) {
client.fetchHistoryBetween(target, after, before, CHATHISTORY_MAX_SIZE).catch((err) => { client.fetchHistoryBetween(target, after, before, CHATHISTORY_MAX_SIZE).catch((err) => {
console.error("Failed to fetch backlog for '" + target + "': ", err); console.error("Failed to fetch backlog for '" + target + "': ", err);
this.setState({ error: "Failed to fetch backlog for '" + target + "'" }); this.showError("Failed to fetch backlog for '" + target + "'");
this.receipts.delete(target); this.receipts.delete(target);
this.saveReceipts(); this.saveReceipts();
}); });
@ -1158,7 +1178,7 @@ export default class App extends Component {
let cmd = commands[name]; let cmd = commands[name];
if (!cmd) { if (!cmd) {
this.setState({ error: `Unknown command "${name}" (run "/help" to get a command list)` }); this.showError(`Unknown command "${name}" (run "/help" to get a command list)`);
return; return;
} }
@ -1166,13 +1186,13 @@ export default class App extends Component {
cmd.execute(this, args); cmd.execute(this, args);
} catch (error) { } catch (error) {
console.error(`Failed to execute command "${name}":`, error); console.error(`Failed to execute command "${name}":`, error);
this.setState({ error: error.message }); this.showError(error.message);
} }
} }
privmsg(target, text) { privmsg(target, text) {
if (target == SERVER_BUFFER) { if (target == SERVER_BUFFER) {
this.setState({ error: "Cannot send message in server buffer" }); this.showError("Cannot send message in server buffer");
return; return;
} }
@ -1548,7 +1568,7 @@ export default class App extends Component {
<div id="error-msg"> <div id="error-msg">
${this.state.error} ${this.state.error}
${" "} ${" "}
<button onClick=${this.dismissError}>×</button> <button onClick=${this.handleDismissError}>×</button>
</div> </div>
`; `;
} }