File size: 3,075 Bytes
b2baa40
2f6fc2d
b2baa40
13c7b45
2f6fc2d
 
3ac6545
2f6fc2d
 
3ac6545
2f6fc2d
 
 
 
3ac6545
2f6fc2d
3ac6545
2f6fc2d
3ac6545
2f6fc2d
 
3ac6545
 
2f6fc2d
3ac6545
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f6fc2d
 
 
 
 
3ac6545
 
2f6fc2d
3ac6545
 
2f6fc2d
 
 
3ac6545
 
 
2f6fc2d
 
b2baa40
 
 
2f6fc2d
3ac6545
b2baa40
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// main.js - Application Entry Point
import { loadInitialAppState, appState } from './state.js';
import { refreshUI } from './ui.js';
import { attachOneTimeListeners } from './events.js';
import { supabase } from './supabaseClient.js';

// UI Elements
const loginScreen = document.getElementById('login-screen');
const mainContent = document.getElementById('main-content');
const loader = document.getElementById('loader');
const userInfo = document.getElementById('user-info');
const userEmailSpan = document.getElementById('user-email');
const logoutBtn = document.getElementById('logout-btn');
const reportsBtn = document.getElementById('show-reports-modal-btn');
const footer = document.getElementById('footer');

// --- MODIFICATION: Robust Auth State Change Handler ---
supabase.auth.onAuthStateChange(async (event, session) => {
    // This handles both manual sign-in and automatic session restoration
    if (event === 'SIGNED_IN' || (event === 'INITIAL_SESSION' && session)) {
        appState.user = session.user;

        // 1. Show loader, hide login screen
        loginScreen.classList.add('hidden');
        loader.classList.remove('hidden');
        loader.classList.add('flex'); // Use flex to center content

        // 2. Load all data from the database
        const loadedSuccessfully = await loadInitialAppState();
        
        // 3. Once data is loaded, update the UI
        if (loadedSuccessfully) {
            refreshUI();
            
            // 4. Show the main content and hide the loader
            mainContent.classList.remove('hidden');
            mainContent.classList.remove('opacity-0');
            footer.classList.remove('hidden');
            userInfo.classList.remove('hidden');
            logoutBtn.classList.remove('hidden');
            reportsBtn.classList.remove('hidden');
            userEmailSpan.textContent = session.user.email;
        } else {
            // Handle case where data loading fails
            // Maybe show an error and a retry button, or force logout
            alert('Failed to load factory data. Please try again.');
            await supabase.auth.signOut();
        }
        
        loader.classList.add('hidden');
        loader.classList.remove('flex');

    } else if (event === 'SIGNED_OUT') {
        appState.user = null;
        appState.materials = [];
        appState.productionLog = [];
        
        // Ensure a clean UI state on logout
        mainContent.classList.add('hidden');
        mainContent.classList.add('opacity-0');
        footer.classList.add('hidden');
        userInfo.classList.add('hidden');
        logoutBtn.classList.add('hidden');
        reportsBtn.classList.add('hidden');
        loader.classList.add('hidden');
        loader.classList.remove('flex');
        loginScreen.classList.remove('hidden');
    }
});

// Initializes the application when the page content is loaded.
function init() {
    attachOneTimeListeners();
    // The opacity transition is now handled by the auth listener
}

document.addEventListener('DOMContentLoaded', init);