This repository was archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
146 lines (138 loc) · 3.5 KB
/
Copy pathApp.js
File metadata and controls
146 lines (138 loc) · 3.5 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React, {useState, useCallback, useRef} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
Button,
TextInput,
StatusBar,
} from 'react-native';
import storage from './storage';
import apiFactory from '@sammacbeth/dat-api-v1';
const defaultAddress =
'41f8a987cfeba80a037e51cc8357d513b62514de36f2f9b3d3eeec7a8fb3b5a5';
// create an API using file persistence
const api = apiFactory({
persistantStorageFactory: address =>
Promise.resolve(file => storage('hyperdrive')(`${address}/${file}`)),
});
const load = async address => {
// load an existing dat in memory
const existing = await api.getDat(address, {
persist: true,
driveOptions: {
sparse: true,
},
});
// wait for data
await new Promise(async (resolve, reject) => {
setTimeout(reject.bind(null, 'timeout'), 10000);
await existing.ready;
resolve();
});
const files = await new Promise((resolve, reject) => {
existing.drive.readdir('/', (err, files) => {
if (err) {
return reject('error listing directory');
}
resolve(files);
});
});
return files;
};
const App = () => {
const [files, setFiles] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const [address, setAddress] = useState(defaultAddress);
const inputRef = useRef();
const loadCallback = useCallback(async () => {
setLoading(true);
setError(false);
try {
const newFiles = await load(address);
setFiles(newFiles);
} catch (e) {
setError(e);
}
setLoading(false);
}, [address]);
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={styles.safeArea}>
<View style={styles.wrapper}>
<View style={styles.topBar}>
<TextInput
ref={inputRef}
editable
style={styles.input}
defaultValue={defaultAddress}
placeholder="Enter DAT address"
placeholderTextColor="#666666"
autoCorrect={false}
autoCompleteType="off"
autoCapitalize="none"
onChangeText={setAddress}
/>
<Button onPress={loadCallback} style={styles.button} title="load" />
</View>
<ScrollView style={styles.scrollView}>
<View style={styles.content}>
{error ? (
<Text>Error: {error}</Text>
) : (
<>
{loading ? (
<Text>Loading...</Text>
) : (
<>
{files.length > 0 && (
<View>
<Text>List of files:</Text>
{files.map(file => (
<Text key={file}>{file}</Text>
))}
</View>
)}
</>
)}
</>
)}
</View>
</ScrollView>
</View>
</SafeAreaView>
</>
);
};
const styles = StyleSheet.create({
safeArea: {
flex: 1,
},
wrapper: {
flexGrow: 1,
flexDirection: 'column',
},
topBar: {
flexDirection: 'row',
alignItems: 'center',
},
input: {
width: '70%',
flexGrow: 1,
padding: 5,
marginLeft: 10,
backgroundColor: '#cccccc',
},
button: {},
scrollView: {
flexGrow: 1,
},
content: {
margin: 10,
},
});
export default App;