github.com/argoproj/argo-cd@v1.8.7/ui/src/app/shared/components/query.tsx (about) 1 import * as PropTypes from 'prop-types'; 2 import * as React from 'react'; 3 import {BehaviorSubject, Observable} from 'rxjs'; 4 5 import {AppContext, Consumer} from '../context'; 6 7 export const Query = (props: {children: (params: URLSearchParams) => React.ReactNode}) => ( 8 <Consumer>{ctx => props.children(new URLSearchParams(ctx.history.location.search))}</Consumer> 9 ); 10 11 export interface ObservableQueryProps { 12 children: (params: Observable<URLSearchParams>) => React.ReactNode; 13 } 14 15 export class ObservableQuery extends React.Component<ObservableQueryProps> { 16 public static contextTypes = { 17 router: PropTypes.object 18 }; 19 20 private search: BehaviorSubject<string>; 21 private stopListen: () => void; 22 23 constructor(props: ObservableQueryProps) { 24 super(props); 25 } 26 27 public componentWillMount() { 28 this.search = new BehaviorSubject(this.appContext.router.history.location.search); 29 this.stopListen = this.appContext.router.history.listen(location => { 30 this.search.next(location.search); 31 }); 32 } 33 34 public componentWillUnmount() { 35 if (this.stopListen) { 36 this.stopListen(); 37 this.stopListen = null; 38 } 39 } 40 41 public render() { 42 return this.props.children(this.search.map(search => new URLSearchParams(search))); 43 } 44 45 private get appContext(): AppContext { 46 return this.context as AppContext; 47 } 48 }