Lutra/ui/src/components/vcs/branches/BranchStatusItem.vue
danny 149a6c7049 M4.5: branch status-bar item and branches popup
The status bar shows the current branch (short hash when detached). Its
popup lists local, then remote branches with a filter and New branch;
checkout runs flushDirty first and toasts git's refusal. Commands
vcs.branches, vcs.newBranch and vcs.checkout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:56:24 +01:00

177 lines
4.9 KiB
Vue

<script setup lang="ts">
/**
* Status-bar item (M4.5): the current Git branch, or the short hash when
* HEAD is detached. Hidden outside a repository and while git is missing.
* A click opens the branches popup ({@link BranchPopup}).
*
* The item supplies the UI commands of the branches feature, so they work
* wherever the status bar is shown:
* - `vcs.branches` opens the popup;
* - `vcs.newBranch {name?, checkout?}` opens it on the new branch's name,
* or, with a `name` (an agent), creates the branch at HEAD and checks it
* out unless `checkout` is false; a refusal becomes an error toast;
* - `vcs.checkout {branch}` saves every dirty buffer, then checks the
* branch out (see the Branches store); without `branch` (the palette)
* it opens the popup to pick one.
* All three need an open project (the engine's `git.branches`).
*/
import { GitBranch } from "@lucide/vue";
import { computed, onBeforeUnmount, onMounted } from "vue";
import { useCommandsStore } from "../../../commands/store";
import { t } from "../../../i18n";
import type { JsonValue } from "../../../ipc";
import { useNotificationsStore } from "../../../stores/notifications";
import { useChangesStore } from "../changes/changes";
import BranchPopup from "./BranchPopup.vue";
import { headLabel, useBranchesStore } from "./branches";
const changes = useChangesStore();
const branches = useBranchesStore();
const commands = useCommandsStore();
const notifications = useNotificationsStore();
/** The repository of the last status, if any. */
const repo = computed(() => (changes.status?.available ? changes.status.repo : null));
/** The text of the item, or null when it is hidden. */
const text = computed(() => headLabel(repo.value));
/** The tooltip and accessible name. */
const label = computed(() => {
const value = text.value ?? "";
return repo.value?.detached
? t("vcs.branchItem.detached", { hash: value })
: t("vcs.branchItem.label", { branch: value });
});
/**
* Reads an object's fields; empty for anything else.
*
* @param params - the command's params; untrusted when an agent sent them.
*/
function fields(params: JsonValue): Record<string, JsonValue> {
return typeof params === "object" && params !== null && !Array.isArray(params) ? params : {};
}
/**
* Runs `vcs.checkout`.
*
* @param params - `{branch}`; without it the popup opens.
*/
async function runCheckout(params: JsonValue): Promise<void> {
const given = fields(params);
if (!("branch" in given)) {
await branches.show("list");
return;
}
const branch = given.branch;
if (typeof branch !== "string" || branch.trim() === "") {
notifications.notify({ level: "error", message: t("vcs.checkout.invalidParams") });
return;
}
branches.hide();
await branches.checkout(branch.trim());
}
/**
* Runs `vcs.newBranch`.
*
* @param params - `{name?, checkout?}`.
*/
async function runNewBranch(params: JsonValue): Promise<void> {
const { name, checkout } = fields(params);
if (typeof name !== "string") {
await branches.show("new");
return;
}
const refused = await branches.create(name, checkout !== false);
if (refused !== null) {
notifications.notify({
level: "error",
messageKey: "notification.vcs.newBranchFailed",
message: refused,
params: { branch: name, reason: refused },
});
}
}
/** Withdraws the command handlers; filled while mounted. */
let withdraw: (() => void)[] = [];
onMounted(() => {
const enabled = (): boolean => commands.isEnabled("git.branches");
withdraw = [
commands.provide("vcs.branches", { run: () => branches.show("list"), enabled }),
commands.provide("vcs.newBranch", { run: runNewBranch, enabled }),
commands.provide("vcs.checkout", { run: runCheckout, enabled }),
];
});
onBeforeUnmount(() => {
for (const fn of withdraw) {
fn();
}
withdraw = [];
});
</script>
<template>
<span
v-if="text !== null"
class="branch-item"
>
<button
type="button"
class="branch-item__button"
:title="label"
:aria-label="label"
aria-haspopup="dialog"
:aria-expanded="branches.open"
data-test="branch-item"
@click="commands.execute('vcs.branches')"
>
<GitBranch
:size="12"
aria-hidden="true"
/>
<span class="branch-item__text">{{ text }}</span>
</button>
</span>
<BranchPopup />
</template>
<style scoped>
.branch-item {
display: inline-flex;
margin-left: auto;
min-width: 0;
}
.branch-item__button {
display: inline-flex;
align-items: center;
gap: 4px;
max-width: 24ch;
padding: 0 6px;
font: inherit;
color: inherit;
background: transparent;
border: none;
border-radius: var(--lt-radius, 4px);
cursor: pointer;
}
.branch-item__button:hover {
background: color-mix(in srgb, var(--lt-statusbar-fg) 12%, transparent);
}
.branch-item__button:focus-visible {
outline: 2px solid var(--lt-accent);
}
.branch-item__text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
</style>