Переработать счётчик непрочитанных на плоский ref

Вместо мутации вложенных свойств дерева папок использовать
отдельный плоский ref<Record<string, number>> (ключ accountId:folderId).
Это гарантирует реактивность при любых изменениях счётчика.

- folders.store: unreadCounts, initUnreadCounts, getFolderUnread
- MessageList: activeUnread computed читает из getFolderUnread
- FolderTreeItem: unreadCount computed читает из getFolderUnread,
  не зависит от folder.unreadCount prop

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
vodyanoysh 2026-03-31 23:05:24 +07:00
parent df5e7f430b
commit c1a49cc627
3 changed files with 36 additions and 21 deletions

View file

@ -16,6 +16,10 @@ const selectMode = ref(false)
const folderName = computed(() => foldersStore.activeFolder?.name ?? '') const folderName = computed(() => foldersStore.activeFolder?.name ?? '')
const hasSelected = computed(() => messagesStore.selectedIds.size > 0) const hasSelected = computed(() => messagesStore.selectedIds.size > 0)
const activeUnread = computed(() => {
if (!accountsStore.activeAccountId || !foldersStore.activeFolderId) return 0
return foldersStore.getFolderUnread(accountsStore.activeAccountId, foldersStore.activeFolderId)
})
const allVisibleSelected = computed(() => const allVisibleSelected = computed(() =>
messagesStore.filteredMessages.length > 0 && messagesStore.filteredMessages.length > 0 &&
messagesStore.filteredMessages.every((m) => messagesStore.selectedIds.has(m.id)) messagesStore.filteredMessages.every((m) => messagesStore.selectedIds.has(m.id))
@ -136,13 +140,13 @@ async function handleSwipeAction(action: SwipeAction, messageId: string): Promis
<div class="message-list-header"> <div class="message-list-header">
<h2 class="folder-title truncate">{{ folderName }}</h2> <h2 class="folder-title truncate">{{ folderName }}</h2>
<button <button
v-if="(foldersStore.activeFolder?.unreadCount ?? 0) > 0" v-if="activeUnread > 0"
class="message-count" class="message-count"
:class="{ 'unread-active': messagesStore.unreadOnly }" :class="{ 'unread-active': messagesStore.unreadOnly }"
title="Только непрочитанные" title="Только непрочитанные"
@click="messagesStore.unreadOnly = !messagesStore.unreadOnly" @click="messagesStore.unreadOnly = !messagesStore.unreadOnly"
> >
{{ foldersStore.activeFolder?.unreadCount }} {{ activeUnread }}
</button> </button>
<div class="header-actions"> <div class="header-actions">
<button <button

View file

@ -3,6 +3,7 @@ import { ref, computed } from 'vue'
import type { Folder } from '@shared/types/folder' import type { Folder } from '@shared/types/folder'
import { SpecialFolderRole } from '@shared/types/folder' import { SpecialFolderRole } from '@shared/types/folder'
import { useFoldersStore } from '../../stores/folders.store' import { useFoldersStore } from '../../stores/folders.store'
import { useAccountsStore } from '../../stores/accounts.store'
import AppIcon from '../AppIcon.vue' import AppIcon from '../AppIcon.vue'
import type { IconName } from '../AppIcon.vue' import type { IconName } from '../AppIcon.vue'
@ -12,10 +13,15 @@ const props = defineProps<{
}>() }>()
const foldersStore = useFoldersStore() const foldersStore = useFoldersStore()
const accountsStore = useAccountsStore()
const expanded = ref(true) const expanded = ref(true)
const isActive = computed(() => foldersStore.activeFolderId === props.folder.id) const isActive = computed(() => foldersStore.activeFolderId === props.folder.id)
const hasChildren = computed(() => props.folder.children.length > 0) const hasChildren = computed(() => props.folder.children.length > 0)
const unreadCount = computed(() => {
if (!accountsStore.activeAccountId) return 0
return foldersStore.getFolderUnread(accountsStore.activeAccountId, props.folder.id)
})
const folderIcon = computed((): IconName => { const folderIcon = computed((): IconName => {
switch (props.folder.role) { switch (props.folder.role) {
@ -59,8 +65,8 @@ function toggleExpand(event: Event): void {
<AppIcon :name="folderIcon" :size="14" class="folder-icon" /> <AppIcon :name="folderIcon" :size="14" class="folder-icon" />
<span class="folder-name truncate">{{ folder.name }}</span> <span class="folder-name truncate">{{ folder.name }}</span>
<span v-if="folder.unreadCount > 0" class="unread-badge"> <span v-if="unreadCount > 0" class="unread-badge">
{{ folder.unreadCount }} {{ unreadCount }}
</span> </span>
</button> </button>

View file

@ -13,6 +13,10 @@ export const useFoldersStore = defineStore('folders', () => {
const activeFolderId = ref<string | null>(null) const activeFolderId = ref<string | null>(null)
const loading = ref(false) const loading = ref(false)
// Плоский счётчик непрочитанных: ключ `${accountId}:${folderId}`
// Хранится отдельно от дерева — гарантированно реактивен
const unreadCounts = ref<Record<string, number>>({})
const folders = computed(() => { const folders = computed(() => {
if (!accountsStore.activeAccountId) return [] if (!accountsStore.activeAccountId) return []
return foldersByAccount.value[accountsStore.activeAccountId] ?? [] return foldersByAccount.value[accountsStore.activeAccountId] ?? []
@ -27,6 +31,7 @@ export const useFoldersStore = defineStore('folders', () => {
try { try {
const result = await invoke('folders:list', { accountId }) const result = await invoke('folders:list', { accountId })
foldersByAccount.value[accountId] = result.folders foldersByAccount.value[accountId] = result.folders
initUnreadCounts(accountId, result.folders)
// Auto-select inbox // Auto-select inbox
const inbox = findFolderByRole(result.folders, SpecialFolderRole.Inbox) const inbox = findFolderByRole(result.folders, SpecialFolderRole.Inbox)
@ -44,10 +49,21 @@ export const useFoldersStore = defineStore('folders', () => {
activeFolderId.value = folderId activeFolderId.value = folderId
} }
function initUnreadCounts(accountId: string, folders: Folder[]): void {
for (const f of folders) {
unreadCounts.value[`${accountId}:${f.id}`] = f.unreadCount
if (f.children.length > 0) initUnreadCounts(accountId, f.children)
}
}
function updateFolderUnread(accountId: string, folderId: string, delta: number): void { function updateFolderUnread(accountId: string, folderId: string, delta: number): void {
const tree = foldersByAccount.value[accountId] const key = `${accountId}:${folderId}`
if (!tree) return const current = unreadCounts.value[key] ?? 0
patchUnreadInTree(tree, folderId, delta) unreadCounts.value[key] = Math.max(0, current + delta)
}
function getFolderUnread(accountId: string, folderId: string): number {
return unreadCounts.value[`${accountId}:${folderId}`] ?? 0
} }
// Auto-load folders when active account changes // Auto-load folders when active account changes
@ -74,28 +90,17 @@ export const useFoldersStore = defineStore('folders', () => {
return { return {
foldersByAccount, foldersByAccount,
folders, folders,
unreadCounts,
activeFolderId, activeFolderId,
activeFolder, activeFolder,
loading, loading,
loadFolders, loadFolders,
setActiveFolder, setActiveFolder,
updateFolderUnread updateFolderUnread,
getFolderUnread
} }
}) })
// Заменяет элемент массива целиком — гарантированно триггерит Vue-реактивность
function patchUnreadInTree(folders: Folder[], folderId: string, delta: number): boolean {
for (let i = 0; i < folders.length; i++) {
if (folders[i].id === folderId) {
folders[i] = { ...folders[i], unreadCount: Math.max(0, folders[i].unreadCount + delta) }
return true
}
if (folders[i].children.length > 0 && patchUnreadInTree(folders[i].children, folderId, delta)) {
return true
}
}
return false
}
function findFolderById(folders: Folder[], id: string | null): Folder | null { function findFolderById(folders: Folder[], id: string | null): Folder | null {
if (!id) return null if (!id) return null