NextChat-U/app/components/auth.tsx

113 lines
3.5 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";
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 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 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>
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
/>
2024-09-19 17:58:51 +09:00
</div>
</div>
);
}