优化 素材库新增处理优化,提速,其余bug处理

This commit is contained in:
Chen committed 2025-12-04 10:57:50 +08:00
1 parent ad774d8a22
commit 4bce6f5d72
67 files changed
+4861 -3111

No files matched your search

+11 -74
View File
@@ -1,10 +1,9 @@
import { useEffect, useRef, useState, useCallback } from 'react';
import { Pause, Play } from 'lucide-react';
import { useLibraryStore } from '../stores/useLibraryStore';
import { useImageStore } from '../stores/useImageStore';
import { useUIStore } from '../stores/useUIStore';
import { useScanStore } from '../stores/useScanStore';
import { imageAPI, scanAPI } from '../api';
import { imageAPI } from '../api';
import { onUserActionStart, onUserActionEnd } from '../services/imageLoadService';
import requestManager, { RequestType } from '../services/requestManager';
import imageCache from '../utils/imageCache';
@@ -21,9 +20,6 @@ function MainContent() {
const currentRequestContextRef = useRef(null);
// 文件夹切换防抖
const debounceTimerRef = useRef(null);
// 扫描控制
const [scanPaused, setScanPaused] = useState(false);
const [isStoppingOrResuming, setIsStoppingOrResuming] = useState(false);
// 计算预估剩余时间
const getEstimatedTime = () => {
@@ -116,8 +112,6 @@ function MainContent() {
hasMore: hasMore || false,
});
console.log(`[MainContent] Loaded page: +${images.length}, total: ${(isInitialLoad ? images.length : imageLoadingState.loadedCount + images.length)}/${total}`);
// 如果还有更多数据,恢复空闲加载
if (hasMore) {
onUserActionEnd(hasMore);
@@ -187,83 +181,26 @@ function MainContent() {
};
}, [cancelCurrentRequest]);
// 停止扫描
const handleStopScan = async () => {
if (!currentLibraryId || isStoppingOrResuming) return;
setIsStoppingOrResuming(true);
try {
await scanAPI.stop(currentLibraryId);
setScanPaused(true);
} catch (error) {
console.error('Error stopping scan:', error);
} finally {
setIsStoppingOrResuming(false);
}
};
// 继续扫描
const handleResumeScan = async () => {
if (!currentLibraryId || isStoppingOrResuming) return;
setIsStoppingOrResuming(true);
try {
if (scanProgress?.needsRescan) {
await scanAPI.sync(currentLibraryId);
} else {
await scanAPI.resume(currentLibraryId);
}
setScanPaused(false);
} catch (error) {
console.error('Error resuming scan:', error);
} finally {
setIsStoppingOrResuming(false);
}
};
// 同步扫描暂停状态
useEffect(() => {
if (!scanProgress) {
setScanPaused(false);
} else if (scanProgress.isPaused) {
setScanPaused(true);
} else if (scanProgress.percent === 100) {
setScanPaused(false);
}
}, [scanProgress]);
return (
<div className="flex flex-col h-full bg-gray-50 dark:bg-gray-900">
{/* Scan Progress */}
{/* Scan Progress - 简化版,无暂停按钮 */}
{scanProgress && (
<div className={`p-4 border-b ${scanPaused
? 'bg-yellow-50 dark:bg-yellow-900 border-yellow-200 dark:border-yellow-700'
: 'bg-blue-50 dark:bg-blue-900 border-blue-200 dark:border-blue-700'
}`}>
<div className="p-4 border-b bg-blue-50 dark:bg-blue-900 border-blue-200 dark:border-blue-700">
<div className="flex items-center justify-between mb-2">
<div className={`text-sm font-medium ${scanPaused ? 'text-yellow-700 dark:text-yellow-300' : 'text-blue-700 dark:text-blue-300'}`}>
{scanPaused ? '扫描已暂停' : scanProgress?.status === 'preparing' ? '正在准备扫描...' : '正在扫描素材库,期间建议勿操作,否则会影响扫描速度'}
</div>
<div className="flex items-center gap-2">
<span className={`text-sm ${scanPaused ? 'text-yellow-600' : 'text-blue-600'}`}>
{scanProgress?.percent || 0}%
</span>
{(scanProgress?.canStop || scanPaused) && (
<button
onClick={scanPaused ? handleResumeScan : handleStopScan}
disabled={isStoppingOrResuming}
className={`p-1.5 rounded-md ${scanPaused ? 'bg-green-500 hover:bg-green-600' : 'bg-yellow-500 hover:bg-yellow-600'} text-white`}
>
{scanPaused ? <Play className="w-4 h-4" /> : <Pause className="w-4 h-4" />}
</button>
)}
<div className="text-sm font-medium text-blue-700 dark:text-blue-300">
{scanProgress?.status === 'preparing' ? '正在准备扫描...' : '正在扫描素材库...'}
</div>
<span className="text-sm text-blue-600 dark:text-blue-400">
{scanProgress?.percent || 0}%
</span>
</div>
<div className={`w-full rounded-full h-2 mb-2 ${scanPaused ? 'bg-yellow-200' : 'bg-blue-200'}`}>
<div className="w-full rounded-full h-2 mb-2 bg-blue-200 dark:bg-blue-800">
<div
className={`h-2 rounded-full transition-all ${scanPaused ? 'bg-yellow-500' : 'bg-blue-500'}`}
className="h-2 rounded-full transition-all bg-blue-500"
style={{ width: `${scanProgress?.percent || 0}%` }}
/>
</div>
<div className={`text-xs ${scanPaused ? 'text-yellow-600' : 'text-blue-600'}`}>
<div className="text-xs text-blue-600 dark:text-blue-400">
已处理 {scanProgress?.current || 0} / {scanProgress?.total || 0} 张图片
{getEstimatedTime() && ` · ${getEstimatedTime()}`}
</div>
+58 -31
View File
@@ -7,11 +7,11 @@ import { libraryAPI, scanAPI, imageAPI } from '../api';
import requestManager from '../services/requestManager';
import { onUserActionStart } from '../services/imageLoadService';
// 检查素材库是否有暂停的扫描
const checkPausedScan = async (libraryId) => {
// 检查素材库扫描状态
const checkScanStatus = async (libraryId) => {
try {
const response = await scanAPI.getStatus(libraryId);
return response.data;
return response.data || response;
} catch (error) {
return null;
}
@@ -20,6 +20,7 @@ const checkPausedScan = async (libraryId) => {
function Sidebar() {
const { libraries, currentLibraryId, setCurrentLibrary, addLibrary, removeLibrary } = useLibraryStore();
const { folders, selectedFolder, totalImageCount, setSelectedFolder } = useImageStore();
const { isScanning } = useScanStore();
const [showAddLibrary, setShowAddLibrary] = useState(false);
const [newLibraryName, setNewLibraryName] = useState('');
@@ -54,6 +55,12 @@ function Sidebar() {
}, []);
const handleAddLibrary = async () => {
// 扫描期间禁止添加素材库
if (isScanning()) {
alert('扫描进行中,请稍后再试或暂停扫描');
return;
}
if (!newLibraryName.trim()) {
alert('请输入素材库名称');
return;
@@ -137,7 +144,29 @@ function Sidebar() {
console.log('✅ 扫描已启动,请等待进度显示...');
} catch (error) {
console.error('❌ Error adding library:', error);
alert('添加素材库失败: ' + error.message);
// 提取错误信息
let errorMessage = error.message || '未知错误';
// 如果是后端返回的错误响应
if (error.response?.data?.message) {
errorMessage = error.response.data.message;
}
// 特殊处理权限错误
if (errorMessage.includes('无法访问') || errorMessage.includes('权限') || errorMessage.includes('数据共享')) {
alert(
'⚠️ 文件夹权限不足\n\n' +
errorMessage + '\n\n' +
'操作步骤:\n' +
'1. 应用中心找到 FlyPic 应用\n' +
'2. 点击 应用设置\n' +
'3. 将该文件夹添加到 FlyPic 应用的读写权限'
);
} else {
alert('添加素材库失败: ' + errorMessage);
}
useScanStore.getState().setScanProgress(null);
setIsAdding(false);
}
@@ -145,6 +174,12 @@ function Sidebar() {
const handleLibraryClick = async (libraryId) => {
if (libraryId === currentLibraryId) return;
// 扫描期间禁止切换素材库
if (isScanning()) {
alert('扫描进行中,请稍后再试或暂停扫描');
return;
}
setIsSwitching(true);
try {
@@ -178,31 +213,12 @@ function Sidebar() {
useImageStore.getState().setTotalImageCount(countRes.count);
setCurrentLibrary(libraryId); // 最后才更新 currentLibraryId
// 7. 后台检查新素材库是否有暂停的扫描(不阻塞主流程)
checkPausedScan(libraryId).then(scanStatus => {
if (scanStatus && scanStatus.status === 'paused') {
if (scanStatus.needsRescan) {
useScanStore.getState().setScanProgress({
...scanStatus.progress,
libraryId,
canStop: true,
isPaused: true,
pendingCount: scanStatus.progress?.total - scanStatus.progress?.current || 0,
needsRescan: true
});
console.log(`⏸️ 发现中断的扫描,需要继续完成`);
} else if (scanStatus.pendingCount > 0) {
useScanStore.getState().setScanProgress({
...scanStatus.progress,
libraryId,
canStop: true,
isPaused: true,
pendingCount: scanStatus.pendingCount
});
console.log(`⏸️ 发现暂停的扫描,待处理: ${scanStatus.pendingCount} 张`);
}
// 7. 检查新素材库是否正在扫描
checkScanStatus(libraryId).then(scanStatus => {
if (scanStatus && scanStatus.status === 'scanning') {
useScanStore.getState().setScanProgress(scanStatus.progress);
}
}).catch(() => { }); // 忽略状态检查错误
}).catch(() => { });
} catch (error) {
console.error('Error setting current library:', error);
alert('切换素材库失败: ' + error.message);
@@ -215,6 +231,12 @@ function Sidebar() {
const handleDeleteLibrary = async () => {
if (!currentLibraryId) return;
// 扫描期间禁止删除素材库
if (isScanning()) {
alert('扫描进行中,请稍后再试或暂停扫描');
return;
}
const currentLib = libraries.find(lib => lib.id === currentLibraryId);
if (!currentLib) return;
@@ -453,12 +475,16 @@ function Sidebar() {
onChange={(e) => {
const value = e.target.value;
if (value === '__add__') {
if (isScanning()) {
alert('扫描进行中,请稍后再试或暂停扫描');
return;
}
setShowAddLibrary(true);
} else if (value) {
handleLibraryClick(value);
}
}}
disabled={isSwitching}
disabled={isSwitching || isScanning()}
className="w-full px-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 appearance-none cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed"
style={{ paddingRight: '2rem' }}
>
@@ -480,8 +506,9 @@ function Sidebar() {
{currentLibraryId && libraries.length > 0 && (
<button
onClick={handleDeleteLibrary}
className="px-3 py-2 border border-red-300 dark:border-red-600 rounded-lg hover:bg-red-50 dark:hover:bg-red-900 text-red-600 dark:text-red-400 transition-colors"
title="删除当前素材库"
disabled={isScanning()}
className="px-3 py-2 border border-red-300 dark:border-red-600 rounded-lg hover:bg-red-50 dark:hover:bg-red-900 text-red-600 dark:text-red-400 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
title={isScanning() ? "扫描进行中,无法删除" : "删除当前素材库"}
>
<Trash2 className="w-4 h-4" />
</button>