Files
NixMsg/web/src/layouts/AdminLayout.vue
T

201 lines
5.3 KiB
Vue

<script setup lang="ts">
import { computed, h, ref, type Component } from "vue";
import { RouterView, useRoute, useRouter } from "vue-router";
import { NAvatar, NButton, NDropdown, NIcon, NLayout, NLayoutContent, NLayoutSider, NMenu, NTooltip } from "naive-ui";
import type { MenuOption } from "naive-ui";
import {
DesktopOutline,
GridOutline,
KeyOutline,
LogOutOutline,
MoonOutline,
PaperPlaneOutline,
PeopleOutline,
PhonePortraitOutline,
SettingsOutline,
SunnyOutline,
} from "@vicons/ionicons5";
import { useAuthStore } from "@/stores/auth";
import { useAppTheme, type ThemeChoice } from "@/theme";
import BrandMark from "@/components/BrandMark.vue";
const route = useRoute();
const router = useRouter();
const auth = useAuthStore();
const { choice, setChoice } = useAppTheme();
const collapsed = ref(false);
function renderIcon(icon: Component) {
return () => h(NIcon, null, { default: () => h(icon) });
}
const menuOptions: MenuOption[] = [
{ label: "概览", key: "/overview", icon: renderIcon(GridOutline) },
{ label: "设备端", key: "/endpoints", icon: renderIcon(PhonePortraitOutline) },
{ label: "群组", key: "/groups", icon: renderIcon(PeopleOutline) },
{ label: "投递记录", key: "/messages", icon: renderIcon(PaperPlaneOutline) },
{ label: "API 令牌", key: "/tokens", icon: renderIcon(KeyOutline) },
{ label: "设置", key: "/settings", icon: renderIcon(SettingsOutline) },
];
const activeKey = computed(() => route.path);
const initial = computed(() => (auth.username || "A").slice(0, 1).toUpperCase());
const themeIcon = computed(() => {
if (choice.value === "dark") return MoonOutline;
if (choice.value === "light") return SunnyOutline;
return DesktopOutline;
});
const themeLabel = computed(() => {
if (choice.value === "dark") return "深色";
if (choice.value === "light") return "浅色";
return "跟随系统";
});
const themeOptions = [
{ label: "跟随系统", key: "auto", icon: renderIcon(DesktopOutline) },
{ label: "浅色", key: "light", icon: renderIcon(SunnyOutline) },
{ label: "深色", key: "dark", icon: renderIcon(MoonOutline) },
];
function onMenuUpdate(key: string) {
if (key.startsWith("/")) void router.push(key);
}
function onTheme(key: string) {
setChoice(key as ThemeChoice);
}
async function onLogout() {
await auth.logout();
void router.push({ name: "login" });
}
</script>
<template>
<n-layout has-sider class="shell">
<n-layout-sider
bordered
collapse-mode="width"
:collapsed-width="64"
:width="220"
:collapsed="collapsed"
show-trigger
:native-scrollbar="false"
content-style="display: flex; flex-direction: column; height: 100%;"
@collapse="collapsed = true"
@expand="collapsed = false"
>
<div class="brand" :class="{ 'is-collapsed': collapsed }">
<BrandMark class="mark" />
<span v-if="!collapsed" class="word">NixMsg</span>
</div>
<n-menu
class="nav"
:value="activeKey"
:collapsed="collapsed"
:collapsed-width="64"
:collapsed-icon-size="20"
:icon-size="18"
:options="menuOptions"
@update:value="onMenuUpdate"
/>
<div class="sider-foot" :class="{ 'is-collapsed': collapsed }">
<div class="account">
<n-dropdown trigger="click" placement="top-start" :options="themeOptions" @select="onTheme">
<n-button quaternary circle :aria-label="`主题:${themeLabel}`" :title="themeLabel">
<template #icon>
<n-icon :component="themeIcon" />
</template>
</n-button>
</n-dropdown>
<n-avatar round :size="28" color="#0969da">{{ initial }}</n-avatar>
<span v-if="!collapsed" class="name">{{ auth.username }}</span>
<n-tooltip placement="right">
<template #trigger>
<n-button class="logout" quaternary circle aria-label="退出" @click="onLogout">
<template #icon>
<n-icon :component="LogOutOutline" />
</template>
</n-button>
</template>
退出
</n-tooltip>
</div>
</div>
</n-layout-sider>
<n-layout-content :native-scrollbar="false" content-style="height: 100vh; display: flex; flex-direction: column; overflow: hidden">
<RouterView />
</n-layout-content>
</n-layout>
</template>
<style scoped>
.shell {
height: 100vh;
}
.brand {
height: 56px;
display: flex;
align-items: center;
gap: 10px;
padding: 0 16px;
flex-shrink: 0;
}
.brand.is-collapsed {
padding: 0;
justify-content: center;
}
.mark {
font-size: 28px;
flex-shrink: 0;
}
.word {
font-size: 16px;
font-weight: 650;
letter-spacing: 0.01em;
min-width: 0;
}
.nav {
flex: 1;
min-height: 0;
}
.sider-foot {
flex-shrink: 0;
margin: 0 12px 12px;
padding-top: 10px;
border-top: 1px solid rgba(128, 128, 128, 0.18);
}
.sider-foot.is-collapsed {
margin: 0 0 12px;
}
.account {
width: 100%;
min-height: 36px;
display: flex;
align-items: center;
gap: 8px;
}
.sider-foot.is-collapsed .account {
width: 64px;
flex-wrap: wrap;
justify-content: center;
gap: 4px;
}
.name {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 13px;
}
.logout {
margin-left: auto;
}
.sider-foot.is-collapsed .logout {
margin-left: 0;
}
</style>