// app/(tabs)/social/global.tsx import React, { useCallback, useState, useRef } from 'react'; import { View, FlatList, RefreshControl, TouchableOpacity } from 'react-native'; import { Text } from '@/components/ui/text'; import EnhancedSocialPost from '@/components/social/EnhancedSocialPost'; import { useGlobalFeed } from '@/lib/hooks/useFeedHooks'; import { router } from 'expo-router'; import { ChevronUp } from 'lucide-react-native'; import { AnyFeedEntry } from '@/types/feed'; // Define the conversion function here to avoid import issues function convertToLegacyFeedItem(entry: AnyFeedEntry) { return { id: entry.eventId, type: entry.type, originalEvent: entry.event!, parsedContent: entry.content!, createdAt: (entry.timestamp || Date.now()) / 1000 }; } export default function GlobalScreen() { const { entries, newEntries, loading, resetFeed, clearNewEntries } = useGlobalFeed(); const [isRefreshing, setIsRefreshing] = useState(false); const [showNewButton, setShowNewButton] = useState(false); // Use ref for FlatList to scroll to top const listRef = useRef(null); // Show new entries button when we have new content React.useEffect(() => { if (newEntries.length > 0) { setShowNewButton(true); } }, [newEntries.length]); // Depend on length, not array reference // Handle showing new entries const handleShowNewEntries = useCallback(() => { clearNewEntries(); setShowNewButton(false); // Scroll to top listRef.current?.scrollToOffset({ offset: 0, animated: true }); }, [clearNewEntries]); // Handle refresh const handleRefresh = useCallback(async () => { setIsRefreshing(true); try { await resetFeed(); // Add a slight delay to ensure the UI updates await new Promise(resolve => setTimeout(resolve, 300)); } catch (error) { console.error('Error refreshing feed:', error); } finally { setIsRefreshing(false); } }, [resetFeed]); // Handle post selection - simplified for testing const handlePostPress = useCallback((entry: AnyFeedEntry) => { // Just show an alert with the entry info for testing alert(`Selected ${entry.type} with ID: ${entry.eventId}`); // Alternatively, log to console for debugging console.log(`Selected ${entry.type}:`, entry); }, []); // Memoize render item function const renderItem = useCallback(({ item }: { item: AnyFeedEntry }) => ( handlePostPress(item)} /> ), [handlePostPress]); // Memoize empty component const renderEmptyComponent = useCallback(() => ( loading ? ( Loading global content... ) : ( No global content found Try connecting to more relays or check back later. ) ), [loading]); return ( {showNewButton && ( New Posts ({newEntries.length}) )} item.id} renderItem={renderItem} refreshControl={ } ListEmptyComponent={renderEmptyComponent} contentContainerStyle={{ paddingVertical: 0 }} /> ); }