github.com/argoproj/argo-cd@v1.8.7/ui/src/app/shared/components/page.tsx (about) 1 import {DataLoader, Page as ArgoPage, Toolbar, Utils} from 'argo-ui'; 2 import * as PropTypes from 'prop-types'; 3 import * as React from 'react'; 4 import {BehaviorSubject, Observable} from 'rxjs'; 5 import {AppContext} from '../context'; 6 import {services} from '../services'; 7 import requests from '../services/requests'; 8 const mostRecentLoggedIn = new BehaviorSubject<boolean>(false); 9 10 function isLoggedIn(): Observable<boolean> { 11 services.users.get().then(info => mostRecentLoggedIn.next(info.loggedIn)); 12 return mostRecentLoggedIn; 13 } 14 15 export class Page extends React.Component<{title: string; toolbar?: Toolbar | Observable<Toolbar>}> { 16 public static contextTypes = { 17 router: PropTypes.object, 18 history: PropTypes.object 19 }; 20 21 public render() { 22 return ( 23 <DataLoader 24 input={new Date()} 25 load={() => 26 Utils.toObservable(this.props.toolbar).map(toolbar => { 27 toolbar = toolbar || {}; 28 toolbar.tools = [ 29 toolbar.tools, 30 <DataLoader key='loginPanel' load={() => isLoggedIn()}> 31 {loggedIn => 32 loggedIn ? ( 33 <a key='logout' onClick={() => this.goToLogin(true)}> 34 Log out 35 </a> 36 ) : ( 37 <a key='login' onClick={() => this.goToLogin(false)}> 38 Log in 39 </a> 40 ) 41 } 42 </DataLoader> 43 ]; 44 return toolbar; 45 }) 46 }> 47 {toolbar => <ArgoPage title={this.props.title} children={this.props.children} toolbar={toolbar} />} 48 </DataLoader> 49 ); 50 } 51 52 private async goToLogin(logout = false) { 53 if (logout) { 54 window.location.href = requests.toAbsURL('/auth/logout'); 55 } else { 56 this.appContext.history.push('/login'); 57 } 58 } 59 60 private get appContext(): AppContext { 61 return this.context as AppContext; 62 } 63 }