<script setup>
import { computed, onMounted, reactive, ref } from "vue";
import {
GnAlert,
GnBadge,
GnButton,
GnCard,
GnConfirmDialog,
GnDescriptionList,
GnEmptyState,
GnPageHeader,
GnToolbar,
useToast
} from "gnexus-ui-kit/vue";
import IssueList from "../components/IssueList.vue";
import { apiGet, apiSend } from "../lib/api.js";
import {
changeStatusVariant,
formatDate,
statusLabel
} from "../lib/format.js";
import { canWrite } from "../lib/session.js";
import { t } from "../i18n/index.js";
const toast = useToast();
const changes = ref([]);
const selectedId = ref("");
const selected = ref(null);
const loading = ref(false);
const error = ref("");
const applying = ref(false);
const confirmState = reactive({
open: false,
title: "",
message: "",
confirmText: "",
variant: "warning",
action: null
});
const detailItems = computed(() => {
const change = selected.value;
if (!change) return [];
return [
{ key: "summary", term: t("common.summary"), value: change.summary },
{ key: "status", term: t("common.status"), value: statusLabel(change.status) },
{ key: "kind", term: t("common.type"), value: change.kind },
{ key: "target", term: t("common.target"), value: change.target },
{ key: "reason", term: t("common.reason"), value: change.reason || "—" },
{ key: "created", term: t("common.created"), value: formatDate(change.created_at) },
{
key: "actor",
term: t("auth.account"),
value: [change.actor_name, change.actor_kind, change.via].filter(Boolean).join(" · ") || "—"
}
];
});
async function refresh() {
error.value = "";
try {
changes.value = (await apiGet("/changes")) || [];
if (selectedId.value && changes.value.some(item => item.id === selectedId.value)) {
await loadChange(selectedId.value);
} else if (changes.value.length) {
await loadChange(changes.value[0].id);
} else {
selectedId.value = "";
selected.value = null;
}
} catch (err) {
error.value = err instanceof Error ? err.message : t("errors.loadFailed");
}
}
async function loadChange(id) {
selectedId.value = id;
loading.value = true;
error.value = "";
try {
selected.value = await apiGet(`/changes/${encodeURIComponent(id)}`);
} catch (err) {
selected.value = null;
error.value = err instanceof Error ? err.message : t("errors.loadFailed");
} finally {
loading.value = false;
}
}
function applyChange(change) {
if (!change) return;
confirmState.title = t("changes.applyConfirm");
confirmState.message = t("changes.applyConfirmText");
confirmState.confirmText = t("changes.apply");
confirmState.variant = "success";
confirmState.action = async () => {
applying.value = true;
try {
await apiSend(`/changes/${encodeURIComponent(change.id)}/apply`, "POST");
toast.success({ title: t("changes.applied") });
await refresh();
} catch (err) {
toast.danger({
title: t("errors.actionFailed"),
text: err instanceof Error ? err.message : t("errors.actionFailed")
});
} finally {
applying.value = false;
}
};
confirmState.open = true;
}
function runConfirm() {
const action = confirmState.action;
confirmState.action = null;
if (action) action();
}
onMounted(refresh);
</script>
<template>
<div class="gnb-page">
<GnPageHeader
:title="t('changes.title')"
:subtitle="t('changes.subtitle')"
:kicker="t('app.kicker')"
accent
>
<template #actions>
<GnButton variant="secondary" icon="ph-arrow-clockwise" @click="refresh">
{{ t("common.refresh") }}
</GnButton>
</template>
</GnPageHeader>
<GnAlert v-if="error" variant="danger">{{ error }}</GnAlert>
<div class="gnb-docs-layout">
<aside class="gnb-doc-list gnb-panel">
<div class="gnb-panel-head">
<span class="gnb-panel-title">{{ t("changes.title") }}</span>
<span class="gnb-muted">{{ changes.length }}</span>
</div>
<div class="gnb-list-body">
<GnEmptyState v-if="!changes.length" :title="t('changes.noChanges')" icon="ph-git-pull-request" />
<button
v-for="change in changes"
:key="change.id"
type="button"
class="gnb-doc-row"
:class="{ 'is-active': change.id === selectedId }"
@click="loadChange(change.id)"
>
<strong>{{ change.summary }}</strong>
<span>{{ statusLabel(change.status) }} · {{ change.kind }} · {{ change.target }}</span>
<GnBadge :variant="changeStatusVariant(change.status)">{{ statusLabel(change.status) }}</GnBadge>
</button>
</div>
</aside>
<section class="gnb-doc-detail">
<GnToolbar
:title="selected?.summary || t('changes.title')"
:meta="loading ? t('common.loading') : statusLabel(selected?.status || '')"
>
<template #actions>
<GnButton
v-if="selected && ['draft', 'pending'].includes(selected.status) && canWrite"
variant="success"
icon="ph-check"
:loading="applying"
@click="applyChange(selected)"
>
{{ t("changes.apply") }}
</GnButton>
</template>
</GnToolbar>
<GnAlert v-if="error" variant="danger">{{ error }}</GnAlert>
<GnEmptyState v-else-if="!selected" :title="t('changes.select')" icon="ph-git-pull-request" />
<div v-else class="gnb-doc">
<GnCard :title="t('common.details')">
<GnDescriptionList :items="detailItems" />
</GnCard>
<GnCard :title="t('changes.validation')">
<p v-if="!selected.validation" class="gnb-muted">{{ t("changes.noValidation") }}</p>
<template v-else>
<div class="gnb-badges">
<GnBadge :variant="selected.validation.status === 'ok' ? 'success' : 'danger'">
{{ selected.validation.status }}
</GnBadge>
<span>{{ t("common.issueCount", { n: selected.validation.issue_count || 0 }) }}</span>
</div>
<IssueList :issues="selected.validation.issues || []" />
</template>
</GnCard>
</div>
</section>
</div>
<GnConfirmDialog
v-model:open="confirmState.open"
:title="confirmState.title"
:message="confirmState.message"
:confirm-text="confirmState.confirmText || t('common.confirm')"
:cancel-text="t('common.cancel')"
:confirm-variant="confirmState.variant"
@confirm="runConfirm"
@cancel="confirmState.action = null"
/>
</div>
</template>