// App.jsx function App() { const normalizeHash = (h) => { if (!h) return '#/'; return h.startsWith('#') ? h : `#${h}`; }; const [route, setRoute] = React.useState(() => normalizeHash(window.location.hash || '#/')); React.useEffect(() => { const onHash = () => setRoute(normalizeHash(window.location.hash || '#/')); window.addEventListener('hashchange', onHash); return () => window.removeEventListener('hashchange', onHash); }, []); const [profile, setProfile] = React.useState(() => { try { const raw = localStorage.getItem('mta_profile'); return raw ? JSON.parse(raw) : null; } catch (e) { console.error('Ошибка парсинга mta_profile при инициализации:', e); return null; } }); // слушаем изменения localStorage из других вкладок React.useEffect(() => { const onStorage = (e) => { if (e.key === 'mta_profile') { try { const raw = localStorage.getItem('mta_profile'); setProfile(raw ? JSON.parse(raw) : null); } catch (err) { console.error('Ошибка парсинга mta_profile при событии storage:', err); setProfile(null); } } }; window.addEventListener('storage', onStorage); return () => window.removeEventListener('storage', onStorage); }, []); // слушаем кастомное событие, которое диспатчим в той же вкладке после регистрации/обновления профиля React.useEffect(() => { const onProfileUpdated = () => { try { const raw = localStorage.getItem('mta_profile'); setProfile(raw ? JSON.parse(raw) : null); } catch (err) { console.error('Ошибка парсинга mta_profile при mta_profile_updated:', err); setProfile(null); } }; window.addEventListener('mta_profile_updated', onProfileUpdated); return () => window.removeEventListener('mta_profile_updated', onProfileUpdated); }, []); // перенаправления в зависимости от наличия профиля и маршрута React.useEffect(() => { if (profile && (route === '#/' || route === '' || route === '#')) { if (window.location.hash !== '#/dashboard') window.location.hash = '#/dashboard'; } if (!profile && route === '#/dashboard') { if (window.location.hash !== '#/') window.location.hash = '#/'; } }, [profile, route]); if (route === '#/dashboard') return ; if (route === '#/login') return ; if (route === '#/register') return ; if (route === '#/reset') return ; return ; }