refactor: improve IntroDB API key verification and handle unmounted state

This commit is contained in:
paregi12 2026-02-01 22:46:33 +05:30
parent f8bac1aa56
commit 682d3f2eb3
2 changed files with 16 additions and 0 deletions

View file

@ -82,6 +82,15 @@ export const PlaybackSettingsContent: React.FC<PlaybackSettingsContentProps> = (
const [apiKeyInput, setApiKeyInput] = useState(settings?.introDbApiKey || '');
const [isVerifyingKey, setIsVerifyingKey] = useState(false);
const isMounted = useRef(true);
useEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);
useEffect(() => {
setApiKeyInput(settings?.introDbApiKey || '');
}, [settings?.introDbApiKey]);
@ -95,6 +104,8 @@ export const PlaybackSettingsContent: React.FC<PlaybackSettingsContentProps> = (
setIsVerifyingKey(true);
const isValid = await introService.verifyApiKey(apiKeyInput);
if (!isMounted.current) return;
setIsVerifyingKey(false);
if (isValid) {

View file

@ -209,6 +209,11 @@ export async function verifyApiKey(apiKey: string): Promise<boolean> {
// 200/201 would also mean valid (though unexpected with empty body)
if (response.status === 200 || response.status === 201) return true;
// Explicitly handle auth failures
if (response.status === 401 || response.status === 403) return false;
// Log warning for unexpected states (500, 429, etc.) but fail safe
logger.warn(`[IntroService] Verification received unexpected status: ${response.status}`);
return false;
} catch (error: any) {
logger.log('[IntroService] API Key verification failed:', error.message);