<script setup>
import { computed, onMounted, ref } from "vue";
import {
GnAlert,
GnButton,
GnCard,
GnChip,
GnDescriptionList,
GnEmptyState,
GnPageHeader,
GnSearchField,
GnTable,
GnTabs
} from "gnexus-ui-kit/vue";
import { apiGet } from "../lib/api.js";
import { displayValue, readableKey } from "../lib/format.js";
import { t } from "../i18n/index.js";
const types = ref([]);
const type = ref("");
const records = ref([]);
const filter = ref("");
const selectedId = ref("");
const rawText = ref("");
const tab = ref("fields");
const loading = ref(false);
const error = ref("");
const filtered = computed(() => {
const query = filter.value.trim().toLowerCase();
if (!query) return records.value;
return records.value.filter(item =>
[item.id, item.name, item.fqdn, item.host]
.filter(Boolean)
.some(value => String(value).toLowerCase().includes(query))
);
});
const selected = computed(
() => records.value.find(item => item.id === selectedId.value) || null
);
const fieldItems = computed(() =>
Object.entries(selected.value || {}).map(([key, value]) => ({
key,
term: readableKey(key),
value: displayValue(value)
}))
);
const columns = computed(() => [
{ key: "name", label: t("common.name") },
{ key: "id", label: t("inventory.recordId") },
{ key: "host", label: t("inventory.host") }
]);
const tabs = computed(() => [
{ id: "fields", label: t("common.fields"), icon: "ph-list-dashes" },
{ id: "json", label: t("common.raw"), icon: "ph-code" }
]);
async function loadTypes() {
error.value = "";
try {
types.value = (await apiGet("/inventory")) || [];
if (types.value.length) {
await loadType(types.value.includes(type.value) ? type.value : types.value[0]);
}
} catch (err) {
error.value = err instanceof Error ? err.message : t("errors.loadFailed");
}
}
async function loadType(value) {
if (!value) return;
type.value = value;
selectedId.value = "";
rawText.value = "";
loading.value = true;
error.value = "";
try {
records.value = (await apiGet(`/inventory/${encodeURIComponent(value)}`)) || [];
selectedId.value = records.value[0]?.id || "";
if (tab.value === "json") await loadRaw();
} catch (err) {
records.value = [];
error.value = err instanceof Error ? err.message : t("errors.loadFailed");
} finally {
loading.value = false;
}
}
async function loadRaw() {
if (!type.value) {
rawText.value = "";
return;
}
try {
const data = await apiGet(`/inventory/${encodeURIComponent(type.value)}?raw=true`);
rawText.value = data.raw || "";
} catch (err) {
error.value = err instanceof Error ? err.message : t("errors.loadFailed");
}
}
function switchTab(value) {
tab.value = value;
if (value === "json" && !rawText.value) loadRaw();
}
onMounted(loadTypes);
</script>
<template>
<div class="gnb-page">
<GnPageHeader
:title="t('inventory.title')"
:subtitle="t('inventory.subtitle')"
:kicker="t('app.kicker')"
accent
>
<template #actions>
<GnButton variant="secondary" icon="ph-arrow-clockwise" @click="loadTypes">
{{ t("common.refresh") }}
</GnButton>
</template>
</GnPageHeader>
<div class="gnb-inventory-toolbar">
<div class="gnb-chip-row">
<GnChip
v-for="item in types"
:key="item"
clickable
variant="secondary"
:selected="item === type"
@click="loadType(item)"
>
{{ item }}
</GnChip>
</div>
<span class="gnb-muted">{{ t("inventory.recordCount", { n: records.length }) }}</span>
</div>
<GnAlert v-if="error" variant="danger">{{ error }}</GnAlert>
<GnEmptyState v-else-if="!types.length" :title="t('inventory.noTypes')" icon="ph-database" />
<div v-else-if="type" class="gnb-inventory-layout">
<aside class="gnb-record-list gnb-panel">
<div class="gnb-panel-head">
<span class="gnb-panel-title">{{ type }}</span>
<span class="gnb-muted">{{ loading ? "…" : filtered.length }}</span>
</div>
<GnSearchField v-model="filter" :placeholder="t('inventory.filterPlaceholder')" />
<GnTable :columns="columns" :rows="filtered" :empty-text="t('inventory.noRecords')">
<template #cell-name="{ row }">
<button
type="button"
class="gnb-table-link"
:class="{ 'is-active': row.id === selectedId }"
@click="selectedId = row.id"
>
{{ row.name || row.fqdn || row.id || t("common.unknown") }}
</button>
</template>
<template #cell-id="{ row }"><code>{{ row.id || "—" }}</code></template>
<template #cell-host="{ row }">{{ row.host || "—" }}</template>
</GnTable>
</aside>
<GnCard class="gnb-record-detail">
<template #title>
<span>{{ selected?.name || selected?.id || t("inventory.selectRecord") }}</span>
</template>
<GnEmptyState v-if="!selected" :title="t('inventory.selectRecord')" icon="ph-database" />
<GnTabs v-else :model-value="tab" :items="tabs" aria-label="Record view" @update:model-value="switchTab">
<template #fields>
<GnDescriptionList :items="fieldItems" />
</template>
<template #json>
<GnAlert variant="info">{{ t("inventory.rawNotice") }}</GnAlert>
<pre v-if="rawText" class="gnb-pre">{{ rawText }}</pre>
<p v-else class="gnb-muted">{{ t("common.loading") }}…</p>
</template>
</GnTabs>
</GnCard>
</div>
<GnEmptyState v-else :title="t('inventory.selectType')" icon="ph-database" />
</div>
</template>