<script setup>
import { computed, ref } from "vue";
import {
GnAlert,
GnButton,
GnCard,
GnDescriptionList,
GnPageHeader,
useToast
} from "gnexus-ui-kit/vue";
import { apiGet, apiSend } from "../lib/api.js";
import { session, userDisplayName } from "../lib/session.js";
import {
locale,
localeOptions,
setLocale,
t
} from "../i18n/index.js";
const toast = useToast();
const error = ref("");
const activeLocaleLabel = computed(
() =>
(localeOptions.find(option => option.value === locale.value) || localeOptions[0]).label
);
const settingsItems = computed(() => [
{ key: "storage", term: t("settings.storage"), value: session.storage || t("common.unknown") },
{ key: "health", term: t("common.status"), value: session.healthStatus || t("common.unknown") },
{
key: "auth",
term: t("settings.auth"),
value: session.authEnabled ? t("auth.title") : t("settings.authDisabled")
},
{
key: "account",
term: t("auth.account"),
value: session.user
? `${userDisplayName.value} ยท ${session.user.email || session.user.user_id}`
: t("common.none")
},
{ key: "language", term: t("settings.language"), value: activeLocaleLabel.value }
]);
async function changeLocale(value) {
error.value = "";
if (!session.authEnabled || !session.user) {
setLocale(value);
session.localeOverride = null;
toast.success({ title: t("common.saved") });
return;
}
try {
if (value === "auto") {
await apiSend("/me", "PATCH", { locale: "" });
const data = await apiGet("/me");
session.user = data.user;
session.localeOverride = null;
setLocale(data.locale_effective || "en");
} else {
await apiSend("/me", "PATCH", { locale: value });
session.localeOverride = value;
setLocale(value);
}
toast.success({ title: t("common.saved") });
} catch (err) {
error.value = err instanceof Error ? err.message : t("errors.saveFailed");
toast.danger({
title: t("errors.saveFailed"),
text: error.value
});
}
}
</script>
<template>
<div class="gnb-page">
<GnPageHeader
:title="t('settings.title')"
:subtitle="t('settings.subtitle')"
:kicker="t('app.kicker')"
accent
/>
<GnAlert v-if="error" variant="danger">{{ error }}</GnAlert>
<div class="gnb-split">
<GnCard :title="t('settings.language')">
<p class="gnb-muted">{{ t("settings.languageMeta") }}</p>
<div class="gnb-actions gnb-language-actions">
<GnButton
v-if="session.authEnabled"
:variant="session.localeOverride ? 'secondary' : 'accent'"
@click="changeLocale('auto')"
>
{{ t("auth.localeAuto") }}
</GnButton>
<GnButton
v-for="option in localeOptions"
:key="option.value"
:variant="locale === option.value ? 'accent' : 'secondary'"
@click="changeLocale(option.value)"
>
{{ option.label }}
</GnButton>
</div>
</GnCard>
<GnCard :title="t('common.details')">
<GnDescriptionList :items="settingsItems" />
</GnCard>
</div>
</div>
</template>