auth.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import styles from "./auth.module.scss";
  2. import { IconButton } from "./button";
  3. import { useState, useEffect } from "react";
  4. import { useNavigate } from "react-router-dom";
  5. import { Path, SAAS_CHAT_URL } from "../constant";
  6. import { useAccessStore } from "../store";
  7. import Locale from "../locales";
  8. import Delete from "../icons/close.svg";
  9. import Arrow from "../icons/arrow.svg";
  10. import Logo from "../icons/logo.svg";
  11. import BotIcon from "../icons/bot.svg";
  12. import { getClientConfig } from "../config/client";
  13. import LeftIcon from "@/app/icons/left.svg";
  14. import { safeLocalStorage } from "@/app/utils";
  15. export function AuthPage() {
  16. const navigate = useNavigate();
  17. const accessStore = useAccessStore();
  18. const goHome = () => navigate(Path.Home);
  19. const goChat = () => navigate(Path.Chat);
  20. const goSaas = () => {
  21. window.location.href = SAAS_CHAT_URL;
  22. };
  23. const resetAccessCode = () => {
  24. accessStore.update((access) => {
  25. access.openaiApiKey = "";
  26. access.accessCode = "";
  27. });
  28. }; // Reset access code to empty string
  29. useEffect(() => {
  30. if (getClientConfig()?.isApp) {
  31. navigate(Path.Settings);
  32. }
  33. // eslint-disable-next-line react-hooks/exhaustive-deps
  34. }, []);
  35. return (
  36. <div className={styles["auth-page"]}>
  37. <TopBanner></TopBanner>
  38. <div className={styles["auth-header"]}>
  39. <IconButton
  40. icon={<LeftIcon />}
  41. text={Locale.Auth.Return}
  42. onClick={() => navigate(Path.Home)}
  43. ></IconButton>
  44. </div>
  45. <div className={`no-dark ${styles["auth-logo"]}`}>
  46. <BotIcon />
  47. </div>
  48. <div className={styles["auth-title"]}>{Locale.Auth.Title}</div>
  49. <div className={styles["auth-tips"]}>{Locale.Auth.Tips}</div>
  50. <input
  51. className={styles["auth-input"]}
  52. type="password"
  53. placeholder={Locale.Auth.Input}
  54. value={accessStore.accessCode}
  55. onChange={(e) => {
  56. accessStore.update(
  57. (access) => (access.accessCode = e.currentTarget.value),
  58. );
  59. }}
  60. />
  61. {!accessStore.hideUserApiKey ? (
  62. <>
  63. <div className={styles["auth-tips"]}>{Locale.Auth.SubTips}</div>
  64. <input
  65. className={styles["auth-input"]}
  66. type="password"
  67. placeholder={Locale.Settings.Access.OpenAI.ApiKey.Placeholder}
  68. value={accessStore.openaiApiKey}
  69. onChange={(e) => {
  70. accessStore.update(
  71. (access) => (access.openaiApiKey = e.currentTarget.value),
  72. );
  73. }}
  74. />
  75. <input
  76. className={styles["auth-input-second"]}
  77. type="password"
  78. placeholder={Locale.Settings.Access.Google.ApiKey.Placeholder}
  79. value={accessStore.googleApiKey}
  80. onChange={(e) => {
  81. accessStore.update(
  82. (access) => (access.googleApiKey = e.currentTarget.value),
  83. );
  84. }}
  85. />
  86. </>
  87. ) : null}
  88. <div className={styles["auth-actions"]}>
  89. <IconButton
  90. text={Locale.Auth.Confirm}
  91. type="primary"
  92. onClick={goChat}
  93. />
  94. <IconButton
  95. text={Locale.Auth.SaasTips}
  96. onClick={() => {
  97. goSaas();
  98. }}
  99. />
  100. </div>
  101. </div>
  102. );
  103. }
  104. function TopBanner() {
  105. const [isHovered, setIsHovered] = useState(false);
  106. const [isVisible, setIsVisible] = useState(true);
  107. const storage = safeLocalStorage();
  108. useEffect(() => {
  109. // 检查 localStorage 中是否有标记
  110. const bannerDismissed = storage.getItem("bannerDismissed");
  111. // 如果标记不存在,存储默认值并显示横幅
  112. if (!bannerDismissed) {
  113. storage.setItem("bannerDismissed", "false");
  114. setIsVisible(true); // 显示横幅
  115. } else if (bannerDismissed === "true") {
  116. // 如果标记为 "true",则隐藏横幅
  117. setIsVisible(false);
  118. }
  119. }, [storage]);
  120. const handleMouseEnter = () => {
  121. setIsHovered(true);
  122. };
  123. const handleMouseLeave = () => {
  124. setIsHovered(false);
  125. };
  126. const handleClose = () => {
  127. setIsVisible(false);
  128. storage.setItem("bannerDismissed", "true");
  129. };
  130. if (!isVisible) {
  131. return null;
  132. }
  133. return (
  134. <div
  135. className={styles["top-banner"]}
  136. onMouseEnter={handleMouseEnter}
  137. onMouseLeave={handleMouseLeave}
  138. >
  139. <div className={styles["top-banner-inner"]}>
  140. <span>
  141. <Logo></Logo>
  142. {Locale.Auth.TopTips}
  143. <a href={SAAS_CHAT_URL} rel="stylesheet">
  144. {Locale.Settings.Access.SaasStart.ChatNow}
  145. <Arrow style={{ marginLeft: "8px" }} />
  146. </a>
  147. </span>
  148. </div>
  149. {isHovered && (
  150. <Delete className={styles["top-banner-close"]} onClick={handleClose} />
  151. )}
  152. </div>
  153. );
  154. }