import {
useTranslation,
type KeybindingAction,
type KeybindingActionEditorProps,
type KeybindingRunContext,
type TermixApp,
} from "@termix/plugin-sdk/frontend";
import { fetchSnippet, runInSession } from "./run-flows";
import { useSnippetOptions } from "./use-snippet-options";
/** Stored as action.type; kept from 2.8 so saved bindings keep working. */
export const RUN_SNIPPET_ACTION = "runSnippet";
function snippetIdOf(action: KeybindingAction): string {
return typeof action.snippetId === "string" ? action.snippetId : "";
}
/** Picks the snippet a "Run snippet" binding runs. */
export function RunSnippetEditor({
action,
onChange,
}: KeybindingActionEditorProps) {
const { t } = useTranslation();
const { options } = useSnippetOptions();
return (
);
}
/** The bound snippet's name in the binding list, or a warning once it is gone. */
export function RunSnippetSummary({ action }: KeybindingActionEditorProps) {
const { t } = useTranslation();
const { options, loaded } = useSnippetOptions();
if (!loaded) return null;
const snippet = options.find((s) => String(s.id) === snippetIdOf(action));
if (snippet) return ({snippet.name});
return (
({t("keybindings.snippetMissing")})
);
}
export function validateRunSnippet(action: KeybindingAction): string | null {
return /^\d+$/.test(snippetIdOf(action))
? null
: "keybindings.snippetRequired";
}
/** Runs a bound snippet in the terminal the key was pressed in. */
export function runSnippetBinding(
app: Pick,
action: KeybindingAction,
context: KeybindingRunContext,
): void {
void fetchSnippet(app, Number(snippetIdOf(action))).then((snippet) => {
if (!snippet) return;
void runInSession(
app,
{ ...snippet, isNote: false },
{ sessionId: context.sessionId, send: context.send },
context.host ?? null,
{ pressEnter: action.appendEnter !== false },
);
});
}