game-village-politics-frontend/app/components/game/MenuBar.vue

183 lines
7.9 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { UiIconName } from '~/components/icon/Ui.vue'
const props = defineProps<{
/** Inside a world: the world's menu (village, map, reports) and its clock. */
inWorld?: boolean
/** Across the whole screen, over the fullscreen village, instead of the centred column. */
wide?: boolean
}>()
const { t } = useI18n()
const { user, logout } = useAuth()
const { current } = useCurrentWorld()
const { worlds, refresh } = useWorld()
const unread = useUnreadReports()
const chat = useChat()
const villageContext = useVillageContextOrNull()
const villageState = computed(() => villageContext?.village.value ?? null)
const { now } = useServerClock()
const route = useRoute()
const world = computed(() => (props.inWorld && current.value ? (worlds.value?.find((w) => w.id === current.value!.worldId) ?? null) : null))
if (props.inWorld && !worlds.value) refresh()
interface Entry {
key: 'village' | 'map' | 'reports' | 'ranking' | 'tribe'
to: string
icon: UiIconName
active: boolean
count?: number
/** The dropdown under the entry, like the menu of Die Stämme. */
sub: { label: string; to: string | { path: string; query: Record<string, string> } }[]
}
// One bar with every section of the world; each entry opens its own page, and on
// hover or focus a dropdown with its sub-pages.
const entries = computed<Entry[]>(() => {
const c = current.value
if (!props.inWorld || !c) return []
const out: Entry[] = []
if (c.villageId) {
const base = `/village/${c.villageId}`
out.push({
key: 'village',
to: base,
icon: 'home',
active: route.path.startsWith('/village/'),
sub: VILLAGE_SCREENS.map((s) => ({ label: t(`village.screens.${s.name}`), to: `${base}${s.path}` })),
})
}
out.push({ key: 'map', to: `/map/${c.worldId}`, icon: 'pin', active: route.path.startsWith('/map/'), sub: [] })
out.push({
key: 'reports',
to: `/reports/${c.worldId}`,
icon: 'report',
active: route.path.startsWith('/reports/'),
count: unread.value,
sub: (['all', 'attack', 'defense', 'support'] as const).map((kind) => ({
label: t(`reports.kinds.${kind}`),
to: kind === 'all' ? `/reports/${c.worldId}` : { path: `/reports/${c.worldId}`, query: { type: kind } },
})),
})
out.push({
key: 'tribe',
to: `/tribe/${c.worldId}`,
icon: 'users',
active: route.path.startsWith('/tribe/'),
// Invites waiting while the player has no tribe.
count: villageState.value?.tribe.tribe ? undefined : villageState.value?.tribe.invites,
sub: [],
})
out.push({
key: 'ranking',
to: `/ranking/${c.worldId}`,
icon: 'rank',
active: route.path.startsWith('/ranking/'),
sub: (['players', 'tribes', 'villages', 'attack', 'defense'] as const).map((kind) => ({
label: t(`ranking.kinds.${kind}`),
to: kind === 'players' ? `/ranking/${c.worldId}` : { path: `/ranking/${c.worldId}`, query: { kind } },
})),
})
return out
})
/** The key that opens a menu entry (the village entry opens its overview). */
const shortcutOf = (key: string) => SHORTCUTS[(key === 'village' ? 'overview' : key) as keyof typeof SHORTCUTS].toUpperCase()
const home = computed(() => (props.inWorld && current.value?.villageId ? `/village/${current.value.villageId}` : '/worlds'))
const connection = computed(() =>
isMockServer
? { text: t('app.mock'), tone: 'bg-gold-400' }
: ws.is_opened.value
? { text: t('app.connected'), tone: 'bg-moss-500' }
: { text: t('app.offline'), tone: 'bg-blood-600' },
)
const clock = computed(() => new Date(now.value).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }))
// A dropdown closes once one of its links is followed.
const menuKey = ref(0)
watch(() => route.fullPath, () => menuKey.value++)
</script>
<!-- The main menu on every page: one dark bar, every section with its dropdown, the server clock and the account. -->
<template>
<header class="border-b-2 border-wood-950 bg-wood-900 text-sm text-parchment-100">
<div :key="menuKey" class="mx-auto flex h-9 items-stretch px-1 sm:px-2" :class="wide ? '' : 'max-w-[1100px]'">
<NuxtLink :to="home" class="mr-1 shrink-0 items-center gap-1.5 px-1.5" :class="entries.length ? 'hidden sm:flex' : 'flex'" :title="t('app.title')">
<span class="grid size-6 place-items-center border border-gold-600 bg-blood-700 text-gold-400">
<IconBuilding type="headquarters" class="size-4" />
</span>
<span class="hidden font-display text-base font-semibold text-gold-400 lg:inline" style="font-variant: small-caps">{{ t('app.title') }}</span>
</NuxtLink>
<nav v-if="entries.length" class="flex items-stretch" :aria-label="t('app.menu')">
<div v-for="e in entries" :key="e.key" class="group relative flex">
<NuxtLink
:to="e.to"
class="menu-item"
:class="{ 'menu-item-active': e.active }"
:aria-current="e.active ? 'page' : undefined"
:title="t('app.shortcut', { key: shortcutOf(e.key) })"
>
<IconUi :name="e.icon" class="size-4" />
<span class="hidden sm:inline">{{ t(`app.${e.key}`) }}</span>
<span v-if="e.count" class="font-bold text-gold-400 tabular-nums">({{ e.count }})</span>
</NuxtLink>
<div v-if="e.sub.length" class="menu-drop">
<NuxtLink v-for="s in e.sub" :key="s.label" :to="s.to" class="menu-drop-item">{{ s.label }}</NuxtLink>
</div>
</div>
</nav>
<div class="ml-auto flex items-stretch">
<span v-if="world" class="hidden items-center border-l border-wood-700 px-2.5 text-parchment-300 md:flex">{{ world.name }}</span>
<button
v-if="entries.length"
type="button"
class="menu-item border-l"
:class="{ 'menu-item-active': chat.state.value.open }"
:title="t('app.chat')"
@click="chat.toggle()"
>
<IconUi name="chat" class="size-4" />
<span class="hidden sm:inline">{{ t('app.chat') }}</span>
<span v-if="chat.unread.value" class="font-bold text-gold-400 tabular-nums">({{ chat.unread.value }})</span>
</button>
<span v-if="user" class="flex items-center gap-1.5 border-l border-wood-700 px-2.5 tabular-nums text-parchment-200" :title="`${t('app.server_time')} · ${connection.text}`">
<span class="size-1.5 rounded-full" :class="connection.tone" />
{{ clock }}
</span>
<div v-if="user" class="group relative flex">
<button type="button" class="menu-item border-r-0 border-l" :title="t('app.account')">
<IconUi name="users" class="size-4" />
<span class="hidden max-w-28 truncate md:inline">{{ user.name }}</span>
</button>
<div class="menu-drop right-0 left-auto">
<NuxtLink to="/worlds" class="menu-drop-item">{{ t('app.change_world') }}</NuxtLink>
<NuxtLink to="/account" class="menu-drop-item">{{ t('app.account') }}</NuxtLink>
<button type="button" class="menu-drop-item w-full text-left" @click="logout()">{{ t('app.logout') }}</button>
</div>
</div>
</div>
</div>
</header>
</template>
<style scoped>
@reference "~/assets/css/main.css";
.menu-item {
@apply flex items-center gap-1.5 border-r border-wood-700 px-2.5 font-semibold text-parchment-100 hover:bg-wood-800 focus-visible:outline-2 focus-visible:outline-gold-500;
}
.menu-item-active {
@apply bg-wood-800 text-gold-400;
}
/* Shown on hover or keyboard focus; touch devices just follow the entry's own link. */
.menu-drop {
@apply absolute top-full left-0 z-50 hidden min-w-44 border border-wood-950 bg-parchment-50 py-0.5 text-ink-900 shadow-[2px_2px_0_0_rgb(31_21_12/0.4)] group-focus-within:block group-hover:block;
}
.menu-drop-item {
@apply block px-2.5 py-1 font-semibold whitespace-nowrap text-wood-900 hover:bg-parchment-200 hover:text-blood-700;
}
</style>