* bug fix #21

* changes

* update setApiFree function in useEffect
This commit is contained in:
Mark Anthony Llego 2023-03-06 22:06:48 +08:00 committed by GitHub
parent db81221e2d
commit 948c14a6eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -16,17 +16,19 @@ const ApiMenu = ({
const apiFree = useStore((state) => state.apiFree); const apiFree = useStore((state) => state.apiFree);
const setApiFree = useStore((state) => state.setApiFree); const setApiFree = useStore((state) => state.setApiFree);
const [_apiFree, _setApiFree] = useState<boolean>(apiFree);
const [_apiKey, _setApiKey] = useState<string>(apiKey || ''); const [_apiKey, _setApiKey] = useState<string>(apiKey || '');
const [error, setError] = useState<boolean>(false); const [error, setError] = useState<boolean>(false);
const handleSave = async () => { const handleSave = async () => {
if (apiFree === true) { if (_apiFree === true) {
setApiFree(true);
setIsModalOpen(false); setIsModalOpen(false);
} else { } else {
const valid = await validateApiKey(_apiKey); const valid = await validateApiKey(_apiKey);
if (valid) { if (valid) {
setApiKey(_apiKey); setApiKey(_apiKey);
setApiFree(false);
setError(false); setError(false);
setIsModalOpen(false); setIsModalOpen(false);
} else { } else {
@ -40,9 +42,10 @@ const ApiMenu = ({
useEffect(() => { useEffect(() => {
if (apiKey) { if (apiKey) {
setApiFree(false); setApiFree(false);
_setApiKey(apiKey); _setApiFree(false);
} _setApiKey(apiKey);
}
}, []); }, []);
return isModalOpen ? ( return isModalOpen ? (
@ -55,9 +58,9 @@ const ApiMenu = ({
<div className='flex items-center mb-2'> <div className='flex items-center mb-2'>
<input <input
type='radio' type='radio'
checked={apiFree === true} checked={_apiFree === true}
className='w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600' className='w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600'
onChange={() => setApiFree(true)} onChange={() => _setApiFree(true)}
/> />
<label className='ml-2 text-sm font-medium text-gray-900 dark:text-gray-300'> <label className='ml-2 text-sm font-medium text-gray-900 dark:text-gray-300'>
Use free API from{' '} Use free API from{' '}
@ -70,20 +73,19 @@ const ApiMenu = ({
</a> </a>
</label> </label>
</div> </div>
<div className='flex items-center'> <div className='flex items-center'>
<input <input
type='radio' type='radio'
checked={apiFree === false} checked={_apiFree === false}
className='w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600' className='w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600'
onChange={() => setApiFree(false)} onChange={() => _setApiFree(false)}
/> />
<label className='ml-2 text-sm font-medium text-gray-900 dark:text-gray-300'> <label className='ml-2 text-sm font-medium text-gray-900 dark:text-gray-300'>
Use your own API key Use your own API key
</label> </label>
</div> </div>
{apiFree === false && ( {_apiFree === false && (
<> <>
<div className='flex gap-2 items-center justify-center mt-2'> <div className='flex gap-2 items-center justify-center mt-2'>
<div className='min-w-fit text-gray-900 dark:text-gray-300 text-sm'> <div className='min-w-fit text-gray-900 dark:text-gray-300 text-sm'>
@ -91,7 +93,7 @@ const ApiMenu = ({
</div> </div>
<input <input
type='text' type='text'
className='text-gray-800 dark:text-white p-3 text-sm border-none bg-gray-200 dark:bg-gray-600 rounded-md p-0 m-0 w-full mr-0 h-8 focus:outline-none' className='text-gray-800 dark:text-white p-3 text-sm border-none bg-gray-200 dark:bg-gray-600 rounded-md m-0 w-full mr-0 h-8 focus:outline-none'
value={_apiKey} value={_apiKey}
onChange={(e) => { onChange={(e) => {
_setApiKey(e.target.value); _setApiKey(e.target.value);
@ -100,7 +102,8 @@ const ApiMenu = ({
</div> </div>
{error && ( {error && (
<div className='bg-red-600/50 p-2 rounded-sm mt-3 text-gray-900 dark:text-gray-300 text-sm'> <div className='bg-red-600/50 p-2 rounded-sm mt-3 text-gray-900 dark:text-gray-300 text-sm'>
Error: Invalid API key or network blocked. Please check your API key and network settings for OpenAI API. Error: Invalid API key or network blocked. Please check your API
key and network settings for OpenAI API.
</div> </div>
)} )}
</> </>