-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.tsx
98 lines (88 loc) · 2.07 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import React, { useState, useEffect } from 'react';
import {
View,
Alert,
NativeModules,
SafeAreaView,
StyleSheet,
} from 'react-native';
import { WebView } from 'react-native-webview';
import AppServerStatusBar from './AppServerStatusBar';
var RNFS = require('react-native-fs');
const AppWebServer = NativeModules.AppWebServer;
interface State {
serverRunning: boolean;
serverUrl: string | null;
pingUrl: string | null;
}
const App = () => {
const [state, setState] = useState<State>({
serverRunning: false,
serverUrl: null,
pingUrl: null,
});
const handleStatusBarClick = () => {
state.serverRunning ? handleServerStop() : handleServerStart();
};
const handleServerStart = async () => {
try {
const serverUrl = await AppWebServer.start(`${RNFS.MainBundlePath}`);
let newState: State = {
serverUrl,
pingUrl: `${serverUrl}/ping`,
serverRunning: true,
};
setState(newState);
} catch (error) {
Alert.alert(error.message);
}
};
const handleServerStop = async () => {
try {
await AppWebServer.stop();
let newState: State = {
serverUrl: null,
pingUrl: null,
serverRunning: false,
};
setState(newState);
} catch (error) {
Alert.alert(error.message);
}
};
return (
<View style={styles.container}>
<SafeAreaView style={styles.container}>
<AppServerStatusBar
serverRunning={state.serverRunning}
onPress={handleStatusBarClick}
/>
<View style={styles.webViewContainer}>
<WebView
style={styles.webview}
source={{ uri: state.serverRunning ? state.pingUrl : undefined }}
/>
</View>
</SafeAreaView>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'lightgray',
},
webViewContainer: {
flex: 1,
padding: 20,
},
webView: {
flex: 1,
backgroundColor: 'white',
},
infoText: {
fontWeight: 'bold',
paddingBottom: 10,
},
});
export default App;