forked from benjaminkomen/expo-static-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
90 lines (81 loc) · 2.27 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
import {useState} from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';
import * as DocumentPicker from 'expo-document-picker';
import StaticServer from '@dr.pogodin/react-native-static-server';
export default function App() {
const [pickedFileUri, setPickedFileUri] = useState<string | undefined>();
const [serverInstance, setServerInstance] = useState<any | null>(null);
async function pickFile() {
const result = await DocumentPicker.getDocumentAsync({
copyToCacheDirectory: true,
});
if (result !== null && result.assets && result.assets?.length > 0) {
setPickedFileUri(result.assets[0].uri);
}
}
function encodeFile(uri: string) {
return uri.split('file://')[1];
}
async function startServer() {
if (!pickedFileUri) {
console.error('No file picked');
return;
}
console.log(pickFile, encodeFile(pickedFileUri));
console.log('Starting server...');
const server = new StaticServer({
fileDir: encodeFile(pickedFileUri),
port: 8080,
nonLocal: true,
});
server.start().then((url: string) => {
console.log('Server started at', url);
});
setServerInstance(server);
}
async function stopServer() {
if (!serverInstance) {
console.error('No server instance to stop.');
return;
}
serverInstance.stop().then(() => {
console.log('Server stopped');
});
setServerInstance(null);
}
return (
<View style={styles.container}>
<Text style={styles.title}>React Native Static Server</Text>
<Text style={styles.subtitle}>Picked File:</Text>
<Text>{JSON.stringify(pickedFileUri, null, 2)}</Text>
<View style={styles.buttons}>
<Button title={'Pick a file'} onPress={pickFile}/>
{!serverInstance ? (
<Button title={'Start server'} onPress={startServer}/>
) : (
<Button title={'Stop server'} onPress={stopServer}/>
)}
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
title: {
fontSize: 24,
marginBottom: 20,
},
subtitle: {
fontSize: 18,
marginBottom: 10,
textAlign: 'left',
},
buttons: {
gap: 10,
},
});