auth.tsx 5.1 KB

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