({ modalOpen, setModalOpen })
| 4 | import PropTypes from 'prop-types'; |
| 5 | |
| 6 | const Setting = ({ modalOpen, setModalOpen }) => { |
| 7 | const apiKey = window.localStorage.getItem('api-key') || ''; |
| 8 | const [loading, setLoading] = useState(false); |
| 9 | const [errorMsg, setErrorMsg] = useState(''); |
| 10 | const [input, setInput] = useState(''); |
| 11 | |
| 12 | const saveKey = async (e) => { |
| 13 | e.preventDefault(); |
| 14 | setLoading(true); |
| 15 | setErrorMsg(''); |
| 16 | const keys = input; |
| 17 | |
| 18 | await checkApiKey(keys) |
| 19 | .then(() => { |
| 20 | window.localStorage.setItem('api-key', keys); |
| 21 | console.log('works'); |
| 22 | setModalOpen(false); |
| 23 | }) |
| 24 | .catch(() => { |
| 25 | console.log('doesnt work'); |
| 26 | setErrorMsg('error: incorrect keys'); |
| 27 | }); |
| 28 | |
| 29 | setLoading(false); |
| 30 | }; |
| 31 | |
| 32 | const removeApiKey = () => { |
| 33 | window.localStorage.removeItem('api-key'); |
| 34 | setInput(''); |
| 35 | }; |
| 36 | |
| 37 | useEffect(() => { |
| 38 | if (modalOpen) { |
| 39 | setInput(apiKey); |
| 40 | } |
| 41 | }, [apiKey, modalOpen]); |
| 42 | |
| 43 | return ( |
| 44 | <form |
| 45 | onSubmit={saveKey} |
| 46 | className='flex flex-col items-center justify-center gap-2'> |
| 47 | <p className='text-lg font-semibold'>Use your own API-key.</p> |
| 48 | <p>keys are saved in your own browser</p> |
| 49 | <p className='italic'> |
| 50 | Get OpenAI API key{' '} |
| 51 | <a |
| 52 | className='text-blue-600' |
| 53 | rel='noreferrer' |
| 54 | target='_blank' |
| 55 | href='https://platform.openai.com/account/api-keys'> |
| 56 | here |
| 57 | </a> |
| 58 | . |
| 59 | </p> |
| 60 | <input |
| 61 | value={input} |
| 62 | onChange={(e) => setInput(e.target.value)} |
| 63 | type='password' |
nothing calls this directly
no outgoing calls
no test coverage detected