|
1 | | -import React, { Component as ReactComponent, Fragment } from 'react'; |
| 1 | +import React, { Component, Fragment } from 'react'; |
2 | 2 | import { View, StyleSheet } from 'react-native'; |
3 | | -import { Observer } from 'mobx-react'; |
4 | | -import { fromStream } from 'mobx-utils'; |
| 3 | +import { from } from 'rxjs'; |
| 4 | +import { componentFromStreamWithConfig } from 'recompose'; |
| 5 | + |
5 | 6 | import { Navigation } from './Navigation'; |
| 7 | +import { map, publish, withLatestFrom } from 'rxjs/operators'; |
| 8 | +import { withBackContext, WithBackContext } from './withBackContext'; |
| 9 | +import { BackContext } from './Navigation/BackContext'; |
| 10 | +import { last } from './utils/Array.last'; |
| 11 | + |
| 12 | +class FullScreenPortalComponent extends Component<WithBackContext<{}>> { |
| 13 | + static FullScreenStack = componentFromStreamWithConfig({ |
| 14 | + fromESObservable: from, |
| 15 | + toESObservable: stream => stream, |
| 16 | + })(() => |
| 17 | + Navigation.instance.fullScreenDelegate.fullSceenStack$.pipe( |
| 18 | + map(fullScreenStack => <>{fullScreenStack}</>) |
| 19 | + ) |
| 20 | + ); |
6 | 21 |
|
7 | | -export class FullScreenPortal extends ReactComponent { |
8 | | - fullScreenStack = fromStream(Navigation.instance.fullScreenDelegate.fullSceenStack$); |
| 22 | + constructor(props: WithBackContext<{}>) { |
| 23 | + super(props); |
| 24 | + this.back$.connect(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @TODO Pipe operator cannot infer return type as ConnectableObservable. |
| 29 | + * See https://github.com/ReactiveX/rxjs/issues/2972. |
| 30 | + */ |
| 31 | + // @ts-ignore |
| 32 | + back$: ConnectableObservable<BackEvent> = this.props.backContext.back$.pipe( |
| 33 | + withLatestFrom(Navigation.instance.fullScreenDelegate.fullSceenStack$), |
| 34 | + map(([_, fullScreenStack]) => { |
| 35 | + const currentScreen = last(fullScreenStack.filter(screen => screen.props.visible)); |
| 36 | + if (currentScreen) { |
| 37 | + return { target: currentScreen.props.name }; |
| 38 | + } |
| 39 | + return { target: null }; |
| 40 | + }), |
| 41 | + publish() |
| 42 | + ); |
9 | 43 |
|
10 | 44 | render() { |
11 | 45 | return ( |
12 | | - <View style={StyleSheet.absoluteFill}> |
13 | | - <Fragment>{this.props.children}</Fragment> |
14 | | - <Observer> |
15 | | - {() => { |
16 | | - if (this.fullScreenStack.current) { |
17 | | - return ( |
18 | | - <Fragment> |
19 | | - {this.fullScreenStack.current && |
20 | | - this.fullScreenStack.current.map(({ Component, name, isAuthorized }) => ( |
21 | | - <Component isAuthorized={isAuthorized} key={name} /> |
22 | | - ))} |
23 | | - </Fragment> |
24 | | - ); |
25 | | - } |
26 | | - /** |
27 | | - * @todo Improve typing of <Observer /> to accept null JSXElement. |
28 | | - * Expected: `return null;` |
29 | | - */ |
30 | | - return <Fragment />; |
31 | | - }} |
32 | | - </Observer> |
33 | | - </View> |
| 46 | + <BackContext.Provider |
| 47 | + value={{ |
| 48 | + back$: this.back$, |
| 49 | + }} |
| 50 | + > |
| 51 | + <View style={StyleSheet.absoluteFill}> |
| 52 | + <Fragment>{this.props.children}</Fragment> |
| 53 | + <FullScreenPortalComponent.FullScreenStack /> |
| 54 | + </View> |
| 55 | + </BackContext.Provider> |
34 | 56 | ); |
35 | 57 | } |
36 | 58 | } |
| 59 | + |
| 60 | +export const FullScreenPortal = withBackContext(FullScreenPortalComponent); |
0 commit comments