import React, { useCallback, useEffect, useMemo, useState } from 'react'; import type { AuthenticationChallenge } from '@netcatty/plugin-contract'; import { ExternalLink } from 'lucide-react'; import { useI18n } from '../../application/i18n/I18nProvider'; import { pluginExtensionBridge } from '../../application/state/pluginExtensionBridge'; import { isSafePluginAuthenticationUrl } from '../../domain/pluginConnection'; import { Button } from '../ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '../ui/dialog'; import { Input } from '../ui/input'; import { Label } from '../ui/label'; type ChallengeEvent = NetcattyPluginAuthenticationChallengeEvent; type ChallengeResponse = string | boolean | ReadonlyArray; const challengeMessage = (challenge: AuthenticationChallenge): string | undefined => ( 'message' in challenge && typeof challenge.message === 'string' ? challenge.message : undefined ); export const PluginAuthenticationHost: React.FC = () => { const { t } = useI18n(); const [queue, setQueue] = useState([]); const [textValue, setTextValue] = useState(''); const [selectedChoices, setSelectedChoices] = useState([]); const [busy, setBusy] = useState(false); const current = queue[0]; const challenge = current?.challenge; useEffect(() => { return pluginExtensionBridge.onAuthenticationChallenge((event) => { setQueue((existing) => { if (existing.some((item) => item.challengeRequestId === event.challengeRequestId)) return existing; if (existing.length >= 32) { void pluginExtensionBridge.respondAuthenticationChallenge({ requestId: event.requestId, challengeRequestId: event.challengeRequestId, challengeId: event.challenge.id, cancelled: true, }).catch(() => {}); return existing; } return [...existing, event].slice(0, 32); }); }); }, []); useEffect(() => { setTextValue(''); setSelectedChoices([]); setBusy(false); }, [current?.challengeRequestId]); const complete = useCallback(async (response?: ChallengeResponse, cancelled = false) => { if (!current || busy) return; setBusy(true); try { await pluginExtensionBridge.respondAuthenticationChallenge({ requestId: current.requestId, challengeRequestId: current.challengeRequestId, challengeId: current.challenge.id, ...(cancelled ? { cancelled: true } : { response }), }); setQueue((existing) => existing.filter((item) => item.challengeRequestId !== current.challengeRequestId)); } catch { setQueue((existing) => existing.filter((item) => item.challengeRequestId !== current.challengeRequestId)); } finally { setBusy(false); } }, [busy, current]); const externalUrl = useMemo(() => { if (!challenge) return null; const value = challenge.kind === 'browser' ? challenge.url : challenge.kind === 'deviceCode' ? challenge.verificationUri : null; return value && isSafePluginAuthenticationUrl(value) ? value : null; }, [challenge]); const openExternal = useCallback(async () => { if (!externalUrl) return; await pluginExtensionBridge.openExternal(externalUrl); }, [externalUrl]); if (!challenge) return null; const message = challengeMessage(challenge); const isText = challenge.kind === 'text' || challenge.kind === 'password' || challenge.kind === 'otp'; const canSubmit = challenge.kind === 'choice' ? selectedChoices.length > 0 : isText ? textValue.length > 0 : challenge.kind === 'browser' || challenge.kind === 'deviceCode' ? externalUrl !== null : true; const submit = () => { if (isText) void complete(textValue); else if (challenge.kind === 'choice') { void complete(challenge.multiple ? selectedChoices : selectedChoices[0]); } else { void complete(true); } }; return ( { if (!open) void complete(undefined, true); }}> {challenge.title} {message || t('plugins.authentication.description')} {isText && (
setTextValue(event.target.value)} onKeyDown={(event) => { if (event.key === 'Enter' && canSubmit) submit(); }} autoFocus />
)} {challenge.kind === 'choice' && (
{challenge.choices.map((choice) => { const selected = selectedChoices.includes(choice.id); return ( ); })}
)} {(challenge.kind === 'browser' || challenge.kind === 'deviceCode') && (
{challenge.kind === 'deviceCode' && (
{t('plugins.authentication.deviceCode')}
{challenge.userCode}
)} {externalUrl ? ( ) : (

{t('plugins.authentication.invalidUrl')}

)}
)} {challenge.kind === 'confirmation' ? ( ) : ( )}
); };