manager-orders-app/src/App.tsx

82 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState, useEffect } from 'react';
import { ErrorScreen } from './components/ErrorScreen';
export function App() {
const [authError, setAuthError] = useState<string | null>(null);
const [loading, setLoading] = useState<boolean>(true);
// Функция выхода
const handleLogout = () => {
localStorage.clear();
sessionStorage.clear();
window.location.href = '/';
};
useEffect(() => {
// Пример запроса за данными (или проверка профиля)
fetch('/api/monthly-orders/matrix', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
})
.then(async (res) => {
if (!res.ok) {
const data = await res.json();
// Если бэкенд вернул ошибку привязки Telegram
if (data && data.error_description) {
throw new Error(data.error_description);
}
throw new Error('Произошла ошибка загрузки данных');
}
return res.json();
})
.catch((err) => {
setAuthError(err.message);
})
.finally(() => {
setLoading(false);
});
}, []);
if (loading) {
return <div className="flex justify-center items-center h-screen">Загрузка...</div>;
}
// Если возникла критическая ошибка доступа — показываем экран с кнопкой выхода
if (authError) {
return <ErrorScreen message={authError} onLogout={handleLogout} />;
}
return (
<div className="relative min-h-screen bg-gray-100">
{/* Всегда доступная кнопка выхода в правом верхнем углу */}
<div className="absolute top-4 right-4 z-50">
<button
onClick={handleLogout}
className="bg-red-600 hover:bg-red-700 text-white text-sm font-medium py-2 px-4 rounded-lg shadow transition-colors cursor-pointer"
>
🚪 Выйти из аккаунта
</button>
</div>
{authError ? (
<div className="flex flex-col items-center justify-center min-h-screen p-4">
<div className="max-w-md w-full bg-white rounded-lg shadow-lg p-6 text-center">
<h2 className="text-xl font-semibold text-gray-800 mb-2">Ошибка доступа</h2>
<p className="text-gray-600 mb-6 bg-gray-50 p-3 rounded border border-gray-200 text-sm">
{authError}
</p>
<button
onClick={handleLogout}
className="w-full bg-red-600 hover:bg-red-700 text-white font-medium py-2 px-4 rounded transition-colors"
>
Выйти и войти заново
</button>
</div>
</div>
) : (
<div>{/* Здесь ваше обычное приложение */}</div>
)}
</div>
);
}