import { useEffect, useRef, useState } from "react"; import { Checkbox, Dialog, Icon, Input, Select, Spinner, T } from "@/ui"; import { useTx } from "@/platform/i18n"; import { extractInterface, interfaceProblem, memberLabel, useExtractInterfaceDialog, type InterfaceRequest, } from "./extractInterface"; import styles from "./ExtractInterfaceDialog.module.css"; export function ExtractInterfaceDialog() { const request = useExtractInterfaceDialog((s) => s.request); if (!request) return null; return ; } function ExtractInterfaceDialogInner({ request }: { request: InterfaceRequest }) { const tx = useTx(); const { info, packages } = request; const [name, setName] = useState(""); const [pkg, setPkg] = useState(() => String( Math.max( 0, packages.findIndex((p) => p.current), ), ), ); const [checked, setChecked] = useState>(new Set()); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const trimmed = name.trim(); const problem = interfaceProblem(trimmed, info.className, checked.size); const target = packages[Number(pkg)]; const ready = !busy && !problem && !!target; const allChecked = checked.size === info.members.length; // Closing the dialog cancels a refactoring still being checked: it applies nothing. const running = useRef(null); useEffect(() => () => running.current?.abort(), []); const close = () => { running.current?.abort(); useExtractInterfaceDialog.setState({ request: null }); request.view.focus(); }; const toggle = (id: string, on: boolean) => setChecked((all) => { const next = new Set(all); if (on) next.add(id); else next.delete(id); return next; }); const submit = async (preview: boolean) => { if (!ready) return; const run = (running.current = new AbortController()); setBusy(true); setError(null); const members = info.members.map((m) => m.handleIdentifier).filter((id) => checked.has(id)); const outcome = await extractInterface(request, trimmed, target, members, preview, run.signal); if (useExtractInterfaceDialog.getState().request !== request) return; setBusy(false); if (outcome.status === "done") close(); else if (outcome.error) setError(outcome.error); }; return ( void submit(true) }, { label: "Refactor", role: "default", disabled: !ready, onClick: () => void submit(false) }, { label: "Cancel", role: "cancel" }, ]} footerStart={ busy ? ( Checking for conflicts… ) : undefined } >