NextChat-U/app/components/home.tsx

190 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-03-10 02:01:40 +09:00
"use client";
2023-07-16 17:32:22 +09:00
// require("../polyfill");
2023-04-22 02:13:23 +09:00
import { useState, useEffect } from "react";
2023-03-12 02:14:07 +09:00
2023-03-13 04:06:21 +09:00
import styles from "./home.module.scss";
2023-03-10 02:01:40 +09:00
import BotIcon from "../icons/bot.svg";
2023-03-11 03:25:33 +09:00
import LoadingIcon from "../icons/three-dots.svg";
2023-04-21 00:20:25 +09:00
import { getCSSVar, useMobileScreen } from "../utils";
2023-03-14 01:25:07 +09:00
import dynamic from "next/dynamic";
2023-04-24 02:15:44 +09:00
import { Path, SlotID } from "../constant";
import { ErrorBoundary } from "./error";
import { getLang } from "../locales";
2023-04-21 02:12:39 +09:00
import {
HashRouter as Router,
Routes,
Route,
useLocation,
} from "react-router-dom";
2023-04-22 00:37:25 +09:00
import { SideBar } from "./sidebar";
2023-04-22 01:12:07 +09:00
import { useAppConfig } from "../store/config";
2023-06-07 03:18:24 +09:00
import { AuthPage } from "./auth";
import { getClientConfig } from "../config/client";
import { api } from "../client/api";
2023-07-11 00:19:43 +09:00
import { useAccessStore } from "../store";
2023-04-21 02:12:39 +09:00
2023-03-21 23:56:27 +09:00
export function Loading(props: { noLogo?: boolean }) {
return (
2023-04-21 02:12:39 +09:00
<div className={styles["loading-content"] + " no-dark"}>
2023-03-21 23:56:27 +09:00
{!props.noLogo && <BotIcon />}
<LoadingIcon />
</div>
);
}
2023-03-21 23:56:27 +09:00
const Settings = dynamic(async () => (await import("./settings")).Settings, {
loading: () => <Loading noLogo />,
});
2023-04-22 02:13:23 +09:00
const Chat = dynamic(async () => (await import("./chat")).Chat, {
loading: () => <Loading noLogo />,
});
2023-04-24 02:17:28 +09:00
const NewChat = dynamic(async () => (await import("./new-chat")).NewChat, {
loading: () => <Loading noLogo />,
});
2023-04-25 01:49:27 +09:00
const MaskPage = dynamic(async () => (await import("./mask")).MaskPage, {
loading: () => <Loading noLogo />,
});
2023-04-21 03:52:53 +09:00
export function useSwitchTheme() {
2023-04-22 01:12:07 +09:00
const config = useAppConfig();
2023-03-13 04:06:21 +09:00
useEffect(() => {
document.body.classList.remove("light");
document.body.classList.remove("dark");
2023-03-13 04:06:21 +09:00
if (config.theme === "dark") {
document.body.classList.add("dark");
} else if (config.theme === "light") {
document.body.classList.add("light");
}
const metaDescriptionDark = document.querySelector(
2023-05-12 19:47:41 +09:00
'meta[name="theme-color"][media*="dark"]',
);
const metaDescriptionLight = document.querySelector(
2023-05-12 19:47:41 +09:00
'meta[name="theme-color"][media*="light"]',
);
2023-03-16 02:24:03 +09:00
if (config.theme === "auto") {
metaDescriptionDark?.setAttribute("content", "#151515");
metaDescriptionLight?.setAttribute("content", "#fafafa");
} else {
2023-05-12 19:47:41 +09:00
const themeColor = getCSSVar("--theme-color");
metaDescriptionDark?.setAttribute("content", themeColor);
metaDescriptionLight?.setAttribute("content", themeColor);
}
}, [config.theme]);
2023-03-20 00:13:10 +09:00
}
2023-03-27 17:58:53 +09:00
const useHasHydrated = () => {
const [hasHydrated, setHasHydrated] = useState<boolean>(false);
useEffect(() => {
setHasHydrated(true);
}, []);
return hasHydrated;
};
2023-05-15 00:25:22 +09:00
const loadAsyncGoogleFont = () => {
const linkEl = document.createElement("link");
2023-06-15 02:48:56 +09:00
const proxyFontUrl = "/google-fonts";
const remoteFontUrl = "https://fonts.googleapis.com";
const googleFontUrl =
getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
2023-05-15 00:25:22 +09:00
linkEl.rel = "stylesheet";
linkEl.href =
2023-06-15 02:48:56 +09:00
googleFontUrl +
"/css2?family=Noto+Sans+SC:wght@300;400;700;900&display=swap";
2023-05-15 00:25:22 +09:00
document.head.appendChild(linkEl);
};
2023-04-24 02:15:44 +09:00
function Screen() {
2023-04-22 01:12:07 +09:00
const config = useAppConfig();
2023-04-21 02:12:39 +09:00
const location = useLocation();
const isHome = location.pathname === Path.Home;
2023-06-07 03:18:24 +09:00
const isAuth = location.pathname === Path.Auth;
2023-04-24 02:15:44 +09:00
const isMobileScreen = useMobileScreen();
2023-04-11 03:56:48 +09:00
2023-05-15 00:25:22 +09:00
useEffect(() => {
loadAsyncGoogleFont();
}, []);
2023-04-21 02:12:39 +09:00
return (
2023-04-24 02:15:44 +09:00
<div
className={
styles.container +
` ${
config.tightBorder && !isMobileScreen
? styles["tight-container"]
: styles.container
} ${getLang() === "ar" ? styles["rtl-screen"] : ""}`
2023-04-24 02:15:44 +09:00
}
>
2023-06-07 03:18:24 +09:00
{isAuth ? (
<>
<AuthPage />
</>
) : (
<>
<SideBar className={isHome ? styles["sidebar-show"] : ""} />
<div className={styles["window-content"]} id={SlotID.AppBody}>
<Routes>
<Route path={Path.Home} element={<Chat />} />
<Route path={Path.NewChat} element={<NewChat />} />
<Route path={Path.Masks} element={<MaskPage />} />
<Route path={Path.Chat} element={<Chat />} />
<Route path={Path.Settings} element={<Settings />} />
</Routes>
</div>
</>
)}
2023-04-11 03:56:48 +09:00
</div>
2023-03-10 02:01:40 +09:00
);
}
export function useLoadData() {
const config = useAppConfig();
useEffect(() => {
(async () => {
const models = await api.llm.models();
config.mergeModels(models);
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}
export function Home() {
2023-04-21 03:52:53 +09:00
useSwitchTheme();
useLoadData();
2023-04-21 02:12:39 +09:00
useEffect(() => {
console.log("[Config] got config from build time", getClientConfig());
2023-07-11 00:19:43 +09:00
useAccessStore.getState().fetch();
}, []);
2023-04-21 02:12:39 +09:00
if (!useHasHydrated()) {
return <Loading />;
}
return (
<ErrorBoundary>
2023-04-24 02:15:44 +09:00
<Router>
<Screen />
</Router>
</ErrorBoundary>
);
}