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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
'use client';
import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { Button } from '@/components/ui/button';
import { authApi, userApi } from '@/lib/api';
import { ThemeToggle } from '@/components/shared/ThemeToggle';
import { PageTransition } from '@/components/shared/PageTransition';
import {
ChevronLeftIcon,
ChevronRightIcon,
LayoutDashboardIcon,
CoinsIcon,
TargetIcon,
SettingsIcon,
LogOutIcon,
MenuIcon
} from 'lucide-react';
import { usePathname } from 'next/navigation';
export default function MainLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const router = useRouter();
const pathname = usePathname();
const [userName, setUserName] = useState<string>('');
const [isLoading, setIsLoading] = useState(true);
const [isSidebarCollapsed, setIsSidebarCollapsed] = useState(false);
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
useEffect(() => {
// Check if user is authenticated
const token = localStorage.getItem('token');
if (!token) {
router.push('/login');
return;
}
// Fetch user profile
userApi.getProfile()
.then(data => {
setUserName(data.user.Name);
setIsLoading(false);
})
.catch(() => {
// If error fetching profile, redirect to login
router.push('/login');
});
// Load sidebar state from localStorage
const savedSidebarState = localStorage.getItem('sidebarCollapsed');
if (savedSidebarState !== null) {
setIsSidebarCollapsed(savedSidebarState === 'true');
}
}, [router]);
// Close mobile menu when changing routes
useEffect(() => {
setIsMobileMenuOpen(false);
}, [pathname]);
const handleLogout = () => {
authApi.logout();
};
const toggleSidebar = () => {
const newState = !isSidebarCollapsed;
setIsSidebarCollapsed(newState);
localStorage.setItem('sidebarCollapsed', newState.toString());
};
const toggleMobileMenu = () => {
setIsMobileMenuOpen(!isMobileMenuOpen);
};
if (isLoading) {
return (
<div className="flex h-screen items-center justify-center bg-background">
<div className="flex flex-col items-center space-y-4">
<div className="relative h-12 w-12">
<div className="absolute top-0 h-full w-full rounded-full border-4 border-t-primary border-r-transparent border-b-transparent border-l-transparent animate-spin"></div>
</div>
<p className="text-sm font-medium text-muted-foreground animate-pulse">Loading...</p>
</div>
</div>
);
}
return (
<div className="min-h-screen bg-background flex flex-col">
{/* Top Navigation */}
<header className="border-b shadow-sm backdrop-blur-sm bg-background/90 sticky top-0 z-50 w-full">
<div className="w-full flex h-16 items-center px-4 md:px-6">
<div className="flex items-center gap-4">
{/* Mobile menu button - only visible on small screens */}
<button
className="md:hidden rounded-full p-2 hover:bg-muted transition-colors"
onClick={toggleMobileMenu}
aria-label="Toggle mobile menu"
>
<MenuIcon size={20} />
</button>
<div className="font-semibold text-xl">
<span className="bg-clip-text text-transparent bg-gradient-to-r from-primary to-primary/70">
Finance Management
</span>
</div>
</div>
<div className="flex items-center ms-auto gap-4">
<div className="hidden md:flex items-center gap-1 bg-muted/50 px-3 py-1.5 rounded-full">
<span className="relative flex h-2 w-2">
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-primary/50 opacity-75"></span>
<span className="relative inline-flex rounded-full h-2 w-2 bg-primary"></span>
</span>
<span className='text-sm font-medium ml-1.5 transition-all duration-300'>
{userName}
</span>
</div>
<ThemeToggle />
<Button
variant="ghost"
size="icon"
onClick={handleLogout}
className="hover:bg-destructive/10 transition-colors duration-300"
aria-label="Logout"
>
<LogOutIcon size={18} className="text-destructive/90 transition-transform hover:scale-110 duration-300" />
</Button>
</div>
</div>
</header>
{/* Mobile Menu - outside the normal flow and only visible when toggled */}
<div
className={`
md:hidden fixed inset-0 z-40 bg-background/95 backdrop-blur-sm transition-all duration-300
${isMobileMenuOpen ? 'opacity-100 pointer-events-auto' : 'opacity-0 pointer-events-none'}
`}
>
<div className="pt-20 px-4">
<nav className="space-y-4">
<Link
href="/dashboard"
className={`
flex items-center p-3 rounded-lg transition-colors
${pathname === '/dashboard' ? 'bg-primary/10 text-primary' : 'hover:bg-muted'}
`}
>
<LayoutDashboardIcon size={20} className="mr-3" />
Dashboard
</Link>
<Link
href="/loans"
className={`
flex items-center p-3 rounded-lg transition-colors
${pathname === '/loans' ? 'bg-primary/10 text-primary' : 'hover:bg-muted'}
`}
>
<CoinsIcon size={20} className="mr-3" />
Loans
</Link>
<Link
href="/goals"
className={`
flex items-center p-3 rounded-lg transition-colors
${pathname === '/goals' ? 'bg-primary/10 text-primary' : 'hover:bg-muted'}
`}
>
<TargetIcon size={20} className="mr-3" />
Goals
</Link>
<Link
href="/settings"
className={`
flex items-center p-3 rounded-lg transition-colors
${pathname === '/settings' ? 'bg-primary/10 text-primary' : 'hover:bg-muted'}
`}
>
<SettingsIcon size={20} className="mr-3" />
Settings
</Link>
</nav>
<div className="mt-8 border-t pt-4">
<Button
variant="outline"
className="w-full justify-start text-destructive hover:text-destructive hover:bg-destructive/10"
onClick={handleLogout}
>
<LogOutIcon size={18} className="mr-2" />
Logout
</Button>
</div>
</div>
</div>
{/* Main Content */}
<div className="flex flex-1 overflow-hidden">
{/* Sidebar - hidden on mobile */}
<aside
className={`
hidden md:flex flex-col h-[calc(100vh-4rem)] border-r bg-background flex-shrink-0 relative
transition-all duration-300 ease-in-out
${isSidebarCollapsed ? 'w-16' : 'w-64'}
`}
>
{/* Sidebar Toggle Button */}
<button
onClick={toggleSidebar}
className="absolute -right-3 top-10 bg-primary text-primary-foreground rounded-full p-1 shadow-md hover:bg-primary/90 transition-colors duration-300 hover:scale-110 group z-10"
aria-label={isSidebarCollapsed ? "Expand sidebar" : "Collapse sidebar"}
>
<div className="transition-transform duration-300 group-hover:rotate-[360deg]">
{isSidebarCollapsed ? <ChevronRightIcon size={16} /> : <ChevronLeftIcon size={16} />}
</div>
</button>
<nav className="space-y-2 px-2 mt-4 flex-1">
<Link
href="/dashboard"
className={`
flex items-center p-2 rounded-md transition-all duration-200 overflow-hidden
${pathname === '/dashboard'
? 'bg-primary/10 text-primary font-medium'
: 'hover:bg-muted text-foreground/80 hover:text-foreground'
}
${isSidebarCollapsed ? 'justify-center' : 'justify-start'}
`}
title="Dashboard"
>
<LayoutDashboardIcon size={18} className={`transition-transform duration-300 ${pathname === '/dashboard' ? 'scale-110' : ''}`} />
<span className={`ml-2 transition-all duration-300 overflow-hidden whitespace-nowrap ${isSidebarCollapsed ? 'w-0 opacity-0' : 'w-auto opacity-100'}`}>
Dashboard
</span>
</Link>
<Link
href="/loans"
className={`
flex items-center p-2 rounded-md transition-all duration-200 overflow-hidden
${pathname === '/loans'
? 'bg-primary/10 text-primary font-medium'
: 'hover:bg-muted text-foreground/80 hover:text-foreground'
}
${isSidebarCollapsed ? 'justify-center' : 'justify-start'}
`}
title="Loans"
>
<CoinsIcon size={18} className={`transition-transform duration-300 ${pathname === '/loans' ? 'scale-110' : ''}`} />
<span className={`ml-2 transition-all duration-300 overflow-hidden whitespace-nowrap ${isSidebarCollapsed ? 'w-0 opacity-0' : 'w-auto opacity-100'}`}>
Loans
</span>
</Link>
<Link
href="/goals"
className={`
flex items-center p-2 rounded-md transition-all duration-200 overflow-hidden
${pathname === '/goals'
? 'bg-primary/10 text-primary font-medium'
: 'hover:bg-muted text-foreground/80 hover:text-foreground'
}
${isSidebarCollapsed ? 'justify-center' : 'justify-start'}
`}
title="Goals"
>
<TargetIcon size={18} className={`transition-transform duration-300 ${pathname === '/goals' ? 'scale-110' : ''}`} />
<span className={`ml-2 transition-all duration-300 overflow-hidden whitespace-nowrap ${isSidebarCollapsed ? 'w-0 opacity-0' : 'w-auto opacity-100'}`}>
Goals
</span>
</Link>
<Link
href="/settings"
className={`
flex items-center p-2 rounded-md transition-all duration-200 overflow-hidden
${pathname === '/settings'
? 'bg-primary/10 text-primary font-medium'
: 'hover:bg-muted text-foreground/80 hover:text-foreground'
}
${isSidebarCollapsed ? 'justify-center' : 'justify-start'}
`}
title="Settings"
>
<SettingsIcon size={18} className={`transition-transform duration-300 ${pathname === '/settings' ? 'scale-110' : ''}`} />
<span className={`ml-2 transition-all duration-300 overflow-hidden whitespace-nowrap ${isSidebarCollapsed ? 'w-0 opacity-0' : 'w-auto opacity-100'}`}>
Settings
</span>
</Link>
</nav>
</aside>
{/* Page Content */}
<main className="flex-1 p-4 md:p-8 overflow-auto h-[calc(100vh-4rem)] transition-all duration-300">
<PageTransition>
{children}
</PageTransition>
</main>
</div>
</div>
);
}
|