account loading.. fix

This commit is contained in:
tapframe 2025-09-25 23:13:09 +05:30
parent e9659ee305
commit a663f58502
2 changed files with 28 additions and 17 deletions

View file

@ -94,27 +94,35 @@ export const AccountProvider: React.FC<{ children: React.ReactNode }> = ({ child
}, },
refreshCurrentUser: async () => { refreshCurrentUser: async () => {
// Don't set loading if already loading to avoid conflicts // Don't set loading if already loading to avoid conflicts
if (loading) return; if (loading) {
if (__DEV__) console.log('[AccountContext] Already loading, skipping refresh');
return;
}
if (__DEV__) console.log('[AccountContext] Starting refreshCurrentUser');
setLoading(true); setLoading(true);
// Set a timeout to prevent loading from getting stuck // Set a timeout to prevent loading from getting stuck
loadingTimeoutRef.current = setTimeout(() => { loadingTimeoutRef.current = setTimeout(() => {
console.warn('Account loading timeout, forcing loading to false'); console.warn('[AccountContext] Account loading timeout, forcing loading to false');
setLoading(false); setLoading(false);
}, 10000); // 10 second timeout }, 5000); // Reduced to 5 seconds for faster fallback
try { try {
const u = await accountService.getCurrentUser(); const u = await accountService.getCurrentUser();
if (__DEV__) console.log('[AccountContext] refreshCurrentUser completed:', u ? 'user found' : 'no user');
setUser(u); setUser(u);
} catch (error) { } catch (error) {
console.error('Failed to refresh current user:', error); console.error('[AccountContext] Failed to refresh current user:', error);
// Still set user to null on error to ensure we don't get stuck
setUser(null);
} finally { } finally {
if (loadingTimeoutRef.current) { if (loadingTimeoutRef.current) {
clearTimeout(loadingTimeoutRef.current); clearTimeout(loadingTimeoutRef.current);
loadingTimeoutRef.current = null; loadingTimeoutRef.current = null;
} }
setLoading(false); setLoading(false);
if (__DEV__) console.log('[AccountContext] refreshCurrentUser finished');
} }
}, },
updateProfile: async (partial) => { updateProfile: async (partial) => {

View file

@ -264,7 +264,14 @@ const SettingsScreen: React.FC = () => {
// Tablet-specific state // Tablet-specific state
const [selectedCategory, setSelectedCategory] = useState('account'); const [selectedCategory, setSelectedCategory] = useState('account');
// States for dynamic content
const [addonCount, setAddonCount] = useState<number>(0);
const [catalogCount, setCatalogCount] = useState<number>(0);
const [mdblistKeySet, setMdblistKeySet] = useState<boolean>(false);
const [openRouterKeySet, setOpenRouterKeySet] = useState<boolean>(false);
const [initialLoadComplete, setInitialLoadComplete] = useState<boolean>(false);
// Add a useEffect to check authentication status on focus // Add a useEffect to check authentication status on focus
useEffect(() => { useEffect(() => {
// This will reload the Trakt auth status whenever the settings screen is focused // This will reload the Trakt auth status whenever the settings screen is focused
@ -277,26 +284,22 @@ const SettingsScreen: React.FC = () => {
if (__DEV__) console.log('SettingsScreen focused, refreshing auth status. Current state:', { isAuthenticated, userProfile: userProfile?.username }); if (__DEV__) console.log('SettingsScreen focused, refreshing auth status. Current state:', { isAuthenticated, userProfile: userProfile?.username });
} }
refreshAuthStatus(); refreshAuthStatus();
// Only refresh account user if we don't have a user or if we're not already loading // Don't refresh account user on every focus - the context handles auth state changes
if (!user && !accountLoading) { // Only refresh if we have no user at all and aren't loading (rare edge case)
if (!user && !accountLoading && !initialLoadComplete) {
refreshCurrentUser(); refreshCurrentUser();
} }
}); });
return unsubscribe; return unsubscribe;
}, [navigation, isAuthenticated, userProfile, refreshAuthStatus, refreshCurrentUser, user, accountLoading]); }, [navigation, isAuthenticated, userProfile, refreshAuthStatus, refreshCurrentUser, user, accountLoading, initialLoadComplete]);
// States for dynamic content
const [addonCount, setAddonCount] = useState<number>(0);
const [catalogCount, setCatalogCount] = useState<number>(0);
const [mdblistKeySet, setMdblistKeySet] = useState<boolean>(false);
const [openRouterKeySet, setOpenRouterKeySet] = useState<boolean>(false);
const loadData = useCallback(async () => { const loadData = useCallback(async () => {
try { try {
// Load addon count and get their catalogs // Load addon count and get their catalogs
const addons = await stremioService.getInstalledAddonsAsync(); const addons = await stremioService.getInstalledAddonsAsync();
setAddonCount(addons.length); setAddonCount(addons.length);
setInitialLoadComplete(true);
// Count total available catalogs // Count total available catalogs
let totalCatalogs = 0; let totalCatalogs = 0;