import { useState, useCallback, useEffect } from 'react' import { AlertDialog, AlertDialogContent, AlertDialogDescription, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/AlertDialog' import Button from '@/components/ui/Button' import Input from '@/components/ui/Input' import { useSettingsStore } from '@/stores/settings' import { useBackendState } from '@/stores/state' import { InvalidApiKeyError, RequireApiKeError } from '@/api/lightrag' interface ApiKeyAlertProps { open: boolean; onOpenChange: (open: boolean) => void; } const ApiKeyAlert = ({ open: opened, onOpenChange: setOpened }: ApiKeyAlertProps) => { const apiKey = useSettingsStore.use.apiKey() const [tempApiKey, setTempApiKey] = useState('') const message = useBackendState.use.message() useEffect(() => { setTempApiKey(apiKey || '') }, [apiKey, opened]) useEffect(() => { if (message) { if (message.includes(InvalidApiKeyError) || message.includes(RequireApiKeError)) { setOpened(true) } } }, [message, setOpened]) const setApiKey = useCallback(() => { useSettingsStore.setState({ apiKey: tempApiKey || null }) setOpened(false) }, [tempApiKey, setOpened]) const handleTempApiKeyChange = useCallback( (e: React.ChangeEvent) => { setTempApiKey(e.target.value) }, [setTempApiKey] ) return ( API Key is required Please enter your API key to access the service
e.preventDefault()}>
{message && (
{message}
)}
) } export default ApiKeyAlert