home.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. "use client";
  2. require("../polyfill");
  3. import React, {
  4. useState,
  5. useEffect
  6. } from "react";
  7. import {
  8. HashRouter as Router,
  9. Routes,
  10. Route,
  11. useLocation
  12. } from "react-router-dom";
  13. import styles from "./home.module.scss";
  14. import loadingIcon from "../icons/loading.gif";
  15. import {
  16. getCSSVar,
  17. useMobileScreen
  18. } from "../utils";
  19. import dynamic from "next/dynamic";
  20. import { Path, SlotID } from "../constant";
  21. import { ErrorBoundary } from "./error";
  22. import { getISOLang } from "../locales";
  23. import { SideBar } from "./sidebar";
  24. import { useAppConfig } from "../store/config";
  25. import { getClientConfig } from "../config/client";
  26. import { type ClientApi, getClientApi } from "../client/api";
  27. import {
  28. useAccessStore
  29. } from "../store";
  30. import api from "../api/api";
  31. export function Loading() {
  32. return (
  33. <div className={styles["loading-content"] + " no-dark"}>
  34. <img src={loadingIcon.src} />
  35. </div>
  36. );
  37. }
  38. // 延时器
  39. export const delayer = (): Promise<any> => {
  40. // 延时时间-秒
  41. const time: number = 0.1;
  42. return new Promise((resolve, reject) => {
  43. setTimeout(() => {
  44. resolve({});
  45. }, time * 1000);
  46. });
  47. }
  48. const Artifacts = dynamic(
  49. async () => {
  50. await delayer();
  51. return (await import("./artifacts")).Artifacts
  52. },
  53. {
  54. loading: () => <Loading />,
  55. }
  56. );
  57. const Settings = dynamic(
  58. async () => {
  59. await delayer();
  60. return (await import("./settings")).Settings
  61. },
  62. {
  63. loading: () => <Loading />,
  64. }
  65. );
  66. const Chat = dynamic(
  67. async () => {
  68. await delayer();
  69. return (await import("./chat")).Chat
  70. },
  71. {
  72. loading: () => <Loading />,
  73. }
  74. );
  75. const DeepSeekChat = dynamic(
  76. async () => {
  77. await delayer();
  78. return (await import("./DeepSeekChat")).Chat
  79. },
  80. {
  81. loading: () => <Loading />,
  82. }
  83. );
  84. const Record = dynamic(
  85. async () => {
  86. await delayer();
  87. return (await import("./Record"))
  88. },
  89. {
  90. loading: () => <Loading />,
  91. }
  92. );
  93. const HomeApp = dynamic(
  94. async () => {
  95. return (await import("./DeekSeekHome"))
  96. },
  97. {
  98. loading: () => <Loading />,
  99. }
  100. );
  101. const MaskChat = dynamic(
  102. async () => {
  103. await delayer();
  104. return (await import("./mask-chat")).MaskChat
  105. },
  106. {
  107. loading: () => <Loading />,
  108. }
  109. );
  110. const MaskPage = dynamic(
  111. async () => {
  112. await delayer();
  113. return (await import("./mask")).MaskPage
  114. },
  115. {
  116. loading: () => <Loading />,
  117. }
  118. );
  119. export function useSwitchTheme() {
  120. const config = useAppConfig();
  121. useEffect(() => {
  122. document.body.classList.remove("light");
  123. document.body.classList.remove("dark");
  124. if (config.theme === "dark") {
  125. document.body.classList.add("dark");
  126. } else if (config.theme === "light") {
  127. document.body.classList.add("light");
  128. }
  129. const metaDescriptionDark = document.querySelector(
  130. 'meta[name="theme-color"][media*="dark"]',
  131. );
  132. const metaDescriptionLight = document.querySelector(
  133. 'meta[name="theme-color"][media*="light"]',
  134. );
  135. if (config.theme === "auto") {
  136. metaDescriptionDark?.setAttribute("content", "#151515");
  137. metaDescriptionLight?.setAttribute("content", "#fafafa");
  138. } else {
  139. const themeColor = getCSSVar("--theme-color");
  140. metaDescriptionDark?.setAttribute("content", themeColor);
  141. metaDescriptionLight?.setAttribute("content", themeColor);
  142. }
  143. }, [config.theme]);
  144. }
  145. function useHtmlLang() {
  146. useEffect(() => {
  147. const lang = getISOLang();
  148. const htmlLang = document.documentElement.lang;
  149. if (lang !== htmlLang) {
  150. document.documentElement.lang = lang;
  151. }
  152. }, []);
  153. }
  154. const useHasHydrated = () => {
  155. const [hasHydrated, setHasHydrated] = useState<boolean>(false);
  156. useEffect(() => {
  157. setHasHydrated(true);
  158. }, []);
  159. return hasHydrated;
  160. };
  161. const loadAsyncGoogleFont = () => {
  162. const linkEl = document.createElement("link");
  163. const proxyFontUrl = "/google-fonts";
  164. const remoteFontUrl = "https://fonts.googleapis.com";
  165. const googleFontUrl =
  166. getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
  167. linkEl.rel = "stylesheet";
  168. linkEl.href =
  169. googleFontUrl +
  170. "/css2?family=" +
  171. encodeURIComponent("Noto Sans:wght@300;400;700;900") +
  172. "&display=swap";
  173. document.head.appendChild(linkEl);
  174. };
  175. export function WindowContent(props: { children: React.ReactNode }) {
  176. return (
  177. <div className={styles["window-content"]} id={SlotID.AppBody}>
  178. {props?.children}
  179. </div>
  180. );
  181. }
  182. function Screen() {
  183. const config = useAppConfig();
  184. const location = useLocation();
  185. const isArtifact = location.pathname.includes(Path.Artifacts);
  186. const isMobileScreen = useMobileScreen();
  187. const shouldTightBorder = getClientConfig()?.isApp || (config.tightBorder && !isMobileScreen);
  188. useEffect(() => {
  189. loadAsyncGoogleFont();
  190. }, []);
  191. if (isArtifact) {
  192. return (
  193. <Routes>
  194. <Route path="/artifacts/:id" element={<Artifacts />} />
  195. </Routes>
  196. );
  197. }
  198. const renderContent = () => {
  199. return (
  200. <>
  201. <SideBar className={styles["sidebar-show"]} />
  202. <WindowContent>
  203. <Routes>
  204. <Route path='/' element={<HomeApp />} />
  205. {/* <Route path='/' element={<DeepSeekChat />} /> */}
  206. <Route path='/knowledgeChat' element={<Chat />} />
  207. <Route path='/newChat' element={<Chat />} />
  208. <Route path='/deepseekChat' element={<DeepSeekChat />} />
  209. <Route path='/newDeepseekChat' element={<HomeApp />} />
  210. {/* 关闭以下入口 后续有需求再开启*/}
  211. {/* <Route path='/settings' element={<Settings />} /> */}
  212. {/* <Route path='/mask-chat' element={<MaskChat />} /> */}
  213. {/* <Route path='/masks' element={<MaskPage />} /> */}
  214. </Routes>
  215. </WindowContent>
  216. </>
  217. );
  218. };
  219. return (
  220. <div
  221. className={`${styles.container} ${shouldTightBorder ? styles["tight-container"] : styles.container
  222. }`}
  223. >
  224. {renderContent()}
  225. </div>
  226. );
  227. }
  228. export function useLoadData() {
  229. const config = useAppConfig();
  230. const api: ClientApi = getClientApi(config.modelConfig.providerName);
  231. useEffect(() => {
  232. (async () => {
  233. const models = await api.llm.models();
  234. config.mergeModels(models);
  235. })();
  236. }, []);
  237. }
  238. export function Home() {
  239. useSwitchTheme();
  240. useLoadData();
  241. useHtmlLang();
  242. useEffect(() => {
  243. useAccessStore.getState().fetch();
  244. }, []);
  245. if (typeof window !== "undefined") {
  246. window.addEventListener("pageshow", (event) => {
  247. const perfEntries = performance.getEntriesByType("navigation");
  248. if (perfEntries.length > 0) {
  249. const navEntry = perfEntries[0] as PerformanceNavigationTiming;
  250. if (navEntry.type === "back_forward") {
  251. window.location.reload();
  252. }
  253. }
  254. });
  255. }
  256. if (!useHasHydrated()) {
  257. return <Loading />;
  258. }
  259. return (
  260. <ErrorBoundary>
  261. <Router>
  262. <Screen />
  263. </Router>
  264. </ErrorBoundary>
  265. );
  266. }