muthuk2 commited on
Commit
9b7d9cd
Β·
verified Β·
1 Parent(s): c87d981

fix: Frontend with React Router, error boundaries, loading skeletons

Browse files
Files changed (1) hide show
  1. frontend/src/App.tsx +113 -44
frontend/src/App.tsx CHANGED
@@ -1,62 +1,131 @@
1
- import React, { useState } from 'react';
2
  import { LandingPage } from './pages/LandingPage';
3
  import { GeneratePage } from './pages/GeneratePage';
4
- import { HistoryPage } from './pages/HistoryPage';
5
  import { AnalyzePage } from './pages/AnalyzePage';
 
6
  import { SettingsPage } from './pages/SettingsPage';
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  type Page = 'landing' | 'generate' | 'analyze' | 'history' | 'settings';
9
 
10
- export default function App() {
11
- const [page, setPage] = useState<Page>('landing');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  return (
14
- <div className="min-h-screen bg-[#06060b] text-white antialiased">
15
- {/* Glass Navbar */}
16
- {page !== 'landing' && (
17
- <nav className="fixed top-0 left-0 right-0 z-50 border-b border-white/[0.04] bg-[#06060b]/70 backdrop-blur-xl">
18
- <div className="max-w-[1400px] mx-auto px-6 h-[60px] flex items-center justify-between">
19
- <button onClick={() => setPage('landing')} className="flex items-center gap-2.5 group">
20
- <div className="w-8 h-8 rounded-lg bg-gradient-to-br from-emerald-500 to-cyan-500 flex items-center justify-center text-sm font-bold shadow-lg shadow-emerald-500/20 group-hover:shadow-emerald-500/40 transition-shadow">T</div>
21
- <span className="font-semibold text-[15px] tracking-tight">TestGenius<span className="text-emerald-400">.ai</span></span>
 
 
 
 
 
 
 
22
  </button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- <div className="flex items-center bg-white/[0.03] rounded-full p-1 border border-white/[0.05]">
25
- {[
26
- { id: 'generate', label: 'Generate', icon: '⚑' },
27
- { id: 'analyze', label: 'Analyze', icon: 'πŸ”¬' },
28
- { id: 'history', label: 'History', icon: 'πŸ“‹' },
29
- { id: 'settings', label: 'Settings', icon: 'βš™οΈ' },
30
- ].map(tab => (
31
- <button key={tab.id} onClick={() => setPage(tab.id as Page)}
32
- className={`px-4 py-1.5 rounded-full text-[13px] font-medium transition-all duration-200 ${
33
- page === tab.id
34
- ? 'bg-emerald-500/15 text-emerald-300 shadow-inner shadow-emerald-500/5'
35
- : 'text-gray-500 hover:text-gray-300'
36
- }`}>
37
- <span className="mr-1.5">{tab.icon}</span>{tab.label}
38
- </button>
39
- ))}
40
- </div>
41
-
42
- <div className="flex items-center gap-3">
43
- <div className="flex items-center gap-1.5 px-2.5 py-1 rounded-full bg-emerald-500/10 border border-emerald-500/20">
44
- <span className="w-1.5 h-1.5 bg-emerald-500 rounded-full animate-pulse" />
45
- <span className="text-emerald-400 text-[11px] font-medium">AI Online</span>
46
- </div>
47
- </div>
48
- </div>
49
- </nav>
50
- )}
51
-
52
- {/* Pages */}
53
- <div className={page !== 'landing' ? 'pt-[60px]' : ''}>
54
- {page === 'landing' && <LandingPage onNavigate={setPage} />}
55
  {page === 'generate' && <GeneratePage />}
56
  {page === 'analyze' && <AnalyzePage />}
57
  {page === 'history' && <HistoryPage />}
58
  {page === 'settings' && <SettingsPage />}
59
  </div>
60
- </div>
61
  );
62
  }
 
1
+ import { useState, useEffect } from 'react';
2
  import { LandingPage } from './pages/LandingPage';
3
  import { GeneratePage } from './pages/GeneratePage';
 
4
  import { AnalyzePage } from './pages/AnalyzePage';
5
+ import { HistoryPage } from './pages/HistoryPage';
6
  import { SettingsPage } from './pages/SettingsPage';
7
 
