home.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. "use client";
  2. require("../polyfill");
  3. import { useState, useEffect } from "react";
  4. import styles from "./home.module.scss";
  5. import BotIcon from "../icons/bot.svg";
  6. import loadingIcon from "../icons/loading.gif";
  7. import { getCSSVar, useMobileScreen } from "../utils";
  8. import dynamic from "next/dynamic";
  9. import { Path, SlotID } from "../constant";
  10. import { ErrorBoundary } from "./error";
  11. import { getISOLang, getLang } from "../locales";
  12. import {
  13. HashRouter as Router,
  14. Routes,
  15. Route,
  16. useLocation,
  17. } from "react-router-dom";
  18. import { SideBar } from "./sidebar";
  19. import { useAppConfig } from "../store/config";
  20. import { AuthPage } from "./auth";
  21. import { getClientConfig } from "../config/client";
  22. import { type ClientApi, getClientApi } from "../client/api";
  23. import { useAccessStore } from "../store";
  24. export function Loading() {
  25. const [progress, setProgress] = useState(0);
  26. useEffect(() => {
  27. let isMounted = true;
  28. const intervalId = setInterval(() => {
  29. if (isMounted && progress < 100) {
  30. // 每隔一段时间增加1%进度
  31. setProgress(prevProgress => prevProgress + 1);
  32. }
  33. }, 10);// 每10毫秒更新1%进度
  34. return () => {
  35. isMounted = false;
  36. clearInterval(intervalId);
  37. };
  38. }, [progress]);
  39. return (
  40. <div className={styles["loading-content"] + " no-dark"}>
  41. <img src={loadingIcon.src} />
  42. <div style={{ width: '60%', height: 15, background: '#F5F6F9', borderRadius: 8, marginTop: '20%', position: 'relative' }}>
  43. <div
  44. style={{
  45. width: `${progress}%`,
  46. height: 15,
  47. background: '#265C7D',
  48. borderRadius: 8,
  49. display: 'flex',
  50. justifyContent: 'flex-end',
  51. alignItems: 'center',
  52. position: 'absolute'
  53. }}
  54. >
  55. <div style={{ color: '#FFFFFF', fontSize: 12, lineHeight: 12, marginRight: 4 }}>
  56. {progress}%
  57. </div>
  58. </div>
  59. </div>
  60. </div>
  61. );
  62. }
  63. // 延时器
  64. export const delayer = (): Promise<any> => {
  65. // 延时时间-秒
  66. const time: number = 0.8;
  67. return new Promise((resolve, reject) => {
  68. setTimeout(() => {
  69. resolve({});
  70. }, time * 1000);
  71. });
  72. }
  73. const Artifacts = dynamic(
  74. async () => {
  75. await delayer();
  76. return (await import("./artifacts")).Artifacts
  77. },
  78. {
  79. loading: () => <Loading />,
  80. }
  81. );
  82. const Settings = dynamic(
  83. async () => {
  84. await delayer();
  85. return (await import("./settings")).Settings
  86. },
  87. {
  88. loading: () => <Loading />,
  89. }
  90. );
  91. const Chat = dynamic(
  92. async () => {
  93. await delayer();
  94. return (await import("./chat")).Chat
  95. },
  96. {
  97. loading: () => <Loading />,
  98. }
  99. );
  100. const Record = dynamic(
  101. async () => {
  102. await delayer();
  103. return (await import("./Record"))
  104. },
  105. {
  106. loading: () => <Loading />,
  107. }
  108. );
  109. const NewChat = dynamic(
  110. async () => {
  111. await delayer();
  112. return (await import("./new-chat")).NewChat
  113. },
  114. {
  115. loading: () => <Loading />,
  116. }
  117. );
  118. const MaskPage = dynamic(
  119. async () => {
  120. await delayer();
  121. return (await import("./mask")).MaskPage
  122. },
  123. {
  124. loading: () => <Loading />,
  125. }
  126. );
  127. const Sd = dynamic(
  128. async () => {
  129. await delayer();
  130. return (await import("./sd")).Sd
  131. },
  132. {
  133. loading: () => <Loading />,
  134. }
  135. );
  136. export function useSwitchTheme() {
  137. const config = useAppConfig();
  138. useEffect(() => {
  139. document.body.classList.remove("light");
  140. document.body.classList.remove("dark");
  141. if (config.theme === "dark") {
  142. document.body.classList.add("dark");
  143. } else if (config.theme === "light") {
  144. document.body.classList.add("light");
  145. }
  146. const metaDescriptionDark = document.querySelector(
  147. 'meta[name="theme-color"][media*="dark"]',
  148. );
  149. const metaDescriptionLight = document.querySelector(
  150. 'meta[name="theme-color"][media*="light"]',
  151. );
  152. if (config.theme === "auto") {
  153. metaDescriptionDark?.setAttribute("content", "#151515");
  154. metaDescriptionLight?.setAttribute("content", "#fafafa");
  155. } else {
  156. const themeColor = getCSSVar("--theme-color");
  157. metaDescriptionDark?.setAttribute("content", themeColor);
  158. metaDescriptionLight?.setAttribute("content", themeColor);
  159. }
  160. }, [config.theme]);
  161. }
  162. function useHtmlLang() {
  163. useEffect(() => {
  164. const lang = getISOLang();
  165. const htmlLang = document.documentElement.lang;
  166. if (lang !== htmlLang) {
  167. document.documentElement.lang = lang;
  168. }
  169. }, []);
  170. }
  171. const useHasHydrated = () => {
  172. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  173. useEffect(() => {
  174. setHasHydrated(true);
  175. }, []);
  176. return hasHydrated;
  177. };
  178. const loadAsyncGoogleFont = () => {
  179. const linkEl = document.createElement("link");
  180. const proxyFontUrl = "/google-fonts";
  181. const remoteFontUrl = "https://fonts.googleapis.com";
  182. const googleFontUrl =
  183. getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
  184. linkEl.rel = "stylesheet";
  185. linkEl.href =
  186. googleFontUrl +
  187. "/css2?family=" +
  188. encodeURIComponent("Noto Sans:wght@300;400;700;900") +
  189. "&display=swap";
  190. document.head.appendChild(linkEl);
  191. };
  192. export function WindowContent(props: { children: React.ReactNode }) {
  193. return (
  194. <div className={styles["window-content"]} id={SlotID.AppBody}>
  195. {props?.children}
  196. </div>
  197. );
  198. }
  199. function Screen() {
  200. const config = useAppConfig();
  201. const location = useLocation();
  202. const isArtifact = location.pathname.includes(Path.Artifacts);
  203. const isHome = location.pathname === Path.Home;
  204. const isAuth = location.pathname === Path.Auth;
  205. const isSd = location.pathname === Path.Sd;
  206. const isSdNew = location.pathname === Path.SdNew;
  207. const isMobileScreen = useMobileScreen();
  208. const shouldTightBorder =
  209. getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen);
  210. useEffect(() => {
  211. loadAsyncGoogleFont();
  212. }, []);
  213. if (isArtifact) {
  214. return (
  215. <Routes>
  216. <Route path="/artifacts/:id" element={<Artifacts />} />
  217. </Routes>
  218. );
  219. }
  220. const renderContent = () => {
  221. if (isAuth) return <AuthPage />;
  222. if (isSd) return <Sd />;
  223. if (isSdNew) return <Sd />;
  224. return (
  225. <>
  226. {/* <SideBar className={isHome ? styles["sidebar-show"] : ""} /> */}
  227. <WindowContent>
  228. <Routes>
  229. <Route path={Path.Home} element={<Chat />} />
  230. <Route path={'/record'} element={<Record />} />
  231. {/* <Route path={Path.NewChat} element={<NewChat />} /> */}
  232. {/* <Route path={Path.Masks} element={<MaskPage />} /> */}
  233. {/* <Route path={Path.Chat} element={<Chat />} /> */}
  234. {/* <Route path={Path.Settings} element={<Settings />} /> */}
  235. </Routes>
  236. </WindowContent>
  237. </>
  238. );
  239. };
  240. return (
  241. <div
  242. className={`${styles.container} ${shouldTightBorder ? styles["tight-container"] : styles.container
  243. } ${getLang() === "ar" ? styles["rtl-screen"] : ""}`}
  244. >
  245. {renderContent()}
  246. </div>
  247. );
  248. }
  249. export function useLoadData() {
  250. const config = useAppConfig();
  251. const api: ClientApi = getClientApi(config.modelConfig.providerName);
  252. useEffect(() => {
  253. (async () => {
  254. const models = await api.llm.models();
  255. config.mergeModels(models);
  256. })();
  257. // eslint-disable-next-line react-hooks/exhaustive-deps
  258. }, []);
  259. }
  260. export function Home() {
  261. useSwitchTheme();
  262. useLoadData();
  263. useHtmlLang();
  264. useEffect(() => {
  265. // console.log("[Config] got config from build time", getClientConfig());
  266. useAccessStore.getState().fetch();
  267. }, []);
  268. if (!useHasHydrated()) {
  269. return <Loading />;
  270. }
  271. return (
  272. <ErrorBoundary>
  273. <Router>
  274. <Screen />
  275. </Router>
  276. </ErrorBoundary>
  277. );
  278. }