-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
219 lines (196 loc) · 6.82 KB
/
App.tsx
File metadata and controls
219 lines (196 loc) · 6.82 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
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
import React, { Suspense, lazy, useEffect, useState } from 'react';
import { MemoryRouter as Router, Routes, Route, useLocation, useNavigate } from 'react-router-dom';
import { AnimatePresence } from 'framer-motion';
import { ToastProvider, useToast } from './context/ToastContext';
import { Navbar } from './components/Navbar';
import { Footer } from './components/Footer';
import { Cursor } from './components/ui/Cursor';
import { TransitionLayout } from './components/TransitionLayout';
import { ShortcutsModal } from './components/ShortcutsModal';
import { ScrollProgress } from './components/ScrollProgress';
import { CommandPalette } from './components/CommandPalette';
import { NoiseOverlay } from './components/ui/NoiseOverlay';
import { useKonamiCode } from './hooks/useKonamiCode';
// Lazy loading pages for performance
const Home = lazy(() => import('./pages/Home'));
const About = lazy(() => import('./pages/About'));
const Projects = lazy(() => import('./pages/Projects'));
const Contact = lazy(() => import('./pages/Contact'));
const NotFound = lazy(() => import('./pages/NotFound'));
const PageLoader: React.FC = () => (
<div className="flex items-center justify-center h-[50vh]">
<div className="animate-spin h-16 w-16 border-8 border-neo-black border-t-neo-pink rounded-full"></div>
</div>
);
// Define interfaces for ErrorBoundary
interface ErrorBoundaryProps {
children?: React.ReactNode;
}
interface ErrorBoundaryState {
hasError: boolean;
}
/**
* ErrorBoundary component to catch errors in child components during rendering.
* Explicitly using React.Component to ensure props and state inheritance is correctly typed.
*/
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
// Initialize state using a class field
public state: ErrorBoundaryState = { hasError: false };
static getDerivedStateFromError(_error: any): ErrorBoundaryState {
return { hasError: true };
}
componentDidCatch(error: any, errorInfo: any) {
console.error("Uncaught error:", error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<div className="min-h-[50vh] flex flex-col items-center justify-center text-center p-8">
<h2 className="text-2xl font-bold mb-4 dark:text-white">Something went wrong.</h2>
<button
onClick={() => window.location.reload()}
className="px-6 py-2 bg-neo-yellow border-4 border-neo-black font-bold uppercase shadow-neo hover:shadow-none hover:translate-x-[2px] hover:translate-y-[2px]"
>
Reload Page
</button>
</div>
);
}
// @ts-ignore
return this.props.children;
}
}
const GlobalLogic = () => {
const { pathname } = useLocation();
const navigate = useNavigate();
const [showShortcuts, setShowShortcuts] = useState(false);
const konamiTriggered = useKonamiCode();
const { showToast } = useToast();
// Scroll to top
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
// Dynamic Tab Title
useEffect(() => {
const handleVisibilityChange = () => {
if (document.hidden) {
document.title = "Anurup R Krishnan | Portfolio";
} else {
document.title = "Anurup R Krishnan | Portfolio";
}
};
document.addEventListener("visibilitychange", handleVisibilityChange);
return () => {
document.removeEventListener("visibilitychange", handleVisibilityChange);
};
}, []);
// Keyboard Shortcuts
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (['INPUT', 'TEXTAREA'].includes((e.target as HTMLElement).tagName)) return;
switch (e.key.toLowerCase()) {
case 'h': navigate('/'); break;
case 'a': navigate('/about'); break;
case 'p': navigate('/projects'); break;
case 'c': navigate('/contact'); break;
case '?': setShowShortcuts(prev => !prev); break;
case 'escape': setShowShortcuts(false); break;
}
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [navigate]);
// Konami Code Effect
useEffect(() => {
if (konamiTriggered) {
document.body.style.transform = "rotate(180deg)";
document.body.style.transition = "transform 1s ease";
showToast("🦄 GOD MODE ENABLED", "success");
setTimeout(() => {
document.body.style.transform = "";
showToast("Normalcy restored...", "info");
}, 5000);
}
}, [konamiTriggered, showToast]);
return (
<AnimatePresence>
{showShortcuts && <ShortcutsModal onClose={() => setShowShortcuts(false)} />}
</AnimatePresence>
);
};
const AnimatedRoutes: React.FC = () => {
const location = useLocation();
return (
<AnimatePresence mode="wait">
{/* @ts-ignore - Routes supports key for Framer Motion, but types don't reflect it */}
<Routes location={location} key={location.pathname}>
<Route path="/" element={
<TransitionLayout>
<ErrorBoundary>
<Suspense fallback={<PageLoader />}>
<Home />
</Suspense>
</ErrorBoundary>
</TransitionLayout>
} />
<Route path="/about" element={
<TransitionLayout>
<ErrorBoundary>
<Suspense fallback={<PageLoader />}>
<About />
</Suspense>
</ErrorBoundary>
</TransitionLayout>
} />
<Route path="/projects" element={
<TransitionLayout>
<ErrorBoundary>
<Suspense fallback={<PageLoader />}>
<Projects />
</Suspense>
</ErrorBoundary>
</TransitionLayout>
} />
<Route path="/contact" element={
<TransitionLayout>
<ErrorBoundary>
<Suspense fallback={<PageLoader />}>
<Contact />
</Suspense>
</ErrorBoundary>
</TransitionLayout>
} />
<Route path="*" element={
<TransitionLayout>
<ErrorBoundary>
<Suspense fallback={<PageLoader />}>
<NotFound />
</Suspense>
</ErrorBoundary>
</TransitionLayout>
} />
</Routes>
</AnimatePresence>
);
};
const App: React.FC = () => {
return (
<ToastProvider>
<Router>
<NoiseOverlay />
<ScrollProgress />
<CommandPalette />
<Cursor />
<GlobalLogic />
<div className="min-h-screen flex flex-col bg-transparent text-neo-black dark:text-white font-sans transition-colors">
<Navbar />
<main className="flex-grow pt-20 px-4 md:px-8 container mx-auto max-w-6xl">
<AnimatedRoutes />
</main>
<Footer />
</div>
</Router>
</ToastProvider>
);
};
export default App;