forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.tsx
More file actions
123 lines (108 loc) · 3.45 KB
/
Copy pathlayout.tsx
File metadata and controls
123 lines (108 loc) · 3.45 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use client';
import React, { useEffect, useRef } from 'react';
import { useAuth } from '@/components/auth-provider';
import { useRouter, usePathname } from 'next/navigation';
import { DashboardSidebar } from "@/components/dashboard/sidebar";
import { DashboardHeader } from "@/components/dashboard/header";
// Simple loading component (Consider moving to a shared UI folder)
const LoadingScreen = () => (
<div className="flex items-center justify-center min-h-screen">
<div>Loading...</div>
</div>
);
const SCROLL_STORAGE_PREFIX = 'omi-admin-scroll:';
function useScrollRestoration(
mainRef: React.RefObject<HTMLElement>,
enabled: boolean,
) {
const pathname = usePathname();
const storageKey = `${SCROLL_STORAGE_PREFIX}${pathname ?? '/'}`;
// Restore scroll position on mount / route change. We poll briefly
// because the page content mounts and grows as SWR data arrives, so
// the target scrollTop may not be reachable on the first frame.
useEffect(() => {
if (!enabled) return;
const el = mainRef.current;
if (!el) return;
let saved: number | null = null;
try {
const raw = window.sessionStorage.getItem(storageKey);
if (raw != null) saved = parseInt(raw, 10);
} catch {
/* noop */
}
if (saved == null || Number.isNaN(saved)) return;
let tries = 0;
const maxTries = 40; // ~2s at 50ms cadence
const tick = () => {
const node = mainRef.current;
if (!node) return;
node.scrollTop = saved!;
if (Math.abs(node.scrollTop - saved!) > 2 && tries++ < maxTries) {
setTimeout(tick, 50);
}
};
tick();
}, [enabled, mainRef, storageKey]);
// Save scroll position as the user scrolls. Throttled via rAF.
useEffect(() => {
if (!enabled) return;
const el = mainRef.current;
if (!el) return;
let pending = false;
const onScroll = () => {
if (pending) return;
pending = true;
requestAnimationFrame(() => {
pending = false;
try {
window.sessionStorage.setItem(storageKey, String(el.scrollTop));
} catch {
/* noop */
}
});
};
el.addEventListener('scroll', onScroll, { passive: true });
return () => el.removeEventListener('scroll', onScroll);
}, [enabled, mainRef, storageKey]);
}
export default function DashboardLayout({
children,
}: {
children: React.ReactNode;
}) {
const { user, isAdmin, loading } = useAuth();
const router = useRouter();
const mainRef = useRef<HTMLElement>(null);
const authenticated = !!(user && isAdmin && !loading);
useScrollRestoration(mainRef, authenticated);
useEffect(() => {
if (loading) {
return; // Wait until loading is complete
}
if (!user || !isAdmin) {
// Redirect non-admins or non-logged-in users to login
router.push('/login?error=unauthorized');
}
}, [user, isAdmin, loading, router]);
// Show loading screen while checking auth
if (loading) {
return <LoadingScreen />;
}
// If user is authenticated and is an admin, render the dashboard layout
if (user && isAdmin) {
return (
<div className="min-h-screen flex bg-background">
<DashboardSidebar />
<div className="flex-1 flex flex-col min-h-screen">
<DashboardHeader />
<main ref={mainRef} className="flex-1 p-4 md:p-6 overflow-y-auto">
{children}
</main>
</div>
</div>
);
}
// Fallback while redirecting
return <LoadingScreen />;
}