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 { useInstanceStore } from "@/models/instance"; import { useGameStore } from "@/stores/game-store"; 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 = useInstanceStore((state) => state.instances); const activeInstance = useInstanceStore((state) => state.activeInstance); const setActiveInstance = useInstanceStore((state) => state.setActiveInstance); const selectedVersion = useGameStore((state) => state.selectedVersion); const setSelectedVersion = useGameStore((state) => state.setSelectedVersion); const startGame = useGameStore((state) => state.startGame); const stopGame = useGameStore((state) => state.stopGame); const runningInstanceId = useGameStore((state) => state.runningInstanceId); const launchingInstanceId = useGameStore((state) => state.launchingInstanceId); const stoppingInstanceId = useGameStore((state) => state.stoppingInstanceId); const [showLoginModal, setShowLoginModal] = useState(false); useEffect(() => { const nextVersion = activeInstance?.versionId ?? ""; if (selectedVersion === nextVersion) { return; } setSelectedVersion(nextVersion); }, [activeInstance?.id, activeInstance?.versionId, selectedVersion, setSelectedVersion]); 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( account, () => setShowLoginModal(true), activeInstance.id, selectedVersion || activeInstance.versionId, () => undefined, ); }; const handleStopGame = async () => { await stopGame(runningInstanceId); }; const renderButton = () => { const isGameRunning = runningInstanceId !== null; if (!account) { return ( ); } if (isGameRunning) { return ( ); } return ( ); }; return (