// app/(tabs)/profile/overview.tsx import React, { useState, useCallback } from 'react'; import { View, ActivityIndicator } from 'react-native'; import { Text } from '@/components/ui/text'; import { Button } from '@/components/ui/button'; import NostrProfileLogin from '@/components/social/NostrProfileLogin'; import ProfileFeed from '@/components/profile/ProfileFeed'; import { useProfilePageData } from '@/lib/hooks/useProfilePageData'; import type { AnyFeedEntry } from '@/types/feed'; /** * Profile overview screen - refactored for consistent hook ordering * This component now uses the useProfilePageData hook to handle all data fetching * and state management, avoiding hook ordering issues. */ export default function OverviewScreen() { // Use our custom hook for all data needs (always calls hooks in same order) const { isAuthenticated, currentUser, stats, bannerImage, feed, renderState, renderError, refreshAll, setRenderError } = useProfilePageData(); // Track refreshing state const [isRefreshing, setIsRefreshing] = useState(false); // Handle refresh const handleRefresh = useCallback(async () => { setIsRefreshing(true); try { await refreshAll(); } finally { setIsRefreshing(false); } }, [refreshAll]); // Handle post selection const handlePostPress = useCallback((entry: AnyFeedEntry) => { // Just log the entry info for now console.log(`Selected ${entry.type}:`, entry); }, []); // Render the login screen const renderLoginScreen = useCallback(() => { return ( ); }, []); // Render loading screen const renderLoadingScreen = useCallback(() => { return ( ); }, []); // Render error screen const renderErrorScreen = useCallback(() => { return ( Something went wrong {renderError?.message || "We had trouble loading your profile. Please try again."} ); }, [renderError, handleRefresh, setRenderError]); // Render the main content const renderMainContent = useCallback(() => { // Create a wrapper for the stats refresh function to match expected Promise type const refreshStatsWrapper = async () => { if (stats.refresh) { try { await stats.refresh(); } catch (error) { console.error('Error refreshing stats:', error); } } }; return ( ); }, [ feed.entries, isRefreshing, handleRefresh, handlePostPress, currentUser, bannerImage.url, bannerImage.defaultUrl, stats.followersCount, stats.followingCount, stats.refresh, stats.isLoading ]); // SINGLE RETURN STATEMENT with conditional rendering // This avoids hook ordering issues by ensuring all hooks are always called return ( {renderState === 'login' && renderLoginScreen()} {renderState === 'loading' && renderLoadingScreen()} {renderState === 'error' && renderErrorScreen()} {renderState === 'content' && renderMainContent()} ); }