Lashtw/Vibecodingex
0
1import { renderLandingView, setupLandingEvents } from './views/LandingView.js';
2import { renderInstructorView, setupInstructorEvents } from './views/InstructorView.js';
3import { renderStudentView, setupStudentEvents } from './views/StudentView.js';
4import { renderAdminView, setupAdminEvents } from './views/AdminView.js';
5
6const app = document.querySelector('#app');
7
8function navigateTo(view) {
9 // Update hash maybe? For now simple switch
10 switch (view) {
11 case 'landing':
12 app.innerHTML = renderLandingView();
13 setupLandingEvents(navigateTo);
14 break;
15 case 'instructor':
16 app.innerHTML = '載入中...';
17 // Async render because Instructor view fetches challenges for column headers
18 renderInstructorView().then(html => {
19 app.innerHTML = html;
20 setupInstructorEvents();
21 });
22 break;
23 case 'student':
24 app.innerHTML = '載入中...';
25 // Async render because Student view fetches challenges
26 renderStudentView().then(html => {
27 app.innerHTML = html;
28 setupStudentEvents();
29 }).catch(err => {
30 console.error("Student View Load Error:", err);
31 app.innerHTML = `<div class="p-10 text-center text-red-500">
32 <h2 class="text-xl font-bold mb-2">載入失敗</h2>
33 <p class="mb-4">${err.message}</p>
34 <button onclick="window.location.reload()" class="bg-gray-700 text-white px-4 py-2 rounded">重新整理</button>
35 </div>`;
36 });
37 break;
38 case 'admin':
39 app.innerHTML = renderAdminView();
40 setupAdminEvents();
41 break;
42 default:
43 app.innerHTML = renderLandingView();
44 setupLandingEvents(navigateTo);
45 }
46}
47
48// Route Handler
49function handleRoute() {
50 const hash = window.location.hash.slice(1);
51 if (hash === 'admin') {
52 navigateTo('admin');
53 return;
54 }
55 if (hash === 'instructor') {
56 navigateTo('instructor');
57 return;
58 }
59
60
61
62 const roomCode = localStorage.getItem('vibecoding_room_code');
63 const userId = localStorage.getItem('vibecoding_user_id'); // Changed key to match new logic
64
65 if (roomCode && userId && !hash) {
66 navigateTo('student');
67 } else {
68 navigateTo('landing');
69 }
70}
71
72// Listen to hash changes
73window.addEventListener('hashchange', handleRoute);
74
75// Initial Load
76handleRoute();
77 