import { Play, User, XIcon } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; import { useAuthStore } from "@/models/auth"; import { useGameStore } from "@/models/game"; import { useInstanceStore } from "@/models/instance"; import { LoginModal } from "./login-modal"; import { Button } from "./ui/button"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "./ui/select"; import { Spinner } from "./ui/spinner"; export function BottomBar() { const account = useAuthStore((state) => state.account); const { instances, activeInstance, setActiveInstance } = useInstanceStore(); const { runningInstanceId, launchingInstanceId, stoppingInstanceId, startGame, stopGame, } = useGameStore(); const [selectedVersion, setSelectedVersion] = useState(null); const [showLoginModal, setShowLoginModal] = useState(false); useEffect(() => { const nextVersion = activeInstance?.versionId ?? ""; if (selectedVersion === nextVersion) { return; } setSelectedVersion(nextVersion); }, [activeInstance?.versionId, selectedVersion]); const handleInstanceChange = useCallback( async (instanceId: string) => { if (activeInstance?.id === instanceId) { return; } const nextInstance = instances.find( (instance) => instance.id === instanceId, ); if (!nextInstance) { return; } try { await setActiveInstance(nextInstance); } catch (error) { console.error("Failed to activate instance:", error); toast.error(`Failed to activate instance: ${String(error)}`); } }, [activeInstance?.id, instances, setActiveInstance], ); const handleStartGame = async () => { if (!activeInstance) { toast.info("Please select an instance first!"); return; } await startGame( activeInstance.id, selectedVersion || activeInstance.versionId, ); }; const handleStopGame = async () => { await stopGame(runningInstanceId); }; const renderButton = () => { const isGameRunning = runningInstanceId !== null; if (!account) { return ( ); } if (isGameRunning) { return ( ); } return ( ); }; return (
{renderButton()}
setShowLoginModal(false)} />
); }