This commit is contained in:
@@ -7,19 +7,43 @@ import { KittyGame } from './components/KittyGame';
|
||||
import { FlappyCat } from './components/FlappyCat';
|
||||
import { useAuth } from './hooks/useAuth';
|
||||
|
||||
// Eksportujemy typ, aby inne pliki mogły go użyć
|
||||
export type View = 'home' | 'login' | 'history' | 'jump-game' | 'flappy-game';
|
||||
|
||||
const getSubdomain = () => {
|
||||
const hostname = window.location.hostname;
|
||||
const parts = hostname.split('.');
|
||||
if (parts.length <= 2) return null;
|
||||
return parts[0];
|
||||
};
|
||||
|
||||
function App() {
|
||||
const [url, setUrl] = useState('');
|
||||
const [view, setView] = useState<View>('home');
|
||||
const { isAuthenticated, logout } = useAuth();
|
||||
const subdomain = getSubdomain();
|
||||
|
||||
/**
|
||||
* STAN POCHODNY (Derived State)
|
||||
* Rozwiązuje błąd "cascading renders". Jeśli użytkownik jest na subdomenie
|
||||
* i nie jest zalogowany, automatycznie renderujemy widok logowania,
|
||||
* ale nie nadpisujemy stanu 'view' w nieskończoność.
|
||||
*/
|
||||
const activeView = (subdomain && !isAuthenticated) ? 'login' : view;
|
||||
|
||||
const renderView = () => {
|
||||
switch (view) {
|
||||
switch (activeView) {
|
||||
case 'login':
|
||||
return <LoginView onBack={() => setView('home')} onSuccess={() => setView('home')} />;
|
||||
return (
|
||||
<LoginView
|
||||
onBack={() => setView('home')}
|
||||
onSuccess={() => setView('home')}
|
||||
/>
|
||||
);
|
||||
case 'history':
|
||||
// Strażnik dostępu dla widoku historii
|
||||
if (!isAuthenticated) {
|
||||
return <LoginView onBack={() => setView('home')} onSuccess={() => setView('home')} />;
|
||||
}
|
||||
return <HistoryView onBack={() => setView('home')} />;
|
||||
case 'jump-game':
|
||||
return <KittyGame onBack={() => setView('home')} />;
|
||||
@@ -37,7 +61,12 @@ function App() {
|
||||
isAuthenticated={isAuthenticated}
|
||||
onLogout={logout}
|
||||
/>
|
||||
<main>{renderView()}</main>
|
||||
<main>
|
||||
{/* Jeśli użytkownik jest zalogowany (SSO), activeView od razu
|
||||
pokaże Generator, zamiast LoginView.
|
||||
*/}
|
||||
{renderView()}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user