Lutra/ui/src/components/deploy/DeploymentDialog.vue
danny 872b5cb258
Some checks are pending
ci / ci (macos-15) (push) Waiting to run
ci / ci (ubuntu-24.04) (push) Waiting to run
ci / ci (windows-2025) (push) Waiting to run
D1: deployment UI, docs and G17
Servers dialog (write-only keychain secret, test connection, unknown and
changed host key), status-bar progress with Cancel, Deployment log tool
window, Upload/Download in the project tree menu and Tools › Deployment.
docs/deployment.md, architecture section, G17 row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:14:58 +01:00

867 lines
25 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
/**
* "Deployment Servers…" (G17): the project's servers on the left, the
* selected one's settings on the right.
*
* - **Fields:** name, type (SFTP, FTP, FTPS, local or mounted folder),
* host, port, user, root folder, sign-in (password, key file with an
* optional passphrase, or the SSH agent for SFTP), path mappings,
* excluded paths (one per line), upload on save (off by default) and
* "default server".
* - **Secrets:** the password or passphrase field is write-only. A typed
* value goes to the engine with Save and is kept only in the OS
* keychain; the field then empties again and says that one is stored.
* - **Test Connection** saves first, then tests. An unknown SFTP host key
* shows its fingerprint with "Trust and Connect"; a changed one is a
* warning whose replace button must be chosen explicitly.
* - Remove asks in place. Escape closes. The dialog is an exclusive modal
* (`commands/modals.ts`).
*/
import { computed, nextTick, ref, watch } from "vue";
import { useModalsStore } from "../../commands/modals";
import { t } from "../../i18n";
import { pickDir, pickFile } from "../../ipc";
import { blankServer, useDeployStore, type DeployServer, type ServerKind } from "./deploy";
const deploy = useDeployStore();
const modals = useModalsStore();
/** The selected server's id, "" for a new one. */
const selected = ref<string>("");
/** The form's values. */
const form = ref<DeployServer>(blankServer());
/** The typed password or passphrase, sent once with Save. */
const secret = ref("");
/** Whether the form's server is (to become) the default. */
const isDefault = ref(false);
/** The excluded paths, one per line. */
const excludedText = ref("");
/** Whether Remove waits for its confirmation. */
const confirmRemove = ref(false);
/** Whether Save or Test runs. */
const working = ref(false);
/** The name field. */
const nameInput = ref<HTMLInputElement | null>(null);
/** The host-key question box. */
const questionBox = ref<HTMLElement | null>(null);
/** The element that had the focus before the dialog opened. */
let returnFocus: HTMLElement | null = null;
/** Server types in the select, with their labels. */
const kindLabels = computed<{ id: ServerKind; label: string }[]>(() => [
{ id: "sftp", label: t("deploy.kind.sftp") },
{ id: "ftps", label: t("deploy.kind.ftps") },
{ id: "ftp", label: t("deploy.kind.ftp") },
{ id: "folder", label: t("deploy.kind.folder") },
]);
/** Whether the form needs a host, user and port. */
const remote = computed(() => form.value.kind !== "folder");
/** The secret the form's server needs, or null. */
const secretKind = computed<"password" | "passphrase" | null>(() => {
if (form.value.kind === "folder" || (form.value.kind === "sftp" && form.value.auth === "agent")) {
return null;
}
return form.value.kind === "sftp" && form.value.auth === "key" ? "passphrase" : "password";
});
/** Whether the selected server has its secret in the keychain. */
const hasSecret = computed(() => deploy.servers.find((s) => s.id === selected.value)?.hasSecret === true);
/** The port field as text; empty means the default port. */
const portText = computed({
get: () => (form.value.port === null ? "" : String(form.value.port)),
set: (text: string) => {
const n = Number.parseInt(text, 10);
form.value.port = text.trim() === "" || !Number.isFinite(n) ? null : n;
},
});
/** The key path field as text. */
const keyText = computed({
get: () => form.value.keyPath ?? "",
set: (text: string) => {
form.value.keyPath = text === "" ? null : text;
},
});
/**
* Shows a server in the form.
*
* @param id - its id, or "" for a new server.
*/
function select(id: string): void {
const server = deploy.servers.find((s) => s.id === id);
const copy = server ? { ...server, mappings: server.mappings.map((m) => ({ ...m })), excluded: [...server.excluded] } : blankServer();
selected.value = server ? id : "";
form.value = copy;
secret.value = "";
isDefault.value = server ? deploy.defaultServer === id || deploy.servers.length === 1 : deploy.servers.length === 0;
excludedText.value = copy.excluded.join("\n");
confirmRemove.value = false;
deploy.tested = null;
deploy.question = null;
}
/** Starts a new server. */
async function addNew(): Promise<void> {
select("");
await nextTick();
nameInput.value?.focus();
}
/** Adds an empty mapping row. */
function addMapping(): void {
form.value.mappings = [...form.value.mappings, { local: "", remote: "" }];
}
/**
* Removes a mapping row.
*
* @param index - the row.
*/
function removeMapping(index: number): void {
form.value.mappings = form.value.mappings.filter((_, i) => i !== index);
}
/**
* Saves the form.
*
* @returns the saved server's id, or null.
*/
async function save(): Promise<string | null> {
working.value = true;
try {
form.value.excluded = excludedText.value.split(/\r?\n/);
const id = await deploy.saveServer(form.value, secretKind.value === null ? "" : secret.value, isDefault.value);
if (id !== null) {
select(id);
}
return id;
} finally {
working.value = false;
}
}
/** Saves, then tests the connection. */
async function test(): Promise<void> {
const id = await save();
if (id === null) {
return;
}
working.value = true;
try {
await deploy.testConnection(id);
} finally {
working.value = false;
}
}
/** Removes the selected server after the confirmation. */
async function remove(): Promise<void> {
if (!confirmRemove.value) {
confirmRemove.value = true;
return;
}
const id = selected.value;
confirmRemove.value = false;
await deploy.removeServer(id);
select(deploy.servers[0]?.id ?? "");
}
/** Chooses the key file with the shell's picker. */
async function browseKey(): Promise<void> {
const picked = await pickFile(t("deploy.dialog.pickKey"));
if (picked.ok && picked.value !== null) {
form.value.keyPath = picked.value;
}
}
/** Chooses the deployment folder with the shell's picker. */
async function browseFolder(): Promise<void> {
const picked = await pickDir(t("deploy.dialog.pickFolder"));
if (picked.ok && picked.value !== null) {
form.value.root = picked.value;
}
}
/** Closes the dialog. */
function close(): void {
deploy.hideDialog();
}
/**
* Escape closes.
*
* @param event - the key.
*/
function onKeydown(event: KeyboardEvent): void {
if (event.key === "Escape") {
event.preventDefault();
close();
}
}
watch(
() => deploy.dialogOpen,
async (isOpen) => {
if (isOpen) {
const active = document.activeElement;
returnFocus = active instanceof HTMLElement ? active : null;
modals.opened("deployment", close);
select(deploy.target?.id ?? deploy.servers[0]?.id ?? "");
await nextTick();
nameInput.value?.focus();
} else {
modals.closed("deployment");
returnFocus?.focus();
returnFocus = null;
}
},
);
/**
* Scrolls a new host-key question into view; environments without
* `scrollIntoView` (tests) skip it.
*/
watch(
() => deploy.question,
async (question) => {
if (question !== null) {
await nextTick();
const box = questionBox.value;
if (box !== null && typeof box.scrollIntoView === "function") {
box.scrollIntoView({ block: "nearest" });
}
}
},
);
watch(
() => deploy.servers,
() => {
if (deploy.dialogOpen && selected.value !== "" && !deploy.servers.some((s) => s.id === selected.value)) {
select(deploy.servers[0]?.id ?? "");
}
},
);
</script>
<template>
<div
v-if="deploy.dialogOpen"
class="deploy-dialog"
data-test="deploy-dialog"
@mousedown.self="close"
>
<div
class="deploy-dialog__box"
role="dialog"
aria-modal="true"
aria-labelledby="lt-deploy-title"
@keydown="onKeydown"
>
<h2
id="lt-deploy-title"
class="deploy-dialog__title"
>
{{ t("deploy.dialog.title") }}
</h2>
<div class="deploy-dialog__body">
<nav
class="deploy-dialog__servers"
:aria-label="t('deploy.dialog.servers')"
>
<ul class="deploy-dialog__list">
<li
v-for="server in deploy.servers"
:key="server.id"
>
<button
type="button"
class="deploy-dialog__server"
:class="{ 'deploy-dialog__server--selected': server.id === selected }"
:aria-current="server.id === selected ? 'true' : undefined"
data-test="deploy-server"
@click="select(server.id)"
>
<span class="deploy-dialog__server-name">{{ server.name }}</span>
<span class="deploy-dialog__server-sub">
{{ server.kind.toUpperCase() }}{{ server.host ? ` · ${server.host}` : "" }}{{ server.id === deploy.defaultServer ? ` · ${t("deploy.dialog.defaultMark")}` : "" }}
</span>
</button>
</li>
</ul>
<button
type="button"
class="deploy-dialog__button"
data-test="deploy-add"
@click="addNew"
>
{{ t("deploy.dialog.add") }}
</button>
</nav>
<form
class="deploy-dialog__form"
@submit.prevent="save"
>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.name") }}</span>
<input
ref="nameInput"
v-model="form.name"
class="deploy-dialog__input"
type="text"
autocomplete="off"
data-test="deploy-name"
>
</label>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.kind") }}</span>
<select
v-model="form.kind"
class="deploy-dialog__input"
data-test="deploy-kind"
>
<option
v-for="kind in kindLabels"
:key="kind.id"
:value="kind.id"
>
{{ kind.label }}
</option>
</select>
</label>
<p
v-if="form.kind === 'ftp'"
class="deploy-dialog__warning"
data-test="deploy-ftp-warning"
>
{{ t("deploy.dialog.ftpWarning") }}
</p>
<template v-if="remote">
<div class="deploy-dialog__pair">
<label class="deploy-dialog__field deploy-dialog__field--grow">
<span>{{ t("deploy.field.host") }}</span>
<input
v-model="form.host"
class="deploy-dialog__input"
type="text"
spellcheck="false"
autocomplete="off"
data-test="deploy-host"
>
</label>
<label class="deploy-dialog__field deploy-dialog__field--port">
<span>{{ t("deploy.field.port") }}</span>
<input
v-model="portText"
class="deploy-dialog__input"
type="text"
inputmode="numeric"
:placeholder="form.kind === 'sftp' ? '22' : '21'"
data-test="deploy-port"
>
</label>
</div>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.user") }}</span>
<input
v-model="form.user"
class="deploy-dialog__input"
type="text"
spellcheck="false"
autocomplete="off"
data-test="deploy-user"
>
</label>
<label
v-if="form.kind === 'sftp'"
class="deploy-dialog__field"
>
<span>{{ t("deploy.field.auth") }}</span>
<select
v-model="form.auth"
class="deploy-dialog__input"
data-test="deploy-auth"
>
<option value="password">{{ t("deploy.auth.password") }}</option>
<option value="key">{{ t("deploy.auth.key") }}</option>
<option value="agent">{{ t("deploy.auth.agent") }}</option>
</select>
</label>
<label
v-if="form.kind === 'sftp' && form.auth === 'key'"
class="deploy-dialog__field"
>
<span>{{ t("deploy.field.keyPath") }}</span>
<span class="deploy-dialog__row">
<input
v-model="keyText"
class="deploy-dialog__input"
type="text"
spellcheck="false"
data-test="deploy-key"
>
<button
type="button"
class="deploy-dialog__button"
@click="browseKey"
>{{ t("deploy.dialog.browse") }}</button>
</span>
</label>
<label
v-if="secretKind !== null"
class="deploy-dialog__field"
>
<span>{{ secretKind === "password" ? t("deploy.field.password") : t("deploy.field.passphrase") }}</span>
<input
v-model="secret"
class="deploy-dialog__input"
type="password"
autocomplete="new-password"
:placeholder="hasSecret ? t('deploy.dialog.secretStored') : t('deploy.dialog.secretNone')"
data-test="deploy-secret"
>
</label>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.root") }}</span>
<input
v-model="form.root"
class="deploy-dialog__input"
type="text"
spellcheck="false"
:placeholder="t('deploy.field.rootPlaceholder')"
data-test="deploy-root"
>
</label>
</template>
<label
v-else
class="deploy-dialog__field"
>
<span>{{ t("deploy.field.folder") }}</span>
<span class="deploy-dialog__row">
<input
v-model="form.root"
class="deploy-dialog__input"
type="text"
spellcheck="false"
data-test="deploy-root"
>
<button
type="button"
class="deploy-dialog__button"
@click="browseFolder"
>{{ t("deploy.dialog.browse") }}</button>
</span>
</label>
<fieldset class="deploy-dialog__group">
<legend>{{ t("deploy.field.mappings") }}</legend>
<p class="deploy-dialog__hint">
{{ t("deploy.dialog.mappingsHint") }}
</p>
<div
v-for="(mapping, index) in form.mappings"
:key="index"
class="deploy-dialog__row"
data-test="deploy-mapping"
>
<input
v-model="mapping.local"
class="deploy-dialog__input"
type="text"
spellcheck="false"
:aria-label="t('deploy.field.mappingLocal')"
:placeholder="t('deploy.field.mappingLocal')"
>
<span aria-hidden="true">{{ t("deploy.dialog.mapsTo") }}</span>
<input
v-model="mapping.remote"
class="deploy-dialog__input"
type="text"
spellcheck="false"
:aria-label="t('deploy.field.mappingRemote')"
:placeholder="t('deploy.field.mappingRemote')"
>
<button
type="button"
class="deploy-dialog__button"
:aria-label="t('deploy.dialog.removeMapping')"
@click="removeMapping(index)"
>
×
</button>
</div>
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--small"
data-test="deploy-add-mapping"
@click="addMapping"
>
{{ t("deploy.dialog.addMapping") }}
</button>
</fieldset>
<label class="deploy-dialog__field">
<span>{{ t("deploy.field.excluded") }}</span>
<textarea
v-model="excludedText"
class="deploy-dialog__input deploy-dialog__textarea"
rows="3"
spellcheck="false"
data-test="deploy-excluded"
/>
</label>
<label class="deploy-dialog__check">
<input
v-model="form.uploadOnSave"
type="checkbox"
data-test="deploy-upload-on-save"
>
{{ t("deploy.field.uploadOnSave") }}
</label>
<label class="deploy-dialog__check">
<input
v-model="isDefault"
type="checkbox"
data-test="deploy-default"
>
{{ t("deploy.field.default") }}
</label>
<div
v-if="deploy.question !== null"
ref="questionBox"
class="deploy-dialog__question"
:class="{ 'deploy-dialog__question--changed': deploy.question.status === 'changed' }"
role="alert"
data-test="deploy-hostkey"
>
<template v-if="deploy.question.status === 'unknown'">
<strong>{{ t("deploy.hostKey.unknownTitle") }}</strong>
<span>{{ t("deploy.hostKey.unknown", { host: deploy.question.host }) }}</span>
<code class="deploy-dialog__fingerprint">{{ deploy.question.algorithm }} {{ deploy.question.fingerprint }}</code>
<span class="deploy-dialog__buttons">
<button
type="button"
class="deploy-dialog__button"
@click="deploy.rejectHostKey()"
>{{ t("deploy.hostKey.reject") }}</button>
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--primary"
data-test="deploy-trust"
@click="deploy.trustHostKey(false)"
>{{ t("deploy.hostKey.trust") }}</button>
</span>
</template>
<template v-else>
<strong>{{ t("deploy.hostKey.changedTitle") }}</strong>
<span>{{ t("deploy.hostKey.changed", { host: deploy.question.host }) }}</span>
<span>{{ t("deploy.hostKey.trusted") }} <code class="deploy-dialog__fingerprint">{{ deploy.question.known }}</code></span>
<span>{{ t("deploy.hostKey.offered") }} <code class="deploy-dialog__fingerprint">{{ deploy.question.algorithm }} {{ deploy.question.fingerprint }}</code></span>
<span class="deploy-dialog__buttons">
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--primary"
@click="deploy.rejectHostKey()"
>{{ t("deploy.hostKey.keep") }}</button>
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--danger"
data-test="deploy-replace"
@click="deploy.trustHostKey(true)"
>{{ t("deploy.hostKey.replace") }}</button>
</span>
</template>
</div>
<p
v-if="deploy.tested !== null && deploy.question === null"
class="deploy-dialog__outcome"
:class="{ 'deploy-dialog__outcome--ok': deploy.tested.ok }"
role="status"
data-test="deploy-tested"
>
{{ deploy.tested.message }}
</p>
<p
v-if="deploy.dialogError !== null"
class="deploy-dialog__error"
role="alert"
data-test="deploy-error"
>
{{ deploy.dialogError }}
</p>
</form>
</div>
<div class="deploy-dialog__buttons">
<button
v-if="selected !== ''"
type="button"
class="deploy-dialog__button"
:class="{ 'deploy-dialog__button--danger': confirmRemove }"
data-test="deploy-remove"
@click="remove"
>
{{ confirmRemove ? t("deploy.dialog.confirmRemove") : t("deploy.dialog.remove") }}
</button>
<span class="deploy-dialog__spacer" />
<button
type="button"
class="deploy-dialog__button"
:disabled="working || deploy.busy"
data-test="deploy-test"
@click="test"
>
{{ t("deploy.dialog.test") }}
</button>
<button
type="button"
class="deploy-dialog__button deploy-dialog__button--primary"
:disabled="working"
data-test="deploy-save"
@click="save"
>
{{ t("deploy.dialog.save") }}
</button>
<button
type="button"
class="deploy-dialog__button"
data-test="deploy-close"
@click="close"
>
{{ t("deploy.dialog.close") }}
</button>
</div>
</div>
</div>
</template>
<style scoped>
.deploy-dialog {
position: fixed;
inset: 0;
display: flex;
align-items: flex-start;
justify-content: center;
padding-top: 5vh;
background: color-mix(in srgb, var(--lt-bg) 50%, transparent);
z-index: 20;
}
.deploy-dialog__box {
width: min(860px, 94vw);
max-height: 90vh;
display: flex;
flex-direction: column;
gap: 10px;
padding: 16px;
background: var(--lt-bg);
color: var(--lt-fg);
border: 1px solid var(--lt-border);
border-radius: 6px;
}
.deploy-dialog__title {
margin: 0;
font-size: var(--lt-font-size-ui);
font-weight: 600;
}
.deploy-dialog__body {
display: flex;
gap: 12px;
min-height: 0;
overflow: hidden;
}
.deploy-dialog__servers {
display: flex;
flex: 0 0 200px;
flex-direction: column;
gap: 8px;
padding-right: 12px;
border-right: 1px solid var(--lt-border);
}
.deploy-dialog__list {
margin: 0;
padding: 0;
list-style: none;
overflow-y: auto;
}
.deploy-dialog__server {
display: flex;
flex-direction: column;
width: 100%;
padding: 4px 6px;
font: inherit;
text-align: left;
color: var(--lt-fg);
background: transparent;
border: none;
border-radius: 4px;
}
.deploy-dialog__server--selected {
background: var(--lt-editor-selection-bg);
}
.deploy-dialog__server-sub {
color: var(--lt-fg-muted);
font-size: var(--lt-font-size-small);
}
.deploy-dialog__form {
display: flex;
flex: 1;
flex-direction: column;
gap: 8px;
min-width: 0;
overflow-y: auto;
padding-right: 4px;
}
.deploy-dialog__field {
display: flex;
flex-direction: column;
gap: 4px;
}
.deploy-dialog__field--grow {
flex: 1;
}
.deploy-dialog__field--port {
flex: 0 0 8ch;
}
.deploy-dialog__pair,
.deploy-dialog__row {
display: flex;
align-items: center;
gap: 8px;
}
.deploy-dialog__input {
flex: 1;
min-width: 0;
padding: 4px 6px;
font: inherit;
color: var(--lt-fg);
background: var(--lt-editor-bg);
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.deploy-dialog__textarea {
resize: vertical;
font-family: var(--lt-font-mono);
}
.deploy-dialog__group {
display: flex;
flex-direction: column;
gap: 6px;
margin: 0;
padding: 6px 8px;
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.deploy-dialog__check {
display: flex;
align-items: center;
gap: 6px;
}
.deploy-dialog__hint {
margin: 0;
color: var(--lt-fg-muted);
font-size: var(--lt-font-size-small);
}
.deploy-dialog__warning {
margin: 0;
color: var(--lt-diag-warning);
font-size: var(--lt-font-size-small);
}
.deploy-dialog__question {
display: flex;
flex-direction: column;
gap: 6px;
padding: 8px;
border: 1px solid var(--lt-accent);
border-radius: 4px;
}
.deploy-dialog__question--changed {
border-color: var(--lt-error);
}
.deploy-dialog__fingerprint {
font-family: var(--lt-font-mono);
overflow-wrap: anywhere;
}
.deploy-dialog__outcome {
margin: 0;
overflow-wrap: anywhere;
}
.deploy-dialog__outcome--ok {
color: var(--lt-diag-info);
}
.deploy-dialog__error {
margin: 0;
color: var(--lt-error);
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.deploy-dialog__buttons {
display: flex;
justify-content: flex-end;
gap: 8px;
}
.deploy-dialog__spacer {
flex: 1;
}
.deploy-dialog__button {
padding: 4px 12px;
font: inherit;
color: var(--lt-fg);
background: var(--lt-editor-bg);
border: 1px solid var(--lt-border);
border-radius: 4px;
}
.deploy-dialog__button--small {
align-self: flex-start;
padding: 2px 8px;
}
.deploy-dialog__button--primary {
border-color: var(--lt-accent);
}
.deploy-dialog__button--danger {
border-color: var(--lt-error);
color: var(--lt-error);
}
.deploy-dialog__button:disabled {
color: var(--lt-fg-muted);
}
.deploy-dialog__input:focus-visible,
.deploy-dialog__button:focus-visible,
.deploy-dialog__server:focus-visible {
outline: 1px solid var(--lt-accent);
}
</style>