-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLayout.tsx
More file actions
149 lines (131 loc) · 3.55 KB
/
Layout.tsx
File metadata and controls
149 lines (131 loc) · 3.55 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { useState } from "react";
import { Loader2 } from "lucide-react";
import { useNavigate } from "react-router-dom";
import { checkCredentials } from "@/lib/auth";
import Header from "@/components/Header";
import Sidebar from "@/components/Sidebar";
import { Card } from "@/components/ui/card";
import {
useSidebarExpanded,
setSidebarExpanded,
useSidebarWidth,
setSidebarWidth,
} from "@/lib/store";
interface LayoutProps {
activeTab?: string;
headerTitle?: string;
children: React.ReactNode;
}
const Layout: React.FC<LayoutProps> = ({
activeTab,
headerTitle,
children,
}) => {
const navigate = useNavigate();
const [loading, setLoading] = React.useState(true);
const isSidebarExpanded = useSidebarExpanded();
const sidebarWidth = useSidebarWidth();
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const [scrollY, setScrollY] = useState(0);
const handleResize = () => {
const width = window.innerWidth;
if (width < 768) {
collapseSidebar();
} else {
expandSidebar();
}
setWindowWidth(width);
};
const scrollHandler = () => {
setScrollY(window.scrollY);
};
React.useEffect(() => {
init();
window.addEventListener("scroll", scrollHandler);
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("scroll", scrollHandler);
window.removeEventListener("resize", handleResize);
};
}, []);
const init = async () => {
const currentRoute = window.location.pathname + window.location.search;
const status = await checkCredentials();
if (status != 0) {
if (currentRoute == "/") {
navigate(`/auth/login`);
} else {
navigate(`/auth/login?route=${encodeURIComponent(currentRoute)}`);
}
} else {
setLoading(false);
}
};
const expandSidebar = () => {
if (windowWidth < 768) {
return;
}
setSidebarExpanded(true);
setSidebarWidth(275);
};
const collapseSidebar = () => {
setSidebarExpanded(false);
setSidebarWidth(90);
};
const toggleSidebar = () => {
if (isSidebarExpanded) {
collapseSidebar();
} else {
expandSidebar();
}
};
const LoadingComponent = () => {
return (
<div className="flex h-screen w-full items-center justify-center">
<Card className="border-none p-8" style={{ width: 500 }}>
<div className="flex flex-col items-center justify-center">
<img
src="/logo/mapache.png"
alt="Mapache"
className="mx-auto h-14 animate-scale"
/>
<Loader2 className="mt-8 h-12 w-12 animate-spin text-primary" />
</div>
</Card>
</div>
);
};
return (
<>
{loading ? (
<LoadingComponent />
) : (
<div className="flex">
<Sidebar
isSidebarExpanded={isSidebarExpanded}
selectedPage={activeTab}
sidebarWidth={sidebarWidth}
toggleSidebar={toggleSidebar}
/>
<div className="w-full">
<Header
scroll={scrollY}
headerTitle={headerTitle}
style={{
left: sidebarWidth,
width: windowWidth - sidebarWidth,
}}
/>
<div
className="mt-14 overflow-auto p-8 transition-all duration-200"
style={{ marginLeft: sidebarWidth }}
>
{children}
</div>
</div>
</div>
)}
</>
);
};
export default Layout;