From f40a689dbf354f4a9c46155bcabf43c724d7cd9e Mon Sep 17 00:00:00 2001 From: Athena <125889247+AthanaD@users.noreply.github.com> Date: Mon, 1 Jan 2024 23:04:08 +0800 Subject: [PATCH] Update ApiMenu.tsx --- src/components/ApiMenu/ApiMenu.tsx | 46 +++++++++++++++++++----------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/components/ApiMenu/ApiMenu.tsx b/src/components/ApiMenu/ApiMenu.tsx index 02860d6..d8f7bdf 100644 --- a/src/components/ApiMenu/ApiMenu.tsx +++ b/src/components/ApiMenu/ApiMenu.tsx @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import { useTranslation, Trans } from 'react-i18next'; // 确保从'react-i18next'导入了Trans +import { useTranslation, Trans } from 'react-i18next'; import useStore from '@store/store'; import useHideOnOutsideClick from '@hooks/useHideOnOutsideClick'; import PopupModal from '@components/PopupModal'; @@ -9,16 +9,12 @@ import DownChevronArrow from '@icon/DownChevronArrow'; const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch>; }) => { const { t } = useTranslation(['main', 'api']); - const apiKey = useStore((state) => state.apiKey); const setApiKey = useStore((state) => state.setApiKey); - const apiEndpoint = useStore((state) => state.apiEndpoint); const setApiEndpoint = useStore((state) => state.setApiEndpoint); - const [_apiKey, _setApiKey] = useState(apiKey || ''); - const [_apiEndpoint, _setApiEndpoint] = useState(apiEndpoint); - const [_customEndpoint, _setCustomEndpoint] = useState( - !availableEndpoints.includes(apiEndpoint) - ); + const [_apiEndpoint, _setApiEndpoint] = useState(defaultAPIEndpoint); + const [_apiKey, _setApiKey] = useState(''); + const [_customEndpoint, _setCustomEndpoint] = useState(false); const handleSave = () => { setApiKey(_apiKey); @@ -27,12 +23,28 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch { - if (_customEndpoint) _setApiEndpoint(defaultAPIEndpoint); - else _setApiEndpoint(''); + if (_customEndpoint) { + _setApiEndpoint(defaultAPIEndpoint); + _setApiKey(import.meta.env.VITE_DEFAULT_API_KEY); + } else { + _setApiEndpoint(''); + _setApiKey(''); + } _setCustomEndpoint(prev => !prev); }; - const preventCopy = (e: React.ClipboardEvent) => { // 明确指定e的类型 + const handleApiEndpointChange = (newEndpoint: string) => { + _setApiEndpoint(newEndpoint); + if (newEndpoint === import.meta.env.VITE_OFFICIAL_API_ENDPOINT) { + _setApiKey(import.meta.env.VITE_OFFICIAL_API_KEY); + } else if (newEndpoint === import.meta.env.VITE_CUSTOM_API_ENDPOINT) { + _setApiKey(import.meta.env.VITE_CUSTOM_API_KEY); + } else { + _setApiKey(''); + } + }; + + const preventCopy = (e: React.ClipboardEvent) => { e.preventDefault(); alert("Copying API key is not allowed."); }; @@ -63,12 +75,12 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch _setApiEndpoint(e.target.value)} + onChange={(e) => handleApiEndpointChange(e.target.value)} /> ) : ( )} @@ -112,10 +124,10 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch>; + handleApiEndpointChange: (endpoint: string) => void; }) => { const [dropDown, setDropDown, dropDownRef] = useHideOnOutsideClick(); @@ -143,7 +155,7 @@ const ApiEndpointSelector = ({
  • { - _setApiEndpoint(endpoint); + handleApiEndpointChange(endpoint); setDropDown(false); }} key={endpoint} @@ -158,3 +170,5 @@ const ApiEndpointSelector = ({ }; export default ApiMenu; + +