import { useEffect, useRef, useState } from "preact/hooks"; import { MiniEditor } from "./mini_editor.tsx"; import type { ComponentChildren, Ref } from "preact"; export function Prompt({ message, defaultValue, darkMode, callback, }: { message: string; defaultValue?: string; darkMode: boolean | undefined; callback: (value?: string) => void; }) { const [text, setText] = useState(defaultValue || ""); const returnEl = ( { callback(); }} > {message} { callback(text); return true; }} onEscape={() => { callback(); }} onChange={(text) => { setText(text); }} editable={true} /> { callback(text); }} > Ok { callback(); }} > Cancel ); return returnEl; } export function Confirm({ message, callback, }: { message: string; callback: (value: boolean) => void; }) { const okButtonRef = useRef(null); setTimeout(() => { okButtonRef.current?.focus(); }); const returnEl = ( { callback(false); }} > {message} { callback(true); }} > Ok { callback(false); }} > Cancel ); return returnEl; } export function Button({ children, primary, onActivate, buttonRef, }: { children: ComponentChildren; primary?: boolean; onActivate: () => void; buttonRef?: Ref; }) { return ( { e.stopPropagation(); e.preventDefault(); onActivate(); }} onKeyDown={(e) => { if (e.key === "Enter") { e.stopPropagation(); e.preventDefault(); onActivate(); } }} > {children} ); } export function AlwaysShownModal({ children, onCancel, }: { children: ComponentChildren; onCancel?: () => void; }) { const dialogRef = useRef(null); useEffect(() => { dialogRef.current?.showModal(); }, []); return ( { e.preventDefault(); onCancel?.(); }} onKeyDown={(e) => { e.stopPropagation(); }} ref={dialogRef} > {children} ); }