auth.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. 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. }, []);
  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. }