File size: 2,652 Bytes
901982e
 
 
66269b4
901982e
 
 
 
66269b4
901982e
 
 
 
 
 
66269b4
 
 
 
 
 
901982e
 
66269b4
 
 
 
 
eb6f7c1
 
 
 
 
 
 
66269b4
 
 
 
 
901982e
 
 
 
 
 
 
66269b4
 
 
 
 
 
 
ce3fd2a
 
 
 
66269b4
b7f16d3
ae34846
66269b4
 
 
 
 
 
 
 
 
 
 
 
 
901982e
66269b4
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
import { renderLandingView, setupLandingEvents } from './views/LandingView.js';
import { renderInstructorView, setupInstructorEvents } from './views/InstructorView.js';
import { renderStudentView, setupStudentEvents } from './views/StudentView.js';
import { renderAdminView, setupAdminEvents } from './views/AdminView.js';

const app = document.querySelector('#app');

function navigateTo(view) {
    // Update hash maybe? For now simple switch
    switch (view) {
        case 'landing':
            app.innerHTML = renderLandingView();
            setupLandingEvents(navigateTo);
            break;
        case 'instructor':
            app.innerHTML = '載入中...';
            // Async render because Instructor view fetches challenges for column headers
            renderInstructorView().then(html => {
                app.innerHTML = html;
                setupInstructorEvents();
            });
            break;
        case 'student':
            app.innerHTML = '載入中...';
            // Async render because Student view fetches challenges
            renderStudentView().then(html => {
                app.innerHTML = html;
                setupStudentEvents();
            }).catch(err => {
                console.error("Student View Load Error:", err);
                app.innerHTML = `<div class="p-10 text-center text-red-500">

                    <h2 class="text-xl font-bold mb-2">載入失敗</h2>

                    <p class="mb-4">${err.message}</p>

                    <button onclick="window.location.reload()" class="bg-gray-700 text-white px-4 py-2 rounded">重新整理</button>

                </div>`;
            });
            break;
        case 'admin':
            app.innerHTML = renderAdminView();
            setupAdminEvents();
            break;
        default:
            app.innerHTML = renderLandingView();
            setupLandingEvents(navigateTo);
    }
}

// Route Handler
function handleRoute() {
    const hash = window.location.hash.slice(1);
    if (hash === 'admin') {
        navigateTo('admin');
        return;
    }
    if (hash === 'instructor') {
        navigateTo('instructor');
        return;
    }



    const roomCode = localStorage.getItem('vibecoding_room_code');
    const userId = localStorage.getItem('vibecoding_user_id'); // Changed key to match new logic

    if (roomCode && userId && !hash) {
        navigateTo('student');
    } else {
        navigateTo('landing');
    }
}

// Listen to hash changes
window.addEventListener('hashchange', handleRoute);

// Initial Load
handleRoute();