<script setup>
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
GnAlert,
GnButton,
GnCard,
GnIconButton,
GnIdentity,
GnLoader,
GnNavList,
GnNavigationShell,
GnToastProvider
} from "gnexus-ui-kit/vue";
import { apiGet, apiSend } from "./lib/api.js";
import { session, userDisplayName, userInitials } from "./lib/session.js";
import { setLocale, t } from "./i18n/index.js";
const route = useRoute();
const router = useRouter();
const authState = ref("checking");
const pageError = ref("");
const currentLabel = computed(() => t(route.meta.titleKey || "nav.docs"));
const navItems = computed(() => {
const items = [
{ id: "docs", label: t("nav.docs"), icon: "ph-files", href: "/" },
{ id: "inventory", label: t("nav.inventory"), icon: "ph-database", href: "/inventory" },
{ id: "changes", label: t("nav.changes"), icon: "ph-git-pull-request", href: "/changes" },
{ id: "status", label: t("nav.status"), icon: "ph-heartbeat", href: "/status" }
];
if (session.authEnabled && session.user) {
items.push({ id: "keys", label: t("nav.keys"), icon: "ph-key", href: "/mcp-keys" });
}
return items.map(item => ({
...item,
active:
item.href === "/"
? route.path === "/"
: route.path === item.href || route.path.startsWith(`${item.href}/`),
onSelect: (entry, event) => {
event.preventDefault();
router.push(entry.href);
}
}));
});
const footerItems = computed(() => [
{
id: "settings",
label: t("nav.settings"),
icon: "ph-gear",
href: "/settings",
active: route.path === "/settings",
onSelect: (entry, event) => {
event.preventDefault();
router.push(entry.href);
}
}
]);
async function loadMe() {
authState.value = "checking";
pageError.value = "";
try {
const data = await apiGet("/me");
session.authEnabled = Boolean(data.auth_enabled);
session.user = data.user || null;
session.localeOverride = data.locale || null;
if (data.user && data.locale_effective) {
setLocale(data.locale_effective);
} else if (!data.user) {
setLocale(data.locale_effective || "en");
}
if (data.auth_enabled && !data.user) {
authState.value = "denied";
} else {
authState.value = "ready";
await loadHealth();
}
} catch (error) {
session.user = null;
authState.value = "denied";
pageError.value = error instanceof Error ? error.message : t("errors.loadFailed");
}
const params = new URLSearchParams(window.location.search);
if (params.get("auth_error")) {
pageError.value = t("auth.denied");
window.history.replaceState({}, "", window.location.pathname);
}
}
async function loadHealth() {
try {
const health = await apiGet("/health");
session.storage = health.storage || "unknown";
session.healthStatus = health.status || "unknown";
} catch {
session.storage = "unknown";
session.healthStatus = "unknown";
}
}
function loginNow() {
const returnTo = window.location.pathname + window.location.search;
window.location.assign(`/auth/login?return_to=${encodeURIComponent(returnTo)}`);
}
function closeDrawer() {
document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));
}
async function logoutNow() {
try {
await apiSend("/auth/logout", "POST");
} catch {
/* the local session is cleared regardless */
}
session.user = null;
authState.value = "denied";
router.replace("/");
}
const onUnauthenticated = () => {
session.user = null;
authState.value = "denied";
};
onMounted(() => {
loadMe();
window.addEventListener("book:unauthenticated", onUnauthenticated);
});
onBeforeUnmount(() => {
window.removeEventListener("book:unauthenticated", onUnauthenticated);
});
</script>
<template>
<GnToastProvider :lifetime="4000">
<div v-if="authState === 'checking'" class="book-splash">
<GnLoader circle />
</div>
<div v-else-if="authState === 'denied'" class="book-gate">
<GnCard class="book-gate-card">
<div class="book-gate-mark">GB</div>
<p class="book-gate-kicker">{{ t("app.kicker") }}</p>
<h1>{{ t("auth.title") }}</h1>
<p class="gnb-muted">{{ t("auth.loginText") }}</p>
<GnAlert v-if="pageError" variant="danger">{{ pageError }}</GnAlert>
<div class="gnb-actions">
<GnButton variant="accent" icon="ph-sign-in" @click="loginNow">
{{ t("auth.login") }}
</GnButton>
<GnButton variant="secondary" icon="ph-arrow-clockwise" @click="loadMe">
{{ t("auth.recheck") }}
</GnButton>
</div>
</GnCard>
</div>
<GnNavigationShell
v-else
brand="Gnexus Book"
logo-src=""
title="Gnexus Book"
subtitle="Knowledge Base"
:current="currentLabel"
>
<template #default="{ close }">
<GnNavList :items="navItems" @select="close" />
</template>
<template #footer>
<div class="book-shell-footer">
<GnIdentity
v-if="session.user"
:title="userDisplayName"
:meta="session.user.email || ''"
:avatar="{ initials: userInitials, size: 'sm' }"
/>
<div class="book-shell-footer-actions">
<GnNavList :items="footerItems" @select="closeDrawer" />
<GnIconButton
v-if="session.authEnabled && session.user"
icon="ph-sign-out"
:label="t('nav.logout')"
@click="logoutNow"
/>
</div>
</div>
</template>
<template #content>
<main class="book-content">
<router-view />
</main>
</template>
</GnNavigationShell>
</GnToastProvider>
</template>