1import Constants from 'expo-constants';
2import React from 'react';
3import { ScrollView, View } from 'react-native';
4
5import HeadingText from '../components/HeadingText';
6import MonoText from '../components/MonoText';
7import Colors from '../constants/Colors';
8
9interface State {
10  value?: string | (() => void);
11  error?: Error;
12}
13
14class ExpoConstant extends React.Component<{ value?: any; name: string }, State> {
15  readonly state: State = {};
16
17  componentDidMount() {
18    this.updateValue();
19  }
20
21  async updateValue() {
22    let value = this.props.value;
23
24    if (!value) {
25      value = Constants[this.props.name];
26    }
27    if (typeof value === 'function') {
28      try {
29        value = await this.props.value();
30      } catch (error) {
31        console.error(error);
32        this.setState({ error: error.message });
33      }
34    }
35    if (typeof value === 'object') {
36      value = JSON.stringify(value, null, 2);
37    } else if (typeof value === 'boolean') {
38      value = value ? 'true' : 'false';
39    }
40    this.setState({ value });
41  }
42
43  render() {
44    const { value, error } = this.state;
45    const { name } = this.props;
46
47    if (!value || typeof value === 'function') {
48      return null;
49    }
50
51    return (
52      <View style={{ marginBottom: 10 }}>
53        <HeadingText>{name}</HeadingText>
54        <MonoText containerStyle={error && { borderColor: 'red' }}>{error || value}</MonoText>
55      </View>
56    );
57  }
58}
59
60// Ignore deprecated properties
61const IGNORED_CONSTANTS = ['__unsafeNoWarnManifest', 'linkingUrl'];
62
63export default class ConstantsScreen extends React.PureComponent {
64  render() {
65    return (
66      <ScrollView style={{ padding: 10, flex: 1, backgroundColor: Colors.greyBackground }}>
67        {Object.keys(Constants)
68          .filter(value => !IGNORED_CONSTANTS.includes(value))
69          .sort()
70          .map(key => {
71            if (typeof Constants[key] === 'function') return null;
72            return <ExpoConstant name={key} key={key} />;
73          })}
74        <ExpoConstant name="webViewUserAgent" value={() => Constants.getWebViewUserAgentAsync()} />
75      </ScrollView>
76    );
77  }
78}
79