Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: implementation of browser tab title #5794

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions ui/console-src/views/system/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import { useRouteQuery } from "@vueuse/router";
import SignupForm from "@/components/signup/SignupForm.vue";
import SocialAuthProviders from "@/components/login/SocialAuthProviders.vue";
import { useGlobalInfoFetch } from "@console/composables/use-global-info";
import { useTitle } from "@vueuse/core";
import { useI18n } from "vue-i18n";
import { AppName } from "@/constants/app";
import MdiKeyboardBackspace from "~icons/mdi/keyboard-backspace";
import LocaleChange from "@/components/common/LocaleChange.vue";
import { useAppTitle } from "@/composables/use-title";

const { globalInfo } = useGlobalInfoFetch();
const { t } = useI18n();

const SIGNUP_TYPE = "signup";

Expand All @@ -32,14 +29,10 @@ function handleChangeType() {

const isLoginType = computed(() => type.value !== SIGNUP_TYPE);

useTitle(
computed(() => {
const siteTitle = globalInfo.value?.siteTitle || AppName;
const routeTitle = t(
`core.${type.value === SIGNUP_TYPE ? SIGNUP_TYPE : "login"}.title`
);
return [routeTitle, siteTitle].join(" - ");
})
useAppTitle(
computed(
() => `core.${type.value === SIGNUP_TYPE ? SIGNUP_TYPE : "login"}.title`
)
);
</script>
<template>
Expand Down
2 changes: 1 addition & 1 deletion ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
name="viewport"
/>
<meta content="noindex,nofollow" name="robots" />
<title>Halo</title>
<title></title>
<link rel="icon" href="/favicon.ico" type="image/png" />
<link rel="icon" href="/favicon.ico" type="image/svg+xml" />
<meta name="msapplication-TileColor" content="#da532c" />
Expand Down
22 changes: 7 additions & 15 deletions ui/src/components/base-app/BaseApp.vue
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
<script lang="ts" setup>
import { RouterView, useRoute } from "vue-router";
import { computed, reactive, onMounted, inject } from "vue";
import { useTitle } from "@vueuse/core";
import { RouterView } from "vue-router";
import { computed, inject, onMounted, reactive } from "vue";
import { useFavicon } from "@vueuse/core";
import { useI18n } from "vue-i18n";
import {
useOverlayScrollbars,
type UseOverlayScrollbarsParams,
} from "overlayscrollbars-vue";
import type { FormKitConfig } from "@formkit/core";
import { i18n } from "@/locales";
import { AppName } from "@/constants/app";
import { useGlobalInfoStore } from "@/stores/global-info";
import { storeToRefs } from "pinia";
import { useAppTitle } from "@/composables/use-title";

const { t } = useI18n();
useAppTitle();

const { globalInfo } = storeToRefs(useGlobalInfoStore());

const route = useRoute();
useTitle(
computed(() => {
const { title: routeTitle } = route.meta;
const siteTitle = globalInfo.value?.siteTitle || AppName;
return [t(routeTitle || ""), siteTitle].filter(Boolean).join(" - ");
})
);

// Favicon
const defaultFavicon = "/console/favicon.ico";
const favicon = computed(() => {
Expand Down Expand Up @@ -66,6 +55,7 @@ formkitConfig.locale = formkitLocales[i18n.global.locale.value] || "zh";
function setViewportProperty(doc: HTMLElement) {
let prevClientHeight: number;
const customVar = "--vh";

function handleResize() {
const clientHeight = doc.clientHeight;
if (clientHeight === prevClientHeight) return;
Expand All @@ -74,9 +64,11 @@ function setViewportProperty(doc: HTMLElement) {
prevClientHeight = clientHeight;
});
}

handleResize();
return handleResize;
}

window.addEventListener(
"resize",
setViewportProperty(document.documentElement)
Expand Down
24 changes: 24 additions & 0 deletions ui/src/composables/use-title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { storeToRefs } from "pinia";
import { useGlobalInfoStore } from "@/stores/global-info";
import { useRoute } from "vue-router";
import { useTitle } from "@vueuse/core";
import { computed, type Ref } from "vue";
import { AppName } from "@/constants/app";
import { useI18n } from "vue-i18n";

export function useAppTitle(baseTitle?: Ref<string>) {
const { globalInfo } = storeToRefs(useGlobalInfoStore());

const { t } = useI18n();
const route = useRoute();

useTitle(
computed(() => {
const { title: routeTitle } = route.meta;
const siteTitle = globalInfo.value?.siteTitle || AppName;
return [t(baseTitle?.value || routeTitle || ""), siteTitle]
.filter(Boolean)
.join(" - ");
})
);
}