// app/(tabs)/social/global.tsx import React, { useState, useCallback, useRef } from 'react'; import { View, FlatList, RefreshControl, TouchableOpacity } from 'react-native'; import { Text } from '@/components/ui/text'; import EnhancedSocialPost from '@/components/social/EnhancedSocialPost'; import { ChevronUp, Globe } from 'lucide-react-native'; import { router } from 'expo-router'; import { withOfflineState } from '@/components/social/SocialOfflineState'; import { useSocialFeed } from '@/lib/hooks/useSocialFeed'; function GlobalScreen() { const { feedItems, loading, refresh, isOffline } = useSocialFeed({ feedType: 'global', limit: 30 }); // Convert feed items to the format expected by the UI const entries = React.useMemo(() => { return feedItems.map(item => ({ id: item.id, eventId: item.id, event: item.originalEvent, content: item.parsedContent, timestamp: item.createdAt * 1000, type: item.type as any })); }, [feedItems]); const [isRefreshing, setIsRefreshing] = useState(false); const [showNewButton, setShowNewButton] = useState(false); const [newEntries, setNewEntries] = useState([]); // 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(() => { setNewEntries([]); setShowNewButton(false); // Scroll to top listRef.current?.scrollToOffset({ offset: 0, animated: true }); }, []); // Handle refresh - updated to use forceRefresh parameter const handleRefresh = useCallback(async () => { setIsRefreshing(true); try { console.log('[GlobalScreen] Starting manual refresh (force=true)'); // Use force=true to bypass cooldown await refresh(true); // Add a slight delay to ensure the UI updates await new Promise(resolve => setTimeout(resolve, 300)); console.log('[GlobalScreen] Manual refresh completed successfully'); } catch (error) { console.error('Error refreshing feed:', error); } finally { setIsRefreshing(false); } }, [refresh]); // Handle post selection - simplified for testing const handlePostPress = useCallback((entry: any) => { // Just show an alert with the entry info for testing alert(`Selected ${entry.type} with ID: ${entry.id || entry.eventId}`); // Alternatively, log to console for debugging console.log(`Selected ${entry.type}:`, entry); }, []); // Memoize render item to prevent re-renders const renderItem = useCallback(({ item }: { item: any }) => ( handlePostPress(item)} /> ), [handlePostPress]); // Header component const renderHeaderComponent = useCallback(() => ( Community Feed Discover workout content from the broader POWR community. ), []); // Memoize empty component const renderEmptyComponent = useCallback(() => ( loading ? ( Loading community content from your relays... ) : ( {isOffline ? "No cached global content available" : "No global content found"} {isOffline && ( You're currently offline. Connect to the internet to see the latest content. )} ) ), [loading, isOffline]); return ( {showNewButton && ( New Posts ({newEntries.length}) )} item.id} renderItem={renderItem} refreshControl={ } ListHeaderComponent={renderHeaderComponent} ListEmptyComponent={renderEmptyComponent} contentContainerStyle={{ flexGrow: 1, paddingBottom: 16 }} /> ); } // Export the component wrapped with the offline state HOC export default withOfflineState(GlobalScreen);