|
8 | 8 | previousScanAtom, |
9 | 9 | changeReportAtom, |
10 | 10 | scanStatusAtom, |
| 11 | + needReindexAtom, |
11 | 12 | errorMessageAtom, |
12 | 13 | isMonitoringAtom, |
13 | 14 | monitorIntervalAtom, |
@@ -43,6 +44,7 @@ export default function ScanControls() { |
43 | 44 | const [showAllFiles, setShowAllFiles] = useAtom(showAllFilesAtom); |
44 | 45 | const [readmeContent, setReadmeContent] = useAtom(readmeContentAtom); |
45 | 46 | const [_, setDockerfiles] = useAtom(dockerfilesAtom); |
| 47 | + const [needReindex, setNeedReindex] = useAtom(needReindexAtom); |
46 | 48 |
|
47 | 49 | const [isDownloading, setIsDownloading] = useState(false); |
48 | 50 | const [isUsingObserver, setIsUsingObserver] = useState(false); |
@@ -135,6 +137,12 @@ export default function ScanControls() { |
135 | 137 | setScanProgress(0); // 重置进度为0 |
136 | 138 | setScanCompleted(false); // 重置完成状态 |
137 | 139 |
|
| 140 | + // 检查是否存在需要重新索引的文件 |
| 141 | + console.log( |
| 142 | + `扫描前检查:needReindex包含 ${needReindex.length} 个文件`, |
| 143 | + needReindex |
| 144 | + ); |
| 145 | + |
138 | 146 | // 尝试读取README.md文件 |
139 | 147 | try { |
140 | 148 | const readmeHandle = await directoryHandle.getFileHandle("README.md", { |
@@ -243,16 +251,33 @@ export default function ScanControls() { |
243 | 251 | // 文件变化处理函数 |
244 | 252 | const handleFileChange = async (isObserverChange: boolean) => { |
245 | 253 | // 如果正在扫描中,跳过 |
246 | | - if (scanStatus === "scanning") return; |
| 254 | + if (scanStatus === "scanning") { |
| 255 | + console.log("正在扫描中,忽略文件变化通知"); |
| 256 | + return; |
| 257 | + } |
247 | 258 |
|
248 | 259 | console.log( |
249 | 260 | `${t("scanControls.changeDetected")},由${ |
250 | 261 | isObserverChange ? "FileSystemObserver" : "轮询" |
251 | 262 | }触发` |
252 | 263 | ); |
253 | 264 |
|
| 265 | + // 检查是否有需要重新索引的文件 |
| 266 | + const currentNeedReindex = needReindex.length > 0; |
| 267 | + if (currentNeedReindex) { |
| 268 | + console.log( |
| 269 | + `有 ${needReindex.length} 个文件需要重新索引,将执行增量扫描` |
| 270 | + ); |
| 271 | + } else { |
| 272 | + // 如果没有检测到特定文件变更,则添加一个标记以确保执行增量扫描 |
| 273 | + console.log("没有检测到具体文件变更,添加通配符到重新索引列表"); |
| 274 | + addToReindexList("__CHANGED__:observer_triggered"); |
| 275 | + } |
| 276 | + |
254 | 277 | // 执行扫描并更新UI |
| 278 | + console.log("开始执行文件扫描..."); |
255 | 279 | await handleScan(); |
| 280 | + console.log("文件扫描完成"); |
256 | 281 |
|
257 | 282 | // 强制重新初始化文件观察器,确保新文件夹被监控 |
258 | 283 | if (isMonitoring && directoryHandle && isObserverChange) { |
@@ -295,6 +320,8 @@ export default function ScanControls() { |
295 | 320 | setCurrentScan(null); |
296 | 321 | setPreviousScan(null); |
297 | 322 | setChangeReport(null); |
| 323 | + // 清空需要重新索引的文件列表 |
| 324 | + setNeedReindex([]); |
298 | 325 | // 执行扫描 |
299 | 326 | setTimeout(() => { |
300 | 327 | handleScan(); |
@@ -371,6 +398,18 @@ export default function ScanControls() { |
371 | 398 | setShowVectorizeModal(false); |
372 | 399 | }; |
373 | 400 |
|
| 401 | + // 当有文件发生变化时,将其添加到需要重新索引的列表中 |
| 402 | + const addToReindexList = (filePath: string) => { |
| 403 | + console.log(`将文件添加到重新索引列表: ${filePath}`); |
| 404 | + // 检查文件是否已在列表中 |
| 405 | + setNeedReindex((prev) => { |
| 406 | + if (prev.includes(filePath)) { |
| 407 | + return prev; |
| 408 | + } |
| 409 | + return [...prev, filePath]; |
| 410 | + }); |
| 411 | + }; |
| 412 | + |
374 | 413 | // 如果没有目录句柄,不显示控制器 |
375 | 414 | if (!directoryHandle) return null; |
376 | 415 |
|
|
0 commit comments