-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
101 lines (90 loc) · 2.74 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
import React, { useEffect, useRef, useState } from 'react';
import { SafeAreaView, View, Text, ActivityIndicator, Platform } from 'react-native';
import { useVideo, VideoContext, useDownload, DownloadContext } from './src/hooks';
import { Screen } from './src/screens';
import { theme } from './src/utils';
import { WebWorker } from './src/components';
import type { Enc } from './src/utils/m3u8';
import type { DecryptData } from './src/components/WebWorker/WebWorker';
const App = () => {
const decryptCh = useRef<((value: string | PromiseLike<string>) => void) | null>(null)
const [decryptData, setDecryptData] = useState<DecryptData>()
const decrypt = async (enc: Enc, encryptedBytes: string) => {
setDecryptData({ enc, encryptedBytes })
const wait = new Promise<string>((res) => {
decryptCh.current = res
})
return await wait
}
const video = useVideo();
const download = useDownload({ getVideoUrl: video.actions.getVideoUrl, decrypt });
const error = video.state.error;
useEffect(() => {
if (!error) {
return;
}
setTimeout(() => {
video.actions.setError('');
}, 1000);
}, [error]);
return (
<VideoContext.Provider value={video}>
<DownloadContext.Provider value={download}>
<SafeAreaView
style={{
flex: 1,
backgroundColor: theme.blackA(),
}}>
{(!Platform.isTV && Platform.OS !== 'web') &&
<View style={{ height: 0, width: 0, overflow: 'hidden' }}>
<WebWorker
data={decryptData}
onDone={(decrypted) => {
if (decryptCh.current) {
decryptCh.current(decrypted)
decryptCh.current = null
setDecryptData(undefined)
}
}} />
</View>
}
{error !== '' && <Error text={error} />}
{video.state.loading && video.state.init && (
<ActivityIndicator
color={theme.primary}
size={48}
style={{
right: 20,
top: 20,
alignSelf: 'flex-end',
position: 'absolute',
zIndex: 99,
}}
/>
)}
<Screen />
</SafeAreaView>
</DownloadContext.Provider>
</VideoContext.Provider>
);
};
const Error: React.FC<{ text: string }> = ({ text }) => {
return (
<View
style={{
padding: 10,
alignItems: 'center',
backgroundColor: theme.warn,
}}>
<Text
style={{
fontSize: 24,
fontWeight: 'bold',
color: theme.whiteA(),
}}>
{text}
</Text>
</View>
)
}
export default App;