Add frontend loading imporvements 2

This commit is contained in:
Urtzi Alfaro
2025-12-28 22:29:27 +01:00
parent 54662dde79
commit 96d8576103
7 changed files with 206 additions and 70 deletions

View File

@@ -69,18 +69,8 @@ export const useTenantInitializer = () => {
const virtualTenantId = localStorage.getItem('virtual_tenant_id');
const storedTier = localStorage.getItem('subscription_tier');
console.log('🔍 [TenantInitializer] Demo mode detected:', {
isDemoMode,
demoSessionId,
virtualTenantId,
demoAccountType,
storedTier,
currentTenant: currentTenant?.id
});
// Guard: If no virtual_tenant_id is available, skip tenant setup
if (!virtualTenantId) {
console.warn('⚠️ [TenantInitializer] No virtual_tenant_id found in localStorage');
return;
}
@@ -90,8 +80,6 @@ export const useTenantInitializer = () => {
currentTenant.id === virtualTenantId;
if (!isValidDemoTenant) {
console.log('🔧 [TenantInitializer] Setting up demo tenant...');
// Determine the appropriate subscription tier based on stored value or account type
const subscriptionTier = storedTier as SubscriptionTier || getDemoTierForAccountType(demoAccountType);
@@ -111,50 +99,40 @@ export const useTenantInitializer = () => {
postal_code: '28001',
phone: null,
is_active: true,
subscription_plan: subscriptionTier, // New field name
subscription_tier: subscriptionTier, // Deprecated but kept for backward compatibility
subscription_plan: subscriptionTier,
subscription_tier: subscriptionTier,
ml_model_trained: false,
last_training_date: null,
owner_id: 'demo-user',
created_at: new Date().toISOString(),
};
console.log(`✅ [TenantInitializer] Setting up tenant with tier: ${subscriptionTier}`);
// Set the demo tenant as current
setCurrentTenant(mockTenant);
// **CRITICAL: Also set tenant ID in API client**
// This ensures API requests include the tenant ID header
// Set tenant ID in API client
import('../api/client').then(({ apiClient }) => {
apiClient.setTenantId(virtualTenantId);
console.log('✅ [TenantInitializer] Set API client tenant ID:', virtualTenantId);
});
// For enterprise demos, load child tenants immediately (session is already ready when we navigate here)
// For enterprise demos, load child tenants immediately
if (demoAccountType === 'enterprise') {
console.log('🔄 [TenantInitializer] Loading available tenants for enterprise demo...');
const mockUserId = 'demo-user';
import('../api/services/tenant').then(({ TenantService }) => {
const tenantService = new TenantService();
tenantService.getUserTenants(mockUserId)
.then(tenants => {
console.log('📋 [TenantInitializer] Loaded available tenants:', tenants.length);
if (tenants.length === 0) {
console.warn('⚠️ [TenantInitializer] No child tenants found yet - they may still be cloning');
}
// Update the tenant store with available tenants
import('../stores/tenant.store').then(({ useTenantStore }) => {
useTenantStore.getState().setAvailableTenants(tenants);
});
})
.catch(error => {
console.error('❌ [TenantInitializer] Failed to load available tenants:', error);
.catch(() => {
// Silently handle error
});
});
}
}
}
}, [isDemoMode, demoSessionId, demoAccountType, currentTenant, setCurrentTenant]);
};;
};