import React, { useRef, useState } from 'react'; import { useI18n } from '../../../application/i18n/I18nProvider'; import { Button } from '../../ui/button'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '../../ui/dialog'; /** Uses the existing renderer and host bridge; opening the manager never starts a plugin. */ export function PluginManagementDialog({ children, trigger, open: controlledOpen, onOpenChange, restoreFocusTo, fallbackFocusTo }: { trigger?: React.ReactNode | null; open?: boolean; onOpenChange?: (open: boolean) => void; restoreFocusTo?: React.RefObject; fallbackFocusTo?: React.RefObject; children: React.ReactNode | ((handoff: (action: () => void) => void) => React.ReactNode); }) { const { t } = useI18n(); const [internalOpen, setInternalOpen] = useState(false); const open = controlledOpen ?? internalOpen; const setOpen = (value: boolean) => { setInternalOpen(value); onOpenChange?.(value); }; const pendingHandoff = useRef<(() => void) | null>(null); const handoff = (action: () => void) => { pendingHandoff.current = action; setOpen(false); }; return ( {trigger !== null && {trigger ?? } } { const action = pendingHandoff.current; pendingHandoff.current = null; if (action) { event.preventDefault(); // Wait for Radix to release modal pointer/focus ownership before opening // the existing external plugin view host (including exit animations). queueMicrotask(action); } else { const previous = restoreFocusTo?.current; const target = previous?.isConnected && !previous.matches(':disabled') ? previous : fallbackFocusTo?.current; if (target?.isConnected) { event.preventDefault(); target.focus({ preventScroll: true }); } } }} className="flex h-[min(88dvh,900px)] w-[calc(100vw-2rem)] max-w-[1180px] flex-col gap-0 overflow-hidden p-0"> {t('settings.plugins.title')}
{typeof children === 'function' ? children(handoff) : children}
); }