-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedScreen.js
64 lines (59 loc) · 1.62 KB
/
FeedScreen.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
import React, { useState, useEffect } from 'react';
import { View, FlatList, Text, StyleSheet } from 'react-native';
import { db } from './firebase';
import { collection, query, onSnapshot, orderBy } from "firebase/firestore";
export default function FeedScreen() {
const [threads, setThreads] = useState([]);
useEffect(() => {
const q = query(collection(db, "threads"), orderBy("createdAt", "desc"));
const unsubscribe = onSnapshot(q, (snapshot) => {
setThreads(snapshot.docs.map(doc => ({
id: doc.id,
data: doc.data()
})));
});
return unsubscribe;
}, []);
return (
<View style={styles.container}>
<FlatList
data={threads}
renderItem={({ item }) => (
<View style={styles.threadContainer}>
<Text style={styles.threadTitle}>{item.data.title}</Text>
<Text style={styles.threadContent}>{item.data.content}</Text>
<Text style={styles.threadInfo}>{item.data.displayName || item.data.user} - {new Date(item.data.createdAt.seconds * 1000).toLocaleString()}</Text>
</View>
)}
keyExtractor={item => item.id}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: 'black',
},
threadContainer: {
backgroundColor: 'rgba(255, 255, 255, 0.1)',
padding: 15,
borderRadius: 10,
marginBottom: 10,
},
threadTitle: {
color: 'white',
fontSize: 18,
fontWeight: 'bold',
},
threadContent: {
color: 'white',
marginTop: 5,
},
threadInfo: {
color: 'gray',
marginTop: 10,
fontSize: 12,
},
});