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 ( {Setting.getShortName(this.state.account.name)} ) } else { return ( {Setting.getShortName(this.state.account.name)} ) } } renderRightDropdown() { const menu = ( {i18next.t("account:My Account")} {i18next.t("account:Sign Out")} ); return (
    { this.renderAvatar() }     {Setting.isMobile() ? null : Setting.getShortName(this.state.account.displayName)}        
) } renderAccount() { let res = []; if (this.state.account === undefined) { return null; } else if (this.state.account === null) { res.push( {i18next.t("account:Sign Up")} ); res.push( {i18next.t("account:Sign In")} ); res.push( {i18next.t("general:Home")} ); } else { res.push(this.renderRightDropdown()); return (
{ res }
) } return res; } renderMenu() { let res = []; if (this.state.account === null || this.state.account === undefined) { return []; } res.push( {i18next.t("general:Home")} {/**/} {/* Home*/} {/**/} ); res.push( {i18next.t("general:Datasets")} ); return res; } renderHomeIfSignedIn(component) { if (this.state.account !== null && this.state.account !== undefined) { return } else { return component; } } renderSigninIfNotSignedIn(component) { if (this.state.account === null) { sessionStorage.setItem("from", window.location.pathname); return } else if (this.state.account === undefined) { return null; } else { return component; } } renderContent() { return (
{ Setting.isMobile() ? null : } { this.renderMenu() } { this.renderAccount() }
}/> this.renderHomeIfSignedIn()}/> this.renderSigninIfNotSignedIn()}/> this.renderSigninIfNotSignedIn()}/>
) } renderFooter() { // How to keep your footer where it belongs ? // https://www.freecodecamp.org/news/how-to-keep-your-footer-where-it-belongs-59c6aa05c59c/ return ( ) } render() { return (
{ this.renderContent() }
{ this.renderFooter() }
); } } export default withRouter(App);