日志优化
This commit is contained in:
1 parent
fa5bb66d14
commit
aeb403f3c0
21 files changed
+402
-209
No files matched your search
@@ -6,6 +6,9 @@ import { useUIStore } from '../stores/useUIStore';
|
||||
import { useScanStore } from '../stores/useScanStore';
|
||||
import { useTheme } from '../hooks/useTheme';
|
||||
import { libraryAPI, scanAPI, watchAPI } from '../api';
|
||||
import { createLogger } from '../utils/logger';
|
||||
|
||||
const logger = createLogger('Header');
|
||||
|
||||
function Header() {
|
||||
const { currentLibraryId } = useLibraryStore();
|
||||
|
||||
@@ -137,27 +137,23 @@ function ImageWaterfall() {
|
||||
useImageKeyboard({
|
||||
onDelete: handleQuickDeleteWithUndoStack,
|
||||
onUndo: async () => {
|
||||
console.log('🔍 撤销前状态:', {
|
||||
undoStack: [...undoStack],
|
||||
moveUndoHistory: moveUndoHistory.length,
|
||||
deleteUndoHistory: undoHistory.length
|
||||
});
|
||||
// 检查撤销栏状态
|
||||
|
||||
if (undoStack.length === 0) return;
|
||||
const last = undoStack[undoStack.length - 1];
|
||||
setUndoStack(prev => prev.slice(0, -1));
|
||||
|
||||
console.log(`🎯 准备撤销: ${last}`);
|
||||
// 准备撤销操作
|
||||
|
||||
if (last === 'move' && moveUndoHistory.length > 0) {
|
||||
console.log('↩️ 执行移动撤销');
|
||||
// 执行移动撤销
|
||||
await handleUndoMove();
|
||||
} else if (last === 'delete' && undoHistory.length > 0) {
|
||||
console.log('↩️ 执行删除撤销');
|
||||
// 执行删除撤销
|
||||
await handleUndo();
|
||||
}
|
||||
|
||||
console.log('✅ 撤销完成');
|
||||
// 撤销完成
|
||||
},
|
||||
onCopy: () => {
|
||||
const result = handleCopy();
|
||||
|
||||
@@ -9,6 +9,9 @@ import requestManager, { RequestType } from '../services/requestManager';
|
||||
import domCleanup from '../utils/domCleanup';
|
||||
import ImageWaterfall from './ImageWaterfall';
|
||||
import Dashboard from './Dashboard';
|
||||
import { createLogger } from '../utils/logger';
|
||||
|
||||
const logger = createLogger('MainContent');
|
||||
|
||||
function MainContent() {
|
||||
const { currentLibraryId } = useLibraryStore();
|
||||
@@ -117,7 +120,7 @@ function MainContent() {
|
||||
if (error.name === 'CanceledError' || error.name === 'AbortError') {
|
||||
return;
|
||||
}
|
||||
console.error('Error loading images:', error);
|
||||
logger.error('加载图片失败:', error.message);
|
||||
requestManager.error(requestContext.id);
|
||||
} finally {
|
||||
// 只有当请求仍然有效时才更新状态
|
||||
@@ -167,7 +170,7 @@ function MainContent() {
|
||||
const { setSelectedFolder } = useImageStore.getState();
|
||||
setSelectedFolder(restoreFolder);
|
||||
localStorage.removeItem('flypic_restore_folder');
|
||||
console.log('✅ 已恢复到文件夹:', restoreFolder);
|
||||
logger.data('恢复文件夹:', restoreFolder);
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ function RightPanel() {
|
||||
filename: actualNewName
|
||||
});
|
||||
|
||||
logger.debug(`✅ 重命名成功: ${oldFilename} → ${actualNewName}`);
|
||||
logger.file(`重命名成功: ${oldFilename} → ${actualNewName}`);
|
||||
} catch (error) {
|
||||
logger.error('重命名失败:', error);
|
||||
alert('重命名失败: ' + (error.message || '未知错误'));
|
||||
@@ -230,7 +230,7 @@ function RightPanel() {
|
||||
const result = await fileAPI.rename(currentLibraryId, oldPath, newName);
|
||||
const newPath = result.newPath;
|
||||
|
||||
logger.debug(`✅ 文件夹重命名成功: ${oldName} → ${newName}, 路径: ${oldPath} → ${newPath}`);
|
||||
logger.file(`文件夹重命名: ${oldName} → ${newName}`);
|
||||
|
||||
const { setFolders, setSelectedFolder: setSelectedFolderGlobal, setSelectedFolderItem } = useImageStore.getState();
|
||||
|
||||
@@ -241,7 +241,7 @@ function RightPanel() {
|
||||
});
|
||||
|
||||
if (isRenamingCurrentFolder) {
|
||||
logger.debug(`📂 重命名当前文件夹: ${oldPath} → ${newPath}`);
|
||||
logger.file(`重命名当前文件夹: ${oldPath} → ${newPath}`);
|
||||
setSelectedFolderGlobal(newPath);
|
||||
|
||||
// 重命名当前浏览的文件夹时,立即刷新该文件夹的图片列表,避免数量显示为 0
|
||||
@@ -256,7 +256,7 @@ function RightPanel() {
|
||||
}
|
||||
|
||||
imageAPI.getFolders(currentLibraryId).then(foldersRes => {
|
||||
logger.debug('📁 重命名后最新文件夹列表:', foldersRes.folders);
|
||||
logger.data('重命名后刷新文件夹列表');
|
||||
setFolders(foldersRes.folders);
|
||||
|
||||
const newFolderItem = foldersRes.folders.find(f => f.path === newPath);
|
||||
@@ -319,7 +319,7 @@ function RightPanel() {
|
||||
setSelectedImages(updatedSelectedImages);
|
||||
}
|
||||
|
||||
logger.debug(`✅ 已更新 ${paths.length} 张图片的评分为 ${newRating} 星`);
|
||||
logger.file(`已更新 ${paths.length} 张图片的评分为 ${newRating} 星`);
|
||||
} catch (error) {
|
||||
logger.error('更新评分失败:', error);
|
||||
alert('更新评分失败: ' + (error.message || '未知错误'));
|
||||
@@ -364,7 +364,7 @@ function RightPanel() {
|
||||
// 对于不支持的格式,只显示缩略图
|
||||
if (!shouldLoadOriginal) {
|
||||
setIsLoadingOriginal(false);
|
||||
logger.debug(`格式 ${imageFormat} 不支持直接显示原图,使用缩略图`);
|
||||
// 不支持直接显示原图,使用缩略图
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -593,7 +593,7 @@ function RightPanel() {
|
||||
// 1. 先写入应用内剪贴板(同步,用于应用内粘贴)
|
||||
const itemsToCopy = [{ type: 'file', path: selectedImage.path, data: selectedImage }];
|
||||
copyToClipboard(itemsToCopy, 'copy');
|
||||
logger.debug(`📋 已复制 1 个文件到应用内剪贴板`);
|
||||
logger.file('已复制 1 个文件到应用内剪贴板');
|
||||
|
||||
// 2. 获取原图URL,写入系统剪贴板
|
||||
const imageUrl = imageAPI.getOriginalUrl(currentLibraryId, selectedImage.path);
|
||||
@@ -645,7 +645,7 @@ function RightPanel() {
|
||||
}
|
||||
|
||||
// 方案2:使用 canvas + contenteditable + execCommand(非 HTTPS 环境)
|
||||
logger.debug('尝试 Canvas + execCommand 方案...');
|
||||
// 尝试 Canvas 方案
|
||||
const canvasSuccess = await fallbackCopyImageViaCanvas(imageUrl);
|
||||
if (canvasSuccess) {
|
||||
setImageCopied(true);
|
||||
@@ -654,7 +654,7 @@ function RightPanel() {
|
||||
}
|
||||
|
||||
// 方案3:直接使用图片 URL + contenteditable
|
||||
logger.debug('尝试直接图片 URL 方案...');
|
||||
// 尝试直接 URL 方案
|
||||
const directSuccess = await fallbackCopyImage(imageUrl);
|
||||
if (directSuccess) {
|
||||
setImageCopied(true);
|
||||
@@ -778,7 +778,7 @@ function RightPanel() {
|
||||
// 1. 先写入应用内剪贴板(同步,用于应用内粘贴)
|
||||
const itemsToCopy = imagesToCopy.map(img => ({ type: 'file', path: img.path, data: img }));
|
||||
copyToClipboard(itemsToCopy, 'copy');
|
||||
logger.debug(`📋 已复制 ${itemsToCopy.length} 个文件到应用内剪贴板`);
|
||||
logger.file(`已复制 ${itemsToCopy.length} 个文件到应用内剪贴板`);
|
||||
|
||||
if (imagesToCopy.length === 1) {
|
||||
// 单张图片:直接复制
|
||||
@@ -837,7 +837,7 @@ function RightPanel() {
|
||||
}
|
||||
|
||||
// 方案2:使用 contenteditable + execCommand
|
||||
logger.debug('尝试 contenteditable 批量复制方案...');
|
||||
// 尝试批量复制
|
||||
const success = await fallbackCopyMultipleImages(imageUrls);
|
||||
if (success) {
|
||||
setImageCopied(true);
|
||||
@@ -901,7 +901,7 @@ function RightPanel() {
|
||||
const progress = Math.round(((i + 1) / imagesToExport.length) * 90); // 90% 用于下载
|
||||
setExportProgress(progress);
|
||||
|
||||
logger.debug(`已添加: ${img.filename} (${i + 1}/${imagesToExport.length})`);
|
||||
// 已添加到 ZIP
|
||||
} catch (error) {
|
||||
logger.error(`下载失败: ${img.filename}`, error);
|
||||
}
|
||||
@@ -909,7 +909,7 @@ function RightPanel() {
|
||||
|
||||
// 生成 ZIP 文件
|
||||
setExportProgress(95);
|
||||
logger.debug('正在生成 ZIP 文件...');
|
||||
// 生成 ZIP
|
||||
const zipBlob = await zip.generateAsync({
|
||||
type: 'blob',
|
||||
compression: 'DEFLATE',
|
||||
@@ -926,7 +926,7 @@ function RightPanel() {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(link.href);
|
||||
|
||||
logger.debug('导出完成!');
|
||||
logger.data('导出完成');
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('导出失败:', error);
|
||||
@@ -982,7 +982,7 @@ function RightPanel() {
|
||||
const progress = Math.round(((i + 1) / folderImages.length) * 90);
|
||||
setFolderExportProgress(progress);
|
||||
|
||||
logger.debug(`已添加: ${filePath} (${i + 1}/${folderImages.length})`);
|
||||
// 已添加到 ZIP
|
||||
} catch (error) {
|
||||
logger.error(`下载失败: ${img.filename}`, error);
|
||||
}
|
||||
@@ -990,7 +990,7 @@ function RightPanel() {
|
||||
|
||||
// 生成 ZIP 文件
|
||||
setFolderExportProgress(95);
|
||||
logger.debug('正在生成 ZIP 文件...');
|
||||
// 生成 ZIP
|
||||
const zipBlob = await zip.generateAsync({
|
||||
type: 'blob',
|
||||
compression: 'DEFLATE',
|
||||
@@ -1007,7 +1007,7 @@ function RightPanel() {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(link.href);
|
||||
|
||||
logger.debug('文件夹导出完成!');
|
||||
logger.data('文件夹导出完成');
|
||||
} catch (error) {
|
||||
logger.error('导出文件夹失败:', error);
|
||||
alert('导出文件夹失败,请重试');
|
||||
|
||||
@@ -304,7 +304,7 @@ function Sidebar() {
|
||||
]).then(([deleteResult, foldersRes]) => {
|
||||
const { setFolders } = useImageStore.getState();
|
||||
if (deleteResult.failed.length > 0) {
|
||||
logger.warn(`⚠️ 删除失败:`, deleteResult.failed);
|
||||
logger.warn('删除失败:', deleteResult.failed);
|
||||
// 失败时回滚
|
||||
setUndoHistory(undoHistory);
|
||||
setUndoToast({ isVisible: false, message: '', count: 0 });
|
||||
@@ -359,7 +359,6 @@ function Sidebar() {
|
||||
|
||||
// 4. 立即跳转到恢复的文件夹
|
||||
setSelectedFolder(lastDeleted.folderPath);
|
||||
logger.debug(`📂 跳转到文件夹: ${lastDeleted.folderPath}`);
|
||||
|
||||
// 5. 后台执行API调用(不阻塞UI)
|
||||
Promise.all([
|
||||
@@ -369,7 +368,7 @@ function Sidebar() {
|
||||
const { setFolders } = useImageStore.getState();
|
||||
// 检查恢复结果
|
||||
if (restoreResult.failed.length > 0) {
|
||||
logger.warn(`⚠️ 恢复失败: ${restoreResult.failed.length} 个文件`);
|
||||
logger.warn(`恢复失败: ${restoreResult.failed.length} 个文件`);
|
||||
const errorMsg = restoreResult.failed[0].error || '未知错误';
|
||||
|
||||
// 失败时回滚
|
||||
@@ -489,7 +488,7 @@ function Sidebar() {
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
logger.debug(`✅ 已移动 ${items.length} 个${hasFolders ? '文件夹' : '文件'}到: ${targetFolderPath}`);
|
||||
logger.file(`移动完成: ${items.length}个${hasFolders ? '文件夹' : '文件'} → ${targetFolderPath}`);
|
||||
}
|
||||
|
||||
// 3. 刷新文件夹列表
|
||||
@@ -656,7 +655,7 @@ function Sidebar() {
|
||||
setImages(response.images);
|
||||
}
|
||||
|
||||
logger.debug(`✅ 撤销拖拽移动完成`);
|
||||
logger.file('撤销拖拽移动完成');
|
||||
} catch (error) {
|
||||
logger.error('撤销拖拽移动失败:', error);
|
||||
// 失败时回滚
|
||||
@@ -696,7 +695,7 @@ function Sidebar() {
|
||||
} else {
|
||||
// 2. 移动成功,选中新位置
|
||||
setSelectedFolder(newPath);
|
||||
logger.debug(`✅ 已移动文件夹: ${moveFolderPath} -> ${newPath}`);
|
||||
logger.file(`移动文件夹: ${moveFolderPath} → ${newPath}`);
|
||||
}
|
||||
|
||||
// 3. 刷新文件夹列表
|
||||
@@ -758,14 +757,14 @@ function Sidebar() {
|
||||
const result = await fileAPI.rename(currentLibraryId, oldPath, newName);
|
||||
const newPath = result.newPath;
|
||||
|
||||
logger.debug(`✅ 文件夹重命名成功: ${oldName} → ${newName}, 路径: ${oldPath} → ${newPath}`);
|
||||
logger.file(`文件夹重命名: ${oldName} → ${newName}`);
|
||||
|
||||
const { setFolders, setSelectedFolder: setSelectedFolderGlobal, setSelectedFolderItem } = useImageStore.getState();
|
||||
|
||||
// 1. 如果重命名的是当前选中的文件夹,立即切换到新路径
|
||||
// 这样可以避免先显示全部图片的闪烁
|
||||
if (isRenamingCurrentFolder) {
|
||||
logger.debug(`📂 重命名当前文件夹: ${oldPath} → ${newPath}`);
|
||||
logger.file(`重命名当前文件夹: ${oldPath} → ${newPath}`);
|
||||
setSelectedFolderGlobal(newPath);
|
||||
|
||||
// 重命名当前浏览的文件夹时,立即刷新该文件夹的图片列表,避免连续重命名导致数量显示为 0
|
||||
@@ -781,7 +780,7 @@ function Sidebar() {
|
||||
|
||||
// 2. 后台刷新文件夹列表(不阻塞UI)
|
||||
imageAPI.getFolders(currentLibraryId).then(foldersRes => {
|
||||
logger.debug('📁 重命名后最新文件夹列表:', foldersRes.folders);
|
||||
logger.data('重命名后刷新文件夹列表');
|
||||
setFolders(foldersRes.folders);
|
||||
|
||||
// 3. 用最新数据更新 selectedFolderItem,让右侧详情面板立即显示新名称
|
||||
@@ -847,7 +846,7 @@ function Sidebar() {
|
||||
|
||||
try {
|
||||
// 1. 添加素材库
|
||||
logger.debug('📝 添加素材库...');
|
||||
logger.data('添加素材库...');
|
||||
const response = await libraryAPI.add(newLibraryName.trim(), newLibraryPath.trim());
|
||||
const newLibId = response.id;
|
||||
const hasExistingIndex = response.hasExistingIndex;
|
||||
@@ -865,7 +864,7 @@ function Sidebar() {
|
||||
setIsAdding(false); // 立即释放按钮
|
||||
|
||||
// 3. 切换到新素材库
|
||||
logger.debug('🔄 切换到新素材库...');
|
||||
logger.data('切换到新素材库...');
|
||||
await libraryAPI.setCurrent(newLibId);
|
||||
setCurrentLibrary(newLibId);
|
||||
setSelectedFolder(null);
|
||||
@@ -887,7 +886,7 @@ function Sidebar() {
|
||||
|
||||
// 6. 如果有已有索引,先快速加载数据库中的数据
|
||||
if (hasExistingIndex) {
|
||||
logger.debug('检测到已有索引,先加载现有数据...');
|
||||
logger.data('检测到已有索引,先加载现有数据...');
|
||||
try {
|
||||
const [foldersRes, countRes] = await Promise.all([
|
||||
imageAPI.getFolders(newLibId),
|
||||
@@ -895,27 +894,27 @@ function Sidebar() {
|
||||
]);
|
||||
useImageStore.getState().setFolders(foldersRes.folders);
|
||||
useImageStore.getState().setTotalImageCount(countRes.count);
|
||||
logger.debug('✅ 已加载现有数据');
|
||||
logger.data('已加载现有数据');
|
||||
} catch (err) {
|
||||
logger.warn('⚠️ 加载现有数据失败:', err);
|
||||
logger.warn('加载现有数据失败:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 开始异步扫描(不等待,Socket.IO 会推送进度)
|
||||
logger.debug('🔍 开始异步扫描...');
|
||||
logger.data('开始异步扫描...');
|
||||
if (hasExistingIndex) {
|
||||
logger.debug('执行增量同步,检测变化...');
|
||||
logger.data('执行增量同步,检测变化...');
|
||||
scanAPI.sync(newLibId, false); // wait=false,异步执行
|
||||
} else {
|
||||
logger.debug('首次添加,执行全量扫描...');
|
||||
logger.data('首次添加,执行全量扫描...');
|
||||
scanAPI.fullScan(newLibId, false); // wait=false,异步执行
|
||||
}
|
||||
|
||||
// 扫描在后台进行,Socket.IO 会推送进度和完成事件
|
||||
// App.jsx 中的 scanComplete 监听器会自动刷新数据
|
||||
logger.debug('✅ 扫描已启动,请等待进度显示...');
|
||||
logger.data('扫描已启动,请等待进度显示...');
|
||||
} catch (error) {
|
||||
logger.error('❌ Error adding library:', error);
|
||||
logger.error('添加素材库失败:', error);
|
||||
|
||||
// 提取错误信息
|
||||
let errorMessage = error.message || '未知错误';
|
||||
@@ -1008,7 +1007,7 @@ function Sidebar() {
|
||||
}
|
||||
}).catch(() => { });
|
||||
} catch (error) {
|
||||
logger.error('Error setting current library:', error);
|
||||
logger.error('切换素材库失败:', error);
|
||||
alert('切换素材库失败: ' + error.message);
|
||||
useImageStore.getState().setImageLoadingState({ isLoading: false });
|
||||
} finally {
|
||||
|
||||
Reference in new issue
Block a user