Files
NqiTools/desktop/src/shell/Shell.vue
T

140 lines
4.6 KiB
Vue

<script setup lang="ts">
import { ref } from "vue";
import { NButton, NIcon, NLayout, NLayoutSider, NMenu, NTooltip, useDialog } from "naive-ui";
import { LogOutOutline, MoonOutline, PersonOutline, SettingsOutline, SunnyOutline } from "@vicons/ionicons5";
import { logout, type UserInfo } from "../features/auth/api";
import { loadActiveDownloadCount } from "../features/download/api";
import { ApiError } from "../shared/http";
import { isDark, toggleTheme } from "../shared/theme";
import { navOptions, type PageKey } from "./navigation";
import GridDownloadView from "../features/download/GridDownloadView.vue";
import DownloadManagerView from "../features/download/DownloadManagerView.vue";
import SettingsView from "../features/settings/SettingsView.vue";
const props = defineProps<{ user: UserInfo }>();
const emit = defineEmits<{ logout: [] }>();
const dialog = useDialog();
const page = ref<PageKey>("grid");
const expanded = ref<string[]>([]);
const focusId = ref("");
const collapsed = ref(false);
function onMenu(key: string) {
if (key === "grid" || key === "tasks") page.value = key;
}
function onStarted(id: string) {
focusId.value = id;
page.value = "tasks";
}
function askLogout(count: number) {
return new Promise<boolean>((resolve) => {
let settled = false;
const finish = (value: boolean) => {
if (settled) return;
settled = true;
resolve(value);
};
dialog.warning({
title: "退出登录",
content: `有 ${count} 个任务正在下载,退出后会暂停,重新登录后点继续`,
positiveText: "退出",
negativeText: "取消",
maskClosable: false,
onPositiveClick: () => finish(true),
onNegativeClick: () => finish(false),
onClose: () => finish(false),
});
});
}
async function signOut() {
let count = 0;
try {
count = await loadActiveDownloadCount();
} catch (error) {
if (error instanceof ApiError && error.status === 401) {
emit("logout");
return;
}
count = 0;
}
if (count > 0) {
const confirmed = await askLogout(count);
if (!confirmed) return;
}
try {
await logout();
} finally {
emit("logout");
}
}
</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="{ height: '100%', display: 'flex', flexDirection: 'column' }"
@collapse="collapsed = true"
@expand="collapsed = false"
>
<div class="sider-body">
<n-menu
v-model:expanded-keys="expanded"
:collapsed="collapsed"
:collapsed-width="64"
:collapsed-icon-size="22"
:value="page"
:options="navOptions"
@update:value="onMenu"
/>
<div class="sider-user" :class="{ compact: collapsed }">
<n-icon :size="18"><PersonOutline /></n-icon>
<div v-if="!collapsed" class="who">
<strong>{{ props.user.userName || props.user.loginId }}</strong>
<span>{{ props.user.cityName || "已登录" }}</span>
</div>
<n-tooltip>
<template #trigger>
<n-button quaternary circle size="small" :type="page === 'settings' ? 'primary' : 'default'" aria-label="设置" @click="page = 'settings'">
<template #icon><n-icon><SettingsOutline /></n-icon></template>
</n-button>
</template>
设置
</n-tooltip>
<n-tooltip>
<template #trigger>
<n-button quaternary circle size="small" :aria-label="isDark ? '切换到日间' : '切换到夜间'" @click="toggleTheme">
<template #icon><n-icon><MoonOutline v-if="!isDark" /><SunnyOutline v-else /></n-icon></template>
</n-button>
</template>
{{ isDark ? "切换到日间" : "切换到夜间" }}
</n-tooltip>
<n-tooltip>
<template #trigger>
<n-button quaternary circle size="small" aria-label="退出" @click="signOut">
<template #icon><n-icon><LogOutOutline /></n-icon></template>
</n-button>
</template>
退出
</n-tooltip>
</div>
</div>
</n-layout-sider>
<n-layout class="content">
<GridDownloadView v-if="page === 'grid'" :city-name="props.user.cityName" @started="onStarted" />
<DownloadManagerView v-else-if="page === 'tasks'" :focus-id="focusId" />
<SettingsView v-else :login-id="props.user.loginId" />
</n-layout>
</n-layout>
</template>