修复 筛选不生效的问题
This commit is contained in:
1 parent
9b258e4abd
commit
9e26770e96
16 files changed
+159
-125
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "flypic-frontend",
|
||||
"version": "0.1.0",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -8,18 +8,20 @@ import { libraryAPI, scanAPI, watchAPI } from '../api';
|
||||
|
||||
function Header() {
|
||||
const { currentLibraryId } = useLibraryStore();
|
||||
const { searchKeywords, images, selectedFolder, setSearchKeywords } = useImageStore();
|
||||
const { searchKeywords, originalImages, selectedFolder, setSearchKeywords, filters, setFilters, resetFilters } = useImageStore();
|
||||
const { theme, toggleTheme, thumbnailHeight, setThumbnailHeight, mobileView } = useUIStore();
|
||||
|
||||
const [showFilters, setShowFilters] = useState(false);
|
||||
const [selectedFormats, setSelectedFormats] = useState([]);
|
||||
const [selectedSizes, setSelectedSizes] = useState([]);
|
||||
const [selectedOrientation, setSelectedOrientation] = useState('');
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
const [isMobile, setIsMobile] = useState(false);
|
||||
const [showMobileSettings, setShowMobileSettings] = useState(false);
|
||||
const [localSearchValue, setLocalSearchValue] = useState(searchKeywords);
|
||||
const searchDebounceRef = useRef(null);
|
||||
|
||||
// 从全局 store 获取筛选状态
|
||||
const selectedFormats = filters.formats || [];
|
||||
const selectedSizes = filters.sizes || [];
|
||||
const selectedOrientation = filters.orientation || '';
|
||||
|
||||
// 检测移动端
|
||||
useEffect(() => {
|
||||
@@ -33,10 +35,8 @@ function Header() {
|
||||
|
||||
// 监听文件夹变化,自动清空筛选
|
||||
useEffect(() => {
|
||||
setSelectedFormats([]);
|
||||
setSelectedSizes([]);
|
||||
setSelectedOrientation('');
|
||||
}, [selectedFolder]);
|
||||
resetFilters();
|
||||
}, [selectedFolder, resetFilters]);
|
||||
|
||||
// 🎯 内存优化:禁用前端启动 chokidar 文件监控
|
||||
// 后端已经使用轻量级监控器(智能轮询),不需要前端启动
|
||||
@@ -167,9 +167,9 @@ function Header() {
|
||||
return false;
|
||||
};
|
||||
|
||||
// 分析当前文件夹的所有图片,生成可选项(只在images变化时计算一次)
|
||||
// 分析原始图片列表,生成可选项(基于 originalImages,不受筛选影响)
|
||||
const filterOptions = useMemo(() => {
|
||||
if (images.length === 0) {
|
||||
if (originalImages.length === 0) {
|
||||
return { formats: [], sizes: [], hasHorizontal: false, hasVertical: false };
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ function Header() {
|
||||
let hasHorizontal = false;
|
||||
let hasVertical = false;
|
||||
|
||||
images.forEach(img => {
|
||||
originalImages.forEach(img => {
|
||||
// 格式
|
||||
if (img.format) {
|
||||
formats.add(img.format.toLowerCase());
|
||||
@@ -199,79 +199,31 @@ function Header() {
|
||||
hasHorizontal,
|
||||
hasVertical
|
||||
};
|
||||
}, [images]);
|
||||
}, [originalImages]);
|
||||
|
||||
// 使用 useMemo 缓存筛选结果,避免重复计算
|
||||
const filteredResult = useMemo(() => {
|
||||
// 如果没有任何筛选条件,直接返回原始图片
|
||||
if (selectedFormats.length === 0 &&
|
||||
selectedSizes.length === 0 &&
|
||||
!selectedOrientation) {
|
||||
return images;
|
||||
}
|
||||
|
||||
// 预先转换为 Set 以提高查找性能
|
||||
const formatSet = new Set(selectedFormats);
|
||||
const sizeSet = new Set(selectedSizes);
|
||||
|
||||
return images.filter(img => {
|
||||
// 格式筛选
|
||||
if (formatSet.size > 0) {
|
||||
if (!formatSet.has(img.format?.toLowerCase())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 文件大小筛选
|
||||
if (sizeSet.size > 0) {
|
||||
const sizeKB = img.size / 1024;
|
||||
let matchesSize = false;
|
||||
|
||||
for (const range of sizeSet) {
|
||||
if (matchSizeRange(sizeKB, range)) {
|
||||
matchesSize = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!matchesSize) return false;
|
||||
}
|
||||
|
||||
// 横竖图筛选
|
||||
if (selectedOrientation === 'horizontal') {
|
||||
if (img.width <= img.height) return false;
|
||||
} else if (selectedOrientation === 'vertical') {
|
||||
if (img.height <= img.width) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}, [images, selectedFormats, selectedSizes, selectedOrientation]);
|
||||
|
||||
// 注意:过滤逻辑已移至后端,这里的 filteredResult 仅用于显示统计
|
||||
|
||||
// 切换选项
|
||||
// 切换选项 - 同步到全局 store
|
||||
const toggleFormat = (format) => {
|
||||
setSelectedFormats(prev =>
|
||||
prev.includes(format) ? prev.filter(f => f !== format) : [...prev, format]
|
||||
);
|
||||
const newFormats = selectedFormats.includes(format)
|
||||
? selectedFormats.filter(f => f !== format)
|
||||
: [...selectedFormats, format];
|
||||
setFilters({ formats: newFormats });
|
||||
};
|
||||
|
||||
const toggleSize = (size) => {
|
||||
setSelectedSizes(prev =>
|
||||
prev.includes(size) ? prev.filter(s => s !== size) : [...prev, size]
|
||||
);
|
||||
const newSizes = selectedSizes.includes(size)
|
||||
? selectedSizes.filter(s => s !== size)
|
||||
: [...selectedSizes, size];
|
||||
setFilters({ sizes: newSizes });
|
||||
};
|
||||
|
||||
const toggleOrientation = (orientation) => {
|
||||
setSelectedOrientation(prev => prev === orientation ? '' : orientation);
|
||||
const newOrientation = selectedOrientation === orientation ? '' : orientation;
|
||||
setFilters({ orientation: newOrientation });
|
||||
};
|
||||
|
||||
// 清除筛选
|
||||
const clearFilters = () => {
|
||||
setSelectedFormats([]);
|
||||
setSelectedSizes([]);
|
||||
setSelectedOrientation('');
|
||||
setFilters({ formats: [], sizes: [], orientation: '' });
|
||||
};
|
||||
|
||||
const handleThemeToggle = async () => {
|
||||
|
||||
@@ -14,6 +14,36 @@ import FileViewer from './FileViewer';
|
||||
// 虚拟滚动阈值:超过此数量启用虚拟滚动
|
||||
const VIRTUAL_SCROLL_THRESHOLD = 100;
|
||||
|
||||
// 解析大小字符串为 KB
|
||||
const parseSizeToKB = (sizeStr) => {
|
||||
const match = sizeStr.match(/^(\d+(?:\.\d+)?)(B|KB|MB|GB)$/);
|
||||
if (!match) return 0;
|
||||
const value = parseFloat(match[1]);
|
||||
const unit = match[2];
|
||||
switch (unit) {
|
||||
case 'B': return value / 1024;
|
||||
case 'KB': return value;
|
||||
case 'MB': return value * 1024;
|
||||
case 'GB': return value * 1024 * 1024;
|
||||
default: return 0;
|
||||
}
|
||||
};
|
||||
|
||||
// 匹配大小范围
|
||||
const matchSizeRange = (sizeKB, range) => {
|
||||
if (range.startsWith('>')) {
|
||||
const minStr = range.substring(1).trim();
|
||||
const minKB = parseSizeToKB(minStr);
|
||||
return sizeKB >= minKB;
|
||||
} else if (range.includes(' - ')) {
|
||||
const [minStr, maxStr] = range.split(' - ').map(s => s.trim());
|
||||
const minKB = parseSizeToKB(minStr);
|
||||
const maxKB = parseSizeToKB(maxStr);
|
||||
return sizeKB >= minKB && sizeKB < maxKB;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// 加载配置
|
||||
const LOAD_CONFIG = {
|
||||
pageSize: 100, // 每次加载 100 张(更轻量)
|
||||
@@ -29,8 +59,52 @@ function ImageWaterfall() {
|
||||
searchKeywords, filters, appendImages, setImageLoadingState
|
||||
} = useImageStore();
|
||||
|
||||
const filteredImages = images;
|
||||
const { thumbnailHeight, isResizingPanels } = useUIStore();
|
||||
|
||||
// 前端筛选逻辑:基于 filters 过滤图片
|
||||
const filteredImages = useMemo(() => {
|
||||
const { formats, sizes, orientation } = filters;
|
||||
|
||||
// 如果没有任何筛选条件,直接返回原始图片
|
||||
if ((!formats || formats.length === 0) &&
|
||||
(!sizes || sizes.length === 0) &&
|
||||
!orientation) {
|
||||
return images;
|
||||
}
|
||||
|
||||
return images.filter(img => {
|
||||
// 格式筛选
|
||||
if (formats && formats.length > 0) {
|
||||
if (!formats.includes(img.format?.toLowerCase())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 文件大小筛选
|
||||
if (sizes && sizes.length > 0) {
|
||||
const sizeKB = img.size / 1024;
|
||||
let matchesSize = false;
|
||||
|
||||
for (const range of sizes) {
|
||||
if (matchSizeRange(sizeKB, range)) {
|
||||
matchesSize = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!matchesSize) return false;
|
||||
}
|
||||
|
||||
// 横竖图筛选
|
||||
if (orientation === 'horizontal') {
|
||||
if (img.width <= img.height) return false;
|
||||
} else if (orientation === 'vertical') {
|
||||
if (img.height <= img.width) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}, [images, filters]);
|
||||
const containerRef = useRef(null);
|
||||
const listRef = useRef(null);
|
||||
const [containerWidth, setContainerWidth] = useState(0);
|
||||
|
||||
@@ -13,7 +13,7 @@ import Dashboard from './Dashboard';
|
||||
|
||||
function MainContent() {
|
||||
const { currentLibraryId } = useLibraryStore();
|
||||
const { searchKeywords, filters, selectedFolder, setImages, imageLoadingState, images } = useImageStore();
|
||||
const { searchKeywords, filters, selectedFolder, setImages, setOriginalImages, imageLoadingState, images } = useImageStore();
|
||||
const { scanProgress } = useScanStore();
|
||||
|
||||
// 使用 ref 跟踪最新的请求上下文
|
||||
@@ -100,9 +100,10 @@ function MainContent() {
|
||||
// 标记请求完成
|
||||
requestManager.complete(requestContext.id);
|
||||
|
||||
// 初次加载:设置图片数据
|
||||
// 初次加载:设置图片数据和原始图片(用于筛选选项)
|
||||
if (isInitialLoad) {
|
||||
setImages(images);
|
||||
setOriginalImages(images);
|
||||
}
|
||||
|
||||
useImageStore.getState().setImageLoadingState({
|
||||
@@ -130,7 +131,7 @@ function MainContent() {
|
||||
currentRequestContextRef.current = null;
|
||||
}
|
||||
}
|
||||
}, [currentLibraryId, searchKeywords, filters, selectedFolder, setImages, cancelCurrentRequest]);
|
||||
}, [currentLibraryId, searchKeywords, selectedFolder, setImages, setOriginalImages, cancelCurrentRequest]);
|
||||
|
||||
// 监听文件夹/搜索/筛选变化
|
||||
useEffect(() => {
|
||||
@@ -166,7 +167,7 @@ function MainContent() {
|
||||
clearTimeout(debounceTimerRef.current);
|
||||
}
|
||||
};
|
||||
}, [currentLibraryId, searchKeywords, filters, selectedFolder, loadImages, cancelCurrentRequest, setImages]);
|
||||
}, [currentLibraryId, searchKeywords, selectedFolder, loadImages, cancelCurrentRequest, setImages]);
|
||||
|
||||
// 组件卸载时清理
|
||||
useEffect(() => {
|
||||
|
||||
@@ -20,12 +20,13 @@ export const useImageStore = create((set, get) => ({
|
||||
searchKeywords: '',
|
||||
filters: {
|
||||
formats: [],
|
||||
minSize: null,
|
||||
maxSize: null,
|
||||
startDate: null,
|
||||
endDate: null
|
||||
sizes: [], // 文件大小范围筛选
|
||||
orientation: '', // 横竖图筛选: 'horizontal' | 'vertical' | ''
|
||||
},
|
||||
|
||||
// 原始图片列表(用于生成筛选选项,不受筛选影响)
|
||||
originalImages: [],
|
||||
|
||||
// 加载状态
|
||||
imageLoadingState: {
|
||||
isLoading: false,
|
||||
@@ -37,12 +38,16 @@ export const useImageStore = create((set, get) => ({
|
||||
// 图片操作
|
||||
setImages: (images) => set({ images }),
|
||||
|
||||
// 设置原始图片(用于筛选选项)
|
||||
setOriginalImages: (images) => set({ originalImages: images }),
|
||||
|
||||
// 追加图片(向下滚动时)
|
||||
appendImages: (newImages) => set((state) => ({
|
||||
images: [...state.images, ...newImages]
|
||||
images: [...state.images, ...newImages],
|
||||
originalImages: [...state.originalImages, ...newImages]
|
||||
})),
|
||||
|
||||
clearImages: () => set({ images: [] }),
|
||||
clearImages: () => set({ images: [], originalImages: [] }),
|
||||
|
||||
// 选择操作
|
||||
setSelectedImage: (image) => set({ selectedImage: image }),
|
||||
@@ -76,10 +81,8 @@ export const useImageStore = create((set, get) => ({
|
||||
searchKeywords: '',
|
||||
filters: {
|
||||
formats: [],
|
||||
minSize: null,
|
||||
maxSize: null,
|
||||
startDate: null,
|
||||
endDate: null
|
||||
sizes: [],
|
||||
orientation: ''
|
||||
}
|
||||
}),
|
||||
|
||||
|
||||
Reference in new issue
Block a user