mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-28 18:36:23 +02:00
refactor: completely remove Local Folder connector references and update folder sync logic
This commit is contained in:
parent
493d720b89
commit
149ccb97dd
8 changed files with 60 additions and 35 deletions
|
|
@ -1,41 +1,73 @@
|
|||
"use client";
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { connectorsApiService } from "@/lib/apis/connectors-api.service";
|
||||
import { documentsApiService } from "@/lib/apis/documents-api.service";
|
||||
|
||||
interface FileChangedEvent {
|
||||
rootFolderId: number | null;
|
||||
searchSpaceId: number;
|
||||
folderPath: string;
|
||||
folderName: string;
|
||||
relativePath: string;
|
||||
fullPath: string;
|
||||
action: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
const DEBOUNCE_MS = 2000;
|
||||
|
||||
export function useFolderSync() {
|
||||
const pendingRef = useRef<Map<string, ReturnType<typeof setTimeout>>>(new Map());
|
||||
const queueRef = useRef<FileChangedEvent[]>([]);
|
||||
const processingRef = useRef(false);
|
||||
const debounceTimers = useRef<Map<string, ReturnType<typeof setTimeout>>>(new Map());
|
||||
|
||||
async function processQueue() {
|
||||
if (processingRef.current) return;
|
||||
processingRef.current = true;
|
||||
while (queueRef.current.length > 0) {
|
||||
const event = queueRef.current.shift()!;
|
||||
try {
|
||||
await documentsApiService.folderIndexFile(event.searchSpaceId, {
|
||||
folder_path: event.folderPath,
|
||||
folder_name: event.folderName,
|
||||
search_space_id: event.searchSpaceId,
|
||||
target_file_path: event.fullPath,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("[FolderSync] Failed to trigger re-index:", err);
|
||||
}
|
||||
}
|
||||
processingRef.current = false;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const api = typeof window !== "undefined" ? window.electronAPI : null;
|
||||
if (!api?.onFileChanged) return;
|
||||
|
||||
const cleanup = api.onFileChanged((event) => {
|
||||
const key = `${event.connectorId}:${event.fullPath}`;
|
||||
// Signal to main process that the renderer is ready to receive events
|
||||
api.signalRendererReady?.();
|
||||
|
||||
const existing = pendingRef.current.get(key);
|
||||
const cleanup = api.onFileChanged((event: FileChangedEvent) => {
|
||||
const key = `${event.folderPath}:${event.fullPath}`;
|
||||
|
||||
const existing = debounceTimers.current.get(key);
|
||||
if (existing) clearTimeout(existing);
|
||||
|
||||
const timeout = setTimeout(async () => {
|
||||
pendingRef.current.delete(key);
|
||||
try {
|
||||
await connectorsApiService.indexFile(event.connectorId, event.fullPath);
|
||||
} catch (err) {
|
||||
console.error("[FolderSync] Failed to trigger re-index:", err);
|
||||
}
|
||||
const timeout = setTimeout(() => {
|
||||
debounceTimers.current.delete(key);
|
||||
queueRef.current.push(event);
|
||||
processQueue();
|
||||
}, DEBOUNCE_MS);
|
||||
|
||||
pendingRef.current.set(key, timeout);
|
||||
debounceTimers.current.set(key, timeout);
|
||||
});
|
||||
|
||||
return () => {
|
||||
cleanup();
|
||||
for (const timeout of pendingRef.current.values()) {
|
||||
for (const timeout of debounceTimers.current.values()) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
pendingRef.current.clear();
|
||||
debounceTimers.current.clear();
|
||||
};
|
||||
}, []);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue