import { TextAttributes } from "@opentui/core" import { useTheme } from "../context/theme" import { useDialog, type DialogContext } from "./dialog" import { createStore } from "solid-js/store" import { For } from "solid-js" import { Locale } from "../util/locale" import { useBindings } from "../keymap" export type DialogConfirmProps = { title: string message: string onConfirm?: () => void onCancel?: () => void label?: string } export type DialogConfirmResult = boolean | undefined export function DialogConfirm(props: DialogConfirmProps) { const dialog = useDialog() const { theme } = useTheme() const [store, setStore] = createStore({ active: "confirm" as "confirm" | "cancel", }) useBindings(() => ({ bindings: [ { key: "return", desc: "Confirm dialog selection", group: "Dialog", cmd: () => { if (store.active === "confirm") props.onConfirm?.() if (store.active === "cancel") props.onCancel?.() dialog.clear() }, }, { key: "left", desc: "Previous dialog option", group: "Dialog", cmd: () => { setStore("active", store.active === "confirm" ? "cancel" : "confirm") }, }, { key: "right", desc: "Next dialog option", group: "Dialog", cmd: () => { setStore("active", store.active === "confirm" ? "cancel" : "confirm") }, }, ], })) return ( {props.title} dialog.clear()}> esc {props.message} {(key) => ( { if (key === "confirm") props.onConfirm?.() if (key === "cancel") props.onCancel?.() dialog.clear() }} > {Locale.titlecase(key === "cancel" ? (props.label ?? key) : key)} )} ) } DialogConfirm.show = (dialog: DialogContext, title: string, message: string, label?: string) => { return new Promise((resolve) => { dialog.replace( () => ( resolve(true)} onCancel={() => resolve(false)} label={label} /> ), () => resolve(undefined), ) }) }