DefaultLayoutWithHorizontalNav.vue 3.07 KB
<script lang="ts" setup>
import { HorizontalNavLayout } from '@layouts'
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { useKeycloakStore } from '@/@core/stores/keycloakStore'
import keycloakInstance from '@/keycloak'

import Footer from '@/layouts/components/Footer.vue'
import NavbarThemeSwitcher from '@/layouts/components/NavbarThemeSwitcher.vue'
import NavbarTokenExpiredTime from '@/layouts/components/NavbarTokenExpiredTime.vue'
import UserProfile from '@/layouts/components/UserProfile.vue'
import navItems from '@/navigation/horizontal'

// Keycloak & state
const keycloakStore = useKeycloakStore()
const authenticated = computed(() => keycloakStore.authenticated)

const now = ref(Math.floor(Date.now() / 1000))
const tokenLifetime = ref(0)

let timer: ReturnType<typeof setInterval>

onMounted(() => {
  // Set tokenLifetime only once on mount
  if (keycloakInstance.tokenParsed?.exp && keycloakInstance.tokenParsed?.iat)
    tokenLifetime.value = keycloakInstance.tokenParsed.exp - keycloakInstance.tokenParsed.iat

  // Start timer
  timer = setInterval(() => {
    now.value = Math.floor(Date.now() / 1000)
  }, 1000)
})

onUnmounted(() => {
  clearInterval(timer)
})

// Computed expiration values
const computedExpIn = computed(() => {
  return authenticated.value && keycloakInstance.tokenParsed?.exp
    ? Math.max(keycloakInstance.tokenParsed?.exp - now.value, 0)
    : 0
})
</script>

<template>
  <HorizontalNavLayout :nav-items="navItems">
    <!-- 👉 navbar -->
    <template #navbar>
      <NuxtLink
        to="/"
        class="d-flex align-start gap-x-4"
      >
        <!-- <VNodeRenderer :nodes="themeConfig.app.logo" /> -->
        <div style="display: inline-flex; align-items: center; gap: 8px;">
          <img
            src="@images/naput/logo-UI.png"
            alt="Logo UI"
            width="35"
            height="auto"
          >
          <h1 style="font-size: 1.25rem; margin-inline-start: 10px; text-transform: uppercase;">
            CIVITAS UI
          </h1>
        </div>
      </NuxtLink>
      <VSpacer />

      <!-- <NavSearchBar /> -->

      <!--
        <NavBarI18n
        v-if="themeConfig.app.i18n.enable && themeConfig.app.i18n.langConfig?.length"
        :languages="themeConfig.app.i18n.langConfig"
        />
      -->
      <NavbarTokenExpiredTime />
      <NavbarThemeSwitcher />
      <!-- <NavbarShortcuts /> -->
      <!-- <NavBarNotifications class="me-2" /> -->
      <!--
        <div class="pa-4">
        <VAlert
        v-if="authenticated"
        type="info"
        border="start"
        variant="outlined"
        class="mb-4"
        >
        <h3 class="text-h6 font-weight-bold">
        Access token expires in {{ computedExpIn }} sec,
        Refresh token in {{ computedRefExpIn }} sec,
        Session: {{ computedSessionTimer }}
        </h3>
        </VAlert>
        </div>
      -->
      <UserProfile />
    </template>

    <!-- 👉 Pages -->
    <slot />

    <!-- 👉 Footer -->
    <template #footer>
      <Footer />
    </template>

    <!-- 👉 Customizer -->
    <TheCustomizer />
  </HorizontalNavLayout>
</template>