Add name to account model

Add the name of the account that's connected to the control link on the
web interface via an additional property on the ready payload for the
control link.
This commit is contained in:
Hornwitser committed 2021-03-18 23:05:47 +01:00
1 parent e1549c1b0c
commit 6458810571
6 files changed
+50 -5

No files matched your search

@@ -52,6 +52,8 @@ export default function SiteLayout(props) {
}
let accountMenu = <Menu>
<Menu.ItemGroup title={account.name}/>
<Menu.Divider/>
<Menu.Item danger onClick={() => { account.logOut(); }}>Log out</Menu.Item>
</Menu>;
+19 -1
View File
@@ -1,17 +1,35 @@
import { useContext } from "react";
import { useContext, useEffect, useState } from "react";
import { notifyErrorHandler } from "../util/notify";
import ControlContext from "../components/ControlContext";
/**
* @typedef {Object} Account
* @property {?string} name - Name of the currently logged in account.
* @property {function()} logout - Logs out of the web interface.
*/
export function useAccount() {
let control = useContext(ControlContext);
let [name, setName] = useState(control.accountName);
useEffect(() => {
function onConnect(data) {
setName(data.account.name);
}
function onClose() {
setName(null);
}
control.connector.on("connect", onConnect);
control.connector.on("close", onClose);
return () => {
control.connector.off("connect", onConnect);
control.connector.off("close", onClose);
};
}, [control]);
return {
name,
logOut() {
control.loggingOut = true;
control.shutdown(notifyErrorHandler("Error logging out"));
+12 -1
View File
@@ -55,13 +55,24 @@ export class Control extends libLink.Link {
*/
this.loggingOut = false;
/**
* Name of the account this control link is connected as.
* @type {?string}
*/
this.accountName = null;
this.instanceLogHandlers = new Map();
this.connector.on("connect", () => {
this.connector.on("connect", data => {
this.accountName = data.account.name;
this.updateLogSubscriptions().catch(err => logger.error(
`Unexpected error updating log subscriptions:\n${err.stack}`
));
});
this.connector.on("close", () => {
this.accountName = null;
});
}
async logMessageEventHandler(message) {