/** * Promise-based input dialogs for shell and feature actions (IDEA's * `Messages.showInputDialog` and a simple chooser): * * const name = await promptInput({ title: "Save Layout", label: "Name:" }); * const picked = await chooseItem({ title: "Delete Layout", items: names }); * * Rendered by `InputDialogsHost` (in `OverlayHost`). */ import { useEffect, useRef, useState, type ReactNode } from "react"; import { create } from "zustand"; import { registerHostPathChooser, type ChoosePathOptions } from "@/platform/choosePath"; import { t, tx, VerbatimTextContext } from "@/platform/i18n"; import { call, commands, type FileEntry } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { Checkbox, Dialog, Given, Icon, IconButton, Input, List, T } from "@/ui"; import styles from "./inputDialogs.module.css"; export interface PromptInputOptions { title: string; label?: string; initial?: string; placeholder?: string; okLabel?: string; /** An error message for `value`, or null when valid (default: must not be blank). */ validate?: (value: string) => string | null; /** Validate on submission without closing the dialog; cancelling aborts pending validation. */ submit?: (value: string, signal: AbortSignal) => Promise; /** Initial server validation error. */ error?: string; /** Extra content under the field (hints). */ hint?: ReactNode; /** Closes the dialog as cancelled when aborted. */ signal?: AbortSignal; /** Resolve with the text as typed instead of trimmed. */ keepWhitespace?: boolean; /** A multi-line field: Enter adds a line, Cmd/Ctrl+Enter confirms. */ multiline?: boolean; /** A password field (characters hidden). */ secret?: boolean; /** Title, text and labels come from outside the IDE (a plugin) and are shown as written. */ verbatim?: boolean; } export interface ChooseItemOptions { title: string; items: readonly string[]; /** Shown when `items` is empty. */ empty?: string; } export interface PickItemsOptions { title: string; items: readonly { key: string; label: string; description?: string | null }[]; multiple: boolean; /** Closes the dialog as cancelled when aborted. */ signal?: AbortSignal; /** Title, text and labels come from outside the IDE (a plugin) and are shown as written. */ verbatim?: boolean; } type Request = | { kind: "prompt"; id: number; options: PromptInputOptions; resolve: (v: string | null) => void } | { kind: "choose"; id: number; options: ChooseItemOptions; resolve: (v: string | null) => void } | { kind: "pick"; id: number; options: PickItemsOptions; resolve: (v: string[] | null) => void } | { kind: "path"; id: number; options: ChoosePathOptions; resolve: (v: string | null) => void }; const useInputDialogs = create<{ stack: Request[] }>(() => ({ stack: [] })); let seq = 0; function open( make: (id: number, finish: (v: T | null) => void) => Request, signal?: AbortSignal, ): Promise { return new Promise((resolve) => { if (signal?.aborted) return resolve(null); const id = ++seq; const finish = (v: T | null) => { signal?.removeEventListener("abort", cancel); useInputDialogs.setState((s) => ({ stack: s.stack.filter((r) => r.id !== id) })); resolve(v); }; const cancel = () => finish(null); signal?.addEventListener("abort", cancel); useInputDialogs.setState((s) => ({ stack: [...s.stack, make(id, finish as never)] })); }); } /** A text field dialog; resolves with the trimmed value, or null when cancelled. */ export function promptInput(options: PromptInputOptions): Promise { return open((id, resolve) => ({ kind: "prompt", id, options, resolve }), options.signal); } /** A list to pick one entry from; resolves with it, or null when cancelled. */ export function chooseItem(options: ChooseItemOptions): Promise { return open((id, resolve) => ({ kind: "choose", id, options, resolve })); } /** A list to pick one or more entries from; resolves with their keys, or null when cancelled. */ export function pickItems(options: PickItemsOptions): Promise { return open((id, resolve) => ({ kind: "pick", id, options, resolve }), options.signal); } /** A file or folder on the host this window's commands run on, browsed in the IDE (a remote window's chooser). */ export function choosePathOnHost(options: ChoosePathOptions): Promise { return open((id, resolve) => ({ kind: "path", id, options, resolve })); } registerHostPathChooser(choosePathOnHost); export function InputDialogsHost() { const stack = useInputDialogs((s) => s.stack); return ( <> {stack.map((r) => r.kind === "prompt" ? ( ) : r.kind === "choose" ? ( ) : r.kind === "path" ? ( ) : ( ), )} ); } const notBlank = (v: string) => (v.trim() ? null : "Value must not be empty"); function PromptDialog({ options, done }: { options: PromptInputOptions; done: (v: string | null) => void }) { const [value, setValue] = useState(options.initial ?? ""); const [touched, setTouched] = useState(false); const [serverError, setServerError] = useState(options.error ?? null); const [pending, setPending] = useState(false); const validation = useRef(null); useEffect(() => () => validation.current?.abort(), []); const localError = (options.validate ?? notBlank)(value); const error = localError ?? serverError; const close = () => { validation.current?.abort(); done(null); }; const submit = async () => { setTouched(true); if (localError || validation.current) return; const result = options.keepWhitespace ? value : value.trim(); if (!options.submit) return done(result); const controller = new AbortController(); validation.current = controller; setPending(true); try { const message = await options.submit(result, controller.signal); if (controller.signal.aborted) return; setServerError(message); if (!message) done(result); } catch (e) { if (!controller.signal.aborted) setServerError(e instanceof Error ? e.message : String(e)); } finally { if (!controller.signal.aborted) { validation.current = null; setPending(false); } } }; return ( void submit(), }, { label: t("common.cancel"), role: "cancel" }, ]} >
{ e.preventDefault(); void submit(); }} > {options.label && ( )} {options.multiline ? (