-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
127 lines (116 loc) · 3.68 KB
/
App.js
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
import "react-native-get-random-values"; // Add this import
import { StatusBar } from "expo-status-bar";
import { StyleSheet, ActivityIndicator, View, Text } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import React, { useEffect, useState } from "react";
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import HomeScreen from "./screens/HomeScreen";
import PlayScreen from "./screens/PlayScreen";
import { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY } from "@env";
const Stack = createNativeStackNavigator();
// Initialize the S3 client
const s3Client = new S3Client({
region: "us-east-2", // Replace with your S3 bucket region
credentials: {
accessKeyId: AWS_ACCESS_KEY_ID, // Replace with your access key ID
secretAccessKey: AWS_SECRET_ACCESS_KEY, // Replace with your secret access key
},
});
const getObjectFromS3 = async (bucket, key) => {
const command = new GetObjectCommand({ Bucket: bucket, Key: key });
const signedUrl = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
const response = await fetch(signedUrl);
const fileContent = await response.text();
return fileContent;
};
export default function App() {
const [words, setWords] = useState([]);
const [loading, setLoading] = useState(true);
const [serverMessage, setServerMessage] = useState("");
useEffect(() => {
const readWordList = async () => {
try {
const bucket = "my-word-list-bucket"; // Replace with your S3 bucket name
const key = "filtered-word-list.txt"; // Replace with your S3 object key
const fileContent = await getObjectFromS3(bucket, key);
const wordsArray = fileContent.split(/\r?\n/).filter((word) => word);
setWords(wordsArray);
} catch (error) {
console.error("Error fetching file", error);
} finally {
setLoading(false);
}
};
const fetchServerMessage = async () => {
try {
//http://172.31.1.36:3000
const response = await fetch("http://172.31.1.36:3000");
const result = await response.text();
setServerMessage(result);
} catch (error) {
console.error("Error fetching server message", error);
}
};
readWordList();
fetchServerMessage();
}, []);
if (loading) {
return (
<View style={styles.container}>
<ActivityIndicator size="large" />
</View>
);
}
return (
<>
<StatusBar style="light" />
<NavigationContainer>
<Stack.Navigator
screenOptions={{
headerStyle: { backgroundColor: "#a02f58" },
headerTintColor: "white",
}}
>
<Stack.Screen
name="HomeScreen"
component={HomeScreen}
options={{
title: "",
}}
/>
<Stack.Screen
name="PlayScreen"
component={PlayScreen}
options={{
title: "",
headerBackTitleVisible: false,
headerBackTitle: "back",
gestureEnabled: false,
}}
initialParams={{ words }}
/>
</Stack.Navigator>
</NavigationContainer>
<View style={styles.messageContainer}>
<Text>{serverMessage}</Text>
</View>
</>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
messageContainer: {
position: "absolute",
bottom: 50,
left: 0,
right: 0,
alignItems: "center",
},
});