/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const RNTesterActions = require('./utils/RNTesterActions');
const RNTesterExampleContainer = require('./components/RNTesterExampleContainer');
const RNTesterExampleList = require('./components/RNTesterExampleList');
const RNTesterList = require('./utils/RNTesterList.ios');
const RNTesterNavigationReducer = require('./utils/RNTesterNavigationReducer');
const React = require('react');
const SnapshotViewIOS = require('./examples/Snapshot/SnapshotViewIOS.ios');
const URIActionMap = require('./utils/URIActionMap');
const {
AppRegistry,
AsyncStorage,
BackHandler,
Button,
Linking,
Platform,
SafeAreaView,
StyleSheet,
Text,
useColorScheme,
View,
LogBox,
} = require('react-native');
import type {RNTesterExample} from './types/RNTesterTypes';
import type {RNTesterAction} from './utils/RNTesterActions';
import type {RNTesterNavigationState} from './utils/RNTesterNavigationReducer';
import {RNTesterThemeContext, themes} from './components/RNTesterTheme';
import type {ColorSchemeName} from '../../Libraries/Utilities/NativeAppearance';
type Props = {exampleFromAppetizeParams?: ?string, ...};
LogBox.ignoreLogs(['Module RCTImagePickerManager requires main queue setup']);
const APP_STATE_KEY = 'RNTesterAppState.v2';
const Header = ({
onBack,
title,
}: {
onBack?: () => mixed,
title: string,
...
}) => (
{theme => {
return (
{title}
{onBack && (
)}
);
}}
);
const RNTesterExampleContainerViaHook = ({
onBack,
title,
module,
}: {
onBack?: () => mixed,
title: string,
module: RNTesterExample,
...
}) => {
const colorScheme: ?ColorSchemeName = useColorScheme();
const theme = colorScheme === 'dark' ? themes.dark : themes.light;
return (
);
};
const RNTesterExampleListViaHook = ({
onNavigate,
list,
}: {
onNavigate?: () => mixed,
list: {
ComponentExamples: Array,
APIExamples: Array,
...
},
...
}) => {
const colorScheme: ?ColorSchemeName = useColorScheme();
const theme = colorScheme === 'dark' ? themes.dark : themes.light;
return (
);
};
class RNTesterApp extends React.Component {
_mounted: boolean;
UNSAFE_componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', this._handleBack);
}
componentDidMount() {
this._mounted = true;
Linking.getInitialURL().then(url => {
AsyncStorage.getItem(APP_STATE_KEY, (err, storedString) => {
if (!this._mounted) {
return;
}
const exampleAction = URIActionMap(
this.props.exampleFromAppetizeParams,
);
const urlAction = URIActionMap(url);
const launchAction = exampleAction || urlAction;
const initialAction = launchAction || {type: 'InitialAction'};
this.setState(RNTesterNavigationReducer(undefined, initialAction));
});
});
Linking.addEventListener('url', url => {
this._handleAction(URIActionMap(url));
});
}
componentWillUnmount() {
this._mounted = false;
}
_handleBack = () => {
this._handleAction(RNTesterActions.Back());
};
_handleAction = (action: ?RNTesterAction) => {
if (!action) {
return;
}
const newState = RNTesterNavigationReducer(this.state, action);
if (this.state !== newState) {
this.setState(newState, () =>
AsyncStorage.setItem(APP_STATE_KEY, JSON.stringify(this.state)),
);
}
};
render(): React.Node | null {
if (!this.state) {
return null;
}
if (this.state.openExample) {
const Component = RNTesterList.Modules[this.state.openExample];
if (Component && Component.external) {
return ;
} else {
return (
);
}
}
return (
);
}
}
const styles = StyleSheet.create({
headerContainer: {
borderBottomWidth: StyleSheet.hairlineWidth,
},
header: {
height: 40,
flexDirection: 'row',
},
headerCenter: {
flex: 1,
position: 'absolute',
top: 7,
left: 0,
right: 0,
alignItems: 'center',
},
title: {
fontSize: 19,
fontWeight: '600',
textAlign: 'center',
},
exampleContainer: {
flex: 1,
},
});
AppRegistry.registerComponent('SetPropertiesExampleApp', () =>
require('./examples/SetPropertiesExample/SetPropertiesExampleApp'),
);
AppRegistry.registerComponent('RootViewSizeFlexibilityExampleApp', () =>
require('./examples/RootViewSizeFlexibilityExample/RootViewSizeFlexibilityExampleApp'),
);
AppRegistry.registerComponent('RNTesterApp', () => RNTesterApp);
// Register suitable examples for snapshot tests
RNTesterList.ComponentExamples.concat(RNTesterList.APIExamples).forEach(
(Example: RNTesterExample) => {
const ExampleModule = Example.module;
if (ExampleModule.displayName) {
class Snapshotter extends React.Component<{...}> {
render() {
return (
);
}
}
AppRegistry.registerComponent(
ExampleModule.displayName,
() => Snapshotter,
);
}
},
);
module.exports = RNTesterApp;