import { useState, useEffect } from 'react';
import { useGameStore } from '@/store';
import { MainLayout } from '@/components/layout/MainLayout';
import { NewGameScreen } from '@/components/game/NewGameScreen';
import { OfflineCatchUp } from '@/components/game/OfflineCatchUp';
import { InviteGateScreen } from '@/components/game/InviteGateScreen';
import { useGameLoop } from '@/hooks/useGameLoop';
import { useAuthGate } from '@/hooks/useAuthGate';
import { TICK_INTERVAL_MS } from '@token-empire/shared';
import { Sparkles, RefreshCw, WifiOff } from 'lucide-react';
function LoadingScreen() {
return (
);
}
function BackendErrorScreen({ error, onRetry }: { error: string; onRetry: () => void }) {
return (
Token Empire
Cannot Connect to Server
{error}
);
}
export function App() {
const { loading: authLoading, backendError, needsInvite, needsPasswordReset, setRegistered, setNeedsPasswordReset, retry } = useAuthGate();
const companyName = useGameStore((s) => s.meta.companyName);
const lastTickTimestamp = useGameStore((s) => s.meta.lastTickTimestamp);
const [catchUpTicks, setCatchUpTicks] = useState(null);
const [catchUpDone, setCatchUpDone] = useState(false);
useEffect(() => {
if (!companyName || catchUpDone) return;
const elapsed = Date.now() - lastTickTimestamp;
const missed = Math.floor(elapsed / TICK_INTERVAL_MS);
if (missed > 10) {
setCatchUpTicks(missed);
} else {
setCatchUpDone(true);
}
}, [companyName, lastTickTimestamp, catchUpDone]);
useGameLoop(!catchUpDone || authLoading || !!backendError || needsInvite || needsPasswordReset);
if (authLoading) {
return ;
}
if (backendError) {
return ;
}
if (needsInvite || needsPasswordReset) {
return (
{
setRegistered(true);
setNeedsPasswordReset(false);
}}
/>
);
}
if (!companyName) {
return ;
}
if (catchUpTicks !== null && !catchUpDone) {
return (
setCatchUpDone(true)}
/>
);
}
return ;
}