"use client"; import { type FormEvent, type RefObject, useEffect, useRef } from "react"; import { Button } from "@/components/shadcn/button/button"; import { DialogFooter } from "@/components/shadcn/dialog"; import { Input } from "@/components/shadcn/input/input"; import { Modal } from "@/components/shadcn/modal/modal"; interface RegistryAccessDialogCommonProps { errorMessage?: string; registryUrl?: string; onOpenChange: (open: boolean) => void; onSubmit: (key: string) => Promise; open: boolean; pending: boolean; returnFocusRef: RefObject; } type ConnectRegistryAccessDialogProps = RegistryAccessDialogCommonProps & { mode: "connect"; onDisconnect?: never; }; type ManageRegistryAccessDialogProps = RegistryAccessDialogCommonProps & { mode: "manage"; onDisconnect: () => Promise; }; type RegistryAccessDialogProps = | ConnectRegistryAccessDialogProps | ManageRegistryAccessDialogProps; export function RegistryAccessDialog({ errorMessage, registryUrl, mode, onDisconnect, onOpenChange, onSubmit, open, pending, returnFocusRef, }: RegistryAccessDialogProps) { const formRef = useRef(null); const keyInputRef = useRef(null); const wasPendingRef = useRef(pending); const actionLabel = mode === "connect" ? "Connect" : "Replace key"; // Re-enabling the form after a watched validation settles loses focus from // the disabled input; hand it back so a retry can start from the keyboard. useEffect(() => { if (wasPendingRef.current && !pending) keyInputRef.current?.focus(); wasPendingRef.current = pending; }, [pending]); async function handleSubmit(event: FormEvent) { event.preventDefault(); if (pending) return; const key = new FormData(event.currentTarget).get("registry-key"); if (typeof key !== "string" || key.trim().length === 0) return; formRef.current?.reset(); await onSubmit(key.trim()); } return ( { event.preventDefault(); keyInputRef.current?.focus(); }} onCloseAutoFocus={(event) => { event.preventDefault(); returnFocusRef.current?.focus(); }} onOpenChange={onOpenChange} open={open} size="md" title={ mode === "connect" ? "Connect Registry API key" : "Manage Registry access" } >
{errorMessage && ( )} {registryUrl && ( )}
{mode === "manage" && ( )}
); }