NextChat-U/app/components/auth.tsx

190 lines
5.6 KiB
TypeScript
Raw Normal View History

2023-06-07 03:18:24 +09:00
import styles from "./auth.module.scss";
import { IconButton } from "./button";
2024-09-19 17:58:51 +09:00
import { useState, useEffect } from "react";
2023-06-07 03:18:24 +09:00
import { useNavigate } from "react-router-dom";
2024-09-19 01:05:06 +09:00
import { Path, SAAS_CHAT_URL } from "../constant";
import { useAccessStore } from "../store";
import Locale from "../locales";
2024-09-19 19:21:26 +09:00
import Delete from "../icons/close.svg";
2024-09-19 17:58:51 +09:00
import Arrow from "../icons/arrow.svg";
import Logo from "../icons/logo.svg";
2024-09-24 12:36:26 +09:00
import { useMobileScreen } from "@/app/utils";
import BotIcon from "../icons/bot.svg";
import { getClientConfig } from "../config/client";
2024-10-11 18:03:20 +09:00
import { PasswordInput } from "./ui-lib";
2024-09-19 09:41:09 +09:00
import LeftIcon from "@/app/icons/left.svg";
2024-09-19 19:21:26 +09:00
import { safeLocalStorage } from "@/app/utils";
2024-09-25 14:08:03 +09:00
import {
trackSettingsPageGuideToCPaymentClick,
trackAuthorizationPageButtonToCPaymentClick,
} from "../utils/auth-settings-events";
2024-11-06 17:58:26 +09:00
import clsx from "clsx";
2024-09-20 11:22:22 +09:00
const storage = safeLocalStorage();
2024-09-24 12:36:26 +09:00
2023-06-07 03:18:24 +09:00
export function AuthPage() {
const navigate = useNavigate();
2023-11-08 01:34:31 +09:00
const accessStore = useAccessStore();
2023-06-07 03:18:24 +09:00
const goHome = () => navigate(Path.Home);
const goChat = () => navigate(Path.Chat);
2024-09-19 09:41:09 +09:00
const goSaas = () => {
2024-09-25 14:08:03 +09:00
trackAuthorizationPageButtonToCPaymentClick();
2024-09-19 09:41:09 +09:00
window.location.href = SAAS_CHAT_URL;
};
2024-09-19 17:58:51 +09:00
2023-11-08 01:34:31 +09:00
const resetAccessCode = () => {
accessStore.update((access) => {
2023-11-10 03:43:30 +09:00
access.openaiApiKey = "";
2023-11-08 01:34:31 +09:00
access.accessCode = "";
});
}; // Reset access code to empty string
2023-06-07 03:18:24 +09:00
useEffect(() => {
if (getClientConfig()?.isApp) {
navigate(Path.Settings);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
2023-06-07 03:18:24 +09:00
return (
<div className={styles["auth-page"]}>
2024-09-19 17:58:51 +09:00
<TopBanner></TopBanner>
2024-09-19 09:41:09 +09:00
<div className={styles["auth-header"]}>
<IconButton
icon={<LeftIcon />}
text={Locale.Auth.Return}
onClick={() => navigate(Path.Home)}
></IconButton>
</div>
2024-11-06 17:58:26 +09:00
<div className={clsx("no-dark", styles["auth-logo"])}>
2023-06-07 03:18:24 +09:00
<BotIcon />
</div>
<div className={styles["auth-title"]}>{Locale.Auth.Title}</div>
<div className={styles["auth-tips"]}>{Locale.Auth.Tips}</div>
2024-10-11 18:03:20 +09:00
<PasswordInput
style={{ marginTop: "3vh", marginBottom: "3vh" }}
aria={Locale.Settings.ShowPassword}
aria-label={Locale.Auth.Input}
2023-11-08 01:34:31 +09:00
value={accessStore.accessCode}
2024-10-11 18:03:20 +09:00
type="text"
placeholder={Locale.Auth.Input}
2023-06-07 03:18:24 +09:00
onChange={(e) => {
2023-11-08 01:34:31 +09:00
accessStore.update(
(access) => (access.accessCode = e.currentTarget.value),
);
2023-06-07 03:18:24 +09:00
}}
/>
2024-10-11 18:03:20 +09:00
2024-05-20 20:02:46 +09:00
{!accessStore.hideUserApiKey ? (
<>
<div className={styles["auth-tips"]}>{Locale.Auth.SubTips}</div>
2024-10-11 19:36:11 +09:00
<PasswordInput
style={{ marginTop: "3vh", marginBottom: "3vh" }}
aria={Locale.Settings.ShowPassword}
aria-label={Locale.Settings.Access.OpenAI.ApiKey.Placeholder}
2024-05-20 20:02:46 +09:00
value={accessStore.openaiApiKey}
2024-10-11 19:36:11 +09:00
type="text"
placeholder={Locale.Settings.Access.OpenAI.ApiKey.Placeholder}
2024-05-20 20:02:46 +09:00
onChange={(e) => {
accessStore.update(
(access) => (access.openaiApiKey = e.currentTarget.value),
);
}}
/>
2024-10-11 19:36:11 +09:00
<PasswordInput
style={{ marginTop: "3vh", marginBottom: "3vh" }}
aria={Locale.Settings.ShowPassword}
aria-label={Locale.Settings.Access.Google.ApiKey.Placeholder}
2024-05-20 20:02:46 +09:00
value={accessStore.googleApiKey}
2024-10-11 19:36:11 +09:00
type="text"
placeholder={Locale.Settings.Access.Google.ApiKey.Placeholder}
2024-05-20 20:02:46 +09:00
onChange={(e) => {
accessStore.update(
(access) => (access.googleApiKey = e.currentTarget.value),
);
}}
/>
</>
) : null}
2023-06-07 03:18:24 +09:00
<div className={styles["auth-actions"]}>
<IconButton
text={Locale.Auth.Confirm}
type="primary"
onClick={goChat}
2023-06-07 03:18:24 +09:00
/>
<IconButton
2024-09-19 01:05:06 +09:00
text={Locale.Auth.SaasTips}
onClick={() => {
2024-09-19 01:05:06 +09:00
goSaas();
}}
/>
2023-06-07 03:18:24 +09:00
</div>
</div>
);
}
2024-09-19 17:58:51 +09:00
function TopBanner() {
const [isHovered, setIsHovered] = useState(false);
const [isVisible, setIsVisible] = useState(true);
2024-09-24 12:36:26 +09:00
const isMobile = useMobileScreen();
2024-09-19 17:58:51 +09:00
useEffect(() => {
// 检查 localStorage 中是否有标记
2024-09-19 19:21:26 +09:00
const bannerDismissed = storage.getItem("bannerDismissed");
2024-09-19 17:58:51 +09:00
// 如果标记不存在,存储默认值并显示横幅
if (!bannerDismissed) {
2024-09-19 19:21:26 +09:00
storage.setItem("bannerDismissed", "false");
2024-09-19 17:58:51 +09:00
setIsVisible(true); // 显示横幅
} else if (bannerDismissed === "true") {
// 如果标记为 "true",则隐藏横幅
setIsVisible(false);
}
2024-09-20 11:22:22 +09:00
}, []);
2024-09-19 17:58:51 +09:00
const handleMouseEnter = () => {
setIsHovered(true);
};
const handleMouseLeave = () => {
setIsHovered(false);
};
const handleClose = () => {
setIsVisible(false);
2024-09-19 19:21:26 +09:00
storage.setItem("bannerDismissed", "true");
2024-09-19 17:58:51 +09:00
};
if (!isVisible) {
return null;
}
return (
<div
className={styles["top-banner"]}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
2024-11-06 17:58:26 +09:00
<div className={clsx(styles["top-banner-inner"], "no-dark")}>
2024-09-23 16:12:45 +09:00
<Logo className={styles["top-banner-logo"]}></Logo>
2024-09-19 17:58:51 +09:00
<span>
{Locale.Auth.TopTips}
2024-09-25 14:08:03 +09:00
<a
href={SAAS_CHAT_URL}
rel="stylesheet"
onClick={() => {
trackSettingsPageGuideToCPaymentClick();
}}
>
2024-09-19 17:58:51 +09:00
{Locale.Settings.Access.SaasStart.ChatNow}
2024-09-23 16:12:45 +09:00
<Arrow style={{ marginLeft: "4px" }} />
2024-09-19 17:58:51 +09:00
</a>
</span>
</div>
2024-09-23 16:12:45 +09:00
{(isHovered || isMobile) && (
2024-09-19 17:58:51 +09:00
<Delete className={styles["top-banner-close"]} onClick={handleClose} />
)}
</div>
);
}