import React, { useState, useCallback, useEffect, useMemo } from 'react'; import { Box, Center, Drawer, DrawerOverlay, DrawerContent, DrawerCloseButton, Flex, Button } from '@chakra-ui/react'; import { LoginPageTypeEnum } from '@/web/support/user/login/constants'; import { useSystemStore } from '@/web/common/system/useSystemStore'; import type { ResLogin } from '@/global/support/api/userRes.d'; import { useUserStore } from '@/web/support/user/useUserStore'; import { useChatStore } from '@/web/core/chat/context/useChatStore'; import dynamic from 'next/dynamic'; import Script from 'next/script'; import Loading from '@fastgpt/web/components/common/MyLoading'; import { useLocalStorageState } from 'ahooks'; import { useTranslation } from 'next-i18next'; import LoginForm from '@/pageComponents/login/LoginForm/LoginForm'; import { getBdVId } from '@/web/support/marketing/utils'; import { GET } from '@/web/common/api/request'; import { getDocPath } from '@/web/common/system/doc'; const RegisterForm = dynamic(() => import('@/pageComponents/login/RegisterForm')); const ForgetPasswordForm = dynamic(() => import('@/pageComponents/login/ForgetPasswordForm')); const WechatForm = dynamic(() => import('@/pageComponents/login/LoginForm/WechatForm')); const CommunityModal = dynamic(() => import('@/components/CommunityModal')); const ipDetectURL = 'https://qifu-api.baidubce.com/ip/local/geo/v1/district'; // Cookies Modal Component const CookiesModal = () => { const { t } = useTranslation(); const [isOpen, setIsOpen] = useState(false); const cookieVersion = '1'; const [localCookieVersion, setLocalCookieVersion] = useLocalStorageState('localCookieVersion'); useEffect(() => { // Check if user has agreed to current cookie version if (localCookieVersion !== cookieVersion) { setIsOpen(true); } }, [localCookieVersion, cookieVersion]); const handleAgree = () => { setLocalCookieVersion(cookieVersion); setIsOpen(false); }; const handleClose = () => { setIsOpen(false); }; // Don't render if not needed if (!isOpen) { return null; } return ( {t('login:cookies_tip')} window.open(getDocPath('/docs/protocol/privacy/'), '_blank')} > {t('login:privacy_policy')} ); }; // Chinese Redirect Modal Component const ChineseRedirectModal = () => { const { t } = useTranslation(); const { feConfigs } = useSystemStore(); const chineseRedirectUrl = feConfigs?.chineseRedirectUrl; const [isOpen, setIsOpen] = useState(false); const [showRedirect, setShowRedirect] = useLocalStorageState('showRedirect', { defaultValue: true }); // IP detection without cache const checkIpInChina = useCallback(async () => { try { const res = await GET(ipDetectURL); const country = res?.country; const isChina = country === '中国' && res.prov !== '中国香港' && res.prov !== '中国澳门' && res.prov !== '中国台湾'; if (isChina) { setIsOpen(true); } } catch (error) { console.log('IP detection failed:', error); } }, []); useEffect(() => { // Only check IP if redirect URL is provided and user hasn't disabled it if (chineseRedirectUrl && showRedirect) { checkIpInChina(); } }, [chineseRedirectUrl, showRedirect, checkIpInChina]); const handleRedirect = () => { if (chineseRedirectUrl) { window.open(chineseRedirectUrl, '_self'); } }; const handleNoRemind = () => { setShowRedirect(false); setIsOpen(false); }; const handleClose = () => { setIsOpen(false); }; // Don't render if no redirect URL provided or not open if (!chineseRedirectUrl || !isOpen) { return null; } return ( {t('login:Chinese_ip_tip')} {t('login:no_remind')} ); }; // login container component export const LoginContainer = ({ children, onSuccess }: { children?: React.ReactNode; onSuccess?: (res: ResLogin) => void; }) => { const { t } = useTranslation(); const { feConfigs } = useSystemStore(); const { setUserInfo } = useUserStore(); const { setLastChatAppId } = useChatStore(); const [pageType, setPageType] = useState<`${LoginPageTypeEnum}` | null>(null); const [showCommunityModal, setShowCommunityModal] = useState(false); // login success handler const loginSuccess = useCallback( (res: ResLogin) => { setUserInfo(res.user); onSuccess?.(res); }, [setUserInfo, onSuccess] ); // initialization logic useEffect(() => { setPageType( feConfigs?.oauth?.wechat ? LoginPageTypeEnum.wechat : LoginPageTypeEnum.passwordLogin ); // reset chat state setLastChatAppId(''); }, [feConfigs?.oauth?.wechat, setLastChatAppId]); // dynamic component based on page type const DynamicComponent = useMemo(() => { if (!pageType) return null; const TypeMap = { [LoginPageTypeEnum.passwordLogin]: LoginForm, [LoginPageTypeEnum.register]: RegisterForm, [LoginPageTypeEnum.forgetPassword]: ForgetPasswordForm, [LoginPageTypeEnum.wechat]: WechatForm }; const Component = TypeMap[pageType]; if (!Component) return null; return ; }, [pageType, loginSuccess]); return ( <> {/* Google reCAPTCHA Script */} {feConfigs.googleClientVerKey && (