-
Notifications
You must be signed in to change notification settings - Fork 183
/
App.tsx
108 lines (101 loc) · 3.06 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
99
100
101
102
103
104
105
106
107
108
/**
* Sample React Native Static Server
* https://github.com/futurepress/react-native-static-server
* @flow
*/
import React, { useState, useEffect } from 'react'
import {
StyleSheet,
Text,
View,
Image
} from 'react-native'
// requires react-native-webview, see: https://github.com/uuidjs/uuid#getrandomvalues-not-supported
import 'react-native-get-random-values'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { v4 as uuidv4 } from 'uuid'
import StaticServer from 'react-native-static-server'
import RNFetchBlob from 'rn-fetch-blob'
import { WebView } from 'react-native-webview'
interface ITestViewProps {
port?: number
root?: string
file?: string
target?: any
}
export default function App (props: ITestViewProps): JSX.Element {
const [origin, setOrigin] = useState<string>('')
const [server, setServer] = useState<StaticServer>(null)
const port = typeof props.port !== 'undefined' ? props.port : 3030
const root = typeof props.root !== 'undefined' ? props.root : 'www/'
const file = typeof props.file !== 'undefined' ? props.file : 'index.html'
const target = typeof props.target !== 'undefined' ? props.target : require('./index.html')
useEffect(() => {
if (origin === '') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const index = target
const { uri } = Image.resolveAssetSource(index)
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + root
const dest = path + file
const startServer = async (): Promise<void> => {
const newServer = new StaticServer(port, root, { localOnly: true })
const origin = await newServer.start()
setOrigin(origin)
setServer(newServer)
}
const prepare = async (): Promise<void> => {
try {
await RNFetchBlob.fs.mkdir(path)
} catch (e) {
console.log(`directory is created ${path}`)
}
let added: Promise<boolean>
if (uri.includes('file://')) {
// Copy file in release
const result = await RNFetchBlob.fs.exists(dest)
if (!result) {
added = RNFetchBlob.fs.cp(uri, dest)
}
} else {
// Download for development
const result = await RNFetchBlob.config({ fileCache: true }).fetch('GET', uri)
added = RNFetchBlob.fs.mv(result.path(), dest)
}
try {
await added
} catch (e) {
console.log(e)
}
await startServer()
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
prepare()
return () => {
if (server !== null) {
server.kill()
}
}
}
}, [])
if (origin !== '') {
return <WebView
source={{ uri: `${origin}/${file}` }}
style={styles.webview}
/>
}
return <View style={styles.container}>
<Text>Loading...</Text>
</View>
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
webview: {
marginTop: 20,
flex: 1
}
})