优化 素材库大小读取逻辑
This commit is contained in:
1 parent
aae226088f
commit
767ac54992
3 files changed
+31
-49
No files matched your search
+26
-29
@@ -116,19 +116,20 @@ function App() {
|
||||
params.folder = imageState.selectedFolder;
|
||||
}
|
||||
|
||||
// 并行加载文件夹和图片
|
||||
// 并行加载文件夹、图片和统计信息
|
||||
Promise.all([
|
||||
imageAPI.getFolders(libraryId),
|
||||
// 如果没有选中文件夹且没有搜索条件,不加载图片(保持在 Dashboard)
|
||||
(imageState.selectedFolder || imageState.searchKeywords || imageState.filters.formats.length > 0)
|
||||
? imageAPI.search(libraryId, params)
|
||||
: Promise.resolve({ images: [] }),
|
||||
// 扫描完成后总是重新获取总数
|
||||
imageAPI.getCount(libraryId)
|
||||
]).then(([foldersRes, imagesRes, countRes]) => {
|
||||
// 扫描完成后重新获取统计信息(包含 totalSize)
|
||||
imageAPI.getStats(libraryId)
|
||||
]).then(([foldersRes, imagesRes, statsRes]) => {
|
||||
useImageStore.getState().setFolders(foldersRes.folders);
|
||||
useImageStore.getState().setImages(imagesRes.images);
|
||||
useImageStore.getState().setTotalImageCount(countRes.count);
|
||||
useImageStore.getState().setTotalImageCount(statsRes.total || 0);
|
||||
useImageStore.getState().setTotalSize(statsRes.totalSize || 0);
|
||||
}).catch(err => {
|
||||
console.error('❌ 加载数据失败:', err.message);
|
||||
}).finally(() => {
|
||||
@@ -155,6 +156,24 @@ function App() {
|
||||
};
|
||||
}, []);
|
||||
|
||||
// 加载文件夹和统计信息(需要在 loadLibraries 之前定义)
|
||||
const loadFolders = async (libraryId) => {
|
||||
try {
|
||||
// 并行获取文件夹和统计信息
|
||||
const [foldersResponse, statsResponse] = await Promise.all([
|
||||
imageAPI.getFolders(libraryId),
|
||||
imageAPI.getStats(libraryId)
|
||||
]);
|
||||
|
||||
useImageStore.getState().setFolders(foldersResponse.folders);
|
||||
// 注意:后端返回的字段是 total 和 totalSize
|
||||
useImageStore.getState().setTotalImageCount(statsResponse.total || 0);
|
||||
useImageStore.getState().setTotalSize(statsResponse.totalSize || 0);
|
||||
} catch (error) {
|
||||
console.error('❌ 加载文件夹失败:', error.message);
|
||||
}
|
||||
};
|
||||
|
||||
const loadLibraries = async () => {
|
||||
try {
|
||||
const response = await libraryAPI.getAll();
|
||||
@@ -177,21 +196,8 @@ function App() {
|
||||
}
|
||||
|
||||
if (libId) {
|
||||
// 并行加载图片和文件夹(加快初始加载速度)
|
||||
// 优化:启动时不加载全部图片,只加载文件夹
|
||||
const [foldersRes] = await Promise.all([
|
||||
imageAPI.getFolders(libId)
|
||||
]);
|
||||
|
||||
useImageStore.getState().setFolders(foldersRes.folders);
|
||||
|
||||
// 获取总数
|
||||
try {
|
||||
const countRes = await imageAPI.getCount(libId);
|
||||
useImageStore.getState().setTotalImageCount(countRes.count);
|
||||
} catch (e) {
|
||||
console.error('❌ 获取图片总数失败:', e.message);
|
||||
}
|
||||
// 加载文件夹和统计信息(包含 totalSize)
|
||||
await loadFolders(libId);
|
||||
|
||||
// 后台检查当前素材库的扫描状态
|
||||
scanAPI.getStatus(libId).then(scanStatus => {
|
||||
@@ -224,15 +230,6 @@ function App() {
|
||||
}
|
||||
};
|
||||
|
||||
const loadFolders = async (libraryId) => {
|
||||
try {
|
||||
const response = await imageAPI.getFolders(libraryId);
|
||||
useImageStore.getState().setFolders(response.folders);
|
||||
} catch (error) {
|
||||
console.error('❌ 加载文件夹失败:', error.message);
|
||||
}
|
||||
};
|
||||
|
||||
// 保存面板宽度
|
||||
const savePanelWidths = async (left, right) => {
|
||||
try {
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import React from 'react';
|
||||
import { useLibraryStore } from '../stores/useLibraryStore';
|
||||
import { useImageStore } from '../stores/useImageStore';
|
||||
import { useUIStore } from '../stores/useUIStore';
|
||||
import { useScanStore } from '../stores/useScanStore';
|
||||
import { Folder, Image as ImageIcon, HardDrive } from 'lucide-react';
|
||||
import { imageAPI } from '../api';
|
||||
|
||||
function Dashboard() {
|
||||
const { currentLibraryId, getCurrentLibrary } = useLibraryStore();
|
||||
const { totalImageCount, folders } = useImageStore();
|
||||
const { totalImageCount, totalSize, folders } = useImageStore();
|
||||
|
||||
const currentLibrary = getCurrentLibrary();
|
||||
const [librarySize, setLibrarySize] = useState(0);
|
||||
|
||||
// Calculate stats
|
||||
const folderCount = folders.length;
|
||||
@@ -19,19 +15,6 @@ function Dashboard() {
|
||||
// Group folders by parent to find top-level folders
|
||||
const topLevelFolders = folders.filter(f => !f.parentPath || f.parentPath === '.' || f.parentPath === '');
|
||||
|
||||
// Fetch library size
|
||||
useEffect(() => {
|
||||
if (currentLibraryId) {
|
||||
imageAPI.getStats(currentLibraryId)
|
||||
.then(res => {
|
||||
setLibrarySize(res.totalSize || 0);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Failed to fetch library stats:', err);
|
||||
});
|
||||
}
|
||||
}, [currentLibraryId, totalImageCount]); // Re-fetch when library or image count changes
|
||||
|
||||
// Format bytes to human readable format
|
||||
const formatBytes = (bytes) => {
|
||||
if (bytes === 0) return '0 B';
|
||||
@@ -90,7 +73,7 @@ function Dashboard() {
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
{formatBytes(librarySize)}
|
||||
{formatBytes(totalSize)}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500 dark:text-gray-400">素材库大小</div>
|
||||
</div>
|
||||
|
||||
@@ -11,6 +11,7 @@ export const useImageStore = create((set, get) => ({
|
||||
selectedImage: null,
|
||||
selectedImages: [],
|
||||
totalImageCount: 0,
|
||||
totalSize: 0, // 素材库总大小(字节)
|
||||
|
||||
// 文件夹
|
||||
folders: [],
|
||||
@@ -88,6 +89,7 @@ export const useImageStore = create((set, get) => ({
|
||||
|
||||
// 统计
|
||||
setTotalImageCount: (count) => set({ totalImageCount: count }),
|
||||
setTotalSize: (size) => set({ totalSize: size }),
|
||||
|
||||
// 加载状态
|
||||
setImageLoadingState: (state) => set((prev) => ({
|
||||
|
||||
Reference in new issue
Block a user