You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.js 8.5 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import React, {Component} from 'react';
  2. import {Switch, Redirect, Route, withRouter, Link} from 'react-router-dom';
  3. import {Avatar, BackTop, Dropdown, Layout, Menu} from 'antd';
  4. import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons';
  5. import './App.less';
  6. import * as Setting from "./Setting";
  7. import * as AccountBackend from "./backend/AccountBackend";
  8. import AuthCallback from "./AuthCallback";
  9. import * as Conf from "./Conf";
  10. import HomePage from "./HomePage";
  11. import DatasetListPage from "./DatasetListPage";
  12. import DatasetEditPage from "./DatasetEditPage";
  13. import SigninPage from "./SigninPage";
  14. import i18next from "i18next";
  15. import SelectLanguageBox from "./SelectLanguageBox";
  16. const {Header, Footer} = Layout;
  17. class App extends Component {
  18. constructor(props) {
  19. super(props);
  20. this.state = {
  21. classes: props,
  22. selectedMenuKey: 0,
  23. account: undefined,
  24. uri: null,
  25. };
  26. Setting.initServerUrl();
  27. Setting.initCasdoorSdk(Conf.AuthConfig);
  28. }
  29. componentWillMount() {
  30. this.updateMenuKey();
  31. this.getAccount();
  32. }
  33. componentDidUpdate() {
  34. // eslint-disable-next-line no-restricted-globals
  35. const uri = location.pathname;
  36. if (this.state.uri !== uri) {
  37. this.updateMenuKey();
  38. }
  39. }
  40. updateMenuKey() {
  41. // eslint-disable-next-line no-restricted-globals
  42. const uri = location.pathname;
  43. this.setState({
  44. uri: uri,
  45. });
  46. if (uri === '/') {
  47. this.setState({selectedMenuKey: '/'});
  48. } else if (uri.includes('/datasets')) {
  49. this.setState({ selectedMenuKey: '/datasets' });
  50. } else {
  51. this.setState({selectedMenuKey: 'null'});
  52. }
  53. }
  54. onUpdateAccount(account) {
  55. this.setState({
  56. account: account
  57. });
  58. }
  59. setLanguage(account) {
  60. // let language = account?.language;
  61. let language = localStorage.getItem("language");
  62. if (language !== "" && language !== i18next.language) {
  63. Setting.setLanguage(language);
  64. }
  65. }
  66. getAccount() {
  67. AccountBackend.getAccount()
  68. .then((res) => {
  69. let account = res.data;
  70. if (account !== null) {
  71. this.setLanguage(account);
  72. }
  73. this.setState({
  74. account: account,
  75. });
  76. });
  77. }
  78. signout() {
  79. AccountBackend.signout()
  80. .then((res) => {
  81. if (res.status === 'ok') {
  82. this.setState({
  83. account: null
  84. });
  85. Setting.showMessage("success", `Successfully signed out, redirected to homepage`);
  86. Setting.goToLink("/");
  87. // this.props.history.push("/");
  88. } else {
  89. Setting.showMessage("error", `Signout failed: ${res.msg}`);
  90. }
  91. });
  92. }
  93. handleRightDropdownClick(e) {
  94. if (e.key === '/account') {
  95. Setting.openLink(Setting.getMyProfileUrl(this.state.account));
  96. } else if (e.key === '/logout') {
  97. this.signout();
  98. }
  99. }
  100. renderAvatar() {
  101. if (this.state.account.avatar === "") {
  102. return (
  103. <Avatar style={{ backgroundColor: Setting.getAvatarColor(this.state.account.name), verticalAlign: 'middle' }} size="large">
  104. {Setting.getShortName(this.state.account.name)}
  105. </Avatar>
  106. )
  107. } else {
  108. return (
  109. <Avatar src={this.state.account.avatar} style={{verticalAlign: 'middle' }} size="large">
  110. {Setting.getShortName(this.state.account.name)}
  111. </Avatar>
  112. )
  113. }
  114. }
  115. renderRightDropdown() {
  116. const menu = (
  117. <Menu onClick={this.handleRightDropdownClick.bind(this)}>
  118. <Menu.Item key="/account">
  119. <SettingOutlined />
  120. {i18next.t("account:My Account")}
  121. </Menu.Item>
  122. <Menu.Item key="/logout">
  123. <LogoutOutlined />
  124. {i18next.t("account:Sign Out")}
  125. </Menu.Item>
  126. </Menu>
  127. );
  128. return (
  129. <Dropdown key="/rightDropDown" overlay={menu} className="rightDropDown">
  130. <div className="ant-dropdown-link" style={{float: 'right', cursor: 'pointer'}}>
  131. &nbsp;
  132. &nbsp;
  133. {
  134. this.renderAvatar()
  135. }
  136. &nbsp;
  137. &nbsp;
  138. {Setting.isMobile() ? null : Setting.getShortName(this.state.account.displayName)} &nbsp; <DownOutlined />
  139. &nbsp;
  140. &nbsp;
  141. &nbsp;
  142. </div>
  143. </Dropdown>
  144. )
  145. }
  146. renderAccount() {
  147. let res = [];
  148. if (this.state.account === undefined) {
  149. return null;
  150. } else if (this.state.account === null) {
  151. res.push(
  152. <Menu.Item key="/signup" style={{float: 'right', marginRight: '20px'}}>
  153. <a href={Setting.getSignupUrl()}>
  154. {i18next.t("account:Sign Up")}
  155. </a>
  156. </Menu.Item>
  157. );
  158. res.push(
  159. <Menu.Item key="/signin" style={{float: 'right'}}>
  160. <a href={Setting.getSigninUrl()}>
  161. {i18next.t("account:Sign In")}
  162. </a>
  163. </Menu.Item>
  164. );
  165. res.push(
  166. <Menu.Item key="/" style={{float: 'right'}}>
  167. <a href="/">
  168. {i18next.t("general:Home")}
  169. </a>
  170. </Menu.Item>
  171. );
  172. } else {
  173. res.push(this.renderRightDropdown());
  174. return (
  175. <div style={{float: 'right', margin: '0px', padding: '0px'}}>
  176. {
  177. res
  178. }
  179. </div>
  180. )
  181. }
  182. return res;
  183. }
  184. renderMenu() {
  185. let res = [];
  186. if (this.state.account === null || this.state.account === undefined) {
  187. return [];
  188. }
  189. res.push(
  190. <Menu.Item key="/">
  191. <a href="/">
  192. {i18next.t("general:Home")}
  193. </a>
  194. {/*<Link to="/">*/}
  195. {/* Home*/}
  196. {/*</Link>*/}
  197. </Menu.Item>
  198. );
  199. res.push(
  200. <Menu.Item key="/datasets">
  201. <Link to="/datasets">
  202. {i18next.t("general:Datasets")}
  203. </Link>
  204. </Menu.Item>
  205. );
  206. return res;
  207. }
  208. renderHomeIfSignedIn(component) {
  209. if (this.state.account !== null && this.state.account !== undefined) {
  210. return <Redirect to='/' />
  211. } else {
  212. return component;
  213. }
  214. }
  215. renderSigninIfNotSignedIn(component) {
  216. if (this.state.account === null) {
  217. sessionStorage.setItem("from", window.location.pathname);
  218. return <Redirect to='/signin' />
  219. } else if (this.state.account === undefined) {
  220. return null;
  221. }
  222. else {
  223. return component;
  224. }
  225. }
  226. renderContent() {
  227. return (
  228. <div>
  229. <Header style={{padding: '0', marginBottom: '3px'}}>
  230. {
  231. Setting.isMobile() ? null : <a className="logo" href={"/"}/>
  232. }
  233. <Menu
  234. // theme="dark"
  235. mode={"horizontal"}
  236. selectedKeys={[`${this.state.selectedMenuKey}`]}
  237. style={{lineHeight: '64px'}}
  238. >
  239. {
  240. this.renderMenu()
  241. }
  242. {
  243. this.renderAccount()
  244. }
  245. <SelectLanguageBox />
  246. </Menu>
  247. </Header>
  248. <Switch>
  249. <Route exact path="/callback" component={AuthCallback}/>
  250. <Route exact path="/" render={(props) => <HomePage account={this.state.account} {...props} />}/>
  251. <Route exact path="/signin" render={(props) => this.renderHomeIfSignedIn(<SigninPage {...props} />)}/>
  252. <Route exact path="/datasets" render={(props) => this.renderSigninIfNotSignedIn(<DatasetListPage account={this.state.account} {...props} />)}/>
  253. <Route exact path="/datasets/:datasetName" render={(props) => this.renderSigninIfNotSignedIn(<DatasetEditPage account={this.state.account} {...props} />)}/>
  254. </Switch>
  255. </div>
  256. )
  257. }
  258. renderFooter() {
  259. // How to keep your footer where it belongs ?
  260. // https://www.freecodecamp.org/news/how-to-keep-your-footer-where-it-belongs-59c6aa05c59c/
  261. return (
  262. <Footer id="footer" style={
  263. {
  264. borderTop: '1px solid #e8e8e8',
  265. backgroundColor: 'white',
  266. textAlign: 'center',
  267. }
  268. }>
  269. Made with <span style={{color: 'rgb(255, 255, 255)'}}>❤️</span> by <a style={{fontWeight: "bold", color: "black"}} target="_blank" href="https://github.com/casbin/casvisor">Casvisor</a>, { Setting.isMobile() ? "Mobile" : "Desktop" } View
  270. </Footer>
  271. )
  272. }
  273. render() {
  274. return (
  275. <div id="parent-area">
  276. <BackTop/>
  277. <div id="content-wrap">
  278. {
  279. this.renderContent()
  280. }
  281. </div>
  282. {
  283. this.renderFooter()
  284. }
  285. </div>
  286. );
  287. }
  288. }
  289. export default withRouter(App);

No Description