import { cx, css } from '@emotion/css' import { useEffect, useState } from 'react' import { useSnapshot } from 'valtio' import uiStates from '@/web/states/uiStates' import { AnimatePresence, motion, useAnimation } from 'framer-motion' import { ease } from '@/web/utils/const' import Icon from '@/web/components/Icon' import LoginWithPhoneOrEmail from './LoginWithPhoneOrEmail' import LoginWithQRCode from './LoginWithQRCode' import persistedUiStates from '@/web/states/persistedUiStates' import useUser from '@/web/api/hooks/useUser' import { useTranslation } from 'react-i18next' const OR = ({ children, onClick }: { children: React.ReactNode; onClick: () => void }) => { const { t } = useTranslation() return ( <>
{t`auth.or`}
) } const Login = () => { const { t } = useTranslation() const { data: user, isLoading: isLoadingUser } = useUser() const { loginType } = useSnapshot(persistedUiStates) const { showLoginPanel } = useSnapshot(uiStates) const [cardType, setCardType] = useState<'qrCode' | 'phone/email'>( loginType === 'qrCode' ? 'qrCode' : 'phone/email' ) // Show login panel when user first loads the page and not logged in useEffect(() => { if (!user?.account && !isLoadingUser) { uiStates.showLoginPanel = true } }, [user?.account, isLoadingUser]) const animateCard = useAnimation() const handleSwitchCard = async () => { const transition = { duration: 0.36, ease } await animateCard.start({ rotateY: 90, opacity: 0, transition, }) setCardType(cardType === 'qrCode' ? 'phone/email' : 'qrCode') persistedUiStates.loginType = cardType === 'qrCode' ? 'phone' : 'qrCode' await animateCard.start({ rotateY: 0, opacity: 1, transition, }) } return ( <> {/* Blur bg */} {showLoginPanel && ( )} {/* Content */} {showLoginPanel && (
{/* Login card */} {cardType === 'qrCode' && } {cardType === 'phone/email' && } {cardType === 'qrCode' ? t`auth.use-phone-or-email` : t`auth.scan-qr-code`} {/* Close button */} (uiStates.showLoginPanel = false)} className='mt-10 flex h-14 w-14 items-center justify-center rounded-full bg-white/10 text-white/50 transition-colors duration-300 hover:bg-white/20 hover:text-white/70' >
)}
) } export default Login