-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
76 lines (61 loc) · 2.15 KB
/
Copy pathrouter.js
File metadata and controls
76 lines (61 loc) · 2.15 KB
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
import { getCurrentUser } from './state/auth.js';
import { HomeView } from './views/Home.js';
import { LoginView } from './views/Login.js';
import { SignupView } from './views/Signup.js';
import { DashboardView } from './views/Dashboard.js';
import { ProfileView } from './views/Profile.js';
import { ChatView } from './views/Chat.js';
const routes = [
{ path: /^#\/?$/, view: HomeView, guard: null },
{ path: /^#\/login\/?$/, view: LoginView, guard: 'guest' },
{ path: /^#\/signup\/?$/, view: SignupView, guard: 'guest' },
{ path: /^#\/dashboard\/?$/, view: DashboardView, guard: 'auth' },
{ path: /^#\/profile\/?$/, view: ProfileView, guard: 'auth' },
{ path: /^#\/chat\/(.+)\/?$/, view: ChatView, guard: 'auth' },
];
export function navigateTo(hash) {
window.location.hash = hash;
}
function resolveRoute() {
const hash = window.location.hash || '#/';
for (const route of routes) {
const match = hash.match(route.path);
if (match) {
return { route, params: match.slice(1) };
}
}
return { route: routes[0], params: [] };
}
export async function initRouter() {
await renderCurrentRoute();
window.addEventListener('hashchange', renderCurrentRoute);
}
async function renderCurrentRoute() {
const { route, params } = resolveRoute();
const user = getCurrentUser();
if (route.guard === 'auth' && !user) {
navigateTo('#/login');
return;
}
if (route.guard === 'guest' && user) {
navigateTo('#/dashboard');
return;
}
// Page transition effect
const transitionEl = document.getElementById('page-transition');
transitionEl.classList.add('active');
await new Promise(resolve => setTimeout(resolve, 150));
const viewEl = document.getElementById('view');
viewEl.className = 'flex-1 container mx-auto px-4 sm:px-6 lg:px-8 pt-20 pb-24';
viewEl.innerHTML = await route.view.render({ params });
// Add entrance animation
viewEl.classList.add('animate-slide-up');
if (route.view.afterRender) {
await route.view.afterRender({ params });
}
// Remove transition overlay
setTimeout(() => {
transitionEl.classList.remove('active');
viewEl.classList.remove('animate-slide-up');
}, 300);
}