Skip to content

Commit

Permalink
fixup! fix: refactor watcher for shouldShowAsterisk
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Nov 6, 2024
1 parent c1ba8f4 commit e469487
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/composables/useDocumentTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function useDocumentTitle() {
const isDocumentVisible = useDocumentVisibility()

const defaultPageTitle = ref<string>(getDefaultPageTitle())
const showAsterisk = ref(false)
const savedLastMessageMap = ref<Record<string, number>>({})

const conversationList = computed(() => store.getters.conversationsList)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -112,30 +113,28 @@ 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('')
}
}

/**
* 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
}
Expand Down

0 comments on commit e469487

Please sign in to comment.