"use client"; import { Modal } from "antd"; require("../polyfill"); import { useState, useEffect } from "react"; import { HashRouter as Router, Routes, Route, useLocation, } from "react-router-dom"; import styles from "./home.module.scss"; import BotIcon from "../icons/bot.svg"; import loadingIcon from "../icons/loading.gif"; import { getCSSVar, useMobileScreen } from "../utils"; import dynamic from "next/dynamic"; import { Path, SlotID } from "../constant"; import { ErrorBoundary } from "./error"; import { getISOLang, getLang } from "../locales"; import { SideBar } from "./sidebar"; import { useAppConfig } from "../store/config"; import { AuthPage } from "./auth"; import { getClientConfig } from "../config/client"; import { type ClientApi, getClientApi } from "../client/api"; import { useAccessStore } from "../store"; import api from "../api/api"; export function Loading() { /** second版本注释掉进度条 */ // const [progress, setProgress] = useState(1); // // useEffect(() => { // let isMounted = true; // // const intervalId = setInterval(() => { // if (isMounted && progress < 100) { // // 每隔一段时间增加1%进度 // setProgress(prevProgress => prevProgress + 1); // } // }, 30);// 每10毫秒更新1%进度 // // return () => { // isMounted = false; // clearInterval(intervalId); // }; // }, [progress]); return (
{/* seceond版本注释掉进度条 */} {/*
*/} {/*
*/} {/*
*/} {/* {progress}% */} {/*
*/} {/*
*/} {/*
*/}
); } // 延时器 export const delayer = (): Promise => { // 延时时间-秒 const time: number = 0.1; return new Promise((resolve, reject) => { setTimeout(() => { resolve({}); }, time * 1000); }); } const Artifacts = dynamic( async () => { await delayer(); return (await import("./artifacts")).Artifacts }, { loading: () => , } ); const Settings = dynamic( async () => { await delayer(); return (await import("./settings")).Settings }, { loading: () => , } ); const Chat = dynamic( async () => { await delayer(); return (await import("./chat")).Chat }, { loading: () => , } ); const DeepSeekChat = dynamic( async () => { await delayer(); return (await import("./DeepSeekChat")).Chat }, { loading: () => , } ); const Record = dynamic( async () => { await delayer(); return (await import("./Record")) }, { loading: () => , } ); const HomeApp = dynamic( async () => { return (await import("./DeekSeekHome")) }, { loading: () => , } ); const NewChat = dynamic( async () => { await delayer(); return (await import("./new-chat")).NewChat }, { loading: () => , } ); const MaskPage = dynamic( async () => { await delayer(); return (await import("./mask")).MaskPage }, { loading: () => , } ); export function useSwitchTheme() { const config = useAppConfig(); useEffect(() => { document.body.classList.remove("light"); document.body.classList.remove("dark"); if (config.theme === "dark") { document.body.classList.add("dark"); } else if (config.theme === "light") { document.body.classList.add("light"); } const metaDescriptionDark = document.querySelector( 'meta[name="theme-color"][media*="dark"]', ); const metaDescriptionLight = document.querySelector( 'meta[name="theme-color"][media*="light"]', ); if (config.theme === "auto") { metaDescriptionDark?.setAttribute("content", "#151515"); metaDescriptionLight?.setAttribute("content", "#fafafa"); } else { const themeColor = getCSSVar("--theme-color"); metaDescriptionDark?.setAttribute("content", themeColor); metaDescriptionLight?.setAttribute("content", themeColor); } }, [config.theme]); } function useHtmlLang() { useEffect(() => { const lang = getISOLang(); const htmlLang = document.documentElement.lang; if (lang !== htmlLang) { document.documentElement.lang = lang; } }, []); } const useHasHydrated = () => { const [hasHydrated, setHasHydrated] = useState(false); useEffect(() => { setHasHydrated(true); }, []); return hasHydrated; }; const loadAsyncGoogleFont = () => { const linkEl = document.createElement("link"); const proxyFontUrl = "/google-fonts"; const remoteFontUrl = "https://fonts.googleapis.com"; const googleFontUrl = getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl; linkEl.rel = "stylesheet"; linkEl.href = googleFontUrl + "/css2?family=" + encodeURIComponent("Noto Sans:wght@300;400;700;900") + "&display=swap"; document.head.appendChild(linkEl); }; export function WindowContent(props: { children: React.ReactNode }) { return (
{props?.children}
); } function Screen() { const config = useAppConfig(); const location = useLocation(); const isArtifact = location.pathname.includes(Path.Artifacts); const isAuth = location.pathname === Path.Auth; const isMobileScreen = useMobileScreen(); const shouldTightBorder = getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen); useEffect(() => { loadAsyncGoogleFont(); }, []); if (isArtifact) { return ( } /> ); } const renderContent = () => { if (isAuth) return ; return ( <> { location.pathname !== '/' && } } /> } /> } /> } /> } /> ); }; return (
{renderContent()}
); } export function useLoadData() { const config = useAppConfig(); const api: ClientApi = getClientApi(config.modelConfig.providerName); useEffect(() => { (async () => { const models = await api.llm.models(); config.mergeModels(models); })(); }, []); } export function Home() { useSwitchTheme(); useLoadData(); useHtmlLang(); useEffect(() => { useAccessStore.getState().fetch(); }, []); const jkLogin = async (data: { code: string, redirectUrl: string }, url: string) => { try { const res = await api.post('jk_code_login', data); localStorage.setItem('userInfo', JSON.stringify(res.data)); window.open(url, '_self'); } catch (error: any) { Modal.error({ title: '登录失败', content: error.msg, }) } } useEffect(() => { // const loginRes = { "nickName": "建科咨询虚拟账号", "userId": "2222331845498970571", "token": "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjAyMDE4Mzg0LTFmNjctNDhkYi05NjNiLTJkOGNhMDMxNTkzMiJ9.zTkTv8gDgJN7tfyxJko_zG1VsESlZACeYkpdMbITqnIpIfvHkZo8l8_Kcv6zo77GnuDyzdpOEt-GzPufD2Ye8A" }; // if (loginRes) { // return localStorage.setItem('userInfo', JSON.stringify(loginRes)); // } const originUrl = window.location.origin; const fullUrl = window.location.href; const urlParams = new URLSearchParams(new URL(fullUrl).search); const code = urlParams.get('code'); const state = urlParams.get('state'); const userInfo = localStorage.getItem('userInfo'); if (fullUrl.includes(originUrl + '/?code') && code && state) {// 通过code登陆 if (!userInfo) { jkLogin({ code: code, redirectUrl: encodeURIComponent(originUrl) }, state); } } else { //测试环境 //const loginUrl = 'https://esctest.sribs.com.cn/esc-sso/oauth2.0/authorize?client_id=e97f94cf93761f4d69e8&response_type=code'; //生产环境 const loginUrl = 'http://esc.sribs.com.cn:8080/esc-sso/oauth2.0/authorize?client_id=e97f94cf93761f4d69e8&response_type=code'; const externalLoginUrl = loginUrl + `&redirect_uri=${encodeURIComponent(originUrl)}&state=${encodeURIComponent(fullUrl)}`; if (!userInfo) { window.open(externalLoginUrl, '_self'); } } }, []); if (!useHasHydrated()) { return ; } return ( ); }