import { useState, useEffect } from 'react'; import { XStack, Paragraph, YStack, Button, Text, H6, Avatar } from 'tamagui'; import { useRouter } from 'expo-router'; export default function ContactsPage() { const [contacts, setContacts] = useState([]); const [error, setError] = useState(''); const [loading, setLoading] = useState(true); const router = useRouter(); // Fetch friends from the server const fetchFriends = async () => { try { const authToken = localStorage.getItem('jwtToken'); // Retrieve JWT token if (!authToken) { throw new Error('Authentication required. Please log in.'); } const response = await fetch('http://localhost:4000/friends', { method: 'GET', headers: { Authorization: `Bearer ${authToken}`, }, }); if (!response.ok) { const errorText = await response.text(); throw new Error(errorText || 'Failed to fetch contacts'); } const friends = await response.json(); setContacts(friends); } catch (err) { console.error('Error fetching friends:', err); setError(err.message || 'Failed to load contacts.'); } finally { setLoading(false); } }; // Navigate to chat screen const navigateToChat = (contact) => { router.push({ pathname: `/chat/${contact.username}`, // Use dynamic route params: { friendUsername: contact.username }, // Pass necessary params }); }; useEffect(() => { fetchFriends(); }, []); return (
Your Contacts
{loading ? ( Loading contacts... ) : error ? ( {error} ) : contacts.length === 0 ? ( No contacts found. Add some friends to start chatting! ) : ( contacts.map((contact) => ( {/* Profile Picture */} {/* Contact Details */} {contact.username} {contact.status} {/* Placeholder for online/offline */} {/* Message Button */} )) )}
); }