From e469487ceca230a8af4e003986aaa5f36fd5ffa2 Mon Sep 17 00:00:00 2001 From: Maksim Sukharev Date: Wed, 6 Nov 2024 12:19:27 +0100 Subject: [PATCH] fixup! fix: refactor watcher for shouldShowAsterisk Signed-off-by: Maksim Sukharev --- src/composables/useDocumentTitle.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/composables/useDocumentTitle.ts b/src/composables/useDocumentTitle.ts index 85c56dbb415..7b2a11ee833 100644 --- a/src/composables/useDocumentTitle.ts +++ b/src/composables/useDocumentTitle.ts @@ -23,6 +23,7 @@ export function useDocumentTitle() { const isDocumentVisible = useDocumentVisibility() const defaultPageTitle = ref(getDefaultPageTitle()) + const showAsterisk = ref(false) const savedLastMessageMap = ref>({}) const conversationList = computed(() => store.getters.conversationsList) @@ -46,16 +47,16 @@ export function useDocumentTitle() { && newLastMessageMap[token] !== -1) // But is not from the current user }) if (shouldShowAsterisk) { - document.title = '* ' + document.title + showAsterisk.value = true + setPageTitleFromRoute(Router.currentRoute) } }) watch(isDocumentVisible, () => { if (isDocumentVisible.value) { // Remove asterisk for unread chat messages - if (document.title.startsWith('* ')) { - document.title = document.title.substring(2) - } + showAsterisk.value = false + setPageTitleFromRoute(Router.currentRoute) } else { // Copy the last message map to the saved version, // this will be our reference to check if any chat got a new @@ -68,13 +69,13 @@ export function useDocumentTitle() { * Adjust the page title to the conversation name once conversationsList is loaded */ EventBus.once('conversations-received', () => { - setPageTitleFromRoute(Router.currentRoute, false) + setPageTitleFromRoute(Router.currentRoute) }) /** * Change the page title after the route was changed */ - Router.afterEach((to) => setPageTitleFromRoute(to, false)) + Router.afterEach((to) => setPageTitleFromRoute(to)) /** * Get a list for all last message ids @@ -112,18 +113,17 @@ export function useDocumentTitle() { /** * * @param route current web route - * @param showAsterisk Whether to show unread indicator */ - function setPageTitleFromRoute(route: Route, showAsterisk: boolean) { + function setPageTitleFromRoute(route: Route) { switch (route.name) { case 'conversation': - setPageTitle(store.getters.conversation(route.params.token)?.displayName ?? '', showAsterisk) + setPageTitle(store.getters.conversation(route.params.token)?.displayName ?? '') break case 'duplicatesession': - setPageTitle(t('spreed', 'Duplicate session'), showAsterisk) + setPageTitle(t('spreed', 'Duplicate session')) break default: - setPageTitle('', showAsterisk) + setPageTitle('') } } @@ -131,11 +131,10 @@ export function useDocumentTitle() { * Set the page title to the conversation name * * @param title Prefix for the page title e.g. conversation name - * @param showAsterisk Whether to show unread indicator */ - function setPageTitle(title: string, showAsterisk: boolean) { + function setPageTitle(title: string) { const newTitle = title ? `${title} - ${defaultPageTitle.value}` : defaultPageTitle.value - document.title = (showAsterisk && !newTitle.startsWith('* ')) + document.title = (showAsterisk.value && !newTitle.startsWith('* ')) ? '* ' + newTitle : newTitle }