diff --git a/src/components/TopBar/TopBar.vue b/src/components/TopBar/TopBar.vue index b20ebba9..2fc16223 100644 --- a/src/components/TopBar/TopBar.vue +++ b/src/components/TopBar/TopBar.vue @@ -42,17 +42,27 @@ const userInfo = reactive>({}) as UnwrapNestedRe const hideTopBar = ref(false) const hoveringTopBar = ref(false) -const showChannelsPop = ref(false) -const showUserPanelPop = ref(false) -const showNotificationsPop = ref(false) -const showMomentsPop = ref(false) -const showFavoritesPop = ref(false) -const showUploadPop = ref(false) -const showHistoryPop = ref(false) -const showWatchLaterPop = ref(false) -const showMorePop = ref(false) +// const showChannelsPop = ref(false) +// const showUserPanelPop = ref(false) +// const showNotificationsPop = ref(false) +// const showMomentsPop = ref(false) +// const showFavoritesPop = ref(false) +// const showHistoryPop = ref(false) +// const showWatchLaterPop = ref(false) +// const showUploadPop = ref(false) +// const showMorePop = ref(false) -const momentsPopKey = ref(`momentsPop[${Number(new Date())}]`) +const popupVisible = reactive({ + channels: false, + userPanel: false, + notifications: false, + moments: false, + favorites: false, + history: false, + watchLater: false, + upload: false, + more: false, +}) const isLogin = ref(false) const unReadMessage = reactive>( @@ -71,48 +81,114 @@ const momentsPopRef = ref() const scrollTop = ref(0) const oldScrollTop = ref(0) +function closeAllTopBarPopup(exceptionKey?: keyof typeof popupVisible) { + Object.keys(popupVisible).forEach((key) => { + if (key !== exceptionKey) + popupVisible[key as keyof typeof popupVisible] = false + }) +} + +// Channels +const channels = useDelayedHover({ + beforeEnter: () => closeAllTopBarPopup('channels'), + enter: () => { + logo.value.classList.add('activated') + popupVisible.channels = true + }, + leave: () => { + logo.value.classList.remove('activated') + popupVisible.channels = false + }, +}) + // Avatar const avatar = useDelayedHover({ - enter: () => openUserPanel(), - leave: () => closeUserPanel(), + beforeEnter: () => closeAllTopBarPopup('userPanel'), + enter: () => { + popupVisible.userPanel = true + avatarImg.value.classList.add('hover') + avatarShadow.value.classList.add('hover') + }, + beforeLeave: () => { + popupVisible.userPanel = false + avatarImg.value.classList.remove('hover') + avatarShadow.value.classList.remove('hover') + }, + leave: () => { + popupVisible.userPanel = false + avatarImg.value.classList.remove('hover') + avatarShadow.value.classList.remove('hover') + }, }) + // Notifications const notifications = useDelayedHover({ - enter: () => showNotificationsPop.value = true, - leave: () => showNotificationsPop.value = false, + beforeEnter: () => closeAllTopBarPopup('notifications'), + enter: () => { + popupVisible.notifications = true + }, + leave: () => { + popupVisible.notifications = false + }, }) // Moments const moments = useDelayedHover({ + beforeEnter: () => closeAllTopBarPopup('moments'), enter: () => { - showMomentsPop.value = true + popupVisible.moments = true momentsPopRef.value && momentsPopRef.value.checkIfHasNewMomentsThenUpdateMoments() }, - leave: () => showMomentsPop.value = false, + leave: () => { + popupVisible.moments = false + }, }) // Favorites const favorites = useDelayedHover({ - enter: () => showFavoritesPop.value = true, - leave: () => showFavoritesPop.value = false, -}) -// Upload -const upload = useDelayedHover({ - enter: () => showUploadPop.value = true, - leave: () => showUploadPop.value = false, + beforeEnter: () => closeAllTopBarPopup('favorites'), + enter: () => { + popupVisible.favorites = true + }, + leave: () => { + popupVisible.favorites = false + }, }) // History const history = useDelayedHover({ - enter: () => showHistoryPop.value = true, - leave: () => showHistoryPop.value = false, + beforeEnter: () => closeAllTopBarPopup('history'), + enter: () => { + popupVisible.history = true + }, + leave: () => { + popupVisible.history = false + }, }) // Watch Later const watchLater = useDelayedHover({ - enter: () => showWatchLaterPop.value = true, - leave: () => showWatchLaterPop.value = false, + beforeEnter: () => closeAllTopBarPopup('watchLater'), + enter: () => { + popupVisible.watchLater = true + }, + leave: () => { + popupVisible.watchLater = false + }, +}) +// Upload +const upload = useDelayedHover({ + beforeEnter: () => closeAllTopBarPopup('upload'), + enter: () => { + popupVisible.upload = true + }, + leave: () => { + popupVisible.upload = false + }, }) // More const more = useDelayedHover({ - enter: () => showMorePop.value = true, - leave: () => showMorePop.value = false, + beforeEnter: () => closeAllTopBarPopup(), + enter: () => { + popupVisible.more = true + }, + leave: () => popupVisible.more = false, }) watch(() => settings.value.autoHideTopBar, (newVal) => { @@ -121,7 +197,7 @@ watch(() => settings.value.autoHideTopBar, (newVal) => { }) watch( - showNotificationsPop, + () => popupVisible.notifications, (newVal, oldVal) => { if (newVal === oldVal) return @@ -132,7 +208,7 @@ watch( ) watch( - showMomentsPop, + () => popupVisible.moments, async (newVal, oldVal) => { if (newVal === oldVal) return @@ -142,7 +218,7 @@ watch( }, ) -watch(showFavoritesPop, (newVal, oldVal) => { +watch(() => popupVisible.favorites, (newVal, oldVal) => { if (newVal === oldVal) return if (newVal) { @@ -203,28 +279,6 @@ function handleScroll() { oldScrollTop.value = scrollTop.value } -function showLogoMenuDropdown() { - logo.value.classList.add('activated') - showChannelsPop.value = true -} - -function closeLogoMenuDropdown() { - logo.value.classList.remove('activated') - showChannelsPop.value = false -} - -function openUserPanel() { - showUserPanelPop.value = true - avatarImg.value.classList.add('hover') - avatarShadow.value.classList.add('hover') -} - -function closeUserPanel() { - showUserPanelPop.value = false - avatarImg.value.classList.remove('hover') - avatarShadow.value.classList.remove('hover') -} - async function getUserInfo() { try { const res = await browser.runtime @@ -251,7 +305,7 @@ async function getUnreadMessageCount() { if (!isLogin.value) return - unReadMessageCount.value = 0 + let result = 0 try { let res @@ -263,7 +317,7 @@ async function getUnreadMessageCount() { Object.entries(unReadMessage).forEach(([key, value]) => { if (key !== 'up') { if (typeof value === 'number') - unReadMessageCount.value += value + result += value } }) } @@ -274,31 +328,34 @@ async function getUnreadMessageCount() { if (res.code === 0) { Object.assign(unReadDm, res.data) if (typeof unReadDm.follow_unread === 'number') - unReadMessageCount.value += unReadDm.follow_unread + result += unReadDm.follow_unread } } catch (error) { - unReadMessageCount.value = 0 console.error(error) } + finally { + unReadMessageCount.value = result + } } async function getTopBarNewMomentsCount() { if (!isLogin) return - newMomentsCount.value = 0 + let result = 0 try { const res = await browser.runtime.sendMessage({ contentScriptQuery: 'getTopBarNewMomentsCount', }) - - if (typeof res.data.update_info.item.count === 'number') - newMomentsCount.value = res.data.update_info.item.count + if (res.code === 0) { + if (typeof res.data.update_info.item.count === 'number') + result = res.data.update_info.item.count + } } - catch { - newMomentsCount.value = 0 + finally { + newMomentsCount.value = result } } @@ -349,13 +406,12 @@ defineExpose({