import React, {Component} from 'react';
import {Switch, Redirect, Route, withRouter, Link} from 'react-router-dom';
import {Avatar, BackTop, Dropdown, Layout, Menu} from 'antd';
import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons';
import './App.less';
import * as Setting from "./Setting";
import * as AccountBackend from "./backend/AccountBackend";
import AuthCallback from "./AuthCallback";
import * as Conf from "./Conf";
import HomePage from "./HomePage";
import DatasetListPage from "./DatasetListPage";
import DatasetEditPage from "./DatasetEditPage";
import SigninPage from "./SigninPage";
import i18next from "i18next";
import SelectLanguageBox from "./SelectLanguageBox";
const {Header, Footer} = Layout;
class App extends Component {
constructor(props) {
super(props);
this.state = {
classes: props,
selectedMenuKey: 0,
account: undefined,
uri: null,
};
Setting.initServerUrl();
Setting.initCasdoorSdk(Conf.AuthConfig);
}
componentWillMount() {
this.updateMenuKey();
this.getAccount();
}
componentDidUpdate() {
// eslint-disable-next-line no-restricted-globals
const uri = location.pathname;
if (this.state.uri !== uri) {
this.updateMenuKey();
}
}
updateMenuKey() {
// eslint-disable-next-line no-restricted-globals
const uri = location.pathname;
this.setState({
uri: uri,
});
if (uri === '/') {
this.setState({selectedMenuKey: '/'});
} else if (uri.includes('/datasets')) {
this.setState({ selectedMenuKey: '/datasets' });
} else {
this.setState({selectedMenuKey: 'null'});
}
}
onUpdateAccount(account) {
this.setState({
account: account
});
}
setLanguage(account) {
// let language = account?.language;
let language = localStorage.getItem("language");
if (language !== "" && language !== i18next.language) {
Setting.setLanguage(language);
}
}
getAccount() {
AccountBackend.getAccount()
.then((res) => {
let account = res.data;
if (account !== null) {
this.setLanguage(account);
}
this.setState({
account: account,
});
});
}
signout() {
AccountBackend.signout()
.then((res) => {
if (res.status === 'ok') {
this.setState({
account: null
});
Setting.showMessage("success", `Successfully signed out, redirected to homepage`);
Setting.goToLink("/");
// this.props.history.push("/");
} else {
Setting.showMessage("error", `Signout failed: ${res.msg}`);
}
});
}
handleRightDropdownClick(e) {
if (e.key === '/account') {
Setting.openLink(Setting.getMyProfileUrl(this.state.account));
} else if (e.key === '/logout') {
this.signout();
}
}
renderAvatar() {
if (this.state.account.avatar === "") {
return (