import { Box, Text, Textarea } from "../ui";
import { useEffect, useRef, useState } from "react";
import { type InputRenderable, type TextareaRenderable } from "../ui";
import { type AlertContext, type PromptContext, useDialogKeyboard } from "../ui/dialog";
import type { WizardStep } from "../types/plugin";
import { colors } from "../theme/colors";
import { Button, DialogFrame, ListView, TextField } from "./ui";
import { listCursorMove } from "./ui/list-view";
import { t } from "../i18n";
export function PaneTemplateInfoStep({
dismiss,
dialogId,
step,
}: AlertContext & { step: WizardStep }) {
useDialogKeyboard((event) => {
event.stopPropagation();
if (event.name === "return" || event.name === "enter" || event.name === "escape") {
dismiss();
}
}, dialogId);
return (
{step.body?.map((line, index) => (
{line ? t(line) : " "}
))}
);
}
export function PaneTemplateInputStep({
resolve,
step,
}: PromptContext & { step: WizardStep }) {
const inputRef = useRef(null);
const [value, setValue] = useState(step.defaultValue ?? "");
useEffect(() => {
inputRef.current?.focus?.();
}, []);
return (
{step.body?.map((line, index) => (
{line ? t(line) : " "}
))}
{
const submitted = submittedValue.trim() || step.defaultValue || "";
if (submitted || step.required === false) resolve(submitted);
}}
/>
);
}
export function PaneTemplateTextareaStep({
resolve,
step,
dialogId,
}: PromptContext & { step: WizardStep }) {
const textareaRef = useRef(null);
useEffect(() => {
textareaRef.current?.focus?.();
}, []);
useDialogKeyboard((event) => {
event.stopPropagation();
if (event.name === "escape") {
resolve("");
return;
}
if (event.ctrl && event.name === "s") {
const submitted = textareaRef.current?.editBuffer.getText().trim() || step.defaultValue || "";
if (submitted) resolve(submitted);
}
}, { scope: dialogId, allowEditable: true });
const commit = () => {
const submitted = textareaRef.current?.editBuffer.getText().trim() || step.defaultValue || "";
if (submitted) resolve(submitted);
};
return (
{step.body?.map((line, index) => (
{line ? t(line) : " "}
))}
);
}
export function PaneTemplateSelectStep({
resolve,
step,
dialogId,
}: PromptContext & { step: WizardStep }) {
const options = step.options ?? [];
const [selectedIndex, setSelectedIndex] = useState(() => {
const defaultIndex = step.defaultValue
? options.findIndex((option) => option.value === step.defaultValue)
: -1;
return defaultIndex >= 0 ? defaultIndex : 0;
});
useDialogKeyboard((event) => {
event.stopPropagation();
// The list shows every option, so a page is the whole list.
const move = listCursorMove(event, options.length);
if (move) {
setSelectedIndex((index) => move(options, index));
} else if (event.name === "return" || event.name === "enter") {
resolve(options[selectedIndex]?.value ?? "");
} else if (event.name === "escape") {
resolve("");
}
}, dialogId);
return (
{step.body?.map((line, index) => (
{line ? t(line) : " "}
))}
({
id: option.value,
label: t(option.label),
}))}
selectedIndex={selectedIndex}
bgColor={colors.commandBg}
onSelect={setSelectedIndex}
onActivate={(item) => resolve(item.id)}
/>
);
}