All files / src/components SpaceHeader.vue

100% Statements 23/23
100% Branches 8/8
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193  101x 4x                       101x 4x     4x                                         1x                 67x 4x                                                     49x   49x   49x 49x 49x     49x 49x 49x 177x               49x 49x 36x                                                                                                                                                                                            
<template>
  <nav class="navbar navbar-expand space-navbar" data-testid="space-header">
    <div class="container-fluid">
      <router-link
        :to="{ name: 'space', params: { slug: space.slug } }"
        class="navbar-brand space-brand"
        data-testid="space-brand"
      >
        <img
          v-if="space.iconUrl"
          :src="space.iconUrl"
          alt=""
          class="space-brand-logo"
          data-testid="space-logo"
        />
        <span class="space-brand-name">{{ space.name }}</span>
      </router-link>
 
      <div class="navbar-nav ms-auto align-items-center">
        <!-- The reader's preferences, for visitors too (UC-SPACE-017.1, UC-SPACE-018.1) -->
        <SpaceHeaderMenu
          v-if="languages.length > 1"
          :model-value="contentLanguage"
          :options="languageOptions"
          icon="language"
          :label="t('space.language')"
          :button-text="contentLanguage"
          test-id="space-language"
          class="me-2"
          @update:model-value="setLanguage"
        />
        <SpaceHeaderMenu
          v-if="followsDarkMode(space.theme)"
          :model-value="themePreference"
          :options="modeOptions"
          :icon="MODE_ICONS[themePreference]"
          :label="t('profile.interfaceTheme')"
          test-id="space-mode"
          class="me-2 me-md-3"
          @update:model-value="setThemePreference($event as ThemePreference)"
        />
        <HeaderUserActions v-if="authStore.isAuthenticated" />
        <router-link
          v-else
          :to="{ name: 'login', query: { redirect: route.path } }"
          class="btn space-sign-in"
          data-testid="space-sign-in"
        >
          <font-awesome-icon icon="right-to-bracket" class="me-md-2" />
          <span class="d-none d-md-inline">{{ t('space.signIn') }}</span>
        </router-link>
      </div>
    </div>
  </nav>
</template>
 
<script setup lang="ts">
// @implements UC-SPACE-015.1, UC-SPACE-017.1, UC-SPACE-018.1
/**
 * The space's header, in the app's place and size: the flow's icon and name
 * instead of the Flows logo, then the reader's language and light / dark mode
 * menus, then the signed-in user's messages, notifications and account menu,
 * or a "Sign in" button for a visitor.
 */
import { computed, defineAsyncComponent, toRef } from 'vue'
import { useRoute } from 'vue-router'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import SpaceHeaderMenu from '@/components/SpaceHeaderMenu.vue'
import { useAuthStore } from '@/stores/auth'
import { useUILanguage } from '@/composables/useUILanguage'
import { useTheme, type ThemePreference } from '@/composables/useTheme'
import { languageOwnName, useSpaceLanguage } from '@/composables/useSpaceLanguage'
import { followsDarkMode } from '@/utils/spaceTheme'
import type { PublicSpace } from '@/components/SpaceLayout.vue'
 
// Loaded only for a signed-in user: a visitor downloads none of it
const HeaderUserActions = defineAsyncComponent(() => import('./HeaderUserActions.vue'))
 
const props = defineProps<{ space: PublicSpace }>()
 
const route = useRoute()
const authStore = useAuthStore()
const { t } = useUILanguage()
 
// The app's own light / dark / system preference
const { themePreference, setThemePreference } = useTheme()
const MODE_ICONS: Record<ThemePreference, string> = { light: 'sun', dark: 'moon', system: 'laptop' }
const modeOptions = computed(() =>
  (['light', 'dark', 'system'] as const).map((mode) => ({
    value: mode,
    label: t(`profile.interfaceTheme${mode.charAt(0).toUpperCase()}${mode.slice(1)}`),
    icon: MODE_ICONS[mode]
  }))
)
 
// The flow's content languages, each in its own name
const { languages, contentLanguage, setLanguage } = useSpaceLanguage(toRef(props, 'space'))
const languageOptions = computed(() =>
  languages.value.map((lang) => ({ value: lang, label: languageOwnName(lang) }))
)
</script>
 
<style scoped>
/* Same place and height as the app's header (FlowsHeader), which the frame's
   offsets (ribbons, menu, content) are computed from */
.space-navbar {
  z-index: 1100;
  margin: 0;
  border-radius: 0;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 74px;
  padding: 0 0.75rem 0 1rem !important;
  background-color: var(--space-primary) !important;
  color: var(--space-on-primary);
}
 
.space-navbar .container-fluid {
  height: 100%;
  align-items: center;
  flex-wrap: nowrap;
}
 
.space-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
  margin-right: 1rem !important;
  color: inherit;
  font-size: 1.5rem;
  font-weight: 600;
  text-decoration: none;
}
 
.space-brand:hover,
.space-brand:focus-visible {
  color: inherit;
}
 
/* Overrides the app's `.navbar-brand img` (70px, offset for the Flows logo) */
.space-brand .space-brand-logo {
  width: 52px;
  height: 52px;
  left: 0;
  object-fit: cover;
  border-radius: 10px;
  background: var(--space-background);
  flex-shrink: 0;
}
 
.space-brand-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
 
.space-sign-in {
  background: var(--space-background);
  color: var(--space-text);
  border: none;
  white-space: nowrap;
}
 
.space-sign-in:hover,
.space-sign-in:focus-visible {
  background: var(--space-background);
  color: var(--space-text);
  filter: brightness(0.92);
}
 
@media (max-width: 767px) {
  .space-navbar {
    height: 62px;
    padding: 0 0.5rem 0 0.75rem !important;
  }
 
  .space-brand {
    font-size: 1.1rem;
    gap: 0.5rem;
  }
 
  .space-brand .space-brand-logo {
    width: 40px;
    height: 40px;
  }
}
</style>