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 () => {
// 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);
// Set a timeout to prevent loading from getting stuck
loadingTimeoutRef.current = setTimeout(() => {
console.warn('Account loading timeout, forcing loading to false');
console.warn('[AccountContext] Account loading timeout, forcing loading to false');
setLoading(false);
}, 10000); // 10 second timeout
}, 5000); // Reduced to 5 seconds for faster fallback
try {
const u = await accountService.getCurrentUser();
if (__DEV__) console.log('[AccountContext] refreshCurrentUser completed:', u ? 'user found' : 'no user');
setUser(u);
} 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 {
if (loadingTimeoutRef.current) {
clearTimeout(loadingTimeoutRef.current);
loadingTimeoutRef.current = null;
}
setLoading(false);
if (__DEV__) console.log('[AccountContext] refreshCurrentUser finished');
}
},
updateProfile: async (partial) => {

View file

@ -264,7 +264,14 @@ const SettingsScreen: React.FC = () => {
// Tablet-specific state
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
useEffect(() => {
// 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 });
}
refreshAuthStatus();
// Only refresh account user if we don't have a user or if we're not already loading
if (!user && !accountLoading) {
// Don't refresh account user on every focus - the context handles auth state changes
// Only refresh if we have no user at all and aren't loading (rare edge case)
if (!user && !accountLoading && !initialLoadComplete) {
refreshCurrentUser();
}
});
return unsubscribe;
}, [navigation, isAuthenticated, userProfile, refreshAuthStatus, refreshCurrentUser, user, accountLoading]);
// 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);
}, [navigation, isAuthenticated, userProfile, refreshAuthStatus, refreshCurrentUser, user, accountLoading, initialLoadComplete]);
const loadData = useCallback(async () => {
try {
// Load addon count and get their catalogs
const addons = await stremioService.getInstalledAddonsAsync();
setAddonCount(addons.length);
setInitialLoadComplete(true);
// Count total available catalogs
let totalCatalogs = 0;