import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { Cloud, LogIn, LogOut, Loader2, ChevronDown, ChevronRight, Settings2, Printer, Droplet, X, Key, RefreshCw, } from 'lucide-react'; import { api } from '../api/client'; import type { SlicerSetting, SlicerSettingsResponse } from '../api/client'; import { Card, CardContent, CardHeader } from '../components/Card'; import { Button } from '../components/Button'; import { useToast } from '../contexts/ToastContext'; type LoginStep = 'email' | 'code' | 'token'; function LoginForm({ onSuccess }: { onSuccess: () => void }) { const { showToast } = useToast(); const [step, setStep] = useState('email'); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [code, setCode] = useState(''); const [token, setToken] = useState(''); const [region, setRegion] = useState('global'); const loginMutation = useMutation({ mutationFn: () => api.cloudLogin(email, password, region), onSuccess: (result) => { if (result.success) { showToast('Logged in successfully'); onSuccess(); } else if (result.needs_verification) { showToast('Verification code sent to your email'); setStep('code'); } else { showToast(result.message, 'error'); } }, onError: (error: Error) => { showToast(error.message, 'error'); }, }); const verifyMutation = useMutation({ mutationFn: () => api.cloudVerify(email, code), onSuccess: (result) => { if (result.success) { showToast('Logged in successfully'); onSuccess(); } else { showToast(result.message, 'error'); } }, onError: (error: Error) => { showToast(error.message, 'error'); }, }); const tokenMutation = useMutation({ mutationFn: () => api.cloudSetToken(token), onSuccess: () => { showToast('Token set successfully'); onSuccess(); }, onError: (error: Error) => { showToast(error.message, 'error'); }, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (step === 'email') { loginMutation.mutate(); } else if (step === 'code') { verifyMutation.mutate(); } else if (step === 'token') { tokenMutation.mutate(); } }; const isPending = loginMutation.isPending || verifyMutation.isPending || tokenMutation.isPending; return (

Connect to Bambu Cloud

{step === 'email' && ( <>
setEmail(e.target.value)} className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none" placeholder="your@email.com" required />
setPassword(e.target.value)} className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white focus:border-bambu-green focus:outline-none" placeholder="••••••••" required />
)} {step === 'code' && (

Check your email ({email}) for a 6-digit code

setCode(e.target.value.replace(/\D/g, '').slice(0, 6))} className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-center text-2xl tracking-widest focus:border-bambu-green focus:outline-none" placeholder="000000" maxLength={6} required />
)} {step === 'token' && (

Paste your Bambu Lab access token (from Bambu Studio)