File size: 10,826 Bytes
25f22bf |
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 304 305 306 307 308 309 310 311 |
import React, { useEffect, useState, lazy, Suspense } from 'react';
import { Routes, Route, useLocation } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import { getCurrentUser, checkCachedAuth, autoLogin } from './store/reducers/authSlice';
import cookieService from './services/cookieService';
import Login from './pages/Login.jsx';
import Register from './pages/Register.jsx';
import Dashboard from './pages/Dashboard.jsx';
import Sources from './pages/Sources.jsx';
import Accounts from './pages/Accounts.jsx';
import Posts from './pages/Posts.jsx';
import Schedule from './pages/Schedule.jsx';
import Home from './pages/Home.jsx';
import Header from './components/Header/Header.jsx';
import Sidebar from './components/Sidebar/Sidebar.jsx';
import LinkedInCallbackHandler from './components/LinkedInAccount/LinkedInCallbackHandler.jsx';
import './css/main.css';
// Lazy load components for better mobile performance
const LazyFeatureCard = lazy(() => import('./components/FeatureCard'));
const LazyTestimonialCard = lazy(() => import('./components/TestimonialCard'));
// Error Boundary Component
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, errorInfo) {
console.error('Error caught by boundary:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<div style={{ padding: '20px', textAlign: 'center' }}>
<h2>Something went wrong.</h2>
<p>Please refresh the page or try again later.</p>
<button onClick={() => window.location.reload()}>
Refresh Page
</button>
</div>
);
}
return this.props.children;
}
}
function App() {
const dispatch = useDispatch();
const { isAuthenticated, loading } = useSelector(state => state.auth);
const location = useLocation();
const [isCheckingAuth, setIsCheckingAuth] = useState(true);
// Auth pages should never render the Sidebar/Header
const isAuthRoute = location.pathname === '/login' || location.pathname === '/register';
const isCallbackRoute = location.pathname === '/linkedin/callback';
const showSidebar = isAuthenticated && !isAuthRoute && !isCallbackRoute && location.pathname !== '/';
const [isSidebarCollapsed, setIsSidebarCollapsed] = useState(false);
const [isMobile, setIsMobile] = useState(false);
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
// Detect mobile devices and set responsive behavior
useEffect(() => {
const checkMobile = () => {
const mobile = window.innerWidth < 1024; // Match sidebar breakpoint
setIsMobile(mobile);
// Auto-collapse sidebar on mobile
if (mobile && !isSidebarCollapsed) {
setIsSidebarCollapsed(true);
}
};
checkMobile();
window.addEventListener('resize', checkMobile);
return () => window.removeEventListener('resize', checkMobile);
}, [isSidebarCollapsed]);
// Optimize performance for mobile devices
useEffect(() => {
if (isMobile) {
// Reduce animation complexity on mobile
document.body.classList.add('mobile-optimized-animation');
// Enable hardware acceleration for mobile elements
const mobileElements = document.querySelectorAll('.mobile-accelerate');
mobileElements.forEach(el => {
el.classList.add('mobile-accelerated');
});
// Optimize touch interactions
const touchElements = document.querySelectorAll('.touch-optimized');
touchElements.forEach(el => {
el.classList.add('touch-optimized');
});
// Prevent double-tap zoom on mobile
const preventZoom = (e) => {
if (e.detail > 1) {
e.preventDefault();
}
};
document.addEventListener('dblclick', preventZoom, { passive: false });
return () => {
document.removeEventListener('dblclick', preventZoom);
document.body.classList.remove('mobile-optimized-animation');
};
} else {
// Clean up mobile optimizations
document.body.classList.remove('mobile-optimized-animation');
}
}, [isMobile]);
const toggleSidebar = () => {
setIsSidebarCollapsed(!isSidebarCollapsed);
};
const toggleMobileMenu = () => {
setIsMobileMenuOpen(!isMobileMenuOpen);
};
// Close mobile menu when clicking outside
useEffect(() => {
const handleClickOutside = (event) => {
// Close mobile menu when clicking outside
if (isMobileMenuOpen &&
!event.target.closest('#mobile-menu') &&
!event.target.closest('.mobile-menu-button')) {
setIsMobileMenuOpen(false);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [isMobileMenuOpen]);
// Handle keyboard navigation for mobile menu
useEffect(() => {
const handleGlobalKeyDown = (e) => {
// Close mobile menu with Escape
if (e.key === 'Escape' && isMobileMenuOpen) {
setIsMobileMenuOpen(false);
}
};
document.addEventListener('keydown', handleGlobalKeyDown);
return () => document.removeEventListener('keydown', handleGlobalKeyDown);
}, [isMobileMenuOpen]);
// Simplified authentication check - only run once on mount
useEffect(() => {
const initializeAuth = async () => {
try {
setIsCheckingAuth(true);
// Check for cached authentication first
const cachedResult = await dispatch(checkCachedAuth());
// If cached auth failed but we have a token, try auto login
const token = localStorage.getItem('token');
const cookieAuth = await cookieService.getAuthTokens();
if (!cachedResult.payload?.success && (token || cookieAuth?.accessToken)) {
try {
await dispatch(autoLogin());
} catch (error) {
console.log('Auto login failed, clearing tokens');
localStorage.removeItem('token');
await cookieService.clearAuthTokens();
}
}
} catch (error) {
console.error('Auth initialization failed:', error);
localStorage.removeItem('token');
await cookieService.clearAuthTokens();
} finally {
setIsCheckingAuth(false);
}
};
// Only run authentication check once on mount
if (isCheckingAuth) {
initializeAuth();
}
}, []); // Empty dependency array to run only once
// Show loading state only while checking auth on initial load
if (isCheckingAuth) {
return (
<div className="loading">
<div className="auth-loading">
<div className="spinner"></div>
<p>Checking authentication...</p>
</div>
</div>
);
}
return (
<ErrorBoundary>
<div className="App min-h-screen bg-gray-50" role="application" aria-label="Lin Application">
{/* Skip to main content link for accessibility */}
<a
href="#main-content"
className="skip-link sr-only"
onClick={(e) => {
e.preventDefault();
const mainContent = document.getElementById('main-content');
if (mainContent) {
mainContent.focus();
}
}}
>
Skip to main content
</a>
{/* Mobile menu overlay */}
{isMobile && isMobileMenuOpen && (
<div
className="fixed inset-0 bg-black bg-opacity-50 z-40 transition-opacity duration-300"
onClick={() => setIsMobileMenuOpen(false)}
aria-label="Close mobile menu"
role="button"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
setIsMobileMenuOpen(false);
}
}}
></div>
)}
{/* Full-width layout without header/sidebar for Home/Auth/Callback */}
{(!isAuthenticated || isAuthRoute || isCallbackRoute || location.pathname === '/') ? (
<div className="content" id="main-content" tabIndex={-1}>
<Routes>
<Route path="/" element={
<Suspense fallback={<div className="mobile-loading-optimized">Loading...</div>}>
<Home />
</Suspense>
} />
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
<Route path="/linkedin/callback" element={<LinkedInCallbackHandler />} />
</Routes>
</div>
) : (
<>
{/* App layout with header + sidebar for authenticated app pages */}
<Header
onMenuToggle={toggleMobileMenu}
isMenuOpen={isMobileMenuOpen}
isMobile={isMobile}
/>
{/* Mobile sidebar overlay */}
{isMobile && !isSidebarCollapsed && (
<div
className="fixed inset-0 bg-black bg-opacity-50 z-40 transition-opacity duration-300"
onClick={() => setIsSidebarCollapsed(true)}
aria-label="Close sidebar"
role="button"
tabIndex={0}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
setIsSidebarCollapsed(true);
}
}}
></div>
)}
<div className={`main-container transition-all duration-300 ease-in-out ${isSidebarCollapsed ? 'sidebar-collapsed' : ''}`} role="main">
<Sidebar
isCollapsed={isSidebarCollapsed}
toggleSidebar={toggleSidebar}
isMobile={isMobile}
/>
<div className="content flex-1 transition-all duration-300 mobile-render-optimized p-4 sm:p-6 overflow-y-auto" id="main-content" tabIndex={-1}>
<Suspense fallback={<div className="mobile-loading-optimized">Loading...</div>}>
<Routes>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/sources" element={<Sources />} />
<Route path="/accounts" element={<Accounts />} />
<Route path="/posts" element={<Posts />} />
<Route path="/schedule" element={<Schedule />} />
</Routes>
</Suspense>
</div>
</div>
</>
)}
</div>
</ErrorBoundary>
);
}
export default App;
|