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 { useKeyboard } from "@opentui/solid" import { Locale } from "@/util/locale" export type DialogConfirmProps = { title: string message: string onConfirm?: () => void onCancel?: () => void } export function DialogConfirm(props: DialogConfirmProps) { const dialog = useDialog() const { theme } = useTheme() const [store, setStore] = createStore({ active: "confirm" as "confirm" | "cancel", }) useKeyboard((evt) => { if (evt.name === "return") { if (store.active === "confirm") props.onConfirm?.() if (store.active === "cancel") props.onCancel?.() dialog.clear() } if (evt.name === "left" || evt.name === "right") { setStore("active", store.active === "confirm" ? "cancel" : "confirm") } }) return ( {props.title} esc {props.message} {(key) => ( { if (key === "confirm") props.onConfirm?.() if (key === "cancel") props.onCancel?.() dialog.clear() }} > {Locale.titlecase(key)} )} ) } DialogConfirm.show = (dialog: DialogContext, title: string, message: string) => { return new Promise((resolve) => { dialog.replace( () => ( resolve(true)} onCancel={() => resolve(false)} /> ), () => resolve(false), ) }) }