You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.8 KiB

11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
10 months ago
11 months ago
  1. import React, { useEffect, useState } from "react";
  2. import { X } from "@phosphor-icons/react";
  3. import BrowserExtensionApiKey from "@/models/browserExtensionApiKey";
  4. import { fullApiUrl, POPUP_BROWSER_EXTENSION_EVENT } from "@/utils/constants";
  5. export default function NewBrowserExtensionApiKeyModal({
  6. closeModal,
  7. onSuccess,
  8. isMultiUser,
  9. }) {
  10. const [apiKey, setApiKey] = useState(null);
  11. const [error, setError] = useState(null);
  12. const [copied, setCopied] = useState(false);
  13. const handleCreate = async (e) => {
  14. setError(null);
  15. e.preventDefault();
  16. const { apiKey: newApiKey, error } =
  17. await BrowserExtensionApiKey.generateKey();
  18. if (!!newApiKey) {
  19. const fullApiKey = `${fullApiUrl()}|${newApiKey}`;
  20. setApiKey(fullApiKey);
  21. onSuccess();
  22. window.postMessage(
  23. { type: POPUP_BROWSER_EXTENSION_EVENT, apiKey: fullApiKey },
  24. "*"
  25. );
  26. }
  27. setError(error);
  28. };
  29. const copyApiKey = () => {
  30. if (!apiKey) return false;
  31. window.navigator.clipboard.writeText(apiKey);
  32. setCopied(true);
  33. };
  34. useEffect(() => {
  35. function resetStatus() {
  36. if (!copied) return false;
  37. setTimeout(() => {
  38. setCopied(false);
  39. }, 3000);
  40. }
  41. resetStatus();
  42. }, [copied]);
  43. return (
  44. <div className="fixed inset-0 z-50 overflow-auto bg-black bg-opacity-50 flex items-center justify-center">
  45. <div className="relative w-full max-w-2xl bg-theme-bg-secondary rounded-lg shadow border-2 border-theme-modal-border">
  46. <div className="relative p-6 border-b rounded-t border-theme-modal-border">
  47. <div className="w-full flex gap-x-2 items-center">
  48. <h3 className="text-xl font-semibold text-white overflow-hidden overflow-ellipsis whitespace-nowrap">
  49. 新的浏览器扩展API密钥
  50. </h3>
  51. </div>
  52. <button
  53. onClick={closeModal}
  54. type="button"
  55. className="absolute top-4 right-4 transition-all duration-300 bg-transparent rounded-lg text-sm p-1 inline-flex items-center hover:bg-theme-modal-border hover:border-theme-modal-border hover:border-opacity-50 border-transparent border"
  56. >
  57. <X size={24} weight="bold" className="text-white" />
  58. </button>
  59. </div>
  60. <div className="px-7 py-6">
  61. <form onSubmit={handleCreate}>
  62. <div className="space-y-6 max-h-[60vh] overflow-y-auto pr-2">
  63. {error && <p className="text-red-400 text-sm">Error: {error}</p>}
  64. {apiKey && (
  65. <input
  66. type="text"
  67. defaultValue={apiKey}
  68. disabled={true}
  69. className="border-none bg-theme-settings-input-bg w-full text-white placeholder:text-theme-settings-input-placeholder text-sm rounded-lg block w-full p-2.5"
  70. />
  71. )}
  72. {isMultiUser && (
  73. <p className="text-yellow-300 light:text-orange-500 text-xs md:text-sm font-semibold">
  74. 警告当前处于多用户模式此API密钥可访问与您帐户关联的所有文件分析区请谨慎分享以防泄露
  75. </p>
  76. )}
  77. <p className="text-white text-opacity-60 text-xs md:text-sm">
  78. 点击创建API密钥琛海AI将尝试自动连接到您的浏览器扩展
  79. </p>
  80. <p className="text-white text-opacity-60 text-xs md:text-sm">
  81. 如果在扩展中显示链接琛海AI则表示连接已成功建立如果未显示请复制连接字符串并手动粘贴到扩展中以完成连接
  82. </p>
  83. </div>
  84. <div className="flex justify-between items-center mt-6 pt-6 border-t border-theme-modal-border">
  85. {!apiKey ? (
  86. <>
  87. <button
  88. onClick={closeModal}
  89. type="button"
  90. className="transition-all duration-300 text-white hover:bg-zinc-700 px-4 py-2 rounded-lg text-sm"
  91. >
  92. Cancel
  93. </button>
  94. <button
  95. type="submit"
  96. className="transition-all duration-300 bg-white text-black hover:opacity-60 px-4 py-2 rounded-lg text-sm"
  97. >
  98. Create API Key
  99. </button>
  100. </>
  101. ) : (
  102. <button
  103. onClick={copyApiKey}
  104. type="button"
  105. disabled={copied}
  106. className="w-full transition-all duration-300 bg-white text-black hover:opacity-60 px-4 py-2 rounded-lg text-sm cursor-pointer"
  107. >
  108. {copied ? "API Key Copied!" : "Copy API Key"}
  109. </button>
  110. )}
  111. </div>
  112. </form>
  113. </div>
  114. </div>
  115. </div>
  116. );
  117. }