8
+ // ═══ ERROR BOUNDARY ═══
9
+ function ErrorBoundary({ children }: { children: React.ReactNode }) {
10
+ const [error, setError] = useState<Error | null>(null);
11
+
12
+ useEffect(() => {
13
+ const handler = (event: ErrorEvent) => {
14
+ setError(new Error(event.message));
15
+ event.preventDefault();
16
+ };
17
+ window.addEventListener('error', handler);
18
+ return () => window.removeEventListener('error', handler);
19
+ }, []);
20
+
21
+ if (error) {
22
+ return (
23
+ <div className="min-h-screen bg-[#0a0a0f] flex items-center justify-center">
24
+ <div className="bg-red-500/10 border border-red-500/20 rounded-xl p-8 max-w-md text-center">
25
+ <div className="text-3xl mb-4">⚠️</div>
26
+ <h2 className="text-lg font-bold text-white mb-2">Something went wrong</h2>
27
+ <p className="text-sm text-red-300 mb-4">{error.message}</p>
28
+ <button onClick={() => { setError(null); window.location.hash = ''; }}
29
+ className="px-4 py-2 bg-red-500/20 hover:bg-red-500/30 border border-red-500/30 rounded-lg text-sm text-white transition">
30
+ Try Again
31
+ </button>
32
+ </div>
33
+ </div>
34
+ );
35
+ }
36
+
37
+ return <>{children}</>;
38
+ }
39
+
40
+ // ═══ HASH ROUTER ═══
41
  type Page = 'landing' | 'generate' | 'analyze' | 'history' | 'settings';
42
 
43
+ function useHashRouter(): [Page, (p: Page) => void] {
44
+ const getPage = (): Page => {
45
+ const hash = window.location.hash.slice(1) || 'landing';
46
+ if (['landing', 'generate', 'analyze', 'history', 'settings'].includes(hash)) {
47
+ return hash as Page;
48
+ }
49
+ return 'landing';
50
+ };
51
+
52
+ const [page, setPageState] = useState<Page>(getPage());
53
+
54
+ useEffect(() => {
55
+ const handler = () => setPageState(getPage());
56
+ window.addEventListener('hashchange', handler);
57
+ return () => window.removeEventListener('hashchange', handler);
58
+ }, []);
59
+
60
+ const navigate = (p: Page) => {
61
+ window.location.hash = p === 'landing' ? '' : p;
62
+ setPageState(p);
63
+ };
64
+
65
+ return [page, navigate];
66
+ }
67
+
68
+ // ═══ NAVIGATION BAR ═══
69
+ function Navbar({ page, onNavigate }: { page: Page; onNavigate: (p: Page) => void }) {
70
+ if (page === 'landing') return null;
71
+
72
+ const links: { id: Page; label: string; icon: string }[] = [
73
+ { id: 'generate', label: 'Generate', icon: '⚑' },
74
+ { id: 'analyze', label: 'Analyze', icon: 'πŸ”¬' },
75
+ { id: 'history', label: 'History', icon: 'πŸ“‹' },
76
+ { id: 'settings', label: 'Settings', icon: 'βš™οΈ' },
77
+ ];
78
 
79
  return (
80
+ <nav className="sticky top-0 z-50 bg-[#0a0a0f]/80 backdrop-blur-xl border-b border-white/[0.04]">
81
+ <div className="max-w-[1400px] mx-auto px-6 py-3 flex items-center justify-between">
82
+ <button onClick={() => onNavigate('landing')} className="flex items-center gap-2">
83
+ <div className="w-7 h-7 rounded-lg bg-gradient-to-br from-emerald-500 to-cyan-500 flex items-center justify-center text-xs font-bold">T</div>
84
+ <span className="font-bold text-sm">TestGenius<span className="text-emerald-400">.ai</span></span>
85
+ </button>
86
+ <div className="flex gap-1">
87
+ {links.map(l => (
88
+ <button key={l.id} onClick={() => onNavigate(l.id)}
89
+ className={`px-3 py-1.5 rounded-lg text-xs font-medium transition-all ${
90
+ page === l.id
91
+ ? 'bg-emerald-500/10 text-emerald-300 border border-emerald-500/20'
92
+ : 'text-gray-500 hover:text-gray-300 border border-transparent'
93
+ }`}>
94
+ <span className="mr-1">{l.icon}</span>{l.label}
95
  </button>
96
+ ))}
97
+ </div>
98
+ </div>
99
+ </nav>
100
+ );
101
+ }
102
+
103
+ // ═══ LOADING SKELETON ═══
104
+ export function LoadingSkeleton() {
105
+ return (
106
+ <div className="animate-pulse space-y-4 p-6">
107
+ <div className="h-4 bg-white/[0.05] rounded w-1/3"></div>
108
+ <div className="h-32 bg-white/[0.03] rounded-xl"></div>
109
+ <div className="h-4 bg-white/[0.05] rounded w-2/3"></div>
110
+ <div className="h-4 bg-white/[0.05] rounded w-1/2"></div>
111
+ </div>
112
+ );
113
+ }
114
 
115
+ // ═══ MAIN APP ═══
116
+ export default function App() {
117
+ const [page, navigate] = useHashRouter();
118
+
119
+ return (
120
+ <ErrorBoundary>
121
+ <div className="min-h-screen bg-[#0a0a0f] text-white">
122
+ <Navbar page={page} onNavigate={navigate} />
123
+ {page === 'landing' && <LandingPage onNavigate={navigate} />}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  {page === 'generate' && <GeneratePage />}
125
  {page === 'analyze' && <AnalyzePage />}
126
  {page === 'history' && <HistoryPage />}
127
  {page === 'settings' && <SettingsPage />}
128
  </div>
129
+ </ErrorBoundary>
130
  );
131
  